Skip to content

Commit

Permalink
Merge pull request #3 from red-explosion/feature/init-workflow
Browse files Browse the repository at this point in the history
feat: init workflow
  • Loading branch information
bensherred authored Nov 23, 2023
2 parents 9663f8f + 2d25da8 commit 821bb76
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 175 deletions.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE /config.yml
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
42 changes: 42 additions & 0 deletions .github/workflows/init.yml
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"
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# :package_name
# package_name

[![Latest Version on Packagist](https://img.shields.io/packagist/v/red-explosion/:package_slug.svg?style=flat-square)](https://packagist.org/packages/red-explosion/:package_slug)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/red-explosion/:package_slug/tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/red-explosion/:package_slug/actions/workflows/tests.yaml?query=branch:main)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/red-explosion/:package_slug/coding-standards.yml?label=code%20style&style=flat-square)](https://github.com/red-explosion/:package_slug/actions/workflows/coding-standards.yml?query=branch:main)
[![Total Downloads](https://img.shields.io/packagist/dt/red-explosion/:package_slug.svg?style=flat-square)](https://packagist.org/packages/red-explosion/:package_slug)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/red-explosion/package_slug.svg?style=flat-square)](https://packagist.org/packages/red-explosion/package_slug)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/red-explosion/package_slug/tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/red-explosion/package_slug/actions/workflows/tests.yaml?query=branch:main)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/red-explosion/package_slug/coding-standards.yml?label=code%20style&style=flat-square)](https://github.com/red-explosion/package_slug/actions/workflows/coding-standards.yml?query=branch:main)
[![Total Downloads](https://img.shields.io/packagist/dt/red-explosion/package_slug.svg?style=flat-square)](https://packagist.org/packages/red-explosion/package_slug)
<!--delete-->
---
This repo can be used to scaffold a Red Explosion Laravel package. Follow these steps to get started:

1. Press the "Use this template" button at the top of this repo to create a new repo with the contents of this skeleton.
2. Run "php ./configure.php" to run a script that will replace all placeholders throughout all the files.
---
<!--/delete-->
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
Expand All @@ -19,33 +18,33 @@ This is where your description should go. Limit it to a paragraph or two. Consid
You can install the package via composer:

```bash
composer require red-explosion/:package_slug
composer require red-explosion/package_slug
```

You can publish and run the migrations with:

```bash
php artisan vendor:publish --tag=":package_slug-migrations"
php artisan vendor:publish --tag="skeleton-migrations"
php artisan migrate
```

You can publish the config file with:

```bash
php artisan vendor:publish --tag=":package_slug-config"
php artisan vendor:publish --tag="skeleton-config"
```

Optionally, you can publish the views using

```bash
php artisan vendor:publish --tag=":package_slug-views"
php artisan vendor:publish --tag="skeleton-views"
```

## Usage

```php
$variable = new RedExplosion\Skeleton();
echo $variable->echoPhrase('Hello, RedExplosion!');
echo $variable->echoPhrase('Hello, Red Explosion!');
```

## Testing
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "red-explosion/:package_slug",
"description": ":package_description",
"name": "red-explosion/package_slug",
"description": "package_description",
"license": "MIT",
"homepage": "https://github.com/red-explosion/:package_slug",
"homepage": "https://github.com/red-explosion/package_slug",
"type": "library",
"keywords": [
"red-explosion",
"laravel",
":package_slug"
"package_slug"
],
"authors": [
{
"name": ":author_name",
"email": "author@domain.com"
"name": "author_name",
"email": "author@email.com"
}
],
"require": {
Expand Down Expand Up @@ -41,7 +41,7 @@
"scripts": {
"lint": "./vendor/bin/pint --config vendor/red-explosion/pint-config/pint.json",
"test:lint": "./vendor/bin/pint --config vendor/red-explosion/pint-config/pint.json --test",
"test:types": "phpstan analyse --ansi",
"test:types": "./vendor/bin/phpstan analyse --ansi",
"test:unit": "./vendor/bin/pest --compact --colors=always",
"test": [
"@test:lint",
Expand Down
1 change: 0 additions & 1 deletion config/skeleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

declare(strict_types=1);

// Config for :package_slug
return [];
154 changes: 0 additions & 154 deletions configure.php

This file was deleted.

90 changes: 90 additions & 0 deletions init.php
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 => [],
};
}

0 comments on commit 821bb76

Please sign in to comment.