Skip to content

Commit

Permalink
Merge pull request #91 from Hi-Folks/develop
Browse files Browse the repository at this point in the history
v0.4.0
  • Loading branch information
roberto-butti authored Sep 15, 2021
2 parents e907a5d + 3e01452 commit ccf44e9
Show file tree
Hide file tree
Showing 19 changed files with 13,165 additions and 22,340 deletions.
99 changes: 99 additions & 0 deletions .github/workflows/auto-generated-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: hifolks/gh-actions-yaml-generator
on:
push:
branches:
- main
- develop
- features/**

jobs:
laravel-tests:
runs-on: ubuntu-latest
# Service container Mysql mysql
services:
# Label used to access the service container
mysql:
# Docker Hub image (also with version)
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: db_test_laravel
## map the "external" 33306 port with the "internal" 3306
ports:
- 33306:3306
# Set health checks to wait until mysql database has started (it takes some seconds to start)
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: [ '7.4','8.0' ]
dependency-stability: [ prefer-stable ]

name: P${{ matrix.php-versions }} - L${{ matrix.laravel }} - ${{ matrix.dependency-stability }} - ${{ matrix.operating-system}}

steps:
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: '16.x'
- name: Install NPM packages
run: npm ci
- name: Build frontend
run: npm run development
- name: Install PHP versions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
if: steps.vendor-cache.outputs.cache-hit != 'true'
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist

- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Run Migrations
# Set environment
env:
DB_CONNECTION: mysql
DB_DATABASE: db_test_laravel
DB_PORT: 33306
DB_USER: root

run: php artisan migrate

- name: Show dir
run: pwd
- name: PHP Version
run: php --version

# Code quality
- name: Execute tests (Unit and Feature tests) via PHPUnit
# Set environment
env:
DB_CONNECTION: mysql
DB_DATABASE: db_test_laravel
DB_PORT: 33306
DB_USER: root

run: vendor/bin/phpunit --testdox


- name: Execute Code Sniffer via phpcs
run: |
vendor/bin/phpcs --standard=PSR12 app
- name: Execute Code Static Analysis (PHP Stan + Larastan)
run: |
vendor/bin/phpstan analyse app -c ./vendor/nunomaduro/larastan/extension.neon --level=4 --no-progress
18 changes: 1 addition & 17 deletions .github/workflows/codequality-php8-featuresbranch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
- name: Get Composer Cache Directory 2
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v2
id: actions-cache
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Cache PHP dependencies
uses: actions/cache@v2
id: vendor-cache
with:
path: vendor
key: ${{ runner.OS }}-build-${{ hashFiles('**/composer.lock') }}

- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
Expand Down
20 changes: 2 additions & 18 deletions .github/workflows/codequality-phpversions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: [ '8.0','7.4','7.3' ]
php-versions: [ '8.0','7.4' ]
dependency-stability: [ prefer-stable ]

name: P${{ matrix.php-versions }} - L${{ matrix.laravel }} - ${{ matrix.dependency-stability }} - ${{ matrix.operating-system}}
Expand All @@ -58,23 +58,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
- name: Get Composer Cache Directory 2
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v2
id: actions-cache
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Cache PHP dependencies
uses: actions/cache@v2
id: vendor-cache
with:
path: vendor
key: ${{ runner.OS }}-build-${{ hashFiles('**/composer.lock') }}

- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Changelog
## 0.4.0 - 2021-09-15
- Add command to generate automatically GitHub Actions workflow from composer.json, .env, migrations, packages.json

## 0.3.7 - 2021-08-11

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ help: ## Show this help.
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

phpstan: ## Execute phpstan
vendor/bin/phpstan analyse app -c ./vendor/nunomaduro/larastan/extension.neon --level=4 --no-progress
vendor/bin/phpstan analyse app -c ./vendor/nunomaduro/larastan/extension.neon --level=5 --no-progress

