Ant.php 7.9 KB

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