Skip to content
Snippets Groups Projects
Commit dc398cc4 authored by Lars Bilke's avatar Lars Bilke Committed by Dmitri Naumov
Browse files

[ogs] Adds support for multiple patch files.

Are applied in the given order.
parent 6acc88dd
No related branches found
No related tags found
No related merge requests found
...@@ -81,11 +81,12 @@ int main(int argc, char* argv[]) ...@@ -81,11 +81,12 @@ int main(int argc, char* argv[])
"PROJECT_FILE"); "PROJECT_FILE");
cmd.add(project_arg); cmd.add(project_arg);
TCLAP::ValueArg<std::string> xml_patch_file( TCLAP::MultiArg<std::string> xml_patch_files(
"p", "xml-patch", "p", "xml-patch",
"the xml patch file which is applied to the PROJECT_FILE", false, "", "the xml patch file(s) which is (are) applied (in the given order) to "
"XML_PATCH_FILE"); "the PROJECT_FILE",
cmd.add(xml_patch_file); false, "");
cmd.add(xml_patch_files);
TCLAP::ValueArg<std::string> outdir_arg("o", "output-directory", TCLAP::ValueArg<std::string> outdir_arg("o", "output-directory",
"the output directory to write to", "the output directory to write to",
...@@ -190,62 +191,70 @@ int main(int argc, char* argv[]) ...@@ -190,62 +191,70 @@ int main(int argc, char* argv[])
run_time.start(); run_time.start();
std::string prj_file; std::string prj_file;
// read diff if (!xml_patch_files.getValue().empty())
if (xml_patch_file.getValue() != "")
{ {
auto patch = xmlParseFile(xml_patch_file.getValue().c_str()); std::string current_prj_file = project_arg.getValue();
if (patch == NULL) 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}.", auto patch = xmlParseFile(patch_file.c_str());
xml_patch_file.getValue()); 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(); xmlCleanupParser();
prj_file = current_prj_file;
} }
else else
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment