Skip to content

Commit fddf89d

Browse files
committed
Added package generator cli tool.
1 parent b0c7422 commit fddf89d

File tree

5 files changed

+121
-1
lines changed

5 files changed

+121
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ Quickly validate your project's compliance by following these steps:
4545
- Install package in your project: `composer require pds/skeleton @dev`
4646
- Run the validator: `vendor/bin/pdsskeleton validate`
4747

48+
## Generator
49+
50+
Generate a compliant package skeleton by following these steps:
51+
52+
- Install package in your project: `composer require pds/skeleton @dev`
53+
- Run the generator: `vendor/bin/pdsskeleton generate`
54+
4855
## Root-Level Directories
4956

5057
### bin/

src/PDS/Skeleton/ComplianceValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function validate($lines)
5757
public function getFiles()
5858
{
5959
if ($this->files == null) {
60-
$files = scandir(__DIR__ . "/../../../../");
60+
$files = scandir(__DIR__ . "/../../../../../../");
6161
foreach ($files as $i => $file) {
6262
if (is_dir($file)) {
6363
$files[$i] .= "/";

src/PDS/Skeleton/Console.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Console
66
{
77
protected $commandsWhitelist = [
88
'validate' => 'PDS\Skeleton\ComplianceValidator',
9+
'generate' => 'PDS\Skeleton\PackageGenerator',
910
];
1011

1112
public function execute($args)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace PDS\Skeleton;
4+
5+
use PDS\Skeleton\ComplianceValidator;
6+
7+
class PackageGenerator
8+
{
9+
public function execute()
10+
{
11+
$validator = new ComplianceValidator();
12+
$lines = $validator->getFiles();
13+
$validatorResults = $validator->validate($lines);
14+
$files = $this->createFiles($validatorResults);
15+
$this->outputResults($files);
16+
return true;
17+
}
18+
19+
public function createFiles($validatorResults, $root = null)
20+
{
21+
if ($root == null) {
22+
$root = realpath(__DIR__ . "/../../../../../../");
23+
}
24+
$files = $this->createFileList($validatorResults);
25+
foreach ($files as $file) {
26+
$isDir = substr($file, -1, 1) == '/';
27+
if ($isDir) {
28+
$path = $root . '/' . substr($file, 0, -1);
29+
mkdir($path, 0755);
30+
continue;
31+
}
32+
$path = $root . '/' . $file . '.md';
33+
file_put_contents($path, '');
34+
chmod($path, 0644);
35+
}
36+
return $files;
37+
}
38+
39+
public function createFileList($validatorResults)
40+
{
41+
$files = [];
42+
foreach ($validatorResults as $label => $complianceResult) {
43+
if (in_array($complianceResult['state'], [
44+
ComplianceValidator::STATE_OPTIONAL_NOT_PRESENT,
45+
ComplianceValidator::STATE_REQUIRED_NOT_PRESENT,
46+
])) {
47+
$files[$label] = $complianceResult['expected'];
48+
}
49+
}
50+
return $files;
51+
}
52+
53+
public function outputResults($results)
54+
{
55+
foreach ($results as $file) {
56+
echo "Created {$file}" . PHP_EOL;
57+
}
58+
}
59+
}

tests/PackageGeneratorTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
$autoloadFiles = [
4+
__DIR__ . '/../vendor/autoload.php',
5+
__DIR__ . '/../../../autoload.php'
6+
];
7+
8+
foreach ($autoloadFiles as $autoloadFile) {
9+
if (file_exists($autoloadFile)) {
10+
require_once $autoloadFile;
11+
break;
12+
}
13+
}
14+
15+
use PDS\Skeleton\ComplianceValidator;
16+
use PDS\Skeleton\PackageGenerator;
17+
18+
$tester = new PackageGeneratorTest();
19+
$tester->testGenerate_WithMissingBin_ReturnsBin();
20+
21+
echo "Errors: {$tester->numErrors}" . PHP_EOL;
22+
23+
class PackageGeneratorTest
24+
{
25+
public $numErrors = 0;
26+
27+
public function testGenerate_WithMissingBin_ReturnsBin()
28+
{
29+
$validatorResults = [
30+
'bin/' => [
31+
'state' => ComplianceValidator::STATE_OPTIONAL_NOT_PRESENT,
32+
'expected' => 'bin/',
33+
],
34+
'config/' => [
35+
'state' => ComplianceValidator::STATE_INCORRECT_PRESENT,
36+
'expected' => 'config/',
37+
],
38+
];
39+
40+
$generator = new PackageGenerator();
41+
$files = $generator->createFileList($validatorResults);
42+
43+
if (!array_key_exists('bin/', $files)) {
44+
$this->numErrors++;
45+
echo __FUNCTION__ . ": Expected bin/ to be present" . PHP_EOL;
46+
}
47+
48+
if (array_key_exists('config/', $files)) {
49+
$this->numErrors++;
50+
echo __FUNCTION__ . ": Expected config/ to be absent" . PHP_EOL;
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)