|
| 1 | +<?php |
| 2 | +namespace Travoltron\Bootstrap4Preset; |
| 3 | + |
| 4 | +use Artisan; |
| 5 | +use Illuminate\Support\Arr; |
| 6 | +use Illuminate\Filesystem\Filesystem; |
| 7 | +use Illuminate\Foundation\Console\Presets\Preset; |
| 8 | + |
| 9 | +class Bootstrap4Preset extends Preset |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Install the preset. |
| 13 | + * |
| 14 | + * @return void |
| 15 | + */ |
| 16 | + public static function install($withAuth = false) |
| 17 | + { |
| 18 | + static::updatePackages(); |
| 19 | + static::updateSass(); |
| 20 | + static::updateBootstrapping(); |
| 21 | + static::updateMix(); |
| 22 | + |
| 23 | + if($withAuth) |
| 24 | + { |
| 25 | + static::addAuthTemplates(); |
| 26 | + } |
| 27 | + else |
| 28 | + { |
| 29 | + static::updateWelcomePage(); |
| 30 | + } |
| 31 | + |
| 32 | + static::removeNodeModules(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Update the given package array. |
| 37 | + * |
| 38 | + * @param array $packages |
| 39 | + * @return array |
| 40 | + */ |
| 41 | + protected static function updatePackageArray(array $packages) |
| 42 | + { |
| 43 | + return [ |
| 44 | + 'bootstrap' => '^4.0.0-beta', |
| 45 | + 'jquery' => '^3.2.1', |
| 46 | + 'popper.js' => '^1.11.0', |
| 47 | + ] + Arr::except($packages, ['foundation-sites', 'bootstrap-sass', 'bulma', 'uikit']); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Update the Sass files for the application. |
| 52 | + * |
| 53 | + * @return void |
| 54 | + */ |
| 55 | + protected static function updateSass() |
| 56 | + { |
| 57 | + // clean up orphan files |
| 58 | + $orphan_sass_files = glob(resource_path('/assets/sass/*.*')); |
| 59 | + |
| 60 | + foreach($orphan_sass_files as $sass_file) |
| 61 | + { |
| 62 | + (new Filesystem)->delete($sass_file); |
| 63 | + } |
| 64 | + |
| 65 | + copy(__DIR__.'/bootstrap4-stubs/_variables.scss', resource_path('assets/sass/_variables.scss')); |
| 66 | + copy(__DIR__.'/bootstrap4-stubs/app.scss', resource_path('assets/sass/app.scss')); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Update the bootstrapping files. |
| 71 | + * |
| 72 | + * @return void |
| 73 | + */ |
| 74 | + protected static function updateBootstrapping() |
| 75 | + { |
| 76 | + (new Filesystem)->delete( |
| 77 | + resource_path('assets/js/bootstrap.js') |
| 78 | + ); |
| 79 | + |
| 80 | + copy(__DIR__.'/bootstrap4-stubs/bootstrap.js', resource_path('assets/js/bootstrap.js')); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Update the mix file. |
| 85 | + * |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + protected static function updateMix() |
| 89 | + { |
| 90 | + (new Filesystem)->delete( |
| 91 | + base_path('webpack.mix.js') |
| 92 | + ); |
| 93 | + |
| 94 | + copy(__DIR__.'/bootstrap4-stubs/webpack.mix.js', base_path('webpack.mix.js')); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Update the default welcome page file with Foundation buttons. |
| 99 | + * |
| 100 | + * @return void |
| 101 | + */ |
| 102 | + protected static function updateWelcomePage() |
| 103 | + { |
| 104 | + // remove default welcome page |
| 105 | + (new Filesystem)->delete( |
| 106 | + resource_path('views/welcome.blade.php') |
| 107 | + ); |
| 108 | + |
| 109 | + // copy new one with Bootstrap buttons |
| 110 | + copy(__DIR__.'/bootstrap4-stubs/views/welcome.blade.php', resource_path('views/welcome.blade.php')); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Copy Bootstrap Auth view templates. |
| 115 | + * |
| 116 | + * @return void |
| 117 | + */ |
| 118 | + protected static function addAuthTemplates() |
| 119 | + { |
| 120 | + // Add Home controller |
| 121 | + copy(__DIR__.'/bootstrap4-stubs/Controllers/HomeController.php', app_path('Http/Controllers/HomeController.php')); |
| 122 | + |
| 123 | + // Add Auth route in 'routes/web.php' |
| 124 | + $auth_route_entry = "Auth::routes();\n\nRoute::get('/home', 'HomeController@index')->name('home');\n\n"; |
| 125 | + file_put_contents('./routes/web.php', $auth_route_entry, FILE_APPEND); |
| 126 | + |
| 127 | + // Copy Bootstrap4 Auth view templates |
| 128 | + (new Filesystem)->copyDirectory(__DIR__.'/bootstrap4-stubs/views', resource_path('views')); |
| 129 | + } |
| 130 | +} |
0 commit comments