CLI File Encryptor
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.

120 lines
3.1KB

  1. <?php
  2. // @codingStandardsIgnoreFile
  3. use Github\Client;
  4. use Github\Exception\RuntimeException as GithubRuntimeException;
  5. use Secondtruth\Compiler\Compiler;
  6. pake_desc('Build phar');
  7. pake_task('build');
  8. /**
  9. * @throws pakeException
  10. */
  11. function run_build()
  12. {
  13. pake_echo('Creating build directory');
  14. $buildDir = __DIR__ . '/build/copy';
  15. $all = pakeFinder::type('any');
  16. // recreate empty build directory
  17. pake_remove($all, $buildDir);
  18. pake_mkdirs(__DIR__ . '/build/copy');
  19. pake_mirror($all, __DIR__ . '/bin', $buildDir . '/bin');
  20. pake_mirror($all, __DIR__ . '/src', $buildDir . '/src');
  21. pake_copy(__DIR__ . '/composer.json', $buildDir . '/composer.json');
  22. pake_copy(__DIR__ . '/composer.lock', $buildDir . '/composer.lock');
  23. pake_echo('Writing version');
  24. $version = trim(`git describe --tags`);
  25. $versionFile = $buildDir . '/src/version.php';
  26. file_put_contents($versionFile, "<?php return '{$version}';" . PHP_EOL);
  27. pake_echo('Installing dependencies');
  28. pake_sh("cd {$buildDir} && composer install --no-dev --optimize-autoloader");
  29. pake_echo('Compiling phar');
  30. $phar = new Compiler($buildDir);
  31. $phar->addIndexFile('bin/encryptor');
  32. $phar->addDirectory('src');
  33. $phar->addDirectory('vendor', [
  34. // ignore non php files
  35. '!*.php',
  36. // test files
  37. '*/Tests/*',
  38. '*/Tester/*',
  39. '*/Test/*',
  40. '*/tests/*',
  41. '*/test/*',
  42. ]);
  43. $phar->compile(__DIR__ . '/build/encryptor.phar');
  44. pake_echo('Done');
  45. }
  46. pake_desc('Upload release to GitHub');
  47. pake_task('upload');
  48. /**
  49. *
  50. * @throws pakeException
  51. */
  52. function run_upload()
  53. {
  54. if (! file_exists(__DIR__ . '/build/encryptor.phar')) {
  55. throw new RuntimeException('No built project');
  56. }
  57. $version = trim(`git describe --tags`);
  58. $token = getenv('GITHUB_TOKEN');
  59. if (empty($token)) {
  60. throw new RuntimeException('Token is not set');
  61. }
  62. $github = new Client();
  63. $github->authenticate($token, '', Client::AUTH_HTTP_TOKEN);
  64. try {
  65. $release = $github->api('repo')->releases()->tag('arokettu', 'php-encryptor', $version);
  66. $assets = $github->api('repo')->releases()->assets()->all('arokettu', 'php-encryptor', $release['id']);
  67. foreach ($assets as $asset) {
  68. if ($asset['name'] === 'encryptor.phar') {
  69. throw new RuntimeException('Asset already exists');
  70. }
  71. }
  72. } catch (GithubRuntimeException $e) {
  73. $tags = $github->api('repo')->tags('arokettu', 'php-encryptor');
  74. $tagFound = false;
  75. foreach ($tags as $tag) {
  76. if ($tag['name'] === $version) {
  77. $tagFound = true;
  78. }
  79. }
  80. if (! $tagFound) {
  81. throw new RuntimeException('Tag does not exist');
  82. }
  83. $release = $github->api('repo')->releases()->create('arokettu', 'php-encryptor', array('tag_name' => $version));
  84. }
  85. $github->api('repo')->releases()->assets()->create(
  86. 'arokettu',
  87. 'php-encryptor',
  88. $release['id'],
  89. 'encryptor.phar',
  90. 'application/octet-stream',
  91. pake_read_file(__DIR__ . '/build/encryptor.phar')
  92. );
  93. }