|
| 1 | +/*++ |
| 2 | +
|
| 3 | +Copyright (C) 2024 Autodesk Inc. |
| 4 | +
|
| 5 | +All rights reserved. |
| 6 | +
|
| 7 | +Redistribution and use in source and binary forms, with or without |
| 8 | +modification, are permitted provided that the following conditions are met: |
| 9 | + * Redistributions of source code must retain the above copyright |
| 10 | + notice, this list of conditions and the following disclaimer. |
| 11 | + * Redistributions in binary form must reproduce the above copyright |
| 12 | + notice, this list of conditions and the following disclaimer in the |
| 13 | + documentation and/or other materials provided with the distribution. |
| 14 | + * Neither the name of the Autodesk Inc. nor the |
| 15 | + names of its contributors may be used to endorse or promote products |
| 16 | + derived from this software without specific prior written permission. |
| 17 | +
|
| 18 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | +DISCLAIMED. IN NO EVENT SHALL AUTODESK INC. BE LIABLE FOR ANY |
| 22 | +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +
|
| 29 | +*/ |
| 30 | + |
| 31 | + |
| 32 | +#include <string> |
| 33 | +#include <iostream> |
| 34 | +#include <memory> |
| 35 | +#include <exception> |
| 36 | +#include <vector> |
| 37 | +#include <algorithm> |
| 38 | +#include <filesystem> |
| 39 | + |
| 40 | +#include "common_importstream_native.hpp" |
| 41 | +#include "common_exportstream_native.hpp" |
| 42 | +#include "common_portablezipwriter.hpp" |
| 43 | + |
| 44 | +static void writeUsage() |
| 45 | +{ |
| 46 | + std::cout << "Please start with createClientSource <inputpath> <outputfile>" << std::endl; |
| 47 | +} |
| 48 | + |
| 49 | +static void addFileToZip(AMCCommon::CPortableZIPWriter& zipWriter, const std::string& sSourcePath, const std::string& sZipPath) |
| 50 | +{ |
| 51 | + AMCCommon::CImportStream_Native importStream(sSourcePath); |
| 52 | + auto pEntryStream = zipWriter.createEntry(sZipPath, 0); |
| 53 | + |
| 54 | + const uint64_t nBufferSize = 1024 * 64; |
| 55 | + std::vector<uint8_t> buffer; |
| 56 | + buffer.resize(nBufferSize); |
| 57 | + |
| 58 | + uint64_t nTotalSize = importStream.retrieveSize(); |
| 59 | + uint64_t nRemaining = nTotalSize; |
| 60 | + |
| 61 | + while (nRemaining > 0) { |
| 62 | + uint64_t nChunk = std::min<uint64_t>(nRemaining, nBufferSize); |
| 63 | + uint64_t nReadBytes = importStream.readBuffer(buffer.data(), nChunk, true); |
| 64 | + if (nReadBytes != nChunk) |
| 65 | + throw std::runtime_error("could not read full data from " + sSourcePath); |
| 66 | + |
| 67 | + pEntryStream->writeBuffer(buffer.data(), nReadBytes); |
| 68 | + nRemaining -= nReadBytes; |
| 69 | + } |
| 70 | + |
| 71 | + zipWriter.closeEntry(); |
| 72 | +} |
| 73 | + |
| 74 | +static void addSourceFolder(AMCCommon::CPortableZIPWriter& zipWriter, const std::filesystem::path& inputPath, const std::string& sPrefix) |
| 75 | +{ |
| 76 | + std::filesystem::path folderPath = inputPath / "src" / sPrefix; |
| 77 | + if (!std::filesystem::exists(folderPath) || !std::filesystem::is_directory(folderPath)) |
| 78 | + throw std::runtime_error("source folder does not exist: " + folderPath.u8string()); |
| 79 | + |
| 80 | + for (auto const& entry : std::filesystem::recursive_directory_iterator(folderPath)) { |
| 81 | + if (!entry.is_regular_file()) |
| 82 | + continue; |
| 83 | + |
| 84 | + std::string sFileName = entry.path().filename().u8string(); |
| 85 | + if (sFileName.empty()) |
| 86 | + continue; |
| 87 | + |
| 88 | + std::string sZipPath = sPrefix + "/" + sFileName; |
| 89 | + std::string sSourcePath = entry.path().u8string(); |
| 90 | + |
| 91 | + std::cout << "adding " << sZipPath << "..." << std::endl; |
| 92 | + addFileToZip(zipWriter, sSourcePath, sZipPath); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +int main(int argc, char* argv[]) |
| 97 | +{ |
| 98 | + try |
| 99 | + { |
| 100 | + if (argc < 3) { |
| 101 | + writeUsage(); |
| 102 | + return 1; |
| 103 | + } |
| 104 | + |
| 105 | + std::filesystem::path inputPath = std::filesystem::path(argv[1]); |
| 106 | + std::string sOutputFileName = argv[2]; |
| 107 | + |
| 108 | + if (!std::filesystem::exists(inputPath) || !std::filesystem::is_directory(inputPath)) |
| 109 | + throw std::runtime_error("input directory does not exist: " + inputPath.u8string()); |
| 110 | + |
| 111 | + AMCCommon::PExportStream pExportStream = std::make_shared<AMCCommon::CExportStream_Native>(sOutputFileName); |
| 112 | + AMCCommon::CPortableZIPWriter zipWriter(pExportStream, true); |
| 113 | + |
| 114 | + addSourceFolder(zipWriter, inputPath, "common"); |
| 115 | + addSourceFolder(zipWriter, inputPath, "modules"); |
| 116 | + addSourceFolder(zipWriter, inputPath, "dialogs"); |
| 117 | + |
| 118 | + addFileToZip(zipWriter, (inputPath / "package.json").u8string(), "packages/package.json"); |
| 119 | + addFileToZip(zipWriter, (inputPath / "package-lock.json").u8string(), "packages/package-lock.json"); |
| 120 | + |
| 121 | + zipWriter.writeDirectory(); |
| 122 | + return 0; |
| 123 | + } |
| 124 | + catch (std::exception& E) |
| 125 | + { |
| 126 | + std::cout << "Fatal error: " << E.what() << std::endl; |
| 127 | + return -1; |
| 128 | + } |
| 129 | + catch (...) |
| 130 | + { |
| 131 | + std::cout << "Unhandled fatal exception!" << std::endl; |
| 132 | + return -1; |
| 133 | + } |
| 134 | +} |
0 commit comments