Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning authored Jan 27, 2025
0 parents commit 73d4473
Show file tree
Hide file tree
Showing 16 changed files with 1,171 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
32 changes: 32 additions & 0 deletions .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: "Update Changelog"

on:
workflow_dispatch:
release:
types: [ published, edited, deleted ]

jobs:
generate:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3
with:
ref: ${{ github.event.release.target_commitish }}
token: ${{ secrets.RAPIDEZ_ACTIONS_ACCOUNT_PAT }}

- name: Generate changelog
uses: justbetter/generate-changelogs-action@main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
repository: ${{ github.repository }}
sha: ${{ github.head_ref || github.ref_name }}

- name: Commit CHANGELOG
uses: stefanzweifel/git-auto-commit-action@v4
with:
branch: ${{ github.event.release.target_commitish }}
commit_message: Update CHANGELOG
file_pattern: CHANGELOG.md
26 changes: 26 additions & 0 deletions .github/workflows/translations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Check translations

on:
push:
branches:
- master
- '*.x'
pull_request:

jobs:
translations:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none
- name: Install dependencies
run: composer install --prefer-dist --no-interaction
- name: Check translations
uses: rapidez/laravel-translation-checker@master
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/composer.lock
/node_modules
/vendor
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Rapidez :package_name_without_prefix
<!--delete-->
This repository can be used as template for a new Rapidez package.

- Click on "Use this template" on the top of this Github repo page
- Run `php ./configure.php`

Credits to [`spatie/package-skeleton-laravel`](https://github.com/spatie/package-skeleton-laravel) for the inpiration for this template.

Keep in mind that if you contribute to this template; it should work for official and unofficial packages!
- `rapidez/something`
- `someone/rapidez-something`
<!--/delete-->
:package_description

## Installation

```
composer require :vendor_slug/:package_slug
```

## Configuration

You can publish the config with:
```
php artisan vendor:publish --tag=rapidez-:package_slug_without_prefix-config
```

## Views

You can publish the views with:
```
php artisan vendor:publish --tag=rapidez-:package_slug_without_prefix-views
```

## License

GNU General Public License v3. Please see [License File](LICENSE) for more information.
38 changes: 38 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": ":vendor_slug/:package_slug",
"description": ":package_description",
"keywords": [
"rapidez",
":package_slug"
],
"homepage": "https://github.com/:vendor_slug/:package_slug",
"license": "GPL-3.0-or-later",
"authors": [
{
"name": ":author_name",
"email": "author@domain.com",
"role": "Developer"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^8.1|^8.2",
"rapidez/core": "^2.0|^3.0"
},
"autoload": {
"psr-4": {
"VendorName\\Skeleton\\": "src"
}
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"VendorName\\Skeleton\\SkeletonServiceProvider"
]
}
}
}
4 changes: 4 additions & 0 deletions config/rapidez/skeleton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// config for VendorName/Skeleton
return [];
217 changes: 217 additions & 0 deletions configure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
#!/usr/bin/env php
<?php

function ask(string $question, string $default = ''): string
{
$answer = readline($question.($default ? " ({$default})" : null).': ');

if (! $answer) {
return $default;
}

return $answer;
}

function confirm(string $question, bool $default = false): bool
{
$answer = ask($question.' ('.($default ? 'Y/n' : 'y/N').')');

if (! $answer) {
return $default;
}

return strtolower($answer) === 'y';
}

function writeln(string $line): void
{
echo $line.PHP_EOL;
}

function run(string $command): string
{
return trim((string) shell_exec($command));
}

function str_after(string $subject, string $search): string
{
$pos = strrpos($subject, $search);

if ($pos === false) {
return $subject;
}

return substr($subject, $pos + strlen($search));
}

function slugify(string $subject): string
{
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $subject), '-'));
}

function title_case(string $subject): string
{
return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $subject)));
}

function title_snake(string $subject, string $replace = '_'): string
{
return str_replace(['-', '_'], $replace, $subject);
}

function replace_in_file(string $file, array $replacements): void
{
$contents = file_get_contents($file);

file_put_contents(
$file,
str_replace(
array_keys($replacements),
array_values($replacements),
$contents
)
);
}

function remove_prefix(string $prefix, string $content): string
{
if (str_starts_with($content, $prefix)) {
return substr($content, strlen($prefix));
}

return $content;
}

