Skip to content

Commit

Permalink
Added package generator cli tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
afilina committed Jan 2, 2017
1 parent b0c7422 commit fddf89d
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ Quickly validate your project's compliance by following these steps:
- Install package in your project: `composer require pds/skeleton @dev`
- Run the validator: `vendor/bin/pdsskeleton validate`

## Generator

Generate a compliant package skeleton by following these steps:

- Install package in your project: `composer require pds/skeleton @dev`
- Run the generator: `vendor/bin/pdsskeleton generate`

## Root-Level Directories

### bin/
Expand Down
2 changes: 1 addition & 1 deletion src/PDS/Skeleton/ComplianceValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function validate($lines)
public function getFiles()
{
if ($this->files == null) {
$files = scandir(__DIR__ . "/../../../../");
$files = scandir(__DIR__ . "/../../../../../../");
foreach ($files as $i => $file) {
if (is_dir($file)) {
$files[$i] .= "/";
Expand Down
1 change: 1 addition & 0 deletions src/PDS/Skeleton/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Console
{
protected $commandsWhitelist = [
'validate' => 'PDS\Skeleton\ComplianceValidator',
'generate' => 'PDS\Skeleton\PackageGenerator',
];

public function execute($args)
Expand Down
59 changes: 59 additions & 0 deletions src/PDS/Skeleton/PackageGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace PDS\Skeleton;

use PDS\Skeleton\ComplianceValidator;

class PackageGenerator
{
public function execute()
{
$validator = new ComplianceValidator();
$lines = $validator->getFiles();
$validatorResults = $validator->validate($lines);
$files = $this->createFiles($validatorResults);
$this->outputResults($files);
return true;
}

public function createFiles($validatorResults, $root = null)
{
if ($root == null) {
$root = realpath(__DIR__ . "/../../../../../../");
}
$files = $this->createFileList($validatorResults);
foreach ($files as $file) {
$isDir = substr($file, -1, 1) == '/';
if ($isDir) {
$path = $root . '/' . substr($file, 0, -1);
mkdir($path, 0755);
continue;
}
$path = $root . '/' . $file . '.md';
file_put_contents($path, '');
chmod($path, 0644);
}
return $files;
}

public function createFileList($validatorResults)
{
$files = [];
foreach ($validatorResults as $label => $complianceResult) {
if (in_array($complianceResult['state'], [
ComplianceValidator::STATE_OPTIONAL_NOT_PRESENT,
ComplianceValidator::STATE_REQUIRED_NOT_PRESENT,
])) {
$files[$label] = $complianceResult['expected'];
}
}
return $files;
}

public function outputResults($results)
{
foreach ($results as $file) {
echo "Created {$file}" . PHP_EOL;
}
}
}
53 changes: 53 additions & 0 deletions tests/PackageGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

$autoloadFiles = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php'
];

foreach ($autoloadFiles as $autoloadFile) {
if (file_exists($autoloadFile)) {
require_once $autoloadFile;
break;
}
}

use PDS\Skeleton\ComplianceValidator;
use PDS\Skeleton\PackageGenerator;

$tester = new PackageGeneratorTest();
$tester->testGenerate_WithMissingBin_ReturnsBin();

echo "Errors: {$tester->numErrors}" . PHP_EOL;

class PackageGeneratorTest
{
public $numErrors = 0;

public function testGenerate_WithMissingBin_ReturnsBin()
{
$validatorResults = [
'bin/' => [
'state' => ComplianceValidator::STATE_OPTIONAL_NOT_PRESENT,
'expected' => 'bin/',
],
'config/' => [
'state' => ComplianceValidator::STATE_INCORRECT_PRESENT,
'expected' => 'config/',
],
];

$generator = new PackageGenerator();
$files = $generator->createFileList($validatorResults);

if (!array_key_exists('bin/', $files)) {
$this->numErrors++;
echo __FUNCTION__ . ": Expected bin/ to be present" . PHP_EOL;
}

if (array_key_exists('config/', $files)) {
$this->numErrors++;
echo __FUNCTION__ . ": Expected config/ to be absent" . PHP_EOL;
}
}
}

0 comments on commit fddf89d

Please sign in to comment.