Skip to content

Commit

Permalink
Enable mustache check to run from phar
Browse files Browse the repository at this point in the history
We need both the php script and the bundled vnu.jar
files to be put in tmp dir and execute/use them from there.

This shouldn't affect normal (non-phar executions).
  • Loading branch information
stronk7 committed Sep 8, 2023
1 parent bfa22ca commit 02cfca8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions box.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
"name": [
"*.php",
"*.xml",
"*.sh"
"*.sh",
"*.jar"
],
"exclude": [
"Tests",
"tests",
"Docs",
"node_modules",
"pix"
]
}
Expand Down
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
The format of this change log follows the advice given at [Keep a CHANGELOG](http://keepachangelog.com).

## [Unreleased]
### Fixed
- Fix the `mustache` command to work from within the PHAR archive.

## [4.1.3] - 2023-09-08
### Changed
- Updated project dependencies to current [moodle-cs](https://github.com/moodlehq/moodle-cs), [moodle-local_moodlecheck](https://github.com/moodlehq/moodle-local_moodlecheck) and [moodle-local_ci](https://github.com/moodlehq/moodle-local_ci) versions. Also, to various internal / development tools.
Expand Down
14 changes: 12 additions & 2 deletions src/Command/MustacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$jarFile = $this->resolveJarFile();

// This is a workaround to execute mustache_lint.php file from within a phar.
// (by copying both the script and the jar file to a temporary directory)
$filesystem = new Filesystem();
$wrapper = tempnam(sys_get_temp_dir(), 'mustache-linter-wrapper');
$tmpDir = sys_get_temp_dir();
$wrapper = tempnam($tmpDir, 'mustache-linter-wrapper');
$jarTmpFile = $tmpDir . '/vnu.jar';
$filesystem->dumpFile($wrapper, sprintf('<?php include \'%s\';', $linter));
$filesystem->copy($jarFile, $jarTmpFile, true);

$code = 0;
foreach ($files as $file) {
Expand All @@ -68,7 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'php',
$wrapper,
'--filename=' . $file,
'--validator=' . $jarFile,
'--validator=' . $jarTmpFile,
'--basename=' . $this->moodle->directory,
];
// _JAVA_OPTIONS is something Travis CI started to set in Trusty. This breaks Mustache because
Expand All @@ -82,6 +86,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$filesystem->remove($wrapper);
$filesystem->remove($jarTmpFile);

return $code;
}
Expand All @@ -94,6 +99,11 @@ private function resolveJarFile(): string
// Check if locally installed.
$file = __DIR__ . '/../../vendor/moodlehq/moodle-local_ci/node_modules/vnu-jar/build/dist/vnu.jar';
if (is_file($file)) {
if (\Phar::running() !== '') {
// No need to use realpath() when running from a phar.
return $file;
}

return realpath($file);
}

Expand Down

0 comments on commit 02cfca8

Please sign in to comment.