Skip to content

Commit

Permalink
Merge pull request #1 from absszero/sample-option
Browse files Browse the repository at this point in the history
Sample option
  • Loading branch information
absszero authored Apr 14, 2024
2 parents 9b57ba0 + aabe557 commit 42731cb
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 53 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Run lint

on:
pull_request:
paths:
- "**.php"
- "phpcs.xml"
- "composer.json"
- ".github/workflows/lint.yaml"

jobs:
phpcs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- run: |
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
php phpcs.phar --version
- run: |
git diff --name-only ${{ github.event.pull_request.base.sha }} |egrep '\.php$' > .list.txt
php phpcs.phar --standard=./phpcs.xml --file-list=./.list.txt
- name: Update composer
run: composer selfupdate

- name: Install dependencies
run: composer install --prefer-dist --no-interaction

- name: Run static analysis
run: composer static-analysis
34 changes: 7 additions & 27 deletions .github/workflows/build.yml → .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,19 @@
name: build
name: Run tests

on:
push:
branches:
- master
- main
pull_request:
paths:
- "**.php"
- "composer.json"
- ".github/workflows/run-tests.yaml"

jobs:
lint:
runs-on: windows-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
extensions: zip, curl, xdebug
coverage: xdebug

- name: Update composer
run: composer selfupdate

- name: Install dependencies
run: composer install --prefer-dist --no-interaction

- name: Run static analysis
run: composer static-analysis
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
os: [windows-latest, macOS-latest]
php: ['8.3', '8.2', '8.1', '8.0', '7.4']

name: PHP ${{ matrix.php }} - ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ A PHP code generator to generate code via your LAYOUT file.

