Skip to content

Commit 872086a

Browse files
committed
Drop support for installaion from PHAR file
1 parent 72283fa commit 872086a

File tree

5 files changed

+3
-493
lines changed

5 files changed

+3
-493
lines changed

DEVELOPMENT.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,3 @@ Then after you have logged out and in the _dcg_ development version will be acce
3131
# Test generated code.
3232
/path/to/drupal-code-generator/scripts/run-sut-tests.sh
3333
```
34-
35-
## Building PHAR executable
36-
37-
1. Install dependencies without dev packages `composer install --no-dev`.
38-
2. Run `scripts/compile.sh` script.
39-
3. Test the generated archive: `php dcg.phar --version`.

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ A command line code generator for Drupal.
88

99
## Installation
1010

11-
1. Download the latest [stable release](https://github.com/Chi-teck/drupal-code-generator/releases) of the code generator.
12-
2. Make the file executable.
13-
3. Move it to a directory that is part of your `PATH`.
14-
15-
Installation using Composer is also supported.
11+
```
12+
composer require chi-teck/drupal-code-generator --dev
13+
```
1614

1715
## Upgrade
1816
Simply repeat installation commands.

scripts/compile.php

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

scripts/dump-hooks.php

100755100644
Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +0,0 @@
1-
#!/usr/bin/env php
2-
<?php declare(strict_types=1);
3-
4-
/**
5-
* @file
6-
* Generates hook templates from API documentation.
7-
*
8-
* phpcs:ignoreFile Drupal.Commenting.FileComment.Missing
9-
*/
10-
11-
use Symfony\Component\Console\Application;
12-
use Symfony\Component\Console\Exception\RuntimeException;
13-
use Symfony\Component\Console\Input\InputArgument;
14-
use Symfony\Component\Console\Input\InputInterface;
15-
use Symfony\Component\Console\Output\OutputInterface;
16-
use Symfony\Component\Filesystem\Filesystem;
17-
18-
require __DIR__ . '/../vendor/autoload.php';
19-
20-
/**
21-
* Dumps hooks.
22-
*/
23-
function dump_hooks(InputInterface $input, OutputInterface $output): int {
24-
25-
$file_system = new Filesystem();
26-
27-
$input_directory = $input->getArgument('input_directory');
28-
if (!$file_system->exists($input_directory)) {
29-
throw new RuntimeException('Input directory does not exist.');
30-
}
31-
32-
$output_directory = $input->getArgument('output_directory');
33-
if (!$file_system->exists($output_directory)) {
34-
throw new RuntimeException('Output directory does not exist.');
35-
}
36-
37-
$iterator = new RecursiveIteratorIterator(
38-
new RecursiveDirectoryIterator($input_directory, RecursiveDirectoryIterator::SKIP_DOTS),
39-
);
40-
41-
$total = 0;
42-
foreach ($iterator as $path => $file) {
43-
$file_name = $file->getFileName();
44-
if (\str_ends_with($file_name, 'api.php')) {
45-
$output->writeln("<comment>$file_name</comment>");
46-
$hooks = \parse_hooks($path);
47-
foreach ($hooks as $hook_name => $hook) {
48-
$output->writeln(' - ' . $hook_name);
49-
\file_put_contents("$output_directory/$hook_name.twig", $hook);
50-
$total++;
51-
}
52-
}
53-
}
54-
55-
$output->writeln('-----------------');
56-
$output->writeln('Dumped hooks: ' . $total);
57-
58-
return 0;
59-
}
60-
61-
/**
62-
* Extracts hooks from a singe PHP file.
63-
*
64-
* @param string $file
65-
* File to parse.
66-
*
67-
* @return array
68-
* Array of parsed hooks keyed by hook name.
69-
*/
70-
function parse_hooks(string $file): array {
71-
$code = file_get_contents($file);
72-
73-
\preg_match_all("/function hook_(.*)\(.*\n\}\n/Us", $code, $matches);
74-
75-
$results = [];
76-
foreach ($matches[0] as $index => $hook) {
77-
$hook_name = $matches[1][$index];
78-
$output = "/**\n * Implements hook_$hook_name().\n */\n";
79-
$output .= \str_replace('function hook_', 'function {{ machine_name }}_', $hook);
80-
$results[$hook_name] = $output;
81-
}
82-
83-
return $results;
84-
}
85-
86-
(new Application('Hooks dumper'))
87-
->register('dump-hooks')
88-
->addArgument('input_directory', InputArgument::REQUIRED, 'Input directory')
89-
->addArgument('output_directory', InputArgument::REQUIRED, 'Output directory')
90-
->setCode('dump_hooks')
91-
->getApplication()
92-
->setDefaultCommand('dump-hooks', TRUE)
93-
->run();

0 commit comments

Comments
 (0)