-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from red-explosion/feature/init-workflow
feat: init workflow
- Loading branch information
Showing
7 changed files
with
151 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
blank_issues_enabled: false | ||
contact_links: | ||
- name: Ask a question | ||
url: https://github.com/red-explosion/:package_slug/discussions/new?category=q-a | ||
url: https://github.com/red-explosion/package_slug/discussions/new?category=q-a | ||
about: Ask the community for help | ||
- name: Request a feature | ||
url: https://github.com/red-explosion/:package_slug/discussions/new?category=ideas | ||
url: https://github.com/red-explosion/package_slug/discussions/new?category=ideas | ||
about: Share ideas for new features |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: init package | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
|
||
jobs: | ||
init: | ||
name: Init Package | ||
runs-on: ubuntu-latest | ||
|
||
# Only run this job when a repository has been created from the template, and it's an initial push (first commit) | ||
if: github.repository != 'red-explosion/laravel-package-skeleton' && github.run_number == 1 && github.event.base_ref == null | ||
|
||
steps: | ||
- name: Checkout the code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: 8.2 | ||
coverage: none | ||
|
||
- name: Init | ||
run: php init.php "$PACKAGE_NAME" "$PACKAGE_DESCRIPTION" "$AUTHOR_NAME" "AUTHOR_USERNAME" "AUTHOR_EMAIL" | ||
env: | ||
PACKAGE_NAME: ${{ github.event.repository.name }} | ||
PACKAGE_DESCRIPTION: ${{ github.event.repository.description }} | ||
AUTHOR_NAME: ${{ github.event.commits[0].author.name }} | ||
AUTHOR_USERNAME: ${{ github.actor }} | ||
AUTHOR_EMAIL: ${{ github.event.pusher.email }} | ||
|
||
- name: Delete init files | ||
run: | | ||
rm .github/workflows/init.yml | ||
rm init.yml | ||
- name: Apply changes | ||
uses: stefanzweifel/git-auto-commit-action@v4 | ||
with: | ||
commit_message: "chore: initialise project" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,4 @@ | |
|
||
declare(strict_types=1); | ||
|
||
// Config for :package_slug | ||
return []; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
function files(): array | ||
{ | ||
$files = explode( | ||
separator: PHP_EOL, | ||
string: run(command: 'grep -E -r -l -i "package_name|package_slug|package_description|author_name|author_username|[email protected]|skeleton|Skeleton" --exclude-dir=vendor ./* ./.github/* | grep -v ' . basename(path: __FILE__)) | ||
); | ||
|
||
$files[] = './config/skeleton.php'; | ||
|
||
return $files; | ||
} | ||
|
||
function run(string $command): string | ||
{ | ||
return trim(string: (string) shell_exec(command: $command)); | ||
} | ||
|
||
function replaceInFile(string $file, array $replacements): void | ||
{ | ||
/** @var string $contents */ | ||
$contents = file_get_contents(filename: $file); | ||
$contents = str_replace(search: array_keys($replacements), replace: array_values($replacements), subject: $contents); | ||
|
||
file_put_contents(filename: $file, data: $contents); | ||
} | ||
|
||
function removePrefix(string $prefix, string $content): string | ||
{ | ||
if (str_starts_with(haystack: $content, needle: $prefix)) { | ||
return mb_substr(string: $content, start: mb_strlen(string: $prefix)); | ||
} | ||
|
||
return $content; | ||
} | ||
|
||
function titleCase(string $subject): string | ||
{ | ||
return str_replace(search: ' ', replace: '', subject: ucwords(string: $subject)); | ||
} | ||
|
||
function determineSeparator(string $path): string | ||
{ | ||
return str_replace(search: '/', replace: DIRECTORY_SEPARATOR, subject: $path); | ||
} | ||
|
||
function removeReadmeParagraphs(string $file): void | ||
{ | ||
/** @var string $contents */ | ||
$contents = file_get_contents(filename: $file); | ||
|
||
file_put_contents( | ||
filename: $file, | ||
data: preg_replace(pattern: '/<!--delete-->.*<!--\/delete-->/s', replacement: '', subject: $contents) ?: $contents | ||
); | ||
} | ||
|
||
$packageSlug = $argv[1]; | ||
$packageSlugWithoutPrefix = removePrefix(prefix: 'laravel-', content: $packageSlug); | ||
$packageName = ucwords(string: str_replace(search: '-', replace: ' ', subject: $packageSlug)); | ||
$packageDescription = $argv[2]; | ||
|
||
$authorName = $argv[3]; | ||
$authorUsername = $argv[4]; | ||
$authorEmail = $argv[5]; | ||
|
||
$className = removePrefix(prefix: 'Laravel', content: titleCase(subject: $packageName)); | ||
|
||
foreach (files() as $file) { | ||
replaceInFile($file, [ | ||
'package_name' => $packageName, | ||
'package_slug' => $packageSlug, | ||
'package_description' => $packageDescription, | ||
'author_name' => $authorName, | ||
'author_username' => $authorUsername, | ||
'[email protected]' => $authorEmail, | ||
'skeleton' => $packageSlugWithoutPrefix, | ||
'Skeleton' => $className, | ||
]); | ||
|
||
match (true) { | ||
str_contains($file, determineSeparator(path: 'src/Skeleton.php')) => rename($file, determineSeparator(path: './src/' . $className . '.php')), | ||
str_contains($file, determineSeparator(path: 'src/SkeletonServiceProvider.php')) => rename($file, determineSeparator(path: './src/' . $className . 'ServiceProvider.php')), | ||
str_contains($file, determineSeparator(path: 'config/skeleton.php')) => rename($file, determineSeparator(path: './config/' . $packageSlugWithoutPrefix . '.php')), | ||
str_contains($file, 'README.md') => removeReadmeParagraphs($file), | ||
default => [], | ||
}; | ||
} |