```shell
# Get a sample layout file.
$ phead sample
$ phead --sample my-layout.yaml
my-layout.yaml is generated.
Expand Down
Empty file modified bin/phead
100644 → 100755
Empty file.
Empty file modified bin/phead.bat
100644 → 100755
Empty file.
15 changes: 15 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="phpcs.xsd">
<description>The coding standard for PHP_CodeSniffer itself.</description>

<file>src</file>
<file>tests</file>

<arg name="basepath" value="./"/>
<arg name="colors"/>
<arg name="parallel" value="8"/>
<arg value="nps"/>

<rule ref="PSR12">
</rule>
</ruleset>
1 change: 1 addition & 0 deletions src/ArrAccess.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Absszero\Phead;

class ArrAccess
Expand Down
1 change: 1 addition & 0 deletions src/BuildInFunction.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Absszero\Phead;

function file_get_contents(string $file): string
Expand Down
9 changes: 5 additions & 4 deletions src/Layout.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

namespace Absszero\Phead;

use Symfony\Component\Yaml\Yaml;

class Layout
{
const BRACKET_PATTERN = '/{{ *([^ ]+) *}}/';
public const BRACKET_PATTERN = '/{{ *([^ ]+) *}}/';

public int $indent = 4;
public string $indentChar = ' ';
Expand All @@ -15,7 +16,7 @@ class Layout
public array $data = [];
public static function parse(string $file): self
{
$layout = new self;
$layout = new self();
$data = (array)Yaml::parseFile($file);
$data = $layout->filter($data);
$data = $layout->replaceEnvs($data);
Expand Down Expand Up @@ -169,9 +170,9 @@ public function appendMethods(array $files): array
foreach ((array)$file['methods'] as $index => $method) {
$lines = array_filter(explode(PHP_EOL, $method));
$lines = array_map(fn($line) => str_pad('', $this->indent, $this->indentChar) . $line, $lines);
$methods[$index] = PHP_EOL . implode(PHP_EOL, $lines);
$methods[$index] = PHP_EOL . join(PHP_EOL, $lines);
}
$methods = implode(PHP_EOL, $methods);
$methods = join(PHP_EOL, $methods);

// insert methods on the bottom of the class file
$length = strlen($file['from']) - 1;
Expand Down
26 changes: 14 additions & 12 deletions src/PheadCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Absszero\Phead;

use Symfony\Component\Console\Command\Command;
Expand All @@ -18,24 +19,25 @@ protected function configure(): void
$this
->setName('phead')
->setDescription('Generate code by layout')
->addArgument('layout', InputArgument::REQUIRED, 'The layout to use.')
->addArgument('layout', InputArgument::REQUIRED, 'The layout file to use.')
->addOption('dry', 'd', InputOption::VALUE_NONE, 'Dry run.')
->addOption('only', 'o', InputOption::VALUE_OPTIONAL, 'Only those file keys are generated. Separate by comma.')
->addOption('force', 'f', InputOption::VALUE_NONE, 'Overwrite existed files.');
->addOption('force', 'f', InputOption::VALUE_NONE, 'Overwrite existed files.')
->addOption('sample', 's', InputOption::VALUE_NONE, 'Generate a sample layout file.');
// ->addOption('var', '$', InputOption::VALUE_NONE, 'Add a variable for the layout file.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$file = $input->getArgument('layout');
if ($file === 'sample') {
return $this->genSample($input, $output);
if ($input->getOption('sample')) {
return $this->generateSample($input, $output);
}

try {
$file = $input->getArgument('layout');
$this->layout = Layout::parse($file);
} catch (ParseException $th) {
$output->writeln('<error>'. $th->getMessage() .'</error>');
$output->writeln('<error>' . $th->getMessage() . '</error>');
return Command::FAILURE;
}

Expand Down Expand Up @@ -66,7 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$text = '';
if (file_exists($file['to'])) {
if (!$input->getOption('force')) {
$output->writeln('');
$output->writeln('<comment> (skip) </comment>');
continue;
}
$text = '<fg=red> (overwrite) </>';
Expand Down Expand Up @@ -119,25 +121,25 @@ protected function getOnlyFiles(array $files, ?string $only): array
*
* @return int [return description]
*/
protected function genSample(InputInterface $input, OutputInterface $output): int
protected function generateSample(InputInterface $input, OutputInterface $output): int
{
$target = getcwd() . '/my-layout.yaml';
$target = $input->getArgument('layout');
$output->writeln('');
if (file_exists($target)) {
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('<comment>my-layout.yaml</comment> <info>already exists. Replace the file?</info>', false);
$question = new ConfirmationQuestion("<comment>$target</comment> <info>already exists. Replace the file?</info>", false);
if (!$helper->ask($input, $output, $question)) {
return Command::SUCCESS;
}
}

$result = copy(__DIR__ . '/../config/sample.yaml', $target);
if ($result) {
$output->writeln('<comment>my-layout.yaml</comment> <info>is generated.</info>');
$output->writeln("<comment>$target</comment> <info>is generated.</info>");
return Command::SUCCESS;
}

$output->writeln('<comment>my-layout.yaml</comment><error> is not generated.</error>');
$output->writeln("<comment>$target</comment><error> is not generated.</error>");
return Command::FAILURE;
}
}
12 changes: 6 additions & 6 deletions tests/LayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testFilter(): void
]
];

$layout = new Layout;
$layout = new Layout();
$data = $layout->filter($data);
$this->assertArrayNotHasKey('a', $data['$files']);
$this->assertArrayHasKey('b', $data['$files']);
Expand All @@ -53,7 +53,7 @@ public function testReplaceEnvs(): void
'foo2' => '{{ $globals.FOO }}',
];

$layout = new Layout;
$layout = new Layout();
$vars = $layout->replaceEnvs($vars);

$this->assertEquals('BAR', $vars['bar']['foo']);
Expand Down Expand Up @@ -83,7 +83,7 @@ public function testReplaceGlobalVars(): void
]
];

$layout = new Layout;
$layout = new Layout();
$methods = $layout->replaceGlobalVars($data)['$files']['a']['methods'];

$this->assertEquals('{{ bar }}', $methods[0]);
Expand All @@ -107,7 +107,7 @@ public function testReplaceLocalVars(): void
],
];

$layout = new Layout;
$layout = new Layout();
$file = $layout->replaceLocalVars($files)['a'];

$this->assertEquals('BAR', $file['from']);
Expand All @@ -128,7 +128,7 @@ public function testCollectFilesVars(): void
'$files' => $files,
];

$layout = new Layout;
$layout = new Layout();
$data = $layout->collectFilesVars($data);
$files = $data['$files'];
$this->assertEquals('UpdateRequest', $files['a']['vars']['class']);
Expand All @@ -138,7 +138,7 @@ public function testCollectFilesVars(): void
// test append methods to class
public function testAppendMethods(): void
{
$layout = new Layout;
$layout = new Layout();
$files = [
'a' => [
'from' => 'class Hello
Expand Down
4 changes: 2 additions & 2 deletions tests/PheadCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public function setUp(): void
require_once __DIR__ . '/../src/BuildInFunction.php';
}

public function testgenSample(): void
public function testGenerateSample(): void
{
$target = getcwd() . '/my-layout.yaml';
file_exists($target) && unlink($target);

$command = $this->addCommand(PheadCommand::class);
$input = ['layout' => 'sample'];
$input = ['layout' => 'my-layout.yaml', '--sample' => true];
$tester = $this->executeCommand($command, $input);
$this->assertEquals(0, $tester->getStatusCode());

Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Tests;

use PHPUnit\Framework\TestCase as PHPUnitTestCase;
Expand Down Expand Up @@ -26,7 +27,7 @@ protected function setUp(): void
protected function addCommand($class): Command
{
if (is_string($class)) {
$class = new $class;
$class = new $class();
}

$this->application->add($class);
Expand Down

0 comments on commit 42731cb

Please sign in to comment.