forked from jbuncle/composer-nexus-upload
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nexus-upload.php
executable file
·274 lines (230 loc) · 6.79 KB
/
nexus-upload.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
#!/usr/bin/env php
<?php
/**
* PHP Script to upload/push a composer project to a Nexus Repository Manager (that supports composer).
*/
/**
*
* @param string $path
* @param array $ignoreList
*
* @return boolean
*/
function isIgnorebale($path, $ignoreList) {
foreach ($ignoreList as $pattern) {
if (preg_match($pattern, $path)) {
return true;
}
}
return false;
}
/**
*
* @param string $directory
* @param string $zipPath
* @param callable $fileFilter
*/
function zipDirectory($directory, $zipPath, $fileFilter) {
// Get real path for our folder
$rootRealPath = realpath($directory);
// Initialize archive object
$zipArchive = new ZipArchive();
$zipArchive->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE);
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootRealPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $file) {
if ($file->isDir()) {
// Skip directories (they would be added automatically)
continue;
}
$realpath = $file->getRealPath();
$relativePath = substr($realpath, strlen($rootRealPath) + 1);
// Decide whether file should be included
if (\call_user_func($fileFilter, $relativePath)) {
$zipArchive->addFile($realpath, $relativePath);
}
}
$zipArchive->close();
}
function curlPutFile($url, $filename, $username, $password) {
$filestream = fopen($filename, "rb");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_INFILE, $filestream);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filename));
// Enable headers
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($statusCode !== 200) {
throw new Exception("Failed to PUT file with response code $statusCode - '$result'");
} else {
return true;
}
}
/**
* Get composer JSON Object
* @staticvar array<stdClass> $composerJsons
* @param string $projectUrl
*
* @return array
*/
function getComposerJson() {
static $composerJsons;
if (!isset($composerJson)) {
$composerJsonPath = getcwd() . '/composer.json';
$fileContents = file_get_contents($composerJsonPath);
$composerJson = \json_decode($fileContents, true);
}
return $composerJson;
}
function getComposerOptions() {
$json = getComposerJson();
if (!isset($json['extra'])) {
return [];
}
if (!isset($json['extra']['nexus-upload'])) {
return [];
}
return $json['extra']['nexus-upload'];
}
/**
* Get option from CLI.
*
* @staticvar array $options
*
* @return string
*/
function getCliOptions() {
static $options;
if (!isset($options)) {
// Followed by single colon = required
// Followed by double colon = optional
$options = getopt(
'',
[
'repository:',
'username:',
'password::',
'version:',
'ignore:',
]
);
}
return $options;
}
function getProperties() {
$filepath = getcwd() . DIRECTORY_SEPARATOR . ".nexus";
if (!file_exists($filepath)) {
return [];
}
$contents = file_get_contents($filepath);
$lines = explode("\n", $contents);
$options = [];
foreach ($lines as $line) {
$ltrimmed = ltrim($line);
if (strpos($ltrimmed, '#') === 0) {
continue;
}
$rtrimmed = rtrim($ltrimmed, "\r\n");
$parts = explode('=', $rtrimmed, 2);
$options[trim($parts[0])] = $parts[1];
}
return $options;
}
function getOption($option) {
static $options;
if (!isset($options)) {
$cliOptions = getCliOptions();
$composerOptions = getComposerOptions();
$properties = getProperties();
$options = array_merge(
$properties,
$composerOptions,
$cliOptions
);
}
if (!array_key_exists($option, $options)) {
return null;
}
return $options[$option];
}
$packageName = getComposerJson()['name'];
$nexusRepo = getOption('repository');
$username = getOption('username');
$password = getOption('password');
$version = getOption('version');
$ignore = getOption('ignore');
$stdIgnore = "/^(\.git|vendor|composer\.lock|\.gitignore|\.nexus)/";
if (is_array($ignore)) {
$ignore[] = $stdIgnore;
} else {
$ignore = [
$stdIgnore, // Standard PHP ignores
$ignore
];
}
// Process
$ignoreList = array_map(function($value) {
if ($value === null) {
return false;
}
if (strpos($value, '/') !== 0) {
// Quote the string
$value = preg_quote($value);
// Escape slashes
$value = str_replace('/', '\/', $value);
// Allow * wildcards
$value = str_replace('\*', '.*', $value);
return '/^' . $value . '/';
} else {
return $value;
}
}, $ignore);
$ignoreList = array_filter($ignoreList);
echo "Running with:\n";
echo "\tRepository: $nexusRepo\n";
echo "\tUsername: $username\n";
echo "\tPassword: " . (!empty($password)) ? '(provided)' : 'missing' . "\n";
echo "\tVersion: $version\n";
echo "\tIgnore patterns: " . implode(', ', $ignoreList) . "\n";
echo "\n";
if (empty($version)) {
echo "Version is empty\n";
exit();
}
$projectDir = getcwd();
$zipFileName = $projectDir . DIRECTORY_SEPARATOR . str_replace('/', '-', $packageName) . '-' . $version . '.zip';
echo "Zipping '{$projectDir}' as '$zipFileName'\n";
zipDirectory($projectDir, $zipFileName, function($path) use ($ignoreList) {
if (isIgnorebale($path, $ignoreList)) {
return false;
}
echo "Adding '" . $path . "' \n";
return true;
});
echo "Created '$zipFileName' (" . filesize($zipFileName) . " bytes)\n";
$url = $nexusRepo . "packages/upload/" . $packageName . '/' . $version;
echo "\n";
echo "Uploading '$zipFileName' to '$url'\n";
if (filesize($zipFileName) === 0) {
throw new Exception("Zip file is empty");
}
$success = curlPutFile($url, $zipFileName, $username, $password);
if (!$success) {
echo "Failed to upload zip to repository\n";
echo "Please note that this may be a false error and check if the file has been uploaded to Nexus anyway.\n";
exit(1);
} else {
echo "Finished\n";
}