Skip to content

Commit

Permalink
Merge pull request #8 from unfunco/feature/dry-run
Browse files Browse the repository at this point in the history
Added a --dry-run option to the licenser command
  • Loading branch information
jameshalsall committed Jan 12, 2016
2 parents 06f866d + d7ffce1 commit f8c2bfa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ and very much in development.

- ~~Add support for replacing an existing license~~ (thanks to @markwilson)
- ~~Add built-in license options~~
- Add dry-run option to see affected files before adding headers
- ~~Add dry-run option to see affected files before adding headers~~
- Add support for processing single files
- Add support for placeholders in custom license files (such as year, owner etc.)
- ~~Add support for placeholders in custom license files (such as year, owner etc.)~~

## Bug Reporting ##

Expand Down
20 changes: 18 additions & 2 deletions src/JamesHalsall/Licenser/Command/LicenserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,18 @@ protected function configure()
'license to add the email address(es) of the license(es) to the license header. Can be a comma ' .
'separated list of email addresses or a single email address'
)
->addOption('remove-existing', 'r', InputOption::VALUE_NONE, 'Remove existing license headers');
->addOption(
'remove-existing',
'r',
InputOption::VALUE_NONE,
'Remove existing license headers'
)
->addOption(
'dry-run',
'',
InputOption::VALUE_NONE,
'If specified, the command will report a list of affected files but will make no modifications'
);
}

/**
Expand Down Expand Up @@ -103,6 +114,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->licenser->setOutputStream($output);

$sources = $input->getArgument('sources');
$this->licenser->process($sources, (boolean) $input->getOption('remove-existing'));

$this->licenser->process(
$sources,
(bool) $input->getOption('remove-existing'),
(bool) $input->getOption('dry-run')
);
}
}
35 changes: 23 additions & 12 deletions src/JamesHalsall/Licenser/Licenser.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,29 @@ public function getLicenseHeader()
/**
* Processes a path and adds licenses
*
* @param string $path The path to the files/directory
* @param boolean $removeExisting True to remove existing license headers in files before adding
* new license (defaults to false)
* @param string $path The path to the files/directory
* @param bool $removeExisting True to remove existing license headers in files before adding
* new license (defaults to false)
* @param bool $dryRun True to report modified files and to not make any modifications
*/
public function process($path, $removeExisting = false)
public function process($path, $removeExisting = false, $dryRun = false)
{
$iterator = $this->finder->name('*.php')
->in(realpath($path));

foreach ($iterator as $file) {
$this->processFile($file, $removeExisting);
$this->processFile($file, $removeExisting, $dryRun);
}
}

/**
* Processes a single file
*
* @param SplFileInfo $file The path to the file
* @param boolean $removeExisting True to remove existing license header before adding new one
* @param bool $removeExisting True to remove existing license header before adding new one
* @param bool $dryRun True to report a modified file and to not make modifications
*/
private function processFile(SplFileInfo $file, $removeExisting)
private function processFile(SplFileInfo $file, $removeExisting, $dryRun)
{
if ($file->isDir()) {
return;
Expand All @@ -128,11 +130,15 @@ private function processFile(SplFileInfo $file, $removeExisting)
}

if (null !== $licenseTokenIndex && true === $removeExisting) {
$this->removeExistingLicense($file, $tokens, $licenseTokenIndex);
$this->removeExistingLicense($file, $tokens, $licenseTokenIndex, $dryRun);
}

if (null === $licenseTokenIndex || true === $removeExisting) {
$this->log(sprintf('Adding license header for "%s"', $file->getRealPath()));
$this->log(sprintf('<fg=green>[+]</> Adding license header for <options=bold>%s</>', $file->getRealPath()));

if (true === $dryRun) {
return;
}

$license = explode(PHP_EOL, $this->licenseHeader);
$license = array_map(function ($licenseLine) {
Expand All @@ -143,7 +149,7 @@ private function processFile(SplFileInfo $file, $removeExisting)
$content = preg_replace('/<\?php/', '<?php' . PHP_EOL . PHP_EOL . '/*' . PHP_EOL . $license . PHP_EOL . ' */', $file->getContents(), 1);
file_put_contents($file->getRealPath(), $content);
} else {
$this->log(sprintf('Skipping "%s"', $file->getRealPath()));
$this->log(sprintf('<fg=cyan>[S]</> Skipping <options=bold>%s</>', $file->getRealPath()));
}
}

Expand All @@ -153,10 +159,15 @@ private function processFile(SplFileInfo $file, $removeExisting)
* @param SplFileInfo $file The file to remove the license header from
* @param array $tokens File token information
* @param integer $licenseIndex License token index
* @param bool $dryRun True to report a modified file and not to make modifications
*/
private function removeExistingLicense(SplFileInfo $file, array $tokens, $licenseIndex)
private function removeExistingLicense(SplFileInfo $file, array $tokens, $licenseIndex, $dryRun)
{
$this->log(sprintf('Removing license header for "%s"', $file->getRealPath()));
$this->log(sprintf('<fg=red>[-]</> Removing license header for <options=bold>%s</>', $file->getRealPath()));

if (true === $dryRun) {
return;
}

$content = $file->getContents();

Expand Down

0 comments on commit f8c2bfa

Please sign in to comment.