-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-phar.php
More file actions
98 lines (82 loc) · 3.13 KB
/
Copy pathbuild-phar.php
File metadata and controls
98 lines (82 loc) · 3.13 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
<?php
declare(strict_types=1);
/**
* Manual PHAR builder for kcode — bypasses Box chdir() bug on PHP 8.4.
*
* The PHAR is intentionally lean: it bundles only the KaririCode\Devkit
* source classes and a minimal PSR-4 autoloader. Dev tools (phpunit,
* phpstan, etc.) are NOT bundled — `kcode init` installs them dynamically
* into .kcode/vendor/ of the target project via composer.
*/
$root = dirname(__DIR__);
$output = $root . '/build/kcode.phar';
if (file_exists($output)) {
unlink($output);
}
@mkdir($root . '/build', 0755, true);
$phar = new Phar($output, 0, 'kcode.phar');
$phar->startBuffering();
echo "📦 Building kcode.phar...\n";
// ── 1. Add src/ ────────────────────────────────────────────
$added = 0;
$srcDir = $root . '/src';
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($srcDir, FilesystemIterator::SKIP_DOTS));
foreach ($it as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$relative = 'src/' . $it->getSubPathname();
$phar[$relative] = file_get_contents($file->getPathname());
$added++;
}
}
echo " + src/: $added PHP files\n";
// ── 2. Minimal PSR-4 autoloader (no Composer vendor/ needed) ─
// The project has zero PHP production dependencies.
// We generate a lean autoloader that maps KaririCode\Devkit → src/.
$autoloader = <<<'PHP'
<?php
spl_autoload_register(static function (string $class): void {
$prefix = 'KaririCode\\Devkit\\';
if (!str_starts_with($class, $prefix)) {
return;
}
$relative = substr($class, strlen($prefix));
$file = 'phar://kcode.phar/src/' . str_replace('\\', '/', $relative) . '.php';
if (is_file($file)) {
require $file;
}
});
PHP;
$phar['vendor/autoload.php'] = $autoloader;
echo " + vendor/autoload.php: inline PSR-4 autoloader\n";
// ── 3. Add LICENSE ──────────────────────────────────────────
$phar['LICENSE'] = file_get_contents($root . '/LICENSE');
echo " + LICENSE\n";
// ── 4. Set bin/kcode as the entry point (stub) ─────────────
$kcodeContent = file_get_contents($root . '/bin/kcode');
$phar['bin/kcode'] = $kcodeContent;
$stub = <<<'STUB'
#!/usr/bin/env php
<?php
/*
* KaririCode Devkit — kcode
*
* Unified quality toolchain for KaririCode Framework.
*
* (c) Walmir Silva <walmir.silva@kariricode.org>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
Phar::mapPhar('kcode.phar');
require 'phar://kcode.phar/bin/kcode';
__HALT_COMPILER();
STUB;
$phar->setStub($stub);
echo " + stub (bin/kcode entry point)\n";
// ── 5. Finalize ─────────────────────────────────────────────
$phar->stopBuffering();
$phar->compressFiles(Phar::GZ);
chmod($output, 0755);
$size = round(filesize($output) / 1024 / 1024, 2);
echo "\n✅ Built: $output ($size MB)\n";
echo " Files: " . count($phar) . "\n";