From 8b5363f7bf596a8af03fdee5fdb141b8e22389b7 Mon Sep 17 00:00:00 2001 From: Glavin Wiechert Date: Mon, 19 Jun 2017 02:36:42 -0300 Subject: [PATCH] Fixes #1725. Improve PHP-CS-Fixer support with handling script path Detect if the executable path is either .phar (PHP) or not and run the executable PHP-CS-Fixer differently considering. --- .../simple-jsbeautifyrc/php/expected/test.php | 14 +++-- src/beautifiers/executable.coffee | 24 ++++--- src/beautifiers/php-cs-fixer.coffee | 63 +++++++++---------- 3 files changed, 53 insertions(+), 48 deletions(-) diff --git a/examples/simple-jsbeautifyrc/php/expected/test.php b/examples/simple-jsbeautifyrc/php/expected/test.php index 095f37d..2055b5f 100644 --- a/examples/simple-jsbeautifyrc/php/expected/test.php +++ b/examples/simple-jsbeautifyrc/php/expected/test.php @@ -1,28 +1,30 @@ query(''); +$q = $mysqli->query(""); $num = $q->num_rows; echo ''; -if ($num > 0) { +if ($num>0) { echo ''; } else { echo ''; } echo ''; + // new messages -$q = $mysqli->query(''); +$q = $mysqli->query(""); $num = $q->num_rows; echo ''; -if ($num > 0) { +if ($num>0) { echo ''; } else { echo ''; diff --git a/src/beautifiers/executable.coffee b/src/beautifiers/executable.coffee index b542601..39e0a9e 100644 --- a/src/beautifiers/executable.coffee +++ b/src/beautifiers/executable.coffee @@ -119,7 +119,10 @@ class Executable @isVersion(@versionsSupported) isVersion: (range) -> - semver.satisfies(@version, range) + @versionSatisfies(@version, range) + + versionSatisfies: (version, range) -> + semver.satisfies(version, range) getConfig: () -> atom?.config.get("#{parentConfigKey}.#{@key}") or {} @@ -129,21 +132,16 @@ class Executable ### run: (args, options = {}) -> @debug("Run: ", @cmd, args, options) - { cwd, ignoreReturnCode, help, onStdin, returnStderr, returnStdoutOrStderr } = options - exeName = @cmd - config = @getConfig() + { cmd, cwd, ignoreReturnCode, help, onStdin, returnStderr, returnStdoutOrStderr } = options + exeName = cmd or @cmd cwd ?= os.tmpDir() # Resolve executable and all args Promise.all([@shellEnv(), this.resolveArgs(args)]) .then(([env, args]) => @debug('exeName, args:', exeName, args) - # Get PATH and other environment variables - if config and config.path - exePath = config.path - else - exePath = @which(exeName) + exePath = @path(exeName) Promise.all([exeName, args, env, exePath]) ) .then(([exeName, args, env, exePath]) => @@ -198,6 +196,14 @@ class Executable ) ) + path: (cmd = @cmd) -> + config = @getConfig() + if config and config.path + Promise.resolve(config.path) + else + exeName = cmd + @which(exeName) + resolveArgs: (args) -> args = _.flatten(args) Promise.all(args) diff --git a/src/beautifiers/php-cs-fixer.coffee b/src/beautifiers/php-cs-fixer.coffee index e72dd6f..ffe9659 100644 --- a/src/beautifiers/php-cs-fixer.coffee +++ b/src/beautifiers/php-cs-fixer.coffee @@ -25,8 +25,13 @@ module.exports = class PHPCSFixer extends Beautifier cmd: "php-cs-fixer" homepage: "https://github.com/FriendsOfPHP/PHP-CS-Fixer" installation: "https://github.com/FriendsOfPHP/PHP-CS-Fixer#installation" + optional: true version: { - parse: (text) -> text.match(/version (.*) by/)[1] + ".0" + parse: (text) -> + try + text.match(/version (.*) by/)[1] + ".0" + catch + text.match(/PHP CS Fixer (\d+\.\d+\.\d+)/)[1] } docker: { image: "unibeautify/php-cs-fixer" @@ -66,7 +71,10 @@ module.exports = class PHPCSFixer extends Beautifier "--allow-risky=#{options.allow_risky}" if options.allow_risky "--using-cache=no" ] - if phpCsFixer.isVersion('1.x') + + isVersion1 = ((phpCsFixer.isInstalled and phpCsFixer.isVersion('1.x')) or \ + (options.cs_fixer_version and phpCsFixer.versionSatisfies("#{options.cs_fixer_version}.0.0", '1.x'))) + if isVersion1 phpCsFixerOptions = [ "fix" "--level=#{options.level}" if options.level @@ -82,43 +90,32 @@ module.exports = class PHPCSFixer extends Beautifier # Find php-cs-fixer.phar script if options.cs_fixer_path - @deprecate("The \"cs_fixer_path\" has been deprecated. Please switch to using the config with path \"Executables - PHP-CS-Fixer - Path\" in Atom-Beautify package settings now.") + deprecationMessage = "The \"PHP - PHP-CS-Fixer Path (cs_fixer_path)\" configuration option has been deprecated. Please switch to using the option named \"Executables - PHP-CS-Fixer - Path\" in Atom-Beautify package settings now." + @deprecate(deprecationMessage) @Promise.all([ @which(options.cs_fixer_path) if options.cs_fixer_path - @which('php-cs-fixer') + phpCsFixer.path() tempFile = @tempFile("temp", text, '.php') - ]).then(([customPath, phpCsFixerPath]) => - paths = [customPath, phpCsFixerPath] - @debug('php-cs-fixer paths', paths) - _ = require 'lodash' + ]).then(([customPhpCsFixerPath, phpCsFixerPath]) => # Get first valid, absolute path - phpCSFixerPath = _.find(paths, (p) -> p and path.isAbsolute(p) ) - @verbose('phpCSFixerPath', phpCSFixerPath) - @debug('phpCSFixerPath', phpCSFixerPath, paths) + finalPhpCsFixerPath = if customPhpCsFixerPath and path.isAbsolute(customPhpCsFixerPath) then \ + customPhpCsFixerPath else phpCsFixerPath + @verbose('finalPhpCsFixerPath', finalPhpCsFixerPath, phpCsFixerPath, customPhpCsFixerPath) - # Check if PHP-CS-Fixer path was found - if phpCSFixerPath? - # Found PHP-CS-Fixer path - if @isWindows - php.run([phpCSFixerPath, phpCsFixerOptions, tempFile], runOptions) - .then(=> - @readFile(tempFile) - ) - else - @run(phpCSFixerPath, [phpCsFixerOptions, tempFile], runOptions) - .then(=> - @readFile(tempFile) - ) + isPhpScript = (finalPhpCsFixerPath.indexOf(".phar") isnt -1) or (finalPhpCsFixerPath.indexOf(".php") isnt -1) + @verbose('isPhpScript', isPhpScript) + + if finalPhpCsFixerPath and isPhpScript + php.run([finalPhpCsFixerPath, phpCsFixerOptions, tempFile], runOptions) + .then(=> + @readFile(tempFile) + ) else - @verbose('php-cs-fixer not found!') - # Could not find PHP-CS-Fixer path - @Promise.reject(@commandNotFoundError( - 'php-cs-fixer' - { - link: "https://github.com/FriendsOfPHP/PHP-CS-Fixer" - program: "php-cs-fixer.phar" - pathOption: "PHP - CS Fixer Path" - }) + phpCsFixer.run([phpCsFixerOptions, tempFile], + Object.assign({}, runOptions, { cmd: finalPhpCsFixerPath }) ) + .then(=> + @readFile(tempFile) + ) )