Ant.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('UI');
  4. Lib::loadClass('DBG');
  5. /**
  6. * Executes ant build for dita file - creates pdf file
  7. * - ant build in SE/schema/ant-url_action/{object_namespace_to_file_path}/{function_name}/build.xml
  8. *
  9. * APP_PATH_SCHEMA . '/ant-url_action/' . {$namespace_sep_by_dot} . '/' . {$function_name} . '/build.xml'
  10. * APP_PATH_SCHEMA . '/ant-tool/' . {$ant_tool_name} . '/build.xml'
  11. *
  12. * usage:
  13. * index.php?_route=Ant
  14. * index.php?_route=Ant&_task=ant&path={$ant_tool_name}
  15. * index.php?_route=UrlAction_Ant
  16. * index.php?_route=UrlAction_Ant&_task=ant&path={$namespace_sep_by_dot}{$function_name}&featureID={featureID}&namespace={$namespace}
  17. */
  18. class Route_Ant extends RouteBase {
  19. public $pathTools = APP_PATH_SCHEMA . "/ant-tool/";
  20. public $pathUrlActions = APP_PATH_SCHEMA . "/ant-url_action/";
  21. public $antBin = APP_PATH_WWW . DS . 'stuff' . DS . 'dita-ot-2.3.3' . DS . 'bin' . DS . 'ant';
  22. public function defaultAction() {
  23. UI::gora();
  24. UI::tag('h1', [], 'Ant');
  25. try {
  26. $taskList = $this->getAntToolList();
  27. DBG::nicePrint($taskList, 'ant-tool');
  28. $featureID = V::get('featureID', '', $_GET);
  29. $namespace = V::get('namespace', '', $_GET);
  30. UI::startTag('ul');
  31. foreach ($taskList as $path => $label) {
  32. $link = "index.php?_route=Ant&_task=ant&path={$path}";
  33. echo UI::h('li', [], [
  34. UI::h('a', ['href'=>$link], "Uruchom '{$label}'")
  35. ]);
  36. }
  37. UI::endTag('ul');
  38. } catch (Exception $e) {
  39. UI::alert('danger', $e->getMessage());
  40. DBG::log($e);
  41. }
  42. UI::dol();
  43. }
  44. public function antAction() {
  45. UI::gora();
  46. try {
  47. $path = V::get('path', '', $_GET);
  48. if (!$path) throw new Exception("Missing Ant path!");
  49. $taskList = $this->getAntToolList();
  50. if (!array_key_exists($path, $taskList)) throw new Exception("Ant path not exists! '{$path}'");
  51. $cmd = "cd {$this->pathTools}{$path} && {$this->antBin} 2>&1";// wymaga java jdk
  52. V::exec($cmd, $out, $ret);
  53. } catch (Exception $e) {
  54. UI::alert('danger', $e->getMessage());
  55. DBG::log($e);
  56. }
  57. UI::dol();
  58. }
  59. public function execAntTool($antTask) {
  60. $antList = $this->getAntToolList();
  61. if (!array_key_exists($antTask, $antList)) throw new Exception("ant task not found", 404);
  62. throw new Exception("TODO: run ant task '{$antTask}'", 501);
  63. }
  64. public function getAntUrlActionList() {
  65. V::exec("ls -1 {$this->pathUrlActions}*/*/build.xml", $out, $ret);
  66. $taskList = array();
  67. foreach ($out as $line) {
  68. try {
  69. if ($this->pathUrlActions != substr($line, 0, strlen($this->pathUrlActions))) throw new Exception("Wrong line prefix '{$line}' - skip");
  70. if ('/build.xml' != substr($line, -1 * strlen('/build.xml'))) throw new Exception("Wrong line suffix '{$line}' - expected '/build.xml' - skip");
  71. } catch (Exception $e) {
  72. UI::alert('warning', $e->getMessage());
  73. continue;
  74. }
  75. $line = substr($line, strlen($this->pathUrlActions));
  76. $name = substr($line, 0, -1 * strlen('/build.xml'));
  77. $taskList[$name] = $name;// TODO: $name => $description (read from build.xml file first xml tag <project...>)
  78. }
  79. return $taskList;
  80. }
  81. public function getAntToolList() {
  82. V::exec("ls -1 {$this->pathTools}*/build.xml", $out, $ret);
  83. $taskList = array();
  84. foreach ($out as $line) {
  85. try {
  86. if ($this->pathTools != substr($line, 0, strlen($this->pathTools))) throw new Exception("Wrong line prefix '{$line}' - skip");
  87. if ('/build.xml' != substr($line, -1 * strlen('/build.xml'))) throw new Exception("Wrong line suffix '{$line}' - expected '/build.xml' - skip");
  88. } catch (Exception $e) {
  89. UI::alert('warning', $e->getMessage());
  90. continue;
  91. }
  92. $line = substr($line, strlen($this->pathTools));
  93. $name = substr($line, 0, -1 * strlen('/build.xml'));
  94. $taskList[$name] = $name;// TODO: $name => $description (read from build.xml file first xml tag <project...>)
  95. }
  96. return $taskList;
  97. }
  98. public function generatePdfAction() {// TODO: OLD TEST
  99. $antPath = V::get('ditaPath', '', $_GET);
  100. $idKoresp = V::get('idKoresp', 0, $_POST, 'int');
  101. $doExecute = V::get('doExecute', '', $_POST);
  102. $antList = $this->getAntToolList();
  103. if (!array_key_exists($antPath, $antList)) throw new Exception("dita path not found", 404);
  104. UI::gora();
  105. UI::startContainer();
  106. try {
  107. UI::tag('h1', [], "Dita: '{$antList[$antPath]}'");
  108. UI::startTag('form', ['action'=>"", 'method'=>"POST", 'class'=>'form-inline']);
  109. UI::tag('input', ['type'=>'number', 'value'=>$idKoresp, 'class'=>'form-control', 'style'=>'max-width:200px']);
  110. UI::tag('input', ['type'=>'hidden', 'name'=>'doExecute', 'value'=>'1']);
  111. UI::tag('input', ['type'=>"submit", 'value'=>"Wykonaj", 'class'=>'btn btn-primary']);
  112. UI::endTag('form');
  113. if ($doExecute) {
  114. // if ($idKoresp <= 0) throw new Exception("Bad Request - missing id koresp");
  115. if ($idKoresp <= 0) UI::alert('danger', "Missing id koresp - using default file");
  116. if ($idKoresp > 0) UI::alert('warning', "TODO: generate xml file for id koresp = {$idKoresp}");
  117. $execPath = APP_PATH_SCHEMA . DS . 'ant' . DS . $antPath;
  118. $ant = APP_PATH_WWW . DS . 'stuff' . DS . 'dita-ot-2.3.3' . DS . 'bin' . DS . 'ant';
  119. $cmd = "cd {$execPath} && {$ant} 2>&1";// wymaga java jdk
  120. V::exec($cmd, $out, $ret);
  121. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  122. }
  123. throw new Exception("TODO: F." . __FUNCTION__ . " . L." . __LINE__);
  124. } catch (Exception $e) {
  125. UI::alert('danger', $e->getMessage());
  126. }
  127. UI::endContainer();
  128. UI::dol();
  129. }
  130. public function reinstallAction() {
  131. UI::gora();
  132. UI::tag('h1', [], 'Reinstall Dita Open Toolkit 2.3.3');
  133. $stuffPath = APP_PATH_WWW . DS . 'stuff';
  134. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && rm dita-ot-2.3.3.zip || echo PASSED";
  135. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && wget https://github.com/dita-ot/dita-ot/releases/download/2.3.3/dita-ot-2.3.3.zip -O dita-ot-2.3.3.zip ";
  136. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && rm dita-ot-2.3.3 || echo PASSED";
  137. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && unzip dita-ot-2.3.3.zip || echo PASSED";
  138. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff/dita-ot-2.3.3 && ./startcmd.sh || echo PASSED";
  139. $wget = "/opt/local/bin/wget";
  140. $unzip = "/usr/bin/unzip";
  141. // $testCmd = "which unzip";
  142. // V::exec($testCmd, $out, $ret);
  143. // DBG::_(true, true, "testCmd: {$testCmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  144. // $testCmd = "which wget";
  145. // V::exec($testCmd, $out, $ret);
  146. // DBG::_(true, true, "testCmd: {$testCmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  147. $cmd = "cd {$stuffPath} ; [ -f dita-ot-2.3.3.zip ] && rm dita-ot-2.3.3.zip";
  148. V::exec($cmd, $out, $ret);
  149. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  150. $cmd = "cd {$stuffPath} ; {$wget} https://github.com/dita-ot/dita-ot/releases/download/2.3.3/dita-ot-2.3.3.zip -O dita-ot-2.3.3.zip";
  151. V::exec($cmd, $out, $ret);
  152. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  153. $cmd = "cd {$stuffPath}
  154. [ -f dita-ot-2.3.3.zip ] && rm dita-ot-2.3.3.zip
  155. {$wget} https://github.com/dita-ot/dita-ot/releases/download/2.3.3/dita-ot-2.3.3.zip -O dita-ot-2.3.3.zip
  156. [ -d dita-ot-2.3.3 ] && rm -rf dita-ot-2.3.3
  157. {$unzip} dita-ot-2.3.3.zip
  158. ";
  159. V::exec($cmd, $out, $ret);
  160. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  161. }
  162. }