Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not keep empty file #18

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/DefaultSingleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protected function configure(): void
->addOption('input-ext', 'i', InputOption::VALUE_REQUIRED, 'Treat only for file having this given extension and remove this extension')
->addOption('output-ext', 'o', InputOption::VALUE_NONE, 'Do not rewrite files but write a new one adding an extension which includes step number and if it\'s state (exercise or solution)')
->addOption('keep-orig', null, InputOption::VALUE_NONE, '<info>--output-ext</info> alias for backward compatibility')
->addOption('no-empty', 'n', InputOption::VALUE_NONE, 'No empty file')
->addOption('keep-tags', 't', InputOption::VALUE_NONE, 'Do not remove start/stop tags')
->addOption('exercise', 'e', InputOption::VALUE_NONE, 'Write exercise\'s worksheet (default)')
//->addOption('worksheet', 'w', InputOption::VALUE_NONE, 'Write exercise\'s worksheet (default)')
Expand Down
18 changes: 14 additions & 4 deletions src/ExerciseCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private function getCommentPattern(string $file = null): ?string
}
}

public function cleanFiles(array $pathList, float $targetStep = 1, bool $solution = false, bool $keepTags = false, string $outputExtension = null, string $inputExtension = null): void
public function cleanFiles(array $pathList, float $targetStep = 1, bool $solution = false, bool $keepTags = false, string $outputExtension = null, string $inputExtension = null, bool $noEmptyFile = false): void
{
$targetStepName = $this->getStepName($targetStep);
$targetStepName = null === $targetStepName ? '' : " “{$targetStepName}”";
Expand Down Expand Up @@ -316,10 +316,20 @@ public function cleanFiles(array $pathList, float $targetStep = 1, bool $solutio
if ($outputExtension) {
$outputFile = "$outputFile$outputExtension";
}
if (false !== file_put_contents($outputFile, implode(PHP_EOL, $this->cleanCodeLines(file($inputFile, FILE_IGNORE_NEW_LINES), $targetStep, $solution, $keepTags, $inputFile)))) {
$this->writeToOutput("<info>…$outputFile written.</info>", OutputInterface::VERBOSITY_VERBOSE);

$outputContent = implode(PHP_EOL, $this->cleanCodeLines(file($inputFile, FILE_IGNORE_NEW_LINES), $targetStep, $solution, $keepTags, $inputFile));
if ($noEmptyFile && '' === trim($outputContent)) {
if (unlink($inputFile)) {
$this->writeToOutput("<info>…$outputFile removed.</info>", OutputInterface::VERBOSITY_VERBOSE);
} else {
trigger_error("Emptied $outputFile couldn't be removed.", E_USER_WARNING);
}
} else {
trigger_error("$outputFile couldn't be written", E_USER_ERROR);
if (false !== file_put_contents($outputFile, $outputContent)) {
$this->writeToOutput("<info>…$outputFile written.</info>", OutputInterface::VERBOSITY_VERBOSE);
} else {
trigger_error("$outputFile couldn't be written", E_USER_ERROR);
}
}
}
}
Expand Down