From e06b227147356fe564fb3ede75ab0929b9fb6d80 Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Sun, 26 Apr 2020 21:53:24 +0100 Subject: [PATCH] Completed the first interation of the new Bootstrapper experience. Downloads and extracts vendors from Gitea and configures the encryption key. Still need to get upgrades implemented. #146 --- .env.example | 6 +- Gruntfile.js | 30 +++++ config/app.php | 2 +- package.json | 2 +- public/bootstrap/src/Bootstrapper.php | 162 ++++++++++++++++++++++++-- public/bootstrap/views/index.php | 12 +- public/css/blue-twilight.css | 23 ++++ public/css/blue-twilight.min.css | 6 + public/js/blue-twilight.js | 116 ++++++++++++++++-- public/js/blue-twilight.min.js | 2 + public/js/blue-twilight.min.js.map | 1 + resources/js/bootstrapper.js | 8 +- resources/js/external_services.js | 4 +- resources/sass/bootstrapper.scss | 2 + 14 files changed, 342 insertions(+), 34 deletions(-) create mode 100644 public/css/blue-twilight.min.css create mode 100644 public/js/blue-twilight.min.js create mode 100644 public/js/blue-twilight.min.js.map diff --git a/.env.example b/.env.example index 3ba91c8..7b22e46 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,7 @@ -APP_ENV=local +APP_ENV=production APP_KEY= -APP_DEBUG=true -APP_LOG_LEVEL=debug +APP_DEBUG=false +APP_LOG_LEVEL=warning APP_URL=http://localhost DB_CONNECTION=mysql diff --git a/Gruntfile.js b/Gruntfile.js index f2117c8..df50c8a 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -14,7 +14,9 @@ module.exports = function(grunt) const sass = require('node-sass'); grunt.loadNpmTasks('grunt-contrib-clean'); + grunt.loadNpmTasks('grunt-contrib-cssmin'); grunt.loadNpmTasks('grunt-contrib-concat'); + grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-curl'); grunt.loadNpmTasks('grunt-dart-sass'); grunt.loadNpmTasks('grunt-exec'); @@ -97,6 +99,23 @@ module.exports = function(grunt) ext: '.css' }] }, + }, + cssmin: { + bt_css: { + files: { + 'public/css/blue-twilight.min.css': ['public/css/blue-twilight.css'] + } + } + }, + uglify: { + bt_js: { + options: { + sourceMap: true + }, + files: { + 'public/js/blue-twilight.min.js': ['public/js/blue-twilight.js'] + } + } } }); @@ -121,7 +140,18 @@ module.exports = function(grunt) 'concat:bt_js' ]); + grunt.registerTask('build-css-release', [ + 'build-css-debug', + 'cssmin:bt_css' + ]); + + grunt.registerTask('build-js-release', [ + 'build-js-debug', + 'uglify:bt_js' + ]); + // Shortcut tasks for the ones above grunt.registerTask('clean-all', ['clean:build_css', 'clean:build_js', 'clean:output']); grunt.registerTask('build-debug', ['clean-all', 'build-css-debug', 'build-js-debug']); + grunt.registerTask('build-release', ['clean-all', 'build-css-release', 'build-js-release']); }; \ No newline at end of file diff --git a/config/app.php b/config/app.php index 62c7e52..763a831 100644 --- a/config/app.php +++ b/config/app.php @@ -2,7 +2,7 @@ return [ // Version number of Blue Twilight - 'version' => '2.1.2', + 'version' => '2.2.0-beta.1', /* |-------------------------------------------------------------------------- diff --git a/package.json b/package.json index ca23a02..ed35856 100644 --- a/package.json +++ b/package.json @@ -12,4 +12,4 @@ "grunt-exec": "^3.0.0", "node-sass": "^4.13.0" } -} \ No newline at end of file +} diff --git a/public/bootstrap/src/Bootstrapper.php b/public/bootstrap/src/Bootstrapper.php index 36f9e7e..19283a1 100644 --- a/public/bootstrap/src/Bootstrapper.php +++ b/public/bootstrap/src/Bootstrapper.php @@ -3,6 +3,7 @@ namespace AppBootstrap; use App\Services\GiteaService; +use Illuminate\Support\Facades\Artisan; /** * This class handles the downloading and extracting of the vendors directory. @@ -13,17 +14,54 @@ use App\Services\GiteaService; */ class Bootstrapper { + /** + * Path to /public/bootstrap + * @var string + */ + private $bootstrapDir; + + /** + * Path to /app/config + * @var string + */ private $configDir; + + /** + * Path to / - the app's root + * @var string + */ private $rootDir; + + /** + * Path to /public/bootstrap/temp + * @var string + */ private $tempDir; + + /** + * Path to /vendor + * @var string + */ + private $vendorDir; + + /** + * Path to /public/bootstrap/views + * @var string + */ private $viewsDir; public function __construct() { - $this->rootDir = dirname(__DIR__); - $this->configDir = sprintf('%s/config', dirname(dirname($this->rootDir))); - $this->tempDir = sprintf('%s/temp', $this->rootDir); - $this->viewsDir = sprintf('%s/views', $this->rootDir); + $this->bootstrapDir = dirname(__DIR__); + $this->rootDir = dirname(dirname($this->bootstrapDir)); + + $this->configDir = sprintf('%s/config', dirname(dirname($this->bootstrapDir))); + $this->tempDir = sprintf('%s/temp', $this->bootstrapDir); + $this->vendorDir = sprintf('%s/vendor', $this->rootDir); + $this->viewsDir = sprintf('%s/views', $this->bootstrapDir); + + chdir($this->rootDir); + putenv('HOME=' . $this->rootDir); } public function handleRequest() @@ -40,13 +78,21 @@ class Bootstrapper $this->download(); return; + case 'extract': + $this->extract(); + return; + + case 'finalise': + $this->finalise(); + return; + default: - throw new \Exception(sprintf('ERROR: Action \'%s\' was not recognised.', $_GET['act'])); + throw new \Exception(sprintf('Action \'%s\' was not recognised.', $_GET['act'])); } } } - private function download() + protected function download() { $appConfig = require_once sprintf('%s/app.php', $this->configDir); $servicesConfig = require_once sprintf('%s/services.php', $this->configDir); @@ -58,16 +104,17 @@ class Bootstrapper if (is_null($releaseInfo)) { - throw new \Exception(sprintf('No release info found in Gitea for Blue Twilight version \'%\'', $versionNumber)); + throw new \Exception(sprintf('No release info found in Gitea for Blue Twilight version \'%s\'', $versionNumber)); } else if (!isset($releaseInfo->assets)) { - throw new \Exception(sprintf('No assets found in Gitea for Blue Twilight version \'%\'', $versionNumber)); + throw new \Exception(sprintf('No assets found in Gitea for Blue Twilight version \'%s\'', $versionNumber)); } $vendorsPrefix = 'vendors'; $vendorsSuffix = '.tar.gz'; $selectedAsset = null; + foreach ($releaseInfo->assets as $asset) { /* @@ -87,13 +134,68 @@ class Bootstrapper if (is_null($selectedAsset)) { - throw new \Exception('No vendors.tar.gz found in Gitea for Blue Twilight version \'%\'', $versionNumber); + throw new \Exception(sprintf('No vendors.tar.gz found in Gitea for Blue Twilight version \'%s\'', $versionNumber)); } - $targetFileName = sprintf('%s/vendors.tar.gz', $this->tempDir); + $targetFileName = $this->getVendorsTempFileName(); $this->downloadFile($selectedAsset->browser_download_url, $targetFileName); - var_dump('done'); + $this->json([ + 'result' => true, + 'fileName' => $targetFileName + ]); + } + + protected function extract() + { + $targetFileName = $this->getVendorsTempFileName(); + if (!file_exists($targetFileName) || !is_readable($targetFileName)) + { + throw new \Exception(sprintf('The file \'%s\' does not exist or is not readable', $targetFileName)); + } + + $phar = new \PharData($targetFileName); + $phar->extractTo($this->rootDir, null, true); + + // We should always have a vendor/autoload.php + $vendorsTestFile = $this->getVendorsAutoloadFileName(); + + if (file_exists($vendorsTestFile)) + { + $this->json([ + 'result' => true, + 'testFile' => $vendorsTestFile + ]); + } + else + { + throw new \Exception('The extraction failed'); + } + } + + protected function finalise() + { + $result = [ + 'envFileCreated' => false + ]; + + $result['envFileCreated'] = $this->createEnvFileIfNotExist(); + + require sprintf('%s/bootstrap/autoload.php', $this->rootDir); + + $app = require_once sprintf('%s/bootstrap/app.php', $this->rootDir); + + $kernel = $app->make(\Illuminate\Contracts\Console\Kernel::class); + $kernel->bootstrap(); + + if ($result['envFileCreated']) + { + Artisan::call('key:generate'); + } + + $kernel->terminate(null, null); + + $this->json($result); } private function downloadFile($sourceURL, $targetFilename) @@ -120,6 +222,44 @@ class Bootstrapper @fclose($tempFilename); } + private function createEnvFileIfNotExist() + { + $envFile = $this->getEnvFileName(); + + if (!file_exists($envFile)) + { + copy($this->getEnvExampleFileName(), $envFile); + return true; + } + + return false; + } + + private function getEnvExampleFileName() + { + return sprintf('%s/.env.example', $this->rootDir); + } + + private function getEnvFileName() + { + return sprintf('%s/.env', $this->rootDir); + } + + private function getVendorsAutoloadFileName() + { + return sprintf('%s/autoload.php', $this->vendorDir); + } + + private function getVendorsTempFileName() + { + return sprintf('%s/vendors.tar.gz', $this->tempDir); + } + + private function json($data) + { + echo json_encode($data); + } + private function view($name, array $viewData = []) { $viewFile = sprintf('%s/%s.php', $this->viewsDir, $name); diff --git a/public/bootstrap/views/index.php b/public/bootstrap/views/index.php index 641ab83..58b3dce 100644 --- a/public/bootstrap/views/index.php +++ b/public/bootstrap/views/index.php @@ -3,7 +3,7 @@ - + <?php echo $appName; ?>