diff options
| author | William Boman <william@redwill.se> | 2022-09-12 16:10:09 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-12 16:10:09 +0200 |
| commit | b56ea0bd8b6b1c265752226666f1069d0a479438 (patch) | |
| tree | 5a90f8d008438962215d18c8ad3852f1bb339875 | |
| parent | feat: add phpcs, phpcbf, phpmd, phpstan and twigcs (#399) (diff) | |
| download | mason-b56ea0bd8b6b1c265752226666f1069d0a479438.tar mason-b56ea0bd8b6b1c265752226666f1069d0a479438.tar.gz mason-b56ea0bd8b6b1c265752226666f1069d0a479438.tar.bz2 mason-b56ea0bd8b6b1c265752226666f1069d0a479438.tar.lz mason-b56ea0bd8b6b1c265752226666f1069d0a479438.tar.xz mason-b56ea0bd8b6b1c265752226666f1069d0a479438.tar.zst mason-b56ea0bd8b6b1c265752226666f1069d0a479438.zip | |
refactor: add ctx:write_php_exec_wrapper utility (#409)
| -rw-r--r-- | lua/mason-core/installer/context.lua | 15 | ||||
| -rw-r--r-- | lua/mason-registry/php-cs-fixer/init.lua | 12 | ||||
| -rw-r--r-- | lua/mason-registry/phpcbf/init.lua | 16 | ||||
| -rw-r--r-- | lua/mason-registry/phpcs/init.lua | 16 | ||||
| -rw-r--r-- | lua/mason-registry/phpmd/init.lua | 12 | ||||
| -rw-r--r-- | lua/mason-registry/phpstan/init.lua | 11 | ||||
| -rw-r--r-- | lua/mason-registry/twigcs/init.lua | 17 | ||||
| -rw-r--r-- | tests/mason-core/installer/context_spec.lua | 35 |
8 files changed, 60 insertions, 74 deletions
diff --git a/lua/mason-core/installer/context.lua b/lua/mason-core/installer/context.lua index 9e1df72f..7d0a505b 100644 --- a/lua/mason-core/installer/context.lua +++ b/lua/mason-core/installer/context.lua @@ -216,6 +216,21 @@ function InstallContext:write_node_exec_wrapper(new_executable_rel_path, script_ end ---@param new_executable_rel_path string Relative path to the executable file to create. +---@param script_rel_path string Relative path to the PHP script. +function InstallContext:write_php_exec_wrapper(new_executable_rel_path, script_rel_path) + if not self.fs:file_exists(script_rel_path) then + error(("Cannot write PHP exec wrapper for path %q as it doesn't exist."):format(script_rel_path), 0) + end + return self:write_shell_exec_wrapper( + new_executable_rel_path, + ("php %q"):format(path.concat { + self.package:get_install_path(), + script_rel_path, + }) + ) +end + +---@param new_executable_rel_path string Relative path to the executable file to create. ---@param module string The python module to call. function InstallContext:write_pyvenv_exec_wrapper(new_executable_rel_path, module) local pip3 = require "mason-core.managers.pip3" diff --git a/lua/mason-registry/php-cs-fixer/init.lua b/lua/mason-registry/php-cs-fixer/init.lua index 0929a2b3..84dd0078 100644 --- a/lua/mason-registry/php-cs-fixer/init.lua +++ b/lua/mason-registry/php-cs-fixer/init.lua @@ -3,7 +3,6 @@ local _ = require "mason-core.functional" local github = require "mason-core.managers.github" local std = require "mason-core.managers.std" local platform = require "mason-core.platform" -local path = require "mason-core.path" return Pkg.new { name = "php-cs-fixer", @@ -31,16 +30,7 @@ return Pkg.new { ctx:link_bin("php-cs-fixer", "php-cs-fixer") end, win = function() - ctx:link_bin( - "php-cs-fixer", - ctx:write_shell_exec_wrapper( - "php-cs-fixer", - ("php %q"):format(path.concat { - ctx.package:get_install_path(), - "php-cs-fixer.phar", - }) - ) - ) + ctx:link_bin("php-cs-fixer", ctx:write_php_exec_wrapper("php-cs-fixer", "php-cs-fixer.phar")) end, } end, diff --git a/lua/mason-registry/phpcbf/init.lua b/lua/mason-registry/phpcbf/init.lua index 8cae909a..6dd062f5 100644 --- a/lua/mason-registry/phpcbf/init.lua +++ b/lua/mason-registry/phpcbf/init.lua @@ -3,13 +3,10 @@ local _ = require "mason-core.functional" local github = require "mason-core.managers.github" local std = require "mason-core.managers.std" local platform = require "mason-core.platform" -local path = require "mason-core.path" return Pkg.new { name = "phpcbf", - desc = _.dedent [[ - PHP_CodeSniffer(phpcbf) automatically corrects coding standard violations that would be detected by PHP_CodeSniffer(phpcs). - ]], + desc = [[phpcbf automatically corrects coding standard violations that would be detected by phpcs.]], homepage = "https://github.com/squizlabs/PHP_CodeSniffer", languages = { Pkg.Lang.PHP }, categories = { Pkg.Cat.Formatter }, @@ -29,16 +26,7 @@ return Pkg.new { ctx:link_bin("phpcbf", "phpcbf") end, win = function() - ctx:link_bin( - "phpcbf", - ctx:write_shell_exec_wrapper( - "phpcbf", - ("php %q"):format(path.concat { - ctx.package:get_install_path(), - "phpcbf.phar", - }) - ) - ) + ctx:link_bin("phpcbf", ctx:write_php_exec_wrapper("phpcbf", "phpcbf.phar")) end, } end, diff --git a/lua/mason-registry/phpcs/init.lua b/lua/mason-registry/phpcs/init.lua index 17515ba2..41f82f7d 100644 --- a/lua/mason-registry/phpcs/init.lua +++ b/lua/mason-registry/phpcs/init.lua @@ -3,13 +3,10 @@ local _ = require "mason-core.functional" local github = require "mason-core.managers.github" local std = require "mason-core.managers.std" local platform = require "mason-core.platform" -local path = require "mason-core.path" return Pkg.new { name = "phpcs", - desc = _.dedent [[ - PHP_CodeSniffer(phpcs) tokenizes PHP, JavaScript and CSS files to detect violations of a defined standard. - ]], + desc = [[phpcs tokenizes PHP, JavaScript and CSS files to detect violations of a defined standard.]], homepage = "https://github.com/squizlabs/PHP_CodeSniffer", languages = { Pkg.Lang.PHP }, categories = { Pkg.Cat.Linter }, @@ -29,16 +26,7 @@ return Pkg.new { ctx:link_bin("phpcs", "phpcs") end, win = function() - ctx:link_bin( - "phpcs", - ctx:write_shell_exec_wrapper( - "phpcs", - ("php %q"):format(path.concat { - ctx.package:get_install_path(), - "phpcs.phar", - }) - ) - ) + ctx:link_bin("phpcs", ctx:write_php_exec_wrapper("phpcs", "phpcs.phar")) end, } end, diff --git a/lua/mason-registry/phpmd/init.lua b/lua/mason-registry/phpmd/init.lua index a7810c54..4543711f 100644 --- a/lua/mason-registry/phpmd/init.lua +++ b/lua/mason-registry/phpmd/init.lua @@ -3,7 +3,6 @@ local _ = require "mason-core.functional" local github = require "mason-core.managers.github" local std = require "mason-core.managers.std" local platform = require "mason-core.platform" -local path = require "mason-core.path" return Pkg.new { name = "phpmd", @@ -30,16 +29,7 @@ return Pkg.new { ctx:link_bin("phpmd", "phpmd") end, win = function() - ctx:link_bin( - "phpmd", - ctx:write_shell_exec_wrapper( - "phpmd", - ("php %q"):format(path.concat { - ctx.package:get_install_path(), - "phpmd.phar", - }) - ) - ) + ctx:link_bin("phpmd", ctx:write_php_exec_wrapper("phpmd", "phpmd.phar")) end, } end, diff --git a/lua/mason-registry/phpstan/init.lua b/lua/mason-registry/phpstan/init.lua index bc0bf3ff..688bd75a 100644 --- a/lua/mason-registry/phpstan/init.lua +++ b/lua/mason-registry/phpstan/init.lua @@ -29,16 +29,7 @@ return Pkg.new { ctx:link_bin("phpstan", "phpstan") end, win = function() - ctx:link_bin( - "phpstan", - ctx:write_shell_exec_wrapper( - "phpstan", - ("php %q"):format(path.concat { - ctx.package:get_install_path(), - "phpstan.phar", - }) - ) - ) + ctx:link_bin("phpstan", ctx:write_php_exec_wrapper("phpstan", "phpstan.phar")) end, } end, diff --git a/lua/mason-registry/twigcs/init.lua b/lua/mason-registry/twigcs/init.lua index ea947c15..4a0a638a 100644 --- a/lua/mason-registry/twigcs/init.lua +++ b/lua/mason-registry/twigcs/init.lua @@ -3,14 +3,12 @@ local _ = require "mason-core.functional" local github = require "mason-core.managers.github" local std = require "mason-core.managers.std" local platform = require "mason-core.platform" -local path = require "mason-core.path" return Pkg.new { name = "twigcs", desc = _.dedent [[ - The missing checkstyle for twig! - Twigcs aims to be what phpcs is to php. - It checks your codebase for violations on coding standards. + The missing checkstyle for twig! Twigcs aims to be what phpcs is to php. It checks your codebase for violations + on coding standards. ]], homepage = "https://github.com/friendsoftwig/twigcs", languages = { Pkg.Lang.Twig }, @@ -31,16 +29,7 @@ return Pkg.new { ctx:link_bin("twigcs", "twigcs") end, win = function() - ctx:link_bin( - "twigcs", - ctx:write_shell_exec_wrapper( - "twigcs", - ("php %q"):format(path.concat { - ctx.package:get_install_path(), - "twigcs.phar", - }) - ) - ) + ctx:link_bin("twigcs", ctx:write_php_exec_wrapper("twigcs", "twigcs.phar")) end, } end, diff --git a/tests/mason-core/installer/context_spec.lua b/tests/mason-core/installer/context_spec.lua index 3f8b23c9..2523d33e 100644 --- a/tests/mason-core/installer/context_spec.lua +++ b/tests/mason-core/installer/context_spec.lua @@ -172,4 +172,39 @@ cmd.exe /C echo %GREETING% %*]] assert.equals([[Cannot write exec wrapper for path "obscure/path/to/server" as it doesn't exist.]], err) assert.spy(ctx.write_shell_exec_wrapper).was_called(0) end) + + it("should write PHP exec wrapper", function() + local php_rel_path = path.concat { "some", "obscure", "path", "cli.php" } + local dummy = registry.get_package "dummy" + local handle = InstallHandleGenerator "dummy" + local ctx = InstallContextGenerator(handle) + stub(ctx, "write_shell_exec_wrapper") + stub(ctx.fs, "file_exists") + ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), php_rel_path).returns(true) + + ctx:write_php_exec_wrapper("my-wrapper-script", php_rel_path) + + assert.spy(ctx.write_shell_exec_wrapper).was_called(1) + assert.spy(ctx.write_shell_exec_wrapper).was_called_with( + match.is_ref(ctx), + "my-wrapper-script", + ("php %q"):format(path.concat { dummy:get_install_path(), php_rel_path }) + ) + end) + + it("should not write PHP exec wrapper if the target script doesn't exist", function() + local php_rel_path = path.concat { "some", "obscure", "path", "cli.php" } + local handle = InstallHandleGenerator "dummy" + local ctx = InstallContextGenerator(handle) + stub(ctx, "write_shell_exec_wrapper") + stub(ctx.fs, "file_exists") + ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), php_rel_path).returns(false) + + local err = assert.has_error(function() + ctx:write_php_exec_wrapper("my-wrapper-script", php_rel_path) + end) + + assert.equals([[Cannot write PHP exec wrapper for path "some/obscure/path/cli.php" as it doesn't exist.]], err) + assert.spy(ctx.write_shell_exec_wrapper).was_called(0) + end) end) |
