Skip to content

Commit

Permalink
Added the option to read files from stdin in json format
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyyrylainen committed May 8, 2020
1 parent ef63fff commit 70a8761
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "third_party/cxxopts"]
path = third_party/cxxopts
url = https://github.com/jarro2783/cxxopts.git
[submodule "third_party/json"]
path = third_party/json
url = https://github.com/nlohmann/json.git
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ include_directories(${PROJECT_BINARY_DIR})

add_subdirectory(third_party)
include_directories(third_party/cxxopts/include)
include_directories(third_party/json/single_include)

include_directories(src)
add_subdirectory(src)
Expand Down
23 changes: 23 additions & 0 deletions LibraryLicenses.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,26 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

json:

MIT License

Copyright (c) 2013-2020 Niels Lohmann

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
61 changes: 53 additions & 8 deletions src/PckTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ PckTool::PckTool(const Options& options) : Opts(options) {}
// ------------------------------------ //
int PckTool::Run()
{
if(!BuildFileList())
return 1;

if(Opts.Action == "list" || Opts.Action == "l") {
auto pck = LoadPck();

Expand All @@ -32,13 +35,13 @@ int PckTool::Run()
if(!pck)
return 2;

if(!Opts.Files.empty()) {
if(Opts.Files.size() != 1) {
if(!Files.empty()) {
if(Files.size() != 1) {
std::cout << "ERROR: only one target file to repack as is allowed\n";
return 1;
}

pck->ChangePath(Opts.Files.front());
pck->ChangePath(Files.front().InputFile);
}

std::cout << "Repacking to: " << pck->GetPath() << "\n";
Expand Down Expand Up @@ -67,7 +70,7 @@ int PckTool::Run()
} else if(Opts.Action == "add" || Opts.Action == "a") {
std::unique_ptr<PckFile> pck;

if(Opts.Files.empty()) {
if(Files.empty()) {
std::cout << "ERROR: no files specified\n";
return 1;
}
Expand All @@ -84,12 +87,21 @@ int PckTool::Run()
}
} else {
pck = std::make_unique<PckFile>(Opts.Pack);

pck->SetGodotVersion(Opts.GodotMajor, Opts.GodotMinor, Opts.GodotPatch);
}

for(const auto& entry : Opts.Files) {
if(!pck->AddFilesFromFilesystem(entry, Opts.RemovePrefix)) {
std::cout << "ERROR: failed to process file to add: " << entry << "\n";
return 3;
for(const auto& entry : Files) {
if(!entry.Target.empty()) {
// Already known target for a single file
pck->AddSingleFile(entry.InputFile, pck->PreparePckPath(entry.Target, ""));
} else {
// Potentially a folder tree with no pre-defined targets
if(!pck->AddFilesFromFilesystem(entry.InputFile, Opts.RemovePrefix)) {
std::cout << "ERROR: failed to process file to add: " << entry.InputFile
<< "\n";
return 3;
}
}
}

Expand Down Expand Up @@ -135,3 +147,36 @@ std::unique_ptr<PckFile> PckTool::LoadPck()

return pck;
}
// ------------------------------------ //
bool PckTool::BuildFileList()
{
// Copy standard files
for(const auto& entry : Opts.Files)
Files.push_back({entry});

// Handle json commands
if(Opts.FileCommands.is_array()) {
for(const auto& entry : Opts.FileCommands) {
Files.push_back(
{entry["file"].get<std::string>(), entry["target"].get<std::string>()});
}
}

// Use first file as the pck if pck is missing
if(Opts.Pack.empty()) {
if(Files.empty()) {
std::cout << "ERROR: No pck file or list of files given\n";
return false;
}

// User first file as the pck file
Opts.Pack = Files.front().InputFile;
Files.erase(Files.begin());
}

if(Opts.Pack.find(pcktool::GODOT_PCK_EXTENSION) == std::string::npos) {
std::cout << "WARNING: Given pck file doesn't contain the pck file extension\n";
}

return true;
}
15 changes: 15 additions & 0 deletions src/PckTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

#include "Define.h"

#include <nlohmann/json.hpp>

#include <memory>
#include <string>
#include <vector>

namespace pcktool {

using json = nlohmann::json;

class PckFile;

//! \brief Main class for the Godot Pck Tool
class PckTool {
struct FileEntry {
std::string InputFile;
std::string Target;
};

public:
struct Options {
std::string Pack;
Expand All @@ -24,6 +33,8 @@ class PckTool {
int GodotMajor;
int GodotMinor;
int GodotPatch;

json FileCommands;
};

public:
Expand All @@ -34,6 +45,8 @@ class PckTool {
int Run();

private:
bool BuildFileList();

bool TargetExists();

bool RequireTargetFileExists();
Expand All @@ -42,6 +55,8 @@ class PckTool {

private:
Options Opts;

std::vector<FileEntry> Files;
};

} // namespace pcktool
54 changes: 35 additions & 19 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ int main(int argc, char* argv[])
std::string output;
std::string removePrefix;
int godotMajor, godotMinor, godotPatch;
nlohmann::json fileCommands;

if(result.count("file")) {
files = result["file"].as<decltype(files)>();
Expand All @@ -97,28 +98,12 @@ int main(int argc, char* argv[])
removePrefix = result["remove-prefix"].as<std::string>();
}

if(result.count("pack") < 1) {
// Use first file as the pack file
if(files.empty()) {
std::cout << "ERROR: No pck file or list of files given\n";
return 1;
}

// User first file as the pck file
pack = files.front();
files.erase(files.begin());

} else {
if(result.count("pack")) {
pack = result["pack"].as<std::string>();
}

action = result["action"].as<std::string>();

if(pack.find(pcktool::GODOT_PCK_EXTENSION) == std::string::npos) {
std::cout << "ERROR: Given pck file doesn't contain the pck file extension\n";
return 1;
}

try {
std::tie(godotMajor, godotMinor, godotPatch) =
ParseGodotVersion(result["set-godot-version"].as<std::string>());
Expand All @@ -127,8 +112,39 @@ int main(int argc, char* argv[])
return 1;
}

auto tool = pcktool::PckTool(
{pack, action, files, output, removePrefix, godotMajor, godotMinor, godotPatch});
bool alreadyRead = false;

// Stdin json commands
for(auto iter = files.begin(); iter != files.end();) {
if(*iter != "-" || alreadyRead) {
++iter;
continue;
}

alreadyRead = true;
iter = files.erase(iter);

std::cout << "Reading JSON file commands from STDIN until EOF...\n";

std::string data;

std::string line;
while(std::getline(std::cin, line)) {
data += line;
}

std::cout << "Finished reading STDIN (total characters: " << data.size() << ").\n";

try {
fileCommands = nlohmann::json::parse(data);
} catch(const nlohmann::json::parse_error& e) {
std::cout << "ERROR: invalid json: " << e.what() << "\n";
return 1;
}
}

auto tool = pcktool::PckTool({pack, action, files, output, removePrefix, godotMajor,
godotMinor, godotPatch, fileCommands});

return tool.Run();
}
1 change: 1 addition & 0 deletions third_party/json
Submodule json added at 5bfb27

0 comments on commit 70a8761

Please sign in to comment.