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

Fix .env support when running from phar archive #239

Merged
merged 2 commits into from
Sep 15, 2023
Merged
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
11 changes: 10 additions & 1 deletion bin/moodle-plugin-ci
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ if (file_exists(__DIR__ . '/../../../autoload.php')) {
define('MOODLE_PLUGIN_CI_VERSION', '4.1.3');

define('MOODLE_PLUGIN_CI_BOXED', '@is_boxed@');
define('ENV_FILE', dirname(__DIR__) . '/.env');

// If we are running moodle-plugin-ci within a PHAR, we need to set the
// path to the dotenv file differently.
if (\Phar::running() !== '') {
// The .env file is in the same directory than the phar.
define('ENV_FILE', dirname(\Phar::running(false)) . '/.env');
} else {
// The .env file is in the root directory of the moodle-plugin-ci project.
define('ENV_FILE', dirname(__DIR__) . '/.env');
}

if (file_exists(ENV_FILE)) {
// Use this file because PHP cannot write to the environment.
Expand Down
8 changes: 8 additions & 0 deletions box.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
"Docs",
"pix"
]
},
{
"in": "vendor/squizlabs/php_codesniffer",
"name": [
"phpcs",
"phpcbf",
"CodeSniffer.conf"
]
}
],
"compactors": "KevinGH\\Box\\Compactor\\Php",
Expand Down
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ The format of this change log follows the advice given at [Keep a CHANGELOG](htt

## [Unreleased]
### Fixed
- Fix the `.env` support when running from within the PHAR archive.
- Fix the `mustache` command to work from within the PHAR archive.
- Fix the `phpcs` and `phpcbf` commands to work from within the PHAR archive.

## [4.1.3] - 2023-09-08
### Changed
Expand Down
2 changes: 1 addition & 1 deletion src/Command/AbstractPluginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function configure(): void

protected function initialize(InputInterface $input, OutputInterface $output): void
{
if (!isset($this->plugin)) {
if (!isset($this->plugin) && $input->getArgument('plugin') !== null) {
$validate = new Validate();
$pluginDir = realpath($validate->directory($input->getArgument('plugin')));
$this->plugin = new MoodlePlugin($pluginDir);
Expand Down
30 changes: 27 additions & 3 deletions src/Command/CodeCheckerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return $this->outputSkip($output);
}

$cmd = [
'php', __DIR__ . '/../../vendor/squizlabs/php_codesniffer/bin/phpcs',
$filesystem = new Filesystem();
$pathToPHPCS = __DIR__ . '/../../vendor/squizlabs/php_codesniffer/bin/phpcs';
$pathToConf = __DIR__ . '/../../vendor/squizlabs/php_codesniffer/CodeSniffer.conf';
$basicCMD = ['php', $pathToPHPCS];
// If we are running phpcs within a PHAR, the command is different, and we need also to copy the .conf file.
// @codeCoverageIgnoreStart
// (This is not executed when running tests, only when within a PHAR)
if (\Phar::running() !== '') {
// Invoke phpcs from the PHAR (via include, own params after --).
$basicCMD = ['php', '-r', 'include "' . $pathToPHPCS . '";', '--'];
// Copy the .conf file to the directory where the PHAR is running. That way phpcs will find it.
$targetPathToConf = dirname(\Phar::running(false)) . '/CodeSniffer.conf';
$filesystem->copy($pathToConf, $targetPathToConf, true);
}
// @codeCoverageIgnoreEnd

$cmd = array_merge($basicCMD, [
'--standard=' . ($input->getOption('standard') ?: 'moodle'),
'--extensions=php',
'-p',
Expand All @@ -73,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'--report-full',
'--report-width=132',
'--encoding=utf-8',
];
]);

// If we aren't using the max-warnings option, then we can forget about warnings and tell phpcs
// to ignore them for exit-code purposes (still they will be reported in the output).
Expand All @@ -98,6 +113,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$process = $this->execute->passThroughProcess(new Process($cmd, $this->plugin->directory, null, null, null));

// If we are running phpcs within a PHAR, we need to remove the previously copied conf file.
// @codeCoverageIgnoreStart
// (This is not executed when running tests, only when within a PHAR)
if (\Phar::running() !== '') {
$targetPathToConf = dirname(\Phar::running(false)) . '/CodeSniffer.conf';
$filesystem->remove($targetPathToConf);
}
// @codeCoverageIgnoreEnd

// If we aren't using the max-warnings option, process exit code is enough for us.
if ($input->getOption('max-warnings') < 0) {
return $process->isSuccessful() ? 0 : 1;
Expand Down
31 changes: 28 additions & 3 deletions src/Command/CodeFixerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -42,8 +43,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return $this->outputSkip($output);
}

$cmd = [
'php', __DIR__ . '/../../vendor/squizlabs/php_codesniffer/bin/phpcbf',
$filesystem = new Filesystem();
$pathToPHPCBF = __DIR__ . '/../../vendor/squizlabs/php_codesniffer/bin/phpcbf';
$pathToConf = __DIR__ . '/../../vendor/squizlabs/php_codesniffer/CodeSniffer.conf';
$basicCMD = ['php', $pathToPHPCBF];
// If we are running phpcs within a PHAR, the command is different, and we need also to copy the .conf file.
// @codeCoverageIgnoreStart
// (This is not executed when running tests, only when within a PHAR)
if (\Phar::running() !== '') {
// Invoke phpcbf from the PHAR (via include, own params after --).
$basicCMD = ['php', '-r', 'include "' . $pathToPHPCBF . '";', '--'];
// Copy the .conf file to the directory where the PHAR is running. That way phpcbf will find it.
$targetPathToConf = dirname(\Phar::running(false)) . '/CodeSniffer.conf';
$filesystem->copy($pathToConf, $targetPathToConf, true);
}
// @codeCoverageIgnoreEnd

$cmd = array_merge($basicCMD, [
'--standard=' . ($input->getOption('standard') ?: 'moodle'),
'--extensions=php',
'-p',
Expand All @@ -54,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'--report-full',
'--report-width=132',
'--encoding=utf-8',
];
]);

// Add the files to process.
foreach ($files as $file) {
Expand All @@ -63,6 +79,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->execute->passThroughProcess(new Process($cmd, $this->plugin->directory, null, null, null));

// If we are running phpcbf within a PHAR, we need to remove the previously copied conf file.
// @codeCoverageIgnoreStart
// (This is not executed when running tests, only when within a PHAR)
if (\Phar::running() !== '') {
$targetPathToConf = dirname(\Phar::running(false)) . '/CodeSniffer.conf';
$filesystem->remove($targetPathToConf);
}
// @codeCoverageIgnoreEnd

return 0;
}
}