-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoboFile.php
More file actions
executable file
·141 lines (127 loc) · 4.73 KB
/
Copy pathRoboFile.php
File metadata and controls
executable file
·141 lines (127 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
if (file_exists($composer = __DIR__ . '/vendor/autoload.php')) {
require_once $composer;
}
class RoboFile extends \Robo\Tasks
{
use \Generoi\Robo\Task\Placeholder\loadTasks;
use \Generoi\Robo\Command\SearchReplaceCommand;
public $machineName = 'text-replacements';
public $name = 'Text Replacements';
public $namespace = 'TextReplacements';
public $description = 'Replace text';
public function rename($machineName = null, $options = [
'force' => false,
'namespace' => null,
'description' => null,
'name' => null,
])
{
$name = $options['name'];
$namespace = $options['namespace'];
$description = $options['description'];
if (empty($machineName)) {
$machineName = $this->askDefault('Machine name', $this->machineName);
}
if (empty($name)) {
$name = $this->askDefault('Plugin name', $this->name);
}
if (empty($namespace)) {
$namespace = $this->askDefault('Namespace', $this->namespace);
}
if (empty($description)) {
$description = $this->askDefault('Description', $this->description);
}
$result = $this->taskPlaceholderFind("$this->machineName|$this->name|$this->namespace|$this->description")
->io($this->io())
->run();
$files = $result['files'];
if (count($files) > 0) {
if (empty($options['force'])) {
$options['force'] = $this->io()->confirm(sprintf('Do you want to replace names in these files?'));
}
if (!empty($options['force'])) {
$this->taskPlaceholderReplace($this->machineName)->with($machineName)->in($files)->run();
$this->taskPlaceholderReplace($this->name)->with($name)->in($files)->run();
$this->taskPlaceholderReplace($this->namespace)->with($namespace)->in($files)->run();
$this->taskPlaceholderReplace($this->description)->with($description)->in($files)->run();
}
if (in_array('./composer.json', $files)) {
$this->taskComposerDumpAutoload()->run();
}
}
}
public function versionBump($version = '', $options = ['stage' => ''])
{
// If the user did not specify a version, then update the current version.
if (empty($version)) {
$version = $this->incrementVersion($this->versionCurrent(), $options['stage']);
}
$this->say('Updated Plugin version to ' . $version);
return $this->writeVersion($version);
}
public function versionCurrent()
{
$pluginPhp = file_get_contents('plugin.php');
if (preg_match('/^[ \t\/*#@]*Version:(.*)$/mi', $pluginPhp, $matches) && $matches[1]) {
return trim($matches[1]);
}
}
protected function writeVersion($version)
{
$this->taskReplaceInFile('plugin.php')
->regex("#version = '[^']*'#")
->to("version = '" . $version . "'")
->run();
$this->taskReplaceInFile('plugin.php')
->regex('#^Version:(\s*)([^\s]*)$#m')
->to('Version:${1}' . $version)
->run();
$this->taskReplaceInFile('package.json')
->regex('#"version": "[^"]*"#')
->to('"version": "' . $version . '"')
->run();
}
protected function incrementVersion($version, $stage = '')
{
if (empty($stage)) {
$stage = 'patch';
}
$stable = ($stage === 'minor') || ($stage === 'major') || ($stage === 'patch');
$versionStageNumber = '0';
preg_match('/-([a-zA-Z]*)([0-9]*)/', $version, $match);
$match += ['', '', ''];
$versionStage = $match[1];
$versionStageNumber = $match[2];
if ($versionStage != $stage) {
$versionStageNumber = 0;
}
$version = preg_replace('/-.*/', '', $version);
$versionParts = explode('.', $version);
if ($stable) {
switch ($stage) {
case 'major':
$versionParts[0]++;
$versionParts[1] = '0';
$versionParts[2] = '0';
break;
case 'minor':
$versionParts[1]++;
$versionParts[2] = '0';
break;
case 'patch':
$versionParts[2]++;
break;
}
}
$version = implode('.', $versionParts);
if (!$stable) {
$version .= '-' . $stage;
if ($stage != 'dev') {
$versionStageNumber++;
$version .= $versionStageNumber;
}
}
return $version;
}
}