-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.php
285 lines (239 loc) · 6.65 KB
/
deploy.php
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* @package Redbit\SimpleShop\WpPlugin
* @license MIT
* @copyright 2016-2022 Redbit s.r.o.
* @author Redbit s.r.o. <[email protected]>
*/
$deploy = new DeployScript( 'simpleshop-cz' );
$deploy->distDir = __DIR__ . '/dist';
try {
$deploy->run( $argv );
} catch ( \Exception $e ) {
$message = $e->getMessage();
echo PHP_EOL;
fwrite( STDERR, 'Error: ' . $message . PHP_EOL );
die( 1 );
}
class DeployScript {
/** @var string */
public $distDir = '';
/** @var string */
private $productName;
/** @var string */
private $scriptName;
/** @var string|null */
private $version;
/**
* @param string $productName
*/
public function __construct( $productName ) {
$this->productName = $productName;
}
/**
* @param array $args
*
* @throws RuntimeException
*/
public function run( array $args ) {
$this->scriptName = $args[0];
$this->version = isset( $args[1] ) ? $args[1] : null;
$this->validateVersion( $this->version );
// Update Composer deps
echo "Updating Composer dependencies...\n";
$this->updateComposer();
echo "Updating Composer dependencides done OK.\n\n";
// Update NPM deps
echo "Building Gutenberg blocks...\n";
$this->buildBlock();
echo "Building Gutenberg blocks done OK.\n\n";
// Building package
echo sprintf( 'Building package for version %s... ', $this->version );
$package = $this->buildPackage();
echo sprintf( "OK\nBuilt package at %s\n", $package );
}
/**
* @param string|null $version
*
* @throws RuntimeException
*/
private function validateVersion( $version ) {
if ( $version === null ) {
echo sprintf( "Usage: php %1\$s <version>\nExample: php %1\$s v1.2.3\n", $this->scriptName );
die( 0 );
}
if ( ! preg_match( '/^v\d+\.\d+\.\d+(-[-.a-z0-9]+)?$/D', $version ) ) {
throw new \RuntimeException( sprintf(
"Argument \"version\" value \"%s\" has invalid format.\n" .
"Example stable: v1.2.3\nExample unstable: v1.2.3-beta",
$version
) );
}
}
/**
* @return string
* @throws RuntimeException
*/
private function buildPackage() {
$this->prepareDist();
$packageFile = $this->getPackageName();
$this->zip( __DIR__, $packageFile );
// Stamp root file
$stampedFile = $this->copyStampedFile(
__DIR__ . '/' . $this->productName . '.php',
array(
'Version: dev-master' => sprintf(
'Version: %s',
$this->getNakedVersion()
),
'define( \'SIMPLESHOP_PLUGIN_VERSION\', \'dev-master\' );' => sprintf(
'define( \'SIMPLESHOP_PLUGIN_VERSION\', \'%s\' );',
$this->version
),
),
$this->distDir
);
$this->zip( $stampedFile, $packageFile, $this->distDir );
unlink( $stampedFile );
// Stamp readme.txt
$stampedFile = $this->copyStampedFile(
__DIR__ . '/readme.txt',
array(
'Stable tag: trunk' => sprintf(
'Stable tag: %s',
$this->getNakedVersion()
),
),
$this->distDir
);
$this->zip( $stampedFile, $packageFile, $this->distDir );
unlink( $stampedFile );
return $packageFile;
}
/**
* @throws RuntimeException
*/
private function prepareDist() {
if ( in_array( $this->distDir, array( null, '', __DIR__ ), true ) ) {
return;
}
$this->createDir( $this->distDir );
$this->cleanDir( $this->distDir );
}
/**
* @param string $dir
*
* @throws RuntimeException
*/
private function createDir( $dir ) {
if ( ! is_dir( $dir ) && ! mkdir( $dir ) && ! is_dir( $dir ) ) {
throw new \RuntimeException( sprintf( 'Directory "%1$s" was not created', $dir ) );
}
}
/**
* @param string $dir
*
* @throws RuntimeException
*/
private function cleanDir( $dir ) {
$files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator(
$dir,
FilesystemIterator::SKIP_DOTS
) );
/** @var SplFileInfo $file */
foreach ( $files as $file ) {
if ( $file->isDir() ) {
continue;
}
if ( ! unlink( $fileName = $file->getPathname() ) ) {
throw new \RuntimeException( sprintf( 'Unable to delete file "%s"', $fileName ) );
}
}
}
private function updateComposer( $baseDir = __DIR__ ) {
$prevDir = getcwd();
$cmd = 'composer install --ignore-platform-reqs && composer status --verbose';
chdir( $baseDir );
passthru( $cmd, $return_var );
chdir( $prevDir );
if ( $return_var !== 0 ) {
throw new \RuntimeException( sprintf( 'Composer command failed, command was: "%s"', $cmd ) );
}
}
private function buildBlock( $baseDir = __DIR__ ) {
$prevDir = getcwd();
$cmd = 'npm install && npm run build';
chdir( $baseDir );
passthru( $cmd, $return_var );
chdir( $prevDir );
if ( $return_var !== 0 ) {
throw new \RuntimeException( sprintf( 'NPM command failed, command was: "%s"', $cmd ) );
}
}
/**
* @return string
*/
private function getPackageName() {
return sprintf(
'%s/%s-%s.zip',
$this->distDir,
$this->productName,
$this->version
);
}
/**
* @param string $fileOrPath
* @param string $zip
* @param string $baseDir
*
* @throws RuntimeException
*/
private function zip( $fileOrPath, $zip, $baseDir = __DIR__ ) {
// Short path to remove root-directory in zip (/home/user/project/data.ext -> ./data.ext)
$relativePath = str_replace( $baseDir, '.', $fileOrPath );
$prevDir = getcwd();
$cmd = sprintf(
'zip -9rq -x@%s %s %s',
__DIR__ . '/deploy-exclude.lst',
escapeshellarg( $zip ),
escapeshellarg( $relativePath )
);
chdir( $baseDir );
passthru( $cmd, $return_var );
chdir( $prevDir );
if ( $return_var !== 0 ) {
throw new \RuntimeException( sprintf( 'Zip failed, command was: "%s"', $cmd ) );
}
}
/**
* Strips "v" from versions (v1.2.3-beta -> 1.2.3)
*
* @return bool|string
*/
private function getNakedVersion() {
return preg_replace( '/^v(\d+\.\d+\.\d+)(?:-.*)*$/D', '$1', $this->version );
}
/**
* @param string $fileName Source file to copy$stamp
* @param array $replacements Stamp replacements for `str_replace()`, key = search pattern, value = replacement
* @param string $targetDir Dir path to save stamped file
*
* @return string Filename of copied file
* @throws RuntimeException
*/
private function copyStampedFile( $fileName, $replacements, $targetDir ) {
$content = file_get_contents( $fileName );
if ( $content === false ) {
throw new \RuntimeException( sprintf( 'Unable to open file: "%s"', $fileName ) );
}
foreach ( $replacements as $from => $to ) {
$content = str_replace( $from, $to, $content, $count );
if ( $count === 0 ) {
throw new \RuntimeException( sprintf( 'Not found pattern "%s" in file: %s', $from, $fileName ) );
}
}
$newFilename = rtrim( $targetDir, '/' ) . '/' . basename( $fileName );
file_put_contents( $newFilename, $content );
return $newFilename;
}
}