Skip to content

Commit 8669be6

Browse files
committed
🎉 Initial commit
0 parents  commit 8669be6

File tree

12 files changed

+483
-0
lines changed

12 files changed

+483
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.php]
12+
indent_size = 4
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.editorconfig export-ignore
2+
/.gitattributes export-ignore

.github/dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: 'composer'
4+
directory: '/'
5+
schedule:
6+
interval: 'daily'

.github/workflows/main.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Main
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
php:
13+
name: PHP ${{ matrix.php }}
14+
runs-on: ubuntu-latest
15+
16+
strategy:
17+
fail-fast: true
18+
matrix:
19+
php: [8.2]
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v3
24+
25+
- name: Setup PHP
26+
uses: shivammathur/setup-php@v2
27+
with:
28+
php-version: ${{ matrix.php }}
29+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite
30+
coverage: none
31+
32+
- name: Install Composer dependencies
33+
run: composer install --prefer-dist --no-interaction --no-progress
34+
35+
- name: Run Pint
36+
run: vendor/bin/pint --test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) :vendor_name
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# :package_description
2+
3+
<!--delete-->
4+
---
5+
This repo can be used to scaffold a Laracord plugin. Follow these steps to get started:
6+
7+
1. Press the "Use this template" button at the top of this repo to create a new repo with the contents of this skeleton.
8+
2. Run "php ./configure.php" to run a script that will replace all placeholders throughout all the files.
9+
3. Make something great!
10+
---
11+
<!--/delete-->
12+
13+
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
14+
15+
## Installation
16+
17+
You can install the package via composer:
18+
19+
```bash
20+
composer require :vendor_slug/:package_slug
21+
```
22+
23+
## License
24+
25+
:package_slug is provided under the [MIT License](LICENSE.md).

composer.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": ":vendor_slug/:package_slug",
3+
"description": ":package_description",
4+
"type": "package",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": ":author_name",
9+
"email": "author@domain.com"
10+
}
11+
],
12+
"keywords": [
13+
"laracord",
14+
"discord"
15+
],
16+
"support": {
17+
"issues": "https://github.com/:vendor_slug/:package_slug/issues",
18+
"source": "https://github.com/:vendor_slug/:package_slug"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"VendorName\\Skeleton\\": "src/"
23+
}
24+
},
25+
"require": {
26+
"php": "^8.2"
27+
},
28+
"require-dev": {
29+
"laravel/pint": "^1.14",
30+
"laracord/framework": "dev-next"
31+
},
32+
"extra": {
33+
"laracord": {
34+
"providers": [
35+
"VendorName\\Skeleton\\SkeletonServiceProvider"
36+
]
37+
}
38+
},
39+
"config": {
40+
"preferred-install": "dist",
41+
"sort-packages": true,
42+
"optimize-autoloader": true
43+
},
44+
"minimum-stability": "dev",
45+
"prefer-stable": true
46+
}