function remove_composer_deps(array $names)
{
$data = json_decode(file_get_contents(__DIR__.'/composer.json'), true);

foreach ($data['require-dev'] as $name => $version) {
if (in_array($name, $names, true)) {
unset($data['require-dev'][$name]);
}
}

file_put_contents(__DIR__.'/composer.json', json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
}

function remove_composer_script($scriptName)
{
$data = json_decode(file_get_contents(__DIR__.'/composer.json'), true);

foreach ($data['scripts'] as $name => $script) {
if ($scriptName === $name) {
unset($data['scripts'][$name]);
break;
}
}

file_put_contents(__DIR__.'/composer.json', json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
}

function remove_readme_paragraphs(string $file): void
{
$contents = file_get_contents($file);

file_put_contents(
$file,
preg_replace('/<!--delete-->.*<!--\/delete-->/s', '', $contents) ?: $contents
);
}

function safeUnlink(string $filename)
{
if (file_exists($filename) && is_file($filename)) {
unlink($filename);
}
}

function determineSeparator(string $path): string
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}

function replaceForWindows(): array
{
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 vendor_name vendor_slug author@domain.com"'));
}

function replaceForAllOtherOSes(): array
{
return explode(PHP_EOL, run('grep -E -r -l -i ":author|:vendor|:package|VendorName|skeleton|vendor_name|vendor_slug|author@domain.com" --exclude-dir=vendor ./* ./.github/* | grep -v '.basename(__FILE__)));
}

$gitName = run('git config user.name');
$authorName = ask('Author name', $gitName);

$gitEmail = run('git config user.email');
$authorEmail = ask('Author email', $gitEmail);

$usernameGuess = explode(':', run('git config remote.origin.url'))[1];
$usernameGuess = dirname($usernameGuess);
$usernameGuess = basename($usernameGuess);
$authorUsername = ask('Author username', $usernameGuess);

$vendorName = ask('Vendor name', $authorUsername);
$vendorSlug = slugify($vendorName);
$vendorNamespace = str_replace('-', '', ucwords($vendorName));
$vendorNamespace = ask('Vendor namespace', $vendorNamespace);

$currentDirectory = getcwd();
$folderName = basename($currentDirectory);

$packageName = ask('Package name', $folderName);
$packageNameWithoutPrefix = remove_prefix('Rapidez ', $packageName);
$packageSlug = slugify($packageName);
$packageSlugWithoutPrefix = remove_prefix('rapidez-', $packageSlug);

$className = title_case($packageName);
$className = ask('Class name', $className);
$variableName = lcfirst($className);
$description = ask('Package description', "This is my package {$packageSlug}");

writeln('------');
writeln("Author : {$authorName} ({$authorUsername}, {$authorEmail})");
writeln("Vendor : {$vendorName} ({$vendorSlug})");
writeln("Package : {$packageSlug} <{$description}>");
writeln("Namespace : {$vendorNamespace}\\{$className}");
writeln("Class name : {$className}");
writeln('------');

writeln('This script will replace the above values in all relevant files in the project directory.');

if (! confirm('Modify files?', true)) {
exit(1);
}

$files = (str_starts_with(strtoupper(PHP_OS), 'WIN') ? replaceForWindows() : replaceForAllOtherOSes());

foreach ($files as $file) {
replace_in_file($file, [
':author_name' => $authorName,
':author_username' => $authorUsername,
'author@domain.com' => $authorEmail,
':vendor_name' => $vendorName,
':vendor_slug' => $vendorSlug,
'VendorName' => $vendorNamespace,
':package_name_without_prefix' => $packageNameWithoutPrefix,
':package_name' => $packageName,
':package_slug_without_prefix' => $packageSlugWithoutPrefix,
':package_slug' => $packageSlug,
'Skeleton' => $className,
'skeleton' => $packageSlug,
'variable' => $variableName,
':package_description' => $description,
]);

match (true) {
str_contains($file, determineSeparator('src/SkeletonServiceProvider.php')) => rename($file, determineSeparator('./src/'.$className.'ServiceProvider.php')),
str_contains($file, determineSeparator('config/rapidez/skeleton.php')) => rename($file, determineSeparator('./config/rapidez/'.$packageSlugWithoutPrefix.'.php')),
str_contains($file, 'README.md') => remove_readme_paragraphs($file),
default => [],
};
}

confirm('Execute `composer install`?') && run('composer install');

confirm('Let this script delete itself?', true) && unlink(__FILE__);
Loading

0 comments on commit 73d4473

Please sign in to comment.