Tools to manage static site generation - php, 2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.2 KiB

  1. <?php
  2. /********************************************************************************
  3. CLI (command line interface) to use tigsite
  4. usage : php run-gauquelin5.php
  5. and follow the instructions
  6. @license GPL
  7. @copyright Thierry Graff
  8. @history 2019-02-02 02:40:27+01:00, Thierry Graff : creation
  9. ********************************************************************************/
  10. define('DS', DIRECTORY_SEPARATOR);
  11. $ROOT_DIR = __DIR__;
  12. require_once $ROOT_DIR . DS . 'src' . DS . 'app' . DS . 'autoload.php';
  13. $USAGE = <<<USAGE
  14. Usage :
  15. php {$argv[0]} <site> <action>
  16. <site> : must be a sub-directory of sites/
  17. <action> : must correspond to a yaml file of sites/<site>/commands/
  18. Example :
  19. php {$argv[0]} tig12.net replace-footer # Updates the footer of all site pages
  20. Uses the command file sites/tig12.net/commands/replace-footer.yml
  21. USAGE;
  22. //
  23. // check arguments
  24. //
  25. if(count($argv) != 3){
  26. echo "Invalid usage\n";
  27. die($USAGE);
  28. }
  29. $siteName = $argv[1];
  30. $command = $argv[2];
  31. $siteDir = $ROOT_DIR . DS . 'sites' . DS . $siteName;
  32. if(!is_dir($siteDir)){
  33. echo "Wrong site name : directory sites/$siteName does not exist\n";
  34. exit;
  35. }
  36. $siteConfigFile = $siteDir . DS . 'config.yml';
  37. if(!is_file($siteConfigFile)){
  38. echo "Missing site configuration file : file sites/$siteName/config.yml does not exist\n";
  39. exit;
  40. }
  41. $commandFile = $siteDir . DS . 'commands' . DS . $command . '.yml';
  42. if(!is_file($commandFile)){
  43. echo "Wrong command name : file sites/$siteName/commands/$command.yml does not exist\n";
  44. exit;
  45. }
  46. //
  47. // run
  48. //
  49. $config = [];
  50. $config['site'] = jthYAML::parse($siteConfigFile);
  51. $config['command'] = jthYAML::parse($commandFile);
  52. if(!isset($config['command']['commandClass'])){
  53. echo "Missing entry 'commandClass' in $commandFile\n";
  54. exit;
  55. }
  56. if(!class_exists($config['command']['commandClass'])){
  57. echo "Entry 'commandClass' does not correspond to an existing command class in $commandFile\n";
  58. exit;
  59. }
  60. try{
  61. $config['command']['commandClass']:: execute($config);
  62. }
  63. catch(Exception $e){
  64. echo 'Exception : ' . $e->getMessage() . "\n";
  65. echo $e->getFile() . ' - line ' . $e->getLine() . "\n";
  66. echo $e->getTraceAsString() . "\n";
  67. }