test: ## Execute phpunit
php artisan test
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ If you want to test and use quickly this tool, I deployed the codebase (main bra

If you want to start using it locally you can clone the repo and install it following the instructions below.

## Command line
Experimental and "magical" feature: Ghygen ships also a command for generating **automatically** a GitHub Actions workflow Yaml file.
Once you installed locally Ghygen (see next section), you can execute:
```shell
php artisan ghygen:generate --projectdir=../otherproject
```
Where "../otherproject" is the directory (absolute or relative path name) with your Laravel project (application or package) that yuo want to automatically generate the GitHub Actions workflow yaml file.
This "magic" command, will extract information from:
- composer.json
- package.json (if it exists)
- .env file
- ... and other assets
- in order to guess a configuration for your GitHub Actions workflow.

## Install
Clone source code, enter the new directory and perform a couple of instructions:
```shell
Expand Down
187 changes: 187 additions & 0 deletions app/Console/Commands/GenerateWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

namespace App\Console\Commands;

use App\Objects\GuesserFiles;
use App\Objects\WorkflowGenerator;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;

class GenerateWorkflow extends Command
{

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ghygen:generate
{--projectdir= : the directory of the project with composer.json}
{--cache : enable caching packages in the workflow}
{--envfile=' . GuesserFiles::ENV_DEFAULT_TEMPLATE_FILE . ' : the .env file to use in the workflow}
';


/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate GitHub Actions workflow automatically from
a project (repository, local filesystem) using composer.json, .env and package.json';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$projectdir = $this->option("projectdir");
if (is_null($projectdir)) {
$projectdir = "";
}
$cache = $this->option("cache");
$optionEnvWorkflowFile = $this->option("envfile");

$guesserFiles = new GuesserFiles();
$guesserFiles->pathFiles($projectdir, $optionEnvWorkflowFile);

//$this->line("Composer : " . $guesserFiles->getComposerPath());
if (! $guesserFiles->composerExists()) {
$this->error("Composer file not found");
return -1;
}
$generator = new WorkflowGenerator();
$generator->loadDefaults();

if ($guesserFiles->composerExists()) {
$composer = json_decode(file_get_contents($guesserFiles->getComposerPath()), true);
$generator->name = Arr::get($composer, 'name');
$phpversion = Arr::get($composer, 'require.php', "");
$generator->detectPhpVersion($phpversion);

// detect packages
$devPackages = Arr::get($composer, 'require-dev');
// testbench
$testbenchVersions = Arr::get($devPackages, "orchestra/testbench", "");
if ($testbenchVersions !== "") {
$laravelVersions = GuesserFiles::detectLaravelVersionFromTestbench($testbenchVersions);
$generator->matrixLaravel = true;
$generator->matrixLaravelVersions = $laravelVersions;
}
// squizlabs/php_codesniffer
$phpCodesniffer = Arr::get($devPackages, "squizlabs/php_codesniffer", "");
if ($phpCodesniffer !== "") {
$generator->stepExecuteCodeSniffer = true;
$generator->stepInstallCodeSniffer = false;
}
// nunomaduro/larastan
$larastan = Arr::get($devPackages, "nunomaduro/larastan", "");
if ($larastan !== "") {
$generator->stepExecuteStaticAnalysis = true;
$generator->stepInstallStaticAnalysis = false;
$generator->stepToolStaticAnalysis = "larastan";
} else {
$phpstan = Arr::get($devPackages, "phpstan/phpstan", "");
if ($phpstan !== "") {
$generator->stepExecuteStaticAnalysis = true;
$generator->stepInstallStaticAnalysis = false;
$generator->stepToolStaticAnalysis = "phpstan";
}
}
$generator->stepDusk = false;
// phpunit/phpunit
$generator->stepExecutePhpunit = false;
$phpunit = Arr::get($devPackages, "phpunit/phpunit", "");
if ($phpunit !== "") {
$generator->stepExecutePhpunit = true;
}
// phpunit/phpunit
$generator->stepExecutePestphp = false;
$pestphp = Arr::get($devPackages, "pestphp/pest", "");
if ($pestphp !== "") {
$generator->stepExecutePestphp = true;
}
}
$generator->detectCache($cache);

$generator->databaseType = WorkflowGenerator::DB_TYPE_NONE;
$generator->stepRunMigrations = false;

if ($guesserFiles->envExists()) {
$envArray = $generator->readDotEnv($guesserFiles->getEnvPath());
$databaseType = Arr::get($envArray, "DB_CONNECTION", "");


$generator->databaseType = WorkflowGenerator::DB_TYPE_NONE;
$generator->stepRunMigrations = false;
if ($databaseType === "mysql") {
$generator->databaseType = WorkflowGenerator::DB_TYPE_MYSQL;
}
if ($databaseType === "sqlite") {
$generator->databaseType = WorkflowGenerator::DB_TYPE_SQLITE;
}
if ($databaseType === "postgresql") {
$generator->databaseType = WorkflowGenerator::DB_TYPE_POSTGRESQL;
}
if ($generator->databaseType !== WorkflowGenerator::DB_TYPE_NONE) {
$migrationFiles = scandir($guesserFiles->getMigrationsPath());
if (count($migrationFiles) > 4) {
$generator->stepRunMigrations = true;
}
}
}
if ($guesserFiles->packageExists()) {
$generator->stepNodejs = true;
$generator->stepNodejsVersion = "16.x";
$versionFromNvmrc = $generator->readNvmrc($guesserFiles
->getNvmrcPath());
if ($versionFromNvmrc !== "") {
$generator->stepNodejsVersion = $versionFromNvmrc;
}
}
$appKey = "";
$generator->stepGenerateKey = false;

if ($guesserFiles->envDefaultTemplateExists()) {
$generator->stepCopyEnvTemplateFile = true;
$generator->stepEnvTemplateFile = $optionEnvWorkflowFile;
// Generate Key
$envArray = $generator->readDotEnv($guesserFiles->getEnvDefaultTemplatePath());
$appKey = Arr::get($envArray, "APP_KEY", "");

$generator->stepGenerateKey = $appKey === "";
} else {
$generator->stepCopyEnvTemplateFile = false;
}
$generator->stepFixStoragePermissions = false;
if ($guesserFiles->artisanExists()) {
//artisan file so:ENV_TEMPLATE_FILE_DEFAULT.
// fix storage permissions
$generator->stepFixStoragePermissions = true;
}


$data = $generator->setData();

$result = $generator->generate($data);
$this->line($result);





return 0;
}
}
Loading

0 comments on commit ccf44e9

Please sign in to comment.