See #390. Use global php-cs-fixer on Windows without running within php

By using [node-which](https://github.com/isaacs/node-which)
I believe I am able to get rid of the `php` command running
`php-cs-fixer` as an argument.
`which` will find the `php-cs-fixer` program by looking through
the `PATH` environment variable listed directories.
This will resolve `php-cs-fixer` to an absolute path to the executable
and that will be used to run the command.
This dramatically cleans up the `PHP-CS-Fixer beautifier` code and specs, too!

Doing some Windows tests now.
This commit is contained in:
Glavin Wiechert 2015-06-12 01:22:14 -03:00
parent 5094d76930
commit 590dbc805e
3 changed files with 22 additions and 60 deletions

View File

@ -123,7 +123,7 @@ describe "PHP-CS-Fixer Beautifier", ->
p.then(cb, cb) p.then(cb, cb)
return p return p
failWhichProgram('php') # failWhichProgram('php')
failWhichProgram('php-cs-fixer') failWhichProgram('php-cs-fixer')
unless isWindows unless isWindows

View File

@ -149,9 +149,13 @@ module.exports = class Beautifier
which: (exe, options = {}) -> which: (exe, options = {}) ->
# Get PATH and other environment variables # Get PATH and other environment variables
@getShellEnvironment() @getShellEnvironment()
.then((env) -> .then((env) =>
new Promise((resolve, reject) -> new Promise((resolve, reject) =>
options.path ?= env.PATH options.path ?= env.PATH
if @isWindows
# Trick node-which into including files
# with no extension as executables
options.pathExt ?= ";#{process.env.PATHEXT ? '.EXE'}"
which(exe, options, (err, path) -> which(exe, options, (err, path) ->
resolve(exe) if err resolve(exe) if err
resolve(path) resolve(path)

View File

@ -15,61 +15,19 @@ module.exports = class PHPCSFixer extends Beautifier
beautify: (text, language, options) -> beautify: (text, language, options) ->
@debug('php-cs-fixer', options) @debug('php-cs-fixer', options)
isWin = @isWindows @run(options.cs_fixer_path or "php-cs-fixer", [
if isWin "fix"
# Find php-cs-fixer.phar script "--level=#{options.level}" if options.level
@Promise.all([ "--fixers=#{options.fixers}" if options.fixers
@which(options.cs_fixer_path) if options.cs_fixer_path tempFile = @tempFile("temp", text)
@which('php-cs-fixer') ], {
]).then((paths) => ignoreReturnCode: true
@debug('php-cs-fixer paths', paths) help: {
_ = require('lodash') link: "https://github.com/FriendsOfPHP/PHP-CS-Fixer"
# Get first valid, absolute path program: "php-cs-fixer"
phpCSFixerPath = _.find(paths, (p) -> p and p.charAt(0) is '/' ) pathOption: "PHP - CS Fixer Path"
@verbose('phpCSFixerPath', phpCSFixerPath) }
@debug('phpCSFixerPath', phpCSFixerPath, paths) })
# Check if PHP-CS-Fixer path was found .then(=>
if phpCSFixerPath? @readFile(tempFile)
# Found PHP-CS-Fixer path
@run("php", [
phpCSFixerPath
"fix"
"--level=#{options.level}" if options.level
"--fixers=#{options.fixers}" if options.fixers
tempFile = @tempFile("temp", text)
], {
ignoreReturnCode: true
help: {
link: "http://php.net/manual/en/install.php"
}
})
.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"
})
)
) )
else
@run("php-cs-fixer", [
"fix"
"--level=#{options.level}" if options.level
"--fixers=#{options.fixers}" if options.fixers
tempFile = @tempFile("temp", text)
], {
ignoreReturnCode: true
help: {
link: "https://github.com/FriendsOfPHP/PHP-CS-Fixer"
}
})
.then(=>
@readFile(tempFile)
)