configure.php

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
$gitName = run('git config user.name');
5+
$authorName = ask('Author name', $gitName);
6+
7+
$gitEmail = run('git config user.email');
8+
$authorEmail = ask('Author email', $gitEmail);
9+
10+
$usernameGuess = explode(':', run('git config remote.origin.url'))[1] ?? '';
11+
if ($usernameGuess !== '') {
12+
$usernameGuess = dirname($usernameGuess);
13+
$usernameGuess = basename($usernameGuess);
14+
}
15+
$authorUsername = ask('Author username', $usernameGuess);
16+
17+
$vendorName = ask('Vendor name', $authorUsername);
18+
$vendorSlug = slugify($vendorName);
19+
$vendorNamespace = str_replace('-', '', ucwords($vendorName));
20+
$vendorNamespace = ask('Vendor namespace', $vendorNamespace);
21+
22+
$currentDirectory = getcwd();
23+
$folderName = basename($currentDirectory);
24+
25+
$packageName = ask('Package name', $folderName);
26+
$packageSlug = slugify($packageName);
27+
$packageSlugWithoutPrefix = removePrefix('laracord-', $packageSlug);
28+
29+
$className = titleCase($packageName);
30+
$className = ask('Class name', $className);
31+
$variableName = lcfirst($className);
32+
$description = ask('Package description', "This is my package $packageSlug");
33+
34+
writeln("\r");
35+
writeln('------');
36+
writeln("Author : \e[0;36m$authorName ($authorUsername, $authorEmail)\e[0m");
37+
writeln("Vendor : \e[0;36m$vendorName ($vendorSlug)\e[0m");
38+
writeln('Package : ' . "\e[0;36m" . $packageSlug . ($description ? " <{$description}>" : '') . "\e[0m");
39+
writeln("Namespace : \e[0;36m$vendorNamespace\\$className\e[0m");
40+
writeln("Class name : \e[0;36m$className\e[0m");
41+
writeln('------');
42+
writeln("\r");
43+
writeln('This script will replace the above values in all relevant files in the project directory.');
44+
writeln("\r");
45+
46+
if (! confirm('Modify files?', true)) {
47+
exit(1);
48+
}
49+
50+
$files = (str_starts_with(strtoupper(PHP_OS), 'WIN') ? replaceForWindows() : replaceForAllOtherOSes());
51+
52+
foreach ($files as $file) {
53+
replaceInFile($file, [
54+
':author_name' => $authorName,
55+
':author_username' => $authorUsername,
56+
'author@domain.com' => $authorEmail,
57+
':vendor_name' => $vendorName,
58+
':vendor_slug' => $vendorSlug,
59+
'VendorName' => $vendorNamespace,
60+
':package_name' => $packageName,
61+
':package_slug' => $packageSlug,
62+
':package_slug_without_prefix' => $packageSlugWithoutPrefix,
63+
'Skeleton' => $className,
64+
'skeleton' => $packageSlug,
65+
'migration_table_name' => titleSnake($packageSlug),
66+
'variable' => $variableName,
67+
':package_description' => $description,
68+
]);
69+
70+
match (true) {
71+
str_contains($file, determineSeparator('src/SkeletonServiceProvider.php')) => rename($file, determineSeparator('./src/' . $className . 'ServiceProvider.php')),
72+
str_contains($file, determineSeparator('src/SkeletonPlugin.php')) => rename($file, determineSeparator('./src/' . $className . 'Plugin.php')),
73+
str_contains($file, determineSeparator('src/Commands/SkeletonCommand.php')) => rename($file, determineSeparator('./src/Commands/' . $className . 'Command.php')),
74+
str_contains($file, determineSeparator('config/skeleton.php')) => rename($file, determineSeparator('./config/' . $packageSlugWithoutPrefix . '.php')),
75+
str_contains($file, 'README.md') => removeTag($file, 'delete'),
76+
default => [],
77+
};
78+
}
79+
80+
confirm('Execute `composer install`?') && run('composer install');
81+
82+
if (confirm('Let this script delete itself?', true)) {
83+
unlink(__FILE__);
84+
}
85+
86+
function ask(string $question, string $default = ''): string
87+
{
88+
$def = $default ? "\e[0;33m ($default)" : '';
89+
$answer = readline("\e[0;32m" . $question . $def . ": \e[0m");
90+
91+
if (! $answer) {
92+
return $default;
93+
}
94+
95+
return $answer;
96+
}
97+
98+
function confirm(string $question, bool $default = false): bool
99+
{
100+
$answer = ask($question, ($default ? 'Y/n' : 'y/N'));
101+
102+
if (strtolower($answer) === 'y/n') {
103+
return $default;
104+
}
105+
106+
return strtolower($answer) === 'y';
107+
}
108+
109+
function writeln(string $line): void
110+
{
111+
echo $line . PHP_EOL;
112+
}
113+
114+
function run(string $command): string
115+
{
116+
return trim((string) shell_exec($command));
117+
}
118+
119+
function slugify(string $subject): string
120+
{
121+
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $subject), '-'));
122+
}
123+
124+
function titleCase(string $subject): string
125+
{
126+
return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $subject)));
127+
}
128+
129+
function titleSnake(string $subject, string $replace = '_'): string
130+
{
131+
return str_replace(['-', '_'], $replace, $subject);
132+
}
133+
134+
function replaceInFile(string $file, array $replacements): void
135+
{
136+
$contents = file_get_contents($file);
137+
138+
file_put_contents(
139+
$file,
140+
str_replace(
141+
array_keys($replacements),
142+
array_values($replacements),
143+
$contents
144+
)
145+
);
146+
}
147+
148+
function removePrefix(string $prefix, string $content): string
149+
{
150+
if (str_starts_with($content, $prefix)) {
151+
return substr($content, strlen($prefix));
152+
}
153+
154+
return $content;
155+
}
156+
157+
function removeComposerDeps(array $names, string $location): void
158+
{
159+
$data = json_decode(file_get_contents(__DIR__ . '/composer.json'), true);
160+
161+
foreach ($data[$location] as $name => $version) {
162+
if (in_array($name, $names, true)) {
163+
unset($data[$location][$name]);
164+
}
165+
}
166+
167+
file_put_contents(__DIR__ . '/composer.json', json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
168+
}
169+
170+
function removeNpmDeps(array $names, string $location): void
171+
{
172+
$data = json_decode(file_get_contents(__DIR__ . '/package.json'), true);
173+
174+
foreach ($data[$location] as $name => $version) {
175+
if (in_array($name, $names, true)) {
176+
unset($data[$location][$name]);
177+
}
178+
}
179+
180+
file_put_contents(__DIR__ . '/package.json', json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES |
181+
JSON_UNESCAPED_UNICODE));
182+
}
183+
184+
function removeTag(string $file, string $tag): void
185+
{
186+
$contents = file_get_contents($file);
187+
188+
file_put_contents(
189+
$file,
190+
preg_replace('/<!--' . $tag . '-->.*<!--\/' . $tag . '-->/s', '', $contents) ?: $contents
191+
);
192+
}
193+
194+
function safeUnlink(string $filename): void
195+
{
196+
if (file_exists($filename) && is_file($filename)) {
197+
unlink($filename);
198+
}
199+
}
200+
201+
function determineSeparator(string $path): string
202+
{
203+
return str_replace('/', DIRECTORY_SEPARATOR, $path);
204+
}
205+
206+
function replaceForWindows(): array
207+
{
208+
return preg_split('/\\r\\n|\\r|\\n/', run('dir /S /B * | findstr /v /i .git\ | findstr /v /i \\vendor\\ | findstr /v /i ' . basename(__FILE__) . ' | findstr /r /i /M /F:/ ":author :vendor :package VendorName skeleton migration_table_name vendor_name vendor_slug author@domain.com"'));
209+
}
210+
211+
function replaceForAllOtherOSes(): array
212+
{
213+
return explode(PHP_EOL, run('find ./* ./.github/* -name "vendor" -type d -prune \
214+
-o -name "configure.php" -prune \
215+
-o -type f -print0 | xargs -0 grep -E -r -l -i ":author|:vendor|:package|VendorName|skeleton|migration_table_name|vendor_name|vendor_slug|author@domain.com"'));
216+
}
217+
218+
function removeDirectory($dir): void
219+
{
220+
if (is_dir($dir)) {
221+
$objects = scandir($dir);
222+
223+
foreach ($objects as $object) {
224+
if ($object != '.' && $object != '..') {
225+
if (filetype($dir . '/' . $object) == 'dir') {
226+
removeDirectory($dir . '/' . $object);
227+
} else {
228+
unlink($dir . '/' . $object);
229+
}
230+
}
231+
}
232+
233+
rmdir($dir);
234+
}
235+
}

0 commit comments

Comments
 (0)