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)
return p
failWhichProgram('php')
# failWhichProgram('php')
failWhichProgram('php-cs-fixer')
unless isWindows

View File

@ -149,9 +149,13 @@ module.exports = class Beautifier
which: (exe, options = {}) ->
# Get PATH and other environment variables
@getShellEnvironment()
.then((env) ->
new Promise((resolve, reject) ->
.then((env) =>
new Promise((resolve, reject) =>
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) ->
resolve(exe) if err
resolve(path)

View File

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