-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforge.php
138 lines (114 loc) · 3.79 KB
/
forge.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
#!/usr/bin/env php
<?php
declare(strict_types=1);
// Command handling
$command = $_SERVER['argv'][1] ?? 'help';
$arguments = array_slice($_SERVER['argv'], 2);
switch ($command) {
case 'publish':
handlePublishCommand($arguments);
break;
case 'help':
default:
displayHelp();
break;
}
function displayHelp(): void
{
echo "Forge Module Publisher\n";
echo "Usage: php forge.php <command> [arguments]\n\n";
echo "Commands:\n";
echo " publish <module-name>@<version> Package and publish a module version.\n";
echo " help Display this help message.\n";
}
function handlePublishCommand(array $arguments): void
{
if (count($arguments) !== 1) {
echo "Error: 'publish' command requires one argument: <module-name>@<version>\n";
displayHelp();
exit(1);
}
$moduleVersionString = $arguments[0];
if (!preg_match('/^([a-z0-9\-]+)@([0-9\.]+)/', $moduleVersionString, $matches)) {
echo "Error: Invalid module version format. Use <module-name>@<version> (e.g., forge-router@1.0.0)\n";
exit(1);
}
$moduleName = $matches[1];
$version = $matches[2];
echo "Publishing module: {$moduleName} version {$version}\n";
$moduleVersionPath = __DIR__ . '/modules/' . $moduleName . '/' . $version;
$zipFilePath = $moduleVersionPath . '/' . $version . '.zip';
if (!is_dir($moduleVersionPath)) {
echo "Error: Module version path not found: {$moduleVersionPath}\n";
exit(1);
}
echo "Creating zip file: {$zipFilePath}\n";
if (createModuleZip($moduleVersionPath, $zipFilePath)) {
echo "Zip file created successfully.\n";
// Git operations
echo "Staging zip file with git...\n";
if (gitAdd($zipFilePath)) {
echo "Zip file staged.\n";
gitAdd('modules.json');
echo "Committing changes with git...\n";
if (gitCommit("Add zip for {$moduleName}@{$version}")) {
echo "Changes committed.\n";
echo "Pushing changes to git repository...\n";
if (gitPush()) {
echo "Changes pushed successfully.\n";
echo "Module {$moduleName} version {$version} published!\n";
exit(0);
} else {
echo "Error: Git push failed.\n";
exit(1);
}
} else {
echo "Error: Git commit failed.\n";
exit(1);
}
} else {
echo "Error: Git add failed.\n";
exit(1);
}
} else {
echo "Error: Failed to create zip file.\n";
exit(1);
}
}
function createModuleZip(string $sourceDir, string $zipFilePath): bool
{
$zip = new ZipArchive();
if ($zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
return false;
}
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($sourceDir),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($sourceDir) + 1);
$zip->addFile($filePath, $relativePath);
}
}
return $zip->close();
}
function gitAdd(string $filePath): bool
{
$command = "git add " . escapeshellarg($filePath);
exec($command, $output, $returnVar);
return $returnVar === 0;
}
function gitCommit(string $message): bool
{
$command = "git commit -m " . escapeshellarg($message);
exec($command, $output, $returnVar);
return $returnVar === 0;
}
function gitPush(): bool
{
$command = "git push";
exec($command, $output, $returnVar);
return $returnVar === 0;
}