in src/Utils/Actions/RunCsFixerCommand.php [68:154]
protected function execute(InputInterface $input, OutputInterface $output)
{
$repo = $input->getArgument('repo');
if (false === strpos($repo, '/')) {
throw new \Exception('Invalid repo name. Use the format: owner/repo');
}
$this->client = new Client(['http_errors' => false]);
$ref = $input->getOption('ref');
$job = ($workflowFile = $input->getOption('workflow-file'))
? $this->getJobFromWorkflowFile($repo, $ref, $workflowFile)
: $this->determineWorkflowFile($repo, $ref);
if (!$job) {
throw new \Exception('No job found for php-tools/code-standards.yaml found in the workflow file(s)');
}
$output->writeln(sprintf('Using workflow job "%s" in "%s"', $job['name'], $job['file']));
// get the default config
$defaultWorkflow = Yaml::parse(file_get_contents(__DIR__ . '/../../../.github/workflows/code-standards.yml'));
$defaults = [];
foreach ($defaultWorkflow['on']['workflow_call']['inputs'] as $name => $inputOptions) {
$defaults[$name] = $inputOptions['default'] ?? '';
}
$options = array_merge($defaults, $job['with'] ?? []);
if (str_starts_with($options['config'], 'GoogleCloudPlatform/php-tools/')) {
// use local file
$options['config'] = str_replace(
'GoogleCloudPlatform/php-tools/',
__DIR__ . '/../../../',
$options['config']
);
// strip branch (we'll ignore it in favor of the current branch)
if (false !== $i = strpos($options['config'], '@')) {
$options['config'] = substr($options['config'], 0, $i);
}
if (!file_exists($options['config'])) {
throw new \Exception('config file not found: ' . realpath($options['config']));
}
}
// go through config options and set env vars accordingly
$rules = json_encode(array_merge(
json_decode($options['rules'], true),
json_decode($options['add-rules'], true)
));
$excludePatterns = str_replace(["\n", ' '], '', $options['exclude-patterns']);
// use config path only if EXCLUDE_PATTERN is empty
if ($options['config']) {
// set environment variables so they're available in the CONFIG file
$env = sprintf(
'CONFIG_PATH=%s RULES=$\'%s\' EXCLUDE_PATTERNS=$\'%s\'',
$options['path'],
$rules,
$excludePatterns,
);
// Run command using the --config flag
$cmd = sprintf(
'%s ~/.composer/vendor/bin/php-cs-fixer fix --config=%s %s',
$env,
$options['config'],
$input->getOption('flags')
);
} else {
// Run command using the --rules flag
$cmd = sprintf(
'~/.composer/vendor/bin/php-cs-fixer fix %s --rules=$\'%s\' %s',
$options['path'],
$rules,
$input->getOption('flags')
);
}
$output->writeln('Executing the following command: ');
$output->writeln('');
$output->writeln("<info>\t" . $cmd . '</>');
$output->writeln('');
// @TODO use Symfony process component to run this
passthru($cmd, $resultCode);
return $resultCode;
}