74 lines
2.9 KiB
PHP
74 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace SparkPlug\Com\Noccy\Git;
|
|
|
|
use Spark\Commands\Command;
|
|
use SparkPlug;
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
class GitIgnoreCommand extends Command
|
|
{
|
|
protected function configure()
|
|
{
|
|
$this->setName("git:ignore")
|
|
->setDescription("List, add or remove paths from gits ignorelists")
|
|
->addOption("local","l",InputOption::VALUE_NONE,"Use the local ignore rather than .gitignore")
|
|
->addOption("add","a",InputOption::VALUE_NONE,"Add a pattern")
|
|
->addOption("remove","r",InputOption::VALUE_NONE,"Attempt to remove a pattern")
|
|
->addArgument("pattern", InputArgument::OPTIONAL, "Pattern to add or remove")
|
|
;
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
{
|
|
$local = $input->getOption("local");
|
|
$root = $this->getEnvironment()->getProjectDirectory();
|
|
$file = $root . (!$local ? "/.gitignore" : "/.git/info/exclude");
|
|
$pattern = $input->getArgument("pattern");
|
|
|
|
if (empty($pattern)) {
|
|
if (file_exists($file)) {
|
|
$ignores = file($file, FILE_IGNORE_NEW_LINES);
|
|
foreach ($ignores as $ignore) {
|
|
if (str_starts_with(trim($ignore),'#')) {
|
|
$output->writeln("<fg=green>".$ignore."</>");
|
|
} else {
|
|
$output->writeln("<fg=white>".$ignore."</>");
|
|
}
|
|
}
|
|
return Command::SUCCESS;
|
|
}
|
|
$output->writeln("<info>Empty list</>");
|
|
return Command::SUCCESS;
|
|
} elseif ($input->getOption("add") && $pattern) {
|
|
if (file_exists($file)) {
|
|
$ignores = file($file, FILE_IGNORE_NEW_LINES);
|
|
} else {
|
|
$ignores = [];
|
|
}
|
|
array_push($ignores, $pattern);
|
|
file_put_contents($file, join("\n", $ignores));
|
|
$output->writeln("<info>Updated {$file}</>");
|
|
return Command::SUCCESS;
|
|
} elseif ($input->getOption("remove") && $pattern) {
|
|
if (file_exists($file)) {
|
|
$ignores = file($file, FILE_IGNORE_NEW_LINES);
|
|
$ignores = array_filter($ignores, function ($v) use ($pattern) {
|
|
return $v != $pattern;
|
|
});
|
|
$output->writeln("<info>Updated {$file}</>");
|
|
file_put_contents($file, join("\n", $ignores));
|
|
return Command::SUCCESS;
|
|
}
|
|
$output->writeln("<info>Not updating non-existing file {$file}</>");
|
|
return Command::SUCCESS;
|
|
}
|
|
|
|
$output->writeln("<error>Expected no pattern, --add pattern or --remove pattern</>");
|
|
return Command::INVALID;
|
|
}
|
|
}
|