Skip to content

Commit 70f6be7

Browse files
committed
Add compile script
1 parent 366dd73 commit 70f6be7

File tree

5 files changed

+81
-58
lines changed

5 files changed

+81
-58
lines changed

DEVELOPMENT.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ Then after you have logged out and in the _dcg_ development version will be acce
3434

3535
## Building PHAR executable
3636

37-
1. Install [Box 2](https://github.com/box-project/box2).
38-
2. Run `scripts/buld.sh` script.
37+
1. Install dependencies without dev packages `composer install --no-dev`.
38+
2. Run `scripts/compile.sh` script.
3939
3. Test the generated archive: `php dcg.phar --version`.

box.json

Lines changed: 0 additions & 36 deletions
This file was deleted.

scripts/build.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

scripts/compile.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
/**
5+
* @file
6+
* Compiles DCG into a PHAR file.
7+
*/
8+
9+
set_error_handler(function ($errno, $errstr, $errfile, $errline): void {
10+
fprintf(STDERR, "Error: %s on %s: %s\n", $errstr, $errfile, $errline);
11+
exit(1);
12+
});
13+
14+
$name = 'dcg.phar';
15+
$phar = new \Phar($name);
16+
$phar->startBuffering();
17+
18+
$stub = <<< EOF
19+
#!/usr/bin/env php
20+
<?php
21+
22+
Phar::mapPhar('$name');
23+
require 'phar://$name/bin/dcg';
24+
__HALT_COMPILER();
25+
EOF;
26+
27+
$phar->setStub($stub);
28+
29+
$files = array_merge(
30+
['bin/dcg', 'LICENSE.txt'],
31+
dcg_scan_dir('src'),
32+
dcg_scan_dir('templates'),
33+
dcg_scan_dir('resources'),
34+
dcg_scan_dir('vendor', 'php'),
35+
);
36+
foreach ($files as $file) {
37+
$extension = pathinfo($file, PATHINFO_EXTENSION);
38+
$content = $extension == 'php' ?
39+
php_strip_whitespace($file) : file_get_contents($file);
40+
if ($file == 'bin/dcg') {
41+
$content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content);
42+
}
43+
$phar->addFromString($file, $content);
44+
printf("Added file: %s\n", $file);
45+
}
46+
47+
$meta_data = [
48+
'build_time' => date('c'),
49+
'total_files' => count($files),
50+
'php_version' => PHP_VERSION,
51+
];
52+
$phar->setMetadata($meta_data);
53+
54+
$phar->stopBuffering();
55+
print "---------------------------------\n";
56+
printf("Total added: %s\n" , count($files));
57+
printf("PHAR file: %s\n", $phar->getPath());
58+
59+
/**
60+
* Recursively scans directory.
61+
*/
62+
function dcg_scan_dir(string $path, string $extension = NULL): array {
63+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
64+
$files = [];
65+
foreach ($iterator as $file) {
66+
if ($file->isDir() || ($extension && $extension != $file->getExtension())) {
67+
fprintf(STDERR, "\033[33mSkipped %s\033[0m\n", $file->getPathName());
68+
continue;
69+
}
70+
$files[] = $file->getPathname();
71+
}
72+
return $files;
73+
}

src/Application.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class Application extends BaseApplication {
2727
*/
2828
public const ROOT = __DIR__ . '/..';
2929

30+
/**
31+
* DCG version
32+
*/
33+
public const VERSION = '2.0.0-dev';
34+
3035
/**
3136
* Path to templates directory.
3237
*/
@@ -36,13 +41,7 @@ class Application extends BaseApplication {
3641
* Creates the application.
3742
*/
3843
public static function create(?ContainerInterface $container = NULL): Application {
39-
// This gets substituted with git version when DCG is packaged to PHAR file.
40-
$version = '@git-version@';
41-
// Fallback for composer installation.
42-
if (!\is_numeric($version[0])) {
43-
$version = 'UNKNOWN';
44-
}
45-
$application = new static('Drupal Code Generator', $version);
44+
$application = new static('Drupal Code Generator', self::VERSION);
4645

4746
$helper_set = new HelperSet([
4847
new QuestionHelper(),

0 commit comments

Comments
 (0)