|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fabrica\Tools\Plugin; |
|
4
|
|
|
|
|
5
|
|
|
use Fabrica\Tools\Builder; |
|
6
|
|
|
use Fabrica\Models\Infra\Ci\Build; |
|
7
|
|
|
use Fabrica\Tools\Plugin; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Grunt Plugin - Provides access to grunt functionality. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Tobias Tom <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
View Code Duplication |
class Grunt extends Plugin |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
protected $task; |
|
17
|
|
|
protected $preferDist; |
|
18
|
|
|
|
|
19
|
|
|
protected $gruntfile; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @return string |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function pluginName() |
|
25
|
|
|
{ |
|
26
|
|
|
return 'grunt'; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Builder $builder, Build $build, array $options = []) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct($builder, $build, $options); |
|
35
|
|
|
|
|
36
|
|
|
$this->task = null; |
|
37
|
|
|
|
|
38
|
|
|
$this->gruntfile = 'Gruntfile.js'; |
|
39
|
|
|
|
|
40
|
|
|
if (isset($options['task'])) { |
|
41
|
|
|
$this->task = $options['task']; |
|
42
|
|
|
} |
|
43
|
|
|
// deprecated compatibility option |
|
44
|
|
|
if (isset($options['grunt']) && !isset($options['executable'])) { |
|
45
|
|
|
$options['executable'] = $options['grunt']; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @deprecated Option "grunt" is deprecated and will be deleted in version 2.0. Use the option "binary_path" and "binary_name" instead. |
|
50
|
|
|
*/ |
|
51
|
|
|
if (isset($options['grunt'])) { |
|
52
|
|
|
$this->builder->logWarning( |
|
53
|
|
|
'[DEPRECATED] Option "grunt" is deprecated and will be deleted in version 2.0. Use the option "binary_path" and "binary_name" instead.' |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
$this->executable = $options['grunt']; |
|
57
|
|
|
} else { |
|
58
|
|
|
$this->executable = $this->findBinary('grunt'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (isset($options['gruntfile'])) { |
|
62
|
|
|
$this->gruntfile = $options['gruntfile']; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Executes grunt and runs a specified command (e.g. install / update) |
|
68
|
|
|
*/ |
|
69
|
|
|
public function execute() |
|
70
|
|
|
{ |
|
71
|
|
|
// if npm does not work, we cannot use grunt, so we return false |
|
72
|
|
|
$cmd = 'cd %s && npm install'; |
|
73
|
|
|
if (!$this->builder->executeCommand($cmd, $this->directory)) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// build the grunt command |
|
78
|
|
|
$cmd = 'cd %s && ' . $this->executable; |
|
79
|
|
|
$cmd .= ' --no-color'; |
|
80
|
|
|
$cmd .= ' --gruntfile %s'; |
|
81
|
|
|
$cmd .= ' %s'; // the task that will be executed |
|
82
|
|
|
|
|
83
|
|
|
// and execute it |
|
84
|
|
|
return $this->builder->executeCommand($cmd, $this->directory, $this->gruntfile, $this->task); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.