|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Iulyanp\ElixirBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
8
|
|
|
use Symfony\Component\Filesystem\Exception\IOExceptionInterface; |
|
9
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ElixirCommand. |
|
13
|
|
|
*/ |
|
14
|
|
|
class ElixirCommand extends ContainerAwareCommand |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Configure. |
|
18
|
|
|
*/ |
|
19
|
|
|
public function configure() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->setName('elixir:init') |
|
22
|
|
|
->setDescription('Init package.json and gulpfile.'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param InputInterface $input |
|
27
|
|
|
* @param OutputInterface $output |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
|
32
|
|
|
{ |
|
33
|
|
|
$container = $this->getContainer(); |
|
34
|
|
|
$this->systemCheck($output); |
|
35
|
|
|
|
|
36
|
|
|
$this->writeInfo($output, 'Installing laravel elixir...'); |
|
37
|
|
|
|
|
38
|
|
|
$appDir = $container->getParameter('kernel.root_dir'); |
|
39
|
|
|
$webDir = $container->getParameter('web_dir'); |
|
40
|
|
|
$buildDir = $container->getParameter('build_dir'); |
|
41
|
|
|
$assetsPath = $container->getParameter('assets_dir'); |
|
42
|
|
|
|
|
43
|
|
|
$rootDir = $this->getRootDir($appDir); |
|
44
|
|
|
$publicPath = $this->getPublicPath($rootDir, $webDir); |
|
45
|
|
|
|
|
46
|
|
|
try { |
|
47
|
|
|
$fs = new Filesystem(); |
|
48
|
|
|
|
|
49
|
|
|
$packageContent = $this->getPackageContent(); |
|
50
|
|
|
$packagePath = sprintf('%s%s', $rootDir, 'package.json'); |
|
51
|
|
|
$fs->touch($packagePath); |
|
52
|
|
|
$fs->dumpFile($packagePath, $packageContent); |
|
53
|
|
|
|
|
54
|
|
|
$gulpContent = $this->getGulpfileContent($publicPath, $buildDir, $assetsPath); |
|
55
|
|
|
$gulpfilePath = sprintf('%s%s', $rootDir, 'gulpfile.js'); |
|
56
|
|
|
$fs->touch($gulpfilePath); |
|
57
|
|
|
$fs->dumpFile($gulpfilePath, $gulpContent); |
|
58
|
|
|
} catch (IOExceptionInterface $e) { |
|
59
|
|
|
$this->writeError($output, $e->getMessage()); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param OutputInterface $output |
|
65
|
|
|
* @param string $error |
|
66
|
|
|
* |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
*/ |
|
69
|
|
|
private function writeError(OutputInterface $output, $error) |
|
70
|
|
|
{ |
|
71
|
|
|
return $output->writeln('<error>'.$error.'</error>'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param OutputInterface $output |
|
76
|
|
|
* @param string $message |
|
77
|
|
|
* |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
private function writeInfo(OutputInterface $output, $message) |
|
81
|
|
|
{ |
|
82
|
|
|
return $output->writeln(sprintf('<info>%s</info>', $message)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
private function getPackageContent() |
|
89
|
|
|
{ |
|
90
|
|
|
return '{ |
|
91
|
|
|
"private": true, |
|
92
|
|
|
"scripts": { |
|
93
|
|
|
"prod": "gulp --production", |
|
94
|
|
|
"dev": "gulp watch" |
|
95
|
|
|
}, |
|
96
|
|
|
"author": "[email protected]", |
|
97
|
|
|
"devDependencies": { |
|
98
|
|
|
"gulp": "^3.9.1", |
|
99
|
|
|
"laravel-elixir": "^6.0.0-10", |
|
100
|
|
|
"gulp-sass": "^2.3.2" |
|
101
|
|
|
} |
|
102
|
|
|
}'; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $webDir |
|
107
|
|
|
* @param string $buildDir |
|
108
|
|
|
* @param string $assetsPath |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
private function getGulpfileContent($webDir, $buildDir, $assetsPath = 'app/Resources/assets') |
|
113
|
|
|
{ |
|
114
|
|
|
return sprintf("// Import Elixir. |
|
115
|
|
|
var elixir = require('laravel-elixir'); |
|
116
|
|
|
|
|
117
|
|
|
// Configure Elixir. |
|
118
|
|
|
elixir.config.publicPath = '%s'; |
|
119
|
|
|
//elixir.config.appPath = 'src'; |
|
120
|
|
|
elixir.config.versioning.buildFolder = '%s'; |
|
121
|
|
|
elixir.config.assetsPath = '%s'; |
|
122
|
|
|
|
|
123
|
|
|
// Set up Elixir tasks. |
|
124
|
|
|
elixir(function(mix) { |
|
125
|
|
|
|
|
126
|
|
|
mix.sass('app.scss') |
|
127
|
|
|
.version(['%s/css/app.css']); |
|
128
|
|
|
|
|
129
|
|
|
})", $webDir, $buildDir, $assetsPath, $webDir); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param OutputInterface $output |
|
134
|
|
|
*/ |
|
135
|
|
|
private function systemCheck(OutputInterface $output) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->writeInfo($output, 'Checking requirements...'); |
|
138
|
|
|
|
|
139
|
|
|
$errors = []; |
|
140
|
|
|
$checks = []; |
|
141
|
|
|
$requirements = ['node', 'npm', 'gulp']; |
|
142
|
|
|
foreach ($requirements as $requirement) { |
|
143
|
|
|
if (!$checks[$requirement] = exec($requirement.' -v')) { |
|
144
|
|
|
$errors[$requirement] = sprintf('You should first install `%s`.', $requirement); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if (!empty($errors)) { |
|
149
|
|
|
foreach ($errors as $error) { |
|
150
|
|
|
$this->writeError($output, $error); |
|
151
|
|
|
} |
|
152
|
|
|
die; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
foreach ($checks as $key => $check) { |
|
156
|
|
|
$output->writeln(sprintf('%s %s: %s', '<info>[OK]</info>', $key, $check)); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param $appDir |
|
162
|
|
|
* |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
private function getRootDir($appDir) |
|
166
|
|
|
{ |
|
167
|
|
|
return sprintf('%s%s%s%s', $appDir, DIRECTORY_SEPARATOR, '..', DIRECTORY_SEPARATOR); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param $rootDir |
|
172
|
|
|
* @param $webDir |
|
173
|
|
|
* |
|
174
|
|
|
* @return mixed |
|
175
|
|
|
*/ |
|
176
|
|
|
private function getPublicPath($rootDir, $webDir) |
|
177
|
|
|
{ |
|
178
|
|
|
return str_replace($rootDir, '', $webDir); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|