Skip to content

Commit

Permalink
Fixes #1730. Correctly parse PHPCBF version and improve handling of E…
Browse files Browse the repository at this point in the history
…xecutable
  • Loading branch information
Glavin001 committed Jun 20, 2017
1 parent e1d9da1 commit 3411fad
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 67 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Next
- Fixes [#1730](https://github.com/Glavin001/atom-beautify/issues/1730). Correctly parse PHPCBF version and improve handling of Executable
- ...

# v0.30.1 (2017-06-19)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Some of the supported beautifiers are developed for Node.js and are automaticall
| ocp-indent | :warning: Manual installation | :construction: Not an executable | :page_facing_up: Go to https://www.typerex.org/ocp-indent.html and follow the instructions. |
| Perltidy | :warning: Manual installation | :construction: Not an executable | :page_facing_up: Go to http://perltidy.sourceforge.net/ and follow the instructions. |
| PHP-CS-Fixer | :warning: 2 executables | :warning: Only 1 of 2 executables | :whale: With [Docker](https://www.docker.com/):<br/>1. Install [PHP-CS-Fixer (`php-cs-fixer`)](https://github.com/FriendsOfPHP/PHP-CS-Fixer) with `docker pull unibeautify/php-cs-fixer`<br/><br/>:bookmark_tabs: Manually:<br/>1. Install [PHP (`php`)](http://php.net/) by following http://php.net/manual/en/install.php<br/>2. Install [PHP-CS-Fixer (`php-cs-fixer`)](https://github.com/FriendsOfPHP/PHP-CS-Fixer) by following https://github.com/FriendsOfPHP/PHP-CS-Fixer#installation<br/> |
| PHPCBF | :warning: 1 executable | :white_check_mark: :100:% of executables | :whale: With [Docker](https://www.docker.com/):<br/>1. Install [PHPCBF (`phpcbf`)](https://github.com/squizlabs/PHP_CodeSniffer) with `docker pull unibeautify/phpcbf`<br/><br/>:bookmark_tabs: Manually:<br/>1. Install [PHPCBF (`phpcbf`)](https://github.com/squizlabs/PHP_CodeSniffer) by following https://github.com/squizlabs/PHP_CodeSniffer#installation<br/> |
| PHPCBF | :warning: 2 executables | :warning: Only 1 of 2 executables | :whale: With [Docker](https://www.docker.com/):<br/>1. Install [PHPCBF (`phpcbf`)](https://github.com/squizlabs/PHP_CodeSniffer) with `docker pull unibeautify/phpcbf`<br/><br/>:bookmark_tabs: Manually:<br/>1. Install [PHP (`php`)](http://php.net/) by following http://php.net/manual/en/install.php<br/>2. Install [PHPCBF (`phpcbf`)](https://github.com/squizlabs/PHP_CodeSniffer) by following https://github.com/squizlabs/PHP_CodeSniffer#installation<br/> |
| Pretty Diff | :white_check_mark: | :ok_hand: Not necessary | :smiley: Nothing! |
| Pug Beautify | :white_check_mark: | :ok_hand: Not necessary | :smiley: Nothing! |
| puppet-lint | :warning: Manual installation | :construction: Not an executable | :page_facing_up: Go to http://puppet-lint.com/ and follow the instructions. |
Expand Down
4 changes: 2 additions & 2 deletions src/beautifiers/php-cs-fixer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ module.exports = class PHPCSFixer extends Beautifier
@Promise.all([
@which(options.cs_fixer_path) if options.cs_fixer_path
phpCsFixer.path()
tempFile = @tempFile("temp", text, '.php')
]).then(([customPhpCsFixerPath, phpCsFixerPath]) =>
@tempFile("temp", text, '.php')
]).then(([customPhpCsFixerPath, phpCsFixerPath, tempFile]) =>
# Get first valid, absolute path
finalPhpCsFixerPath = if customPhpCsFixerPath and path.isAbsolute(customPhpCsFixerPath) then \
customPhpCsFixerPath else phpCsFixerPath
Expand Down
122 changes: 58 additions & 64 deletions src/beautifiers/phpcbf.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,28 @@ module.exports = class PHPCBF extends Beautifier
name: "PHPCBF"
link: "http://php.net/manual/en/install.php"
executables: [
{
name: "PHP"
cmd: "php"
homepage: "http://php.net/"
installation: "http://php.net/manual/en/install.php"
version: {
parse: (text) -> text.match(/PHP (.*) \(cli\)/)[1]
}
}
{
name: "PHPCBF"
cmd: "phpcbf"
homepage: "https://github.com/squizlabs/PHP_CodeSniffer"
installation: "https://github.com/squizlabs/PHP_CodeSniffer#installation"
version: {
args: ['--version']
parse: (text) -> text.match(/version (\d+\.\d+\.\d+)/)[1]
}
docker: {
image: "unibeautify/phpcbf"
}
}
]
isPreInstalled: false

options: {
PHP:
Expand All @@ -38,69 +46,55 @@ module.exports = class PHPCBF extends Beautifier

options.standard = standardFile if standardFile

isWin = @isWindows
if isWin
# Find phpcbf.phar script
@Promise.all([
@which(options.phpcbf_path) if options.phpcbf_path
@which('phpcbf')
]).then((paths) =>
@debug('phpcbf paths', paths)
_ = require 'lodash'
path = require 'path'
# Get first valid, absolute path
phpcbfPath = _.find(paths, (p) -> p and path.isAbsolute(p) )
@verbose('phpcbfPath', phpcbfPath)
@debug('phpcbfPath', phpcbfPath, paths)
# Check if phpcbf path was found
if phpcbfPath?
# Found phpcbf path
php = @exe('php')
phpcbf = @exe('phpcbf')

# Check if phpcbf is an executable
isExec = path.extname(phpcbfPath) isnt ''
exec = if isExec then phpcbfPath else "php"
if options.phpcbf_path
deprecationMessage = "The \"PHP - PHPCBF Path (phpcbf_path)\" configuration option has been deprecated. Please switch to using the option named \"Executables - PHPCBF - Path\" in Atom-Beautify package settings now."
@deprecate(deprecationMessage)

@run(exec, [
phpcbfPath unless isExec
"--no-patch" unless options.phpcbf_version is 3
"--standard=#{options.standard}" if options.standard
tempFile = @tempFile("temp", text, ".php")
], {
ignoreReturnCode: true
help: {
link: "http://php.net/manual/en/install.php"
}
onStdin: (stdin) ->
stdin.end()
})
.then(=>
@readFile(tempFile)
)
else
@verbose('phpcbf not found!')
# Could not find phpcbf path
@Promise.reject(@commandNotFoundError(
'phpcbf'
{
link: "https://github.com/squizlabs/PHP_CodeSniffer"
program: "phpcbf.phar"
pathOption: "PHPCBF Path"
})
# Find phpcbf.phar script
@Promise.all([
@which(options.phpcbf_path) if options.phpcbf_path
phpcbf.path()
@tempFile("temp", text, ".php")
]).then(([customPhpcbfPath, phpcbfPath, tempFile]) =>
# Get first valid, absolute path
finalPhpcbfPath = if customPhpcbfPath and path.isAbsolute(customPhpcbfPath) then \
customPhpcbfPath else phpcbfPath
@verbose('finalPhpcbfPath', finalPhpcbfPath, phpcbfPath, customPhpcbfPath)

isVersion3 = ((phpcbf.isInstalled and phpcbf.isVersion('3.x')) or \
(options.phpcbf_version and phpcbf.versionSatisfies("#{options.phpcbf_version}.0.0", '3.x')))

isPhpScript = (finalPhpcbfPath.indexOf(".phar") isnt -1) or (finalPhpcbfPath.indexOf(".php") isnt -1)
@verbose('isPhpScript', isPhpScript)

if isPhpScript
php.run([
phpcbfPath,
"--no-patch" unless isVersion3
"--standard=#{options.standard}" if options.standard
tempFile
], {
ignoreReturnCode: true
onStdin: (stdin) ->
stdin.end()
})
.then(=>
@readFile(tempFile)
)
else
phpcbf.run([
"--no-patch" unless isVersion3
"--standard=#{options.standard}" if options.standard
tempFile = @tempFile("temp", text, ".php")
], {
ignoreReturnCode: true
onStdin: (stdin) ->
stdin.end()
})
.then(=>
@readFile(tempFile)
)
)
else
@run("phpcbf", [
"--no-patch" unless options.phpcbf_version is 3
"--standard=#{options.standard}" if options.standard
tempFile = @tempFile("temp", text, ".php")
], {
ignoreReturnCode: true
help: {
link: "https://github.com/squizlabs/PHP_CodeSniffer"
}
onStdin: (stdin) ->
stdin.end()
})
.then(=>
@readFile(tempFile)
)

0 comments on commit 3411fad

Please sign in to comment.