ConfiguratorMain.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2016 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "ConfiguratorMain.hpp"
  14. #include "EnvironmentConfiguration.hpp"
  15. #include "WizardBase.hpp"
  16. #include "ConfigSchemaHelper.hpp"
  17. #include "SchemaCommon.hpp"
  18. #include "ExceptionStrings.hpp"
  19. #include <iostream>
  20. #include "jfile.hpp"
  21. #include "jstring.hpp"
  22. #define BUFF_SIZE 1024
  23. using namespace CONFIGURATOR;
  24. const char *pDefaultDocExt = ".mod.xml";
  25. const char *pDefaultJSONExt = ".json";
  26. void usage()
  27. {
  28. ::std::cout << "configurator --use <xsd files> -b <base dir path>" << ::std::endl;
  29. ::std::cout << "Example Usage: ./configurator --use dali.xsd -b /opt/HPCCSystems/componentfiles/configxml -d -t /tmp " << ::std::endl;
  30. ::std::cout << "-d --doc : generate docs" << ::std::endl;
  31. ::std::cout << "-b --base <base dir path> : base directory path to use with --use option and for xs:include references in xsd files" << ::std::endl;
  32. ::std::cout << "-t --target <target directory> : directory to which to docs will be written. If not specified, then output will go to ::std::out" << ::std::endl;
  33. ::std::cout << "-u --use <schema xsd> : use specified xsd schema instead of buildset file" << ::std::endl;
  34. ::std::cout << "-h -help : prints out this usage" << ::std::endl;
  35. ::std::cout << ::std::endl;
  36. ::std::cout << ::std::endl << "** Experimental **" << ::std::endl;
  37. ::std::cout <<"Example Usage: ./configurator --use esp.xsd -b /opt/HPCCSystems/componentfiles/configxml/ --doc" << ::std::endl;
  38. ::std::cout << "-f --file <build set file> : buildset file name (required if base directory is specfied" << ::std::endl;
  39. ::std::cout << "-p --path <base dir path> : base directory path (required if buildset file name is specified)" << ::std::endl;
  40. ::std::cout << "-x --xsd <xsd file name> : xsd file name (can be more than one) - For use with buildset file" << ::std::endl;
  41. ::std::cout << "-l --list : list available xsd files" << ::std::endl;
  42. ::std::cout << "-m --xml : generate XML configuration file" << ::std::endl;
  43. ::std::cout << "-j --json <component key> : prints JSON" << ::std::endl;
  44. ::std::cout << "-c --env -config <path to env xml file> : load environment config xml file (e.g. environment.xml) " << ::std::endl;
  45. ::std::cout << "--dump : dump out xsd internal structure and values" << ::std::endl;
  46. }
  47. #ifndef CONFIGURATOR_LIB
  48. int main(int argc, char *argv[])
  49. #else
  50. int ConfiguratorMain(int argc, char* argv[])
  51. #endif // CONFIGURATOR_LIB
  52. {
  53. InitModuleObjects();
  54. int idx = 1;
  55. CConfigSchemaHelper *pSchemaHelper = nullptr;
  56. char pBuildSetFile[BUFF_SIZE];
  57. char pBuildSetFileDir[BUFF_SIZE];
  58. char pTargetDocDir[BUFF_SIZE];
  59. char pTargetDocExt[BUFF_SIZE];
  60. char pOverrideSchema[BUFF_SIZE];
  61. char pBasePath[BUFF_SIZE];
  62. char pEnvXMLPath[BUFF_SIZE];
  63. StringBuffer strJSONFile;
  64. StringBuffer strComponentKey;
  65. memset(pBuildSetFile, 0, sizeof(pBuildSetFile));
  66. memset(pBuildSetFileDir, 0, sizeof(pBuildSetFileDir));
  67. memset(pTargetDocDir, 0, sizeof(pTargetDocDir));
  68. memset(pOverrideSchema, 0, sizeof(pOverrideSchema));
  69. memset(pBasePath, 0, sizeof(pBasePath));
  70. memset(pEnvXMLPath, 0, sizeof(pEnvXMLPath));
  71. strncpy(pTargetDocExt, pDefaultDocExt, sizeof(pTargetDocExt));
  72. bool bListXSDs = false;
  73. bool bGenDocs = false;
  74. bool bGenJSON = false;
  75. bool bDump = false;
  76. bool bLoadEnvXML = false;
  77. StringArray arrXSDs;
  78. if (argc == 1)
  79. {
  80. usage();
  81. return 0;
  82. }
  83. while(idx < argc)
  84. {
  85. if (stricmp(argv[idx], "-help") == 0 || stricmp(argv[idx], "-h") == 0)
  86. {
  87. usage();
  88. return 0;
  89. }
  90. if (stricmp(argv[idx], "--dump") == 0)
  91. bDump = true;
  92. if (stricmp(argv[idx], "-config") == 0 || stricmp(argv[idx], "-c") == 0 || stricmp(argv[idx], "--env") == 0)
  93. {
  94. idx++;
  95. bLoadEnvXML = true;
  96. if (argv[idx] == nullptr)
  97. {
  98. ::std::cout << "Missing env xml file parameter!" << ::std::endl;
  99. return 0;
  100. }
  101. strncpy(pEnvXMLPath, argv[idx], BUFF_SIZE);
  102. }
  103. else if (stricmp(argv[idx], "--file") == 0 || stricmp(argv[idx], "-f") == 0)
  104. {
  105. idx++;
  106. assert(argv[idx]);
  107. if (argv[idx] == nullptr)
  108. {
  109. ::std::cout << "Missing file parameter!" << ::std::endl;
  110. return 0;
  111. }
  112. strncpy(pBuildSetFile, argv[idx], BUFF_SIZE);
  113. }
  114. else if (stricmp(argv[idx], "--path") == 0 || stricmp(argv[idx], "-p") == 0)
  115. {
  116. idx++;
  117. assert(argv[idx]);
  118. if (argv[idx] == nullptr)
  119. {
  120. ::std::cout << "Missing path parameter!" << ::std::endl;
  121. return 0;
  122. }
  123. strncpy(pBuildSetFileDir, argv[idx], BUFF_SIZE);
  124. }
  125. else if (stricmp(argv[idx], "--base") == 0 || stricmp(argv[idx], "-b") == 0)
  126. {
  127. idx++;
  128. assert(argv[idx]);
  129. if (argv[idx] == nullptr)
  130. {
  131. ::std::cout << "Missing base dir parameter!" << ::std::endl;
  132. return 0;
  133. }
  134. strncpy(pBasePath, argv[idx], BUFF_SIZE);
  135. }
  136. else if (stricmp(argv[idx], "--xsd") == 0 || stricmp(argv[idx], "-x") == 0)
  137. {
  138. idx++;
  139. assert(argv[idx]);
  140. if (argv[idx] == nullptr)
  141. {
  142. ::std::cout << "Missing XSD file!" << ::std::endl;
  143. return 0;
  144. }
  145. arrXSDs.append(argv[idx]);
  146. }
  147. else if (stricmp(argv[idx], "--list") == 0 || stricmp(argv[idx], "-l") == 0)
  148. bListXSDs = true;
  149. else if (stricmp(argv[idx], "--doc") == 0 || stricmp(argv[idx], "-d") == 0)
  150. bGenDocs = true;
  151. else if (stricmp(argv[idx], "--target") == 0 || stricmp(argv[idx], "-t") == 0)
  152. {
  153. idx++;
  154. assert(argv[idx]);
  155. if (argv[idx] == nullptr)
  156. {
  157. ::std::cout << "Missing target!" << ::std::endl;
  158. return 0;
  159. }
  160. strcpy(pTargetDocDir,argv[idx]);
  161. }
  162. else if (stricmp(argv[idx], "-extension") == 0 || stricmp(argv[idx], "-e") == 0)
  163. {
  164. idx++;
  165. assert(argv[idx]);
  166. if (argv[idx] == nullptr)
  167. {
  168. ::std::cout << "Missing extension!" << ::std::endl;
  169. return 0;
  170. }
  171. if (argv[idx][0] != '.')
  172. {
  173. strcat(pTargetDocExt, ".");
  174. strcpy(&(pTargetDocExt[1]),argv[idx]);
  175. }
  176. else if (*pTargetDocExt == 0)
  177. strcat(pTargetDocExt, "");
  178. else
  179. strcpy(pTargetDocExt,argv[idx]);
  180. }
  181. else if (stricmp(argv[idx], "--use") == 0 || stricmp(argv[idx], "-u") == 0)
  182. {
  183. idx++;
  184. assert(argv[idx]);
  185. if (argv[idx] == nullptr)
  186. {
  187. ::std::cout << "Missing schema xsd!" << ::std::endl;
  188. return 0;
  189. }
  190. else
  191. {
  192. strcpy(pOverrideSchema, argv[idx]);
  193. arrXSDs.append(argv[idx]);
  194. }
  195. }
  196. else if (stricmp(argv[idx], "--json") == 0 || stricmp(argv[idx], "-j") == 0)
  197. {
  198. bGenJSON = true;
  199. idx++;
  200. strComponentKey.set(argv[idx]);
  201. }
  202. idx++;
  203. }
  204. if ((pBuildSetFile[0] != 0) ^ (pBuildSetFileDir[0] != 0))
  205. {
  206. puts("--file and --path need to be both set or neither one!");
  207. return 0;
  208. }
  209. if (bGenDocs == true && arrXSDs.length() == 0)
  210. {
  211. puts("No XSDs specified for doc generation!");
  212. return 0;
  213. }
  214. if (pBuildSetFile[0] == 0 && pOverrideSchema[0] == 0)
  215. {
  216. pSchemaHelper = CConfigSchemaHelper::getInstance();
  217. }
  218. else if (pBuildSetFile[0] == 0)
  219. {
  220. pSchemaHelper = CConfigSchemaHelper::getInstance(pBasePath);
  221. }
  222. else
  223. {
  224. pSchemaHelper = CConfigSchemaHelper::getInstance(pBuildSetFile, pBuildSetFileDir);
  225. }
  226. assert(pSchemaHelper);
  227. if (pOverrideSchema[0] != 0)
  228. CBuildSetManager::getInstance()->setBuildSetArray(arrXSDs);
  229. try
  230. {
  231. pSchemaHelper->populateSchema();
  232. }
  233. CATCH_EXCEPTION_AND_EXIT
  234. if (bListXSDs == true)
  235. {
  236. StringArray arrXSDs;
  237. CBuildSetManager::getInstance()->getBuildSetComponents(arrXSDs);
  238. int length = arrXSDs.length();
  239. if (length > 0)
  240. ::std::cout << "XSD files (" << length << ")" << ::std::endl;
  241. for (int idx = 0; idx < length; idx++)
  242. ::std::cout << "(" << idx+1 << ") " << arrXSDs.item(idx) << ::std::endl;
  243. }
  244. for (int idx = 0; bGenDocs == true && idx < arrXSDs.length(); idx++)
  245. {
  246. char *pDoc = nullptr;
  247. if (pTargetDocDir[0] == 0)
  248. {
  249. pSchemaHelper->printDocumentation(arrXSDs.item(idx), &pDoc);
  250. ::std::cout << pDoc;
  251. }
  252. else
  253. {
  254. Owned<IFile> pFile;
  255. Owned<IFileIO> pFileIO;
  256. StringBuffer strTargetPath;
  257. const char *pXSDFile = strrchr(arrXSDs.item(idx), '/') == nullptr ? arrXSDs.item(idx) : strrchr(arrXSDs.item(idx),'/');
  258. strTargetPath.append(pTargetDocDir).append("/").append(pXSDFile).append(pTargetDocExt);
  259. pFile.setown(createIFile(strTargetPath.str()));
  260. pFileIO.setown(pFile->open(IFOcreaterw));
  261. char *pDoc = nullptr;
  262. pSchemaHelper->printDocumentation(arrXSDs.item(idx), &pDoc);
  263. if (pDoc == nullptr)
  264. continue;
  265. pFileIO->write(0, strlen(pDoc), pDoc);
  266. delete[] pDoc;
  267. }
  268. }
  269. for (int idx = 0; bGenJSON == true && idx < arrXSDs.length(); idx++)
  270. {
  271. if (bLoadEnvXML == true)
  272. pSchemaHelper->loadEnvFromConfig(pEnvXMLPath);
  273. char *pJSON = nullptr;
  274. if (pTargetDocDir[0] == 0)
  275. {
  276. pSchemaHelper->printJSON(strComponentKey.str(), &pJSON);
  277. ::std::cout << pJSON;
  278. }
  279. else
  280. {
  281. Owned<IFile> pFile;
  282. Owned<IFileIO> pFileIO;
  283. StringBuffer strTargetPath;
  284. const char *pXSDFile = strrchr(arrXSDs.item(idx), '/') == nullptr ? arrXSDs.item(idx) : strrchr(arrXSDs.item(idx),'/');
  285. strTargetPath.append(pTargetDocDir).append("/").append(pXSDFile).append(pDefaultJSONExt);
  286. pFile.setown(createIFile(strTargetPath.str()));
  287. pFileIO.setown(pFile->open(IFOcreaterw));
  288. pSchemaHelper->printJSONByKey(strComponentKey.str(), &pJSON);
  289. if (pJSON == nullptr)
  290. continue;
  291. pFileIO->write(0, strlen(pJSON), pJSON);
  292. }
  293. delete[] pJSON;
  294. }
  295. for (int idx = 0; (bDump == true || bLoadEnvXML == true) && idx < arrXSDs.length(); idx++)
  296. {
  297. if (bLoadEnvXML == true)
  298. pSchemaHelper->loadEnvFromConfig(pEnvXMLPath);
  299. if (bDump == true)
  300. pSchemaHelper->printDump(arrXSDs.item(idx));
  301. }
  302. releaseAtoms();
  303. return 0;
  304. }