diff --git a/Applications/CLI/ogs.cpp b/Applications/CLI/ogs.cpp index fe3e3d18d8b830f7d11defc5180d4f34a09d01a8..cd94d9bca143689db908aec0c0b10d843b6989b0 100644 --- a/Applications/CLI/ogs.cpp +++ b/Applications/CLI/ogs.cpp @@ -81,11 +81,12 @@ int main(int argc, char* argv[]) "PROJECT_FILE"); cmd.add(project_arg); - TCLAP::ValueArg<std::string> xml_patch_file( + TCLAP::MultiArg<std::string> xml_patch_files( "p", "xml-patch", - "the xml patch file which is applied to the PROJECT_FILE", false, "", - "XML_PATCH_FILE"); - cmd.add(xml_patch_file); + "the xml patch file(s) which is (are) applied (in the given order) to " + "the PROJECT_FILE", + false, ""); + cmd.add(xml_patch_files); TCLAP::ValueArg<std::string> outdir_arg("o", "output-directory", "the output directory to write to", @@ -190,62 +191,70 @@ int main(int argc, char* argv[]) run_time.start(); std::string prj_file; - // read diff - if (xml_patch_file.getValue() != "") + if (!xml_patch_files.getValue().empty()) { - auto patch = xmlParseFile(xml_patch_file.getValue().c_str()); - if (patch == NULL) + std::string current_prj_file = project_arg.getValue(); + std::string current_prj_file_base = + BaseLib::extractBaseNameWithoutExtension(current_prj_file); + // apply patch files + for (const auto& patch_file : xml_patch_files.getValue()) { - OGS_FATAL("Error reading XML diff file {:s}.", - xml_patch_file.getValue()); + auto patch = xmlParseFile(patch_file.c_str()); + if (patch == NULL) + { + OGS_FATAL("Error reading XML diff file {:s}.", + patch_file); + } + + auto doc = xmlParseFile(current_prj_file.c_str()); + if (doc == NULL) + { + OGS_FATAL("Error reading project file {:s}.", + current_prj_file); + } + + auto node = xmlDocGetRootElement(patch); + int rc = 0; + for (node = node ? node->children : NULL; node; + node = node->next) + { + if (node->type != XML_ELEMENT_NODE) + continue; + + if (!strcmp((char*)node->name, "add")) + rc = xml_patch_add(doc, node); + else if (!strcmp((char*)node->name, "replace")) + rc = xml_patch_replace(doc, node); + else if (!strcmp((char*)node->name, "remove")) + rc = xml_patch_remove(doc, node); + else + rc = -1; + + if (rc) + break; + } + + if (rc != 0) + { + OGS_FATAL( + "Error while patching prj file {:s} with patch " + "file " + "{:}.", + project_arg.getValue(), patch_file); + } + current_prj_file_base = + current_prj_file_base + "_" + + BaseLib::extractBaseNameWithoutExtension(patch_file); + current_prj_file = BaseLib::joinPaths( + outdir_arg.getValue(), current_prj_file_base + ".prj"); + + xmlSaveFile(current_prj_file.c_str(), doc); + + xmlFreeDoc(doc); + xmlFreeDoc(patch); } - - auto doc = xmlParseFile(project_arg.getValue().c_str()); - if (doc == NULL) - { - OGS_FATAL("Error reading project file {:s}.", - project_arg.getValue()); - } - - auto node = xmlDocGetRootElement(patch); - int rc = 0; - for (node = node ? node->children : NULL; node; - node = node->next) - { - if (node->type != XML_ELEMENT_NODE) - continue; - - if (!strcmp((char*)node->name, "add")) - rc = xml_patch_add(doc, node); - else if (!strcmp((char*)node->name, "replace")) - rc = xml_patch_replace(doc, node); - else if (!strcmp((char*)node->name, "remove")) - rc = xml_patch_remove(doc, node); - else - rc = -1; - - if (rc) - break; - } - - if (rc != 0) - { - OGS_FATAL( - "Error while patching prj file {:s} with patch file " - "{:}.", - project_arg.getValue(), xml_patch_file.getValue()); - } - - prj_file = - BaseLib::joinPaths(outdir_arg.getValue(), - BaseLib::extractBaseNameWithoutExtension( - xml_patch_file.getValue()) + - ".prj"); - xmlSaveFile(prj_file.c_str(), doc); - - xmlFreeDoc(doc); - xmlFreeDoc(patch); xmlCleanupParser(); + prj_file = current_prj_file; } else {