-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnexus-upload.php
More file actions
executable file
·240 lines (199 loc) · 6.51 KB
/
nexus-upload.php
File metadata and controls
executable file
·240 lines (199 loc) · 6.51 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
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
#!/usr/bin/env php
<?php
const RESET = "\033[0m";
const RED = "\033[1;31m";
const GREEN = "\033[1;32m";
const YELLOW = "\033[1;33m";
const BLUE = "\033[1;34m";
/**
* @param string $path
* @param array $ignoreList
* @return bool
*/
function isIgnorebale($path, $ignoreList): bool
{
foreach ($ignoreList as $pattern) {
if (preg_match($pattern, $path)) {
return true;
}
}
return false;
}
function zipDirectory(string $directory, string $zipPath, callable $fileFilter): void
{
$rootRealPath = realpath($directory);
$zipArchive = new ZipArchive();
$zipArchive->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootRealPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $file) {
if ($file->isDir()) continue;
$realpath = $file->getRealPath();
$relativePath = substr($realpath, strlen($rootRealPath) + 1);
if ($fileFilter($relativePath)) {
$zipArchive->addFile($realpath, $relativePath);
echo BLUE . "Adding: $relativePath" . RESET . PHP_EOL;
}
}
$zipArchive->close();
}
function curlPutFile(string $url, string $filename, string $username, string $password): bool
{
echo YELLOW . "Preparing HTTP PUT request...\n" . RESET;
echo "\tURL: $url\n";
echo "\tFile: $filename\n";
echo "\tSize: " . filesize($filename) . " bytes\n";
echo "\tUsername: $username\n\n";
$filestream = fopen($filename, "rb");
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_USERPWD => "$username:$password",
CURLOPT_PUT => true,
CURLOPT_INFILE => $filestream,
CURLOPT_INFILESIZE => filesize($filename),
CURLOPT_HEADER => true,
]);
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
$errno = curl_errno($ch);
curl_close($ch);
echo RED . "cURL Error ($errno): $error\n" . RESET;
return false;
}
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headers = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
curl_close($ch);
echo BLUE . "Response Status: HTTP $statusCode\n" . RESET;
echo BLUE . "Response Headers:\n" . RESET;
foreach (explode("\n", trim($headers)) as $headerLine) {
echo "\t$headerLine\n";
}
// Print short body summary
$trimmedBody = trim($body);
if ($trimmedBody !== '') {
echo BLUE . "Response Body (first 500 chars):\n" . RESET;
echo substr($trimmedBody, 0, 500) . (strlen($trimmedBody) > 500 ? "..." : "") . "\n";
} else {
echo BLUE . "Response Body: (empty)\n" . RESET;
}
if ($statusCode !== 200) {
echo RED . "Upload failed: HTTP $statusCode\n" . RESET;
return false;
}
echo GREEN . "Upload succeeded with HTTP $statusCode\n" . RESET;
return true;
}
function getComposerJson(): array
{
static $composerJson;
if (!isset($composerJson)) {
$path = getcwd() . '/composer.json';
$composerJson = json_decode(file_get_contents($path), true);
}
return $composerJson;
}
function getComposerOptions(): array
{
return getComposerJson()['extra']['nexus-upload'] ?? [];
}
function getCliOptions(): array
{
static $options;
if (!isset($options)) {
$options = getopt('', [
'repository:',
'username:',
'password::',
'version:',
'ignore:',
]);
}
return $options;
}
function getProperties(): array
{
$path = getcwd() . '/.nexus';
if (!file_exists($path)) return [];
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$options = [];
foreach ($lines as $line) {
$line = trim($line);
if (str_starts_with($line, '#')) continue;
[$key, $value] = explode('=', $line, 2);
$options[trim($key)] = trim($value);
}
return $options;
}
/**
* @param string $option
* @return mixed
*/
function getOption(string $option) {
static $options;
if (!isset($options)) {
$options = array_merge(
getProperties(),
getComposerOptions(),
getCliOptions()
);
}
return $options[$option] ?? null;
}
// === Main Execution ===
$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)/";
$ignore = is_array($ignore) ? [...$ignore, $stdIgnore] : [$ignore, $stdIgnore];
$ignoreList = array_filter(array_map(function ($pattern) {
if ($pattern === null) return false;
if (str_starts_with($pattern, '/')) return $pattern;
$pattern = str_replace(['*', '/'], ['.*', '\/'], preg_quote($pattern));
return '/^' . $pattern . '/';
}, $ignore));
// Summary
echo YELLOW . "Running with:\n" . RESET;
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\n";
if (empty($version)) {
echo RED . "Version is required.\n" . RESET;
exit(1);
}
$projectDir = getcwd();
$zipFileName = $projectDir . '/' . str_replace('/', '-', $packageName) . "-$version.zip";
echo YELLOW . "Zipping project directory...\n" . RESET;
zipDirectory($projectDir, $zipFileName, fn($path) => !isIgnorebale($path, $ignoreList));
$filesize = filesize($zipFileName);
echo GREEN . "Created: $zipFileName ($filesize bytes)\n" . RESET;
if ($filesize === 0) {
echo RED . "Zip file is empty. Aborting.\n" . RESET;
exit(1);
}
$url = rtrim($nexusRepo, '/') . "/packages/upload/$packageName/$version";
echo YELLOW . "Uploading to: $url\n" . RESET;
try {
if (curlPutFile($url, $zipFileName, $username, $password)) {
echo GREEN . "Upload complete.\n" . RESET;
} else {
echo RED . "Upload failed.\n" . RESET;
exit(1);
}
} catch (Exception $e) {
echo RED . "Error: " . $e->getMessage() . "\n" . RESET;
exit(1);
}