Ant.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. public function getAntUrlActionList() {
  67. V::exec("ls -1 {$this->pathUrlActions}*/*/build.xml", $out, $ret);
  68. $taskList = array();
  69. foreach ($out as $line) {
  70. try {
  71. if ($this->pathUrlActions != substr($line, 0, strlen($this->pathUrlActions))) throw new Exception("Wrong line prefix '{$line}' - skip");
  72. if ('/build.xml' != substr($line, -1 * strlen('/build.xml'))) throw new Exception("Wrong line suffix '{$line}' - expected '/build.xml' - skip");
  73. } catch (Exception $e) {
  74. UI::alert('warning', $e->getMessage());
  75. continue;
  76. }
  77. $line = substr($line, strlen($this->pathUrlActions));
  78. $name = substr($line, 0, -1 * strlen('/build.xml'));
  79. $taskList[$name] = $name;// TODO: $name => $description (read from build.xml file first xml tag <project...>)
  80. }
  81. return $taskList;
  82. }
  83. public function getAntToolList() {
  84. V::exec("ls -1 {$this->pathTools}*/build.xml", $out, $ret);
  85. $taskList = array();
  86. foreach ($out as $line) {
  87. try {
  88. if ($this->pathTools != substr($line, 0, strlen($this->pathTools))) throw new Exception("Wrong line prefix '{$line}' - skip");
  89. if ('/build.xml' != substr($line, -1 * strlen('/build.xml'))) throw new Exception("Wrong line suffix '{$line}' - expected '/build.xml' - skip");
  90. } catch (Exception $e) {
  91. UI::alert('warning', $e->getMessage());
  92. continue;
  93. }
  94. $line = substr($line, strlen($this->pathTools));
  95. $name = substr($line, 0, -1 * strlen('/build.xml'));
  96. $taskList[$name] = $name;// TODO: $name => $description (read from build.xml file first xml tag <project...>)
  97. }
  98. return $taskList;
  99. }
  100. public function generatePdfAction() {// TODO: OLD TEST
  101. $antPath = V::get('ditaPath', '', $_GET);
  102. $idKoresp = V::get('idKoresp', 0, $_POST, 'int');
  103. $doExecute = V::get('doExecute', '', $_POST);
  104. $antList = $this->getAntToolList();
  105. if (!array_key_exists($antPath, $antList)) throw new Exception("dita path not found", 404);
  106. UI::gora();
  107. UI::startContainer();
  108. try {
  109. UI::tag('h1', [], "Dita: '{$antList[$antPath]}'");
  110. UI::startTag('form', ['action'=>"", 'method'=>"POST", 'class'=>'form-inline']);
  111. UI::tag('input', ['type'=>'number', 'value'=>$idKoresp, 'class'=>'form-control', 'style'=>'max-width:200px']);
  112. UI::tag('input', ['type'=>'hidden', 'name'=>'doExecute', 'value'=>'1']);
  113. UI::tag('input', ['type'=>"submit", 'value'=>"Wykonaj", 'class'=>'btn btn-primary']);
  114. UI::endTag('form');
  115. if ($doExecute) {
  116. // if ($idKoresp <= 0) throw new Exception("Bad Request - missing id koresp");
  117. if ($idKoresp <= 0) UI::alert('danger', "Missing id koresp - using default file");
  118. if ($idKoresp > 0) UI::alert('warning', "TODO: generate xml file for id koresp = {$idKoresp}");
  119. $execPath = APP_PATH_SCHEMA . DS . 'ant' . DS . $antPath;
  120. $ant = APP_PATH_WWW . DS . 'stuff' . DS . 'dita-ot-2.3.3' . DS . 'bin' . DS . 'ant';
  121. $cmd = "cd {$execPath} && {$ant} 2>&1";// wymaga java jdk
  122. V::exec($cmd, $out, $ret);
  123. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  124. }
  125. throw new Exception("TODO: F." . __FUNCTION__ . " . L." . __LINE__);
  126. } catch (Exception $e) {
  127. UI::alert('danger', $e->getMessage());
  128. }
  129. UI::endContainer();
  130. UI::dol();
  131. }
  132. public function reinstallAction() {
  133. UI::gora();
  134. UI::tag('h1', [], 'Reinstall Dita Open Toolkit 2.3.3');
  135. $stuffPath = APP_PATH_WWW . DS . 'stuff';
  136. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && rm dita-ot-2.3.3.zip || echo PASSED";
  137. // $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 ";
  138. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && rm dita-ot-2.3.3 || echo PASSED";
  139. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff && unzip dita-ot-2.3.3.zip || echo PASSED";
  140. // $cmd[]['rsh']="cd /Library/Server/Web/Data/Sites/Default/SE/stuff/dita-ot-2.3.3 && ./startcmd.sh || echo PASSED";
  141. $wget = "/opt/local/bin/wget";
  142. $unzip = "/usr/bin/unzip";
  143. // $testCmd = "which unzip";
  144. // V::exec($testCmd, $out, $ret);
  145. // DBG::_(true, true, "testCmd: {$testCmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  146. // $testCmd = "which wget";
  147. // V::exec($testCmd, $out, $ret);
  148. // DBG::_(true, true, "testCmd: {$testCmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  149. $cmd = "cd {$stuffPath} ; [ -f dita-ot-2.3.3.zip ] && rm dita-ot-2.3.3.zip";
  150. V::exec($cmd, $out, $ret);
  151. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  152. $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";
  153. V::exec($cmd, $out, $ret);
  154. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  155. $cmd = "cd {$stuffPath}
  156. [ -f dita-ot-2.3.3.zip ] && rm dita-ot-2.3.3.zip
  157. {$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
  158. [ -d dita-ot-2.3.3 ] && rm -rf dita-ot-2.3.3
  159. {$unzip} dita-ot-2.3.3.zip
  160. ";
  161. V::exec($cmd, $out, $ret);
  162. DBG::_(true, true, "cmd: {$cmd} (return: {$ret})", $out, __CLASS__, __FUNCTION__, __LINE__);
  163. }
  164. }