-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolymorphine-skeleton
More file actions
85 lines (71 loc) · 3.62 KB
/
polymorphine-skeleton
File metadata and controls
85 lines (71 loc) · 3.62 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
#!/usr/bin/env php
<?php declare(strict_types=1);
/*
* This file is part of Polymorphine/Dev package.
*
* (c) Shudd3r <q3.shudder@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
use Shudd3r\Skeletons\Application;
use Shudd3r\Skeletons\InputArgs;
use Shudd3r\Skeletons\Environment\Files\Directory\LocalDirectory;
use Shudd3r\Skeletons\Replacements\Replacement;
use Shudd3r\Skeletons\Replacements\Source;
use Shudd3r\Skeletons\Templates\Contents;
use Shudd3r\Skeletons\Templates\Template;
// This script should be executed from package root directory
$rootDirectory = getcwd();
if (!file_exists($rootDirectory . '/vendor/autoload.php')) {
fwrite(STDERR, 'Cannot find vendor/autoload.php file in package root directory');
die(1);
}
if (!file_exists($rootDirectory . '/composer.json')) {
fwrite(STDERR, 'Cannot find composer.json file in package root directory');
die(1);
}
require_once $rootDirectory . '/vendor/autoload.php';
$args = new InputArgs($argv ?? []);
$skeleton = new LocalDirectory(__DIR__ . '/template');
$package = new LocalDirectory($rootDirectory);
$app = new Application($package, $skeleton);
$app->backup(new LocalDirectory($rootDirectory . '/.dev/.skeleton-backup'));
$app->replacement('package.name')->add(new Replacement\PackageName());
$app->replacement('repository.name')->add(new Replacement\RepositoryName('package.name'));
$app->replacement('package.description')->add(new Replacement\PackageDescription('package.name'));
$app->replacement('namespace.src')->add(new Replacement\SrcNamespace('package.name'));
$app->replacement('author.name')
->build(fn (Source $source) => $source->composer()->value('authors.0.name') ?? 'Author Name')
->argumentName('author')
->inputPrompt('Author\'s name')
->description('Name of package author [format: non-empty string]' . PHP_EOL . 'Replaces {%s} placeholder')
->validate(fn (string $value) => !empty($value));
$app->replacement('author.email')
->build(fn (Source $source) => $source->composer()->value('authors.0.email') ?? 'default@example.com')
->argumentName('email')
->inputPrompt('Author\'s email address')
->description('Email address of package author [format: <username>@<domain>]' . PHP_EOL . 'Replaces {%s} placeholder')
->validate(fn (string $value) => $value === filter_var($value, FILTER_VALIDATE_EMAIL));
$isUpdate = $args->command() === 'update';
$isSelf = $rootDirectory === __DIR__;
$app->template('composer.json')->createWith(function (Contents $contents) use ($isSelf, $isUpdate) {
$placeholders = ['{$tpl.REQUIRE_DEV}', '{$tpl.PHP_EXEC}', '{$tpl.PHPCS}'];
$replacements = $isSelf
? ['null', '@php ', 'phpcs.xml.dist']
: ['{ "polymorphine/dev": null }', '', 'vendor/polymorphine/dev/phpcs.xml'];
$baseTemplate = new Template\BasicTemplate(str_replace($placeholders, $replacements, $contents->template()));
return new Template\MergedJsonTemplate($baseTemplate, $contents->package(), $isUpdate);
});
$app->template('.github/workflows/build.yml')->createWith(function (Contents $contents) use ($isSelf) {
$placeholders = ['${tpl.PHP_EXEC}', '${tpl.PHPCS}'];
$replacements = $isSelf
? ['php ', 'phpcs.xml.dist']
: ['vendor/bin/', 'vendor/polymorphine/dev/phpcs.xml'];
return new Template\BasicTemplate(str_replace($placeholders, $replacements, $contents->template()));
});
$app->template('LICENSE')->createWith(function (Contents $contents) {
return new Template\BasicTemplate(str_replace('{$tpl.CURRENT_YEAR}', date('Y'), $contents->template()));
});
$exitCode = $app->run($args);
exit($exitCode);