From 4f8f4d68f8ca5941cc4b9015f1df067c93eb1399 Mon Sep 17 00:00:00 2001 From: Brian Bugh Date: Sun, 23 Jul 2017 13:49:07 -0500 Subject: [PATCH 01/13] Improved Rubocop configuration and temp files --- src/beautifiers/executable.coffee | 5 +- src/beautifiers/rubocop.coffee | 98 +++++++++++++++++++------------ 2 files changed, 61 insertions(+), 42 deletions(-) diff --git a/src/beautifiers/executable.coffee b/src/beautifiers/executable.coffee index 61f42b3..6a9b315 100644 --- a/src/beautifiers/executable.coffee +++ b/src/beautifiers/executable.coffee @@ -153,7 +153,7 @@ class Executable @debug('env:', env) @debug('PATH:', env.PATH) @debug('args', args) - args = this.relativizePaths(args) + args = this.relativizePaths(args, cwd) @debug('relativized args', args) exe = exePath ? exeName @@ -212,8 +212,7 @@ class Executable args = _.flatten(args) Promise.all(args) - relativizePaths: (args) -> - tmpDir = os.tmpDir() + relativizePaths: (args, tmpDir) -> newArgs = args.map((arg) -> isTmpFile = (typeof arg is 'string' and not arg.includes(':') and \ path.isAbsolute(arg) and path.dirname(arg).startsWith(tmpDir)) diff --git a/src/beautifiers/rubocop.coffee b/src/beautifiers/rubocop.coffee index deafffa..3ff9f2f 100644 --- a/src/beautifiers/rubocop.coffee +++ b/src/beautifiers/rubocop.coffee @@ -4,6 +4,9 @@ Requires https://github.com/bbatsov/rubocop "use strict" Beautifier = require('./beautifier') +path = require('path') +fs = require('fs') +temp = require('temp').track() module.exports = class Rubocop extends Beautifier name: "Rubocop" @@ -16,53 +19,70 @@ module.exports = class Rubocop extends Beautifier rubocop_path: true } + createTempFile: (originalFile) -> + new @Promise((resolve, reject) => + tempOptions = { + prefix: "_beautify", + suffix: path.basename(originalFile), + dir: path.dirname(originalFile)} + temp.open(tempOptions, (err, info) => + return reject(err) if err? + @debug('rubocopTempFile', info.path) + resolve(info.path) + ) + ).disposer((filename) => + if fs.existsSync(filename) + @debug("unlinking rubocop temp file", filename) + fs.unlink(filename) + ) + beautify: (text, language, options) -> + editor = atom?.workspace?.getActiveTextEditor() + if editor? + fullPath = editor.getPath() + projectPath = atom.project.relativizePath(fullPath)[0] + else + throw new Error("No active editor found!") + + # Find the rubocop path + rubocopPath = "rubocop" @Promise.all([ @which(options.rubocop_path) if options.rubocop_path @which('rubocop') - ]).then((paths) => + ]) + .then((paths) => @debug('rubocop paths', paths) - _ = require 'lodash' - path = require 'path' # Get first valid, absolute path - rubocopPath = _.find(paths, (p) -> p and path.isAbsolute(p) ) + rubocopPath = paths.find((p) -> p and path.isAbsolute(p)) @verbose('rubocopPath', rubocopPath) @debug('rubocopPath', rubocopPath, paths) + ) - configFile = path.join(atom.project.getPaths()[0], ".rubocop.yml") + # Find or generate a config file if non exists + configFile = @findFile(path.dirname(fullPath), ".rubocop.yml") + if !configFile? + yaml = require("yaml-front-matter") + config = { + "Style/IndentationWidth": + "Width": options.indent_size + } + tempConfig = @tempFile("rubocop-config", yaml.safeDump(config)) - fs = require 'fs' - - if fs.existsSync(configFile) - @debug("rubocop", config, fs.readFileSync(configFile, 'utf8')) - else - yaml = require("yaml-front-matter") - # Generate config file - config = { - "Style/IndentationWidth": - "Width": options.indent_size - } - - configFile = @tempFile("rubocop-config", yaml.safeDump(config)) - @debug("rubocop", config, configFile) - - # Check if PHP-CS-Fixer path was found - if rubocopPath? - @run(rubocopPath, [ + @Promise.using(@createTempFile(fullPath), (tempFileName) => + new @Promise((resolve, reject) -> + fs.writeFile(tempFileName, text, 'utf8', (err) -> + return reject(err) if err? + resolve tempFileName + ) + ) + .then(=> + rubocopArguments = [ "--auto-correct" - "--config", configFile - tempFile = @tempFile("temp", text, '.rb') - ], {ignoreReturnCode: true}) - .then(=> - @readFile(tempFile) - ) - else - @run("rubocop", [ - "--auto-correct" - "--config", configFile - tempFile = @tempFile("temp", text, '.rb') - ], {ignoreReturnCode: true}) - .then(=> - @readFile(tempFile) - ) -) + "--force-exclusion" + tempFileName + ] + rubocopArguments.push("--config", tempConfig) if tempConfig? + @run(rubocopPath, rubocopArguments, {ignoreReturnCode: true, cwd: projectPath}) + ) + .then(=> @readFile(tempFileName)) + ) From de21bc64129c1d754f86c86545ca19bb0606e33e Mon Sep 17 00:00:00 2001 From: Brian Bugh Date: Sun, 23 Jul 2017 18:23:30 -0500 Subject: [PATCH 02/13] Updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 352c8fc..fa7c34e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Next - Fixes #1638. update type-formatter and Fixes indent size not supported - Add support for rustfmt-nightly +- Fixes #1773 and #793. ruby-beautify with Rubocop now respects `Exclude` settings, and properly loads inherited config files (like `~/.rubocop.yml`) # v0.30.4 (2017-07-14) - Fixes #1732. Improve deprecation message for old options for new Executables From 82706a4dc89dfb7175067315a6983e187d7d707c Mon Sep 17 00:00:00 2001 From: Brian Bugh Date: Tue, 25 Jul 2017 11:52:03 -0500 Subject: [PATCH 03/13] Performance and experience refactor --- src/beautifiers/rubocop.coffee | 54 +++++++++++----------------------- 1 file changed, 17 insertions(+), 37 deletions(-) diff --git a/src/beautifiers/rubocop.coffee b/src/beautifiers/rubocop.coffee index 3ff9f2f..34406a0 100644 --- a/src/beautifiers/rubocop.coffee +++ b/src/beautifiers/rubocop.coffee @@ -5,8 +5,6 @@ Requires https://github.com/bbatsov/rubocop "use strict" Beautifier = require('./beautifier') path = require('path') -fs = require('fs') -temp = require('temp').track() module.exports = class Rubocop extends Beautifier name: "Rubocop" @@ -19,28 +17,11 @@ module.exports = class Rubocop extends Beautifier rubocop_path: true } - createTempFile: (originalFile) -> - new @Promise((resolve, reject) => - tempOptions = { - prefix: "_beautify", - suffix: path.basename(originalFile), - dir: path.dirname(originalFile)} - temp.open(tempOptions, (err, info) => - return reject(err) if err? - @debug('rubocopTempFile', info.path) - resolve(info.path) - ) - ).disposer((filename) => - if fs.existsSync(filename) - @debug("unlinking rubocop temp file", filename) - fs.unlink(filename) - ) - beautify: (text, language, options) -> editor = atom?.workspace?.getActiveTextEditor() if editor? fullPath = editor.getPath() - projectPath = atom.project.relativizePath(fullPath)[0] + [projectPath, relativePath] = atom.project.relativizePath(fullPath) else throw new Error("No active editor found!") @@ -68,21 +49,20 @@ module.exports = class Rubocop extends Beautifier } tempConfig = @tempFile("rubocop-config", yaml.safeDump(config)) - @Promise.using(@createTempFile(fullPath), (tempFileName) => - new @Promise((resolve, reject) -> - fs.writeFile(tempFileName, text, 'utf8', (err) -> - return reject(err) if err? - resolve tempFileName - ) - ) - .then(=> - rubocopArguments = [ - "--auto-correct" - "--force-exclusion" - tempFileName - ] - rubocopArguments.push("--config", tempConfig) if tempConfig? - @run(rubocopPath, rubocopArguments, {ignoreReturnCode: true, cwd: projectPath}) - ) - .then(=> @readFile(tempFileName)) + rubocopArguments = [ + "--auto-correct" + "--force-exclusion" + "--stdin", relativePath + ] + rubocopArguments.push("--config", tempConfig) if tempConfig? + @debug("rubocop arguments", rubocopArguments) + + @run(rubocopPath, rubocopArguments, { + ignoreReturnCode: true, + cwd: projectPath, + onStdin: (stdin) -> stdin.end text + }).then((stdout) => + @debug("rubocop output", stdout) + result = stdout.split("====================\n") + result[result.length - 1] ) From ea0e0677e51f48e9483c54981dfb2c026ad3210b Mon Sep 17 00:00:00 2001 From: Brian Bugh Date: Tue, 25 Jul 2017 11:58:28 -0500 Subject: [PATCH 04/13] Reverted relativizePaths change --- src/beautifiers/executable.coffee | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/beautifiers/executable.coffee b/src/beautifiers/executable.coffee index 6a9b315..61f42b3 100644 --- a/src/beautifiers/executable.coffee +++ b/src/beautifiers/executable.coffee @@ -153,7 +153,7 @@ class Executable @debug('env:', env) @debug('PATH:', env.PATH) @debug('args', args) - args = this.relativizePaths(args, cwd) + args = this.relativizePaths(args) @debug('relativized args', args) exe = exePath ? exeName @@ -212,7 +212,8 @@ class Executable args = _.flatten(args) Promise.all(args) - relativizePaths: (args, tmpDir) -> + relativizePaths: (args) -> + tmpDir = os.tmpDir() newArgs = args.map((arg) -> isTmpFile = (typeof arg is 'string' and not arg.includes(':') and \ path.isAbsolute(arg) and path.dirname(arg).startsWith(tmpDir)) From a719fa224174d6b12d1142a249a883e2db4e7e28 Mon Sep 17 00:00:00 2001 From: Brian Bugh Date: Wed, 2 Aug 2017 14:12:08 -0500 Subject: [PATCH 05/13] Handles rubocop internal error --- src/beautifiers/rubocop.coffee | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/beautifiers/rubocop.coffee b/src/beautifiers/rubocop.coffee index 34406a0..5d63c21 100644 --- a/src/beautifiers/rubocop.coffee +++ b/src/beautifiers/rubocop.coffee @@ -63,6 +63,9 @@ module.exports = class Rubocop extends Beautifier onStdin: (stdin) -> stdin.end text }).then((stdout) => @debug("rubocop output", stdout) + # Rubocop output an error if stdout is empty + return text if stdout.length == 0 + result = stdout.split("====================\n") result[result.length - 1] ) From 851a24f16037d26f4afef480f470403dd0071797 Mon Sep 17 00:00:00 2001 From: Brian Bugh Date: Sat, 7 Oct 2017 15:28:39 -0500 Subject: [PATCH 06/13] Removed getActiveTextEditor() --- src/beautifiers/rubocop.coffee | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/beautifiers/rubocop.coffee b/src/beautifiers/rubocop.coffee index 5d63c21..68a2a04 100644 --- a/src/beautifiers/rubocop.coffee +++ b/src/beautifiers/rubocop.coffee @@ -17,13 +17,9 @@ module.exports = class Rubocop extends Beautifier rubocop_path: true } - beautify: (text, language, options) -> - editor = atom?.workspace?.getActiveTextEditor() - if editor? - fullPath = editor.getPath() - [projectPath, relativePath] = atom.project.relativizePath(fullPath) - else - throw new Error("No active editor found!") + beautify: (text, language, options, context) -> + fullPath = context.filePath + [projectPath, relativePath] = atom.project.relativizePath(fullPath) # Find the rubocop path rubocopPath = "rubocop" From c842af63bc8096201d3e0489c8d652a40f424b59 Mon Sep 17 00:00:00 2001 From: Brian Bugh Date: Sat, 7 Oct 2017 15:51:19 -0500 Subject: [PATCH 07/13] Removed configFile variable definition --- src/beautifiers/rubocop.coffee | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/beautifiers/rubocop.coffee b/src/beautifiers/rubocop.coffee index 68a2a04..d5b74ef 100644 --- a/src/beautifiers/rubocop.coffee +++ b/src/beautifiers/rubocop.coffee @@ -36,8 +36,7 @@ module.exports = class Rubocop extends Beautifier ) # Find or generate a config file if non exists - configFile = @findFile(path.dirname(fullPath), ".rubocop.yml") - if !configFile? + if !@findFile(path.dirname(fullPath), ".rubocop.yml")? yaml = require("yaml-front-matter") config = { "Style/IndentationWidth": From 8188719685ac8cda4c5fd701074144638081cc83 Mon Sep 17 00:00:00 2001 From: Brian Bugh Date: Sat, 7 Oct 2017 16:13:49 -0500 Subject: [PATCH 08/13] Fixed beautify handling when context.filePath is not set --- src/beautifiers/rubocop.coffee | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/beautifiers/rubocop.coffee b/src/beautifiers/rubocop.coffee index d5b74ef..c73d5e1 100644 --- a/src/beautifiers/rubocop.coffee +++ b/src/beautifiers/rubocop.coffee @@ -5,6 +5,7 @@ Requires https://github.com/bbatsov/rubocop "use strict" Beautifier = require('./beautifier') path = require('path') +temp = require('temp').track() module.exports = class Rubocop extends Beautifier name: "Rubocop" @@ -47,7 +48,7 @@ module.exports = class Rubocop extends Beautifier rubocopArguments = [ "--auto-correct" "--force-exclusion" - "--stdin", relativePath + "--stdin", relativePath || temp.path({suffix: '.rb'}) ] rubocopArguments.push("--config", tempConfig) if tempConfig? @debug("rubocop arguments", rubocopArguments) From bd93afa64c5d4a91fdf3767afe058ea2d0af5449 Mon Sep 17 00:00:00 2001 From: Brian Bugh Date: Sat, 7 Oct 2017 20:01:47 -0500 Subject: [PATCH 09/13] Restored !configFile? --- src/beautifiers/rubocop.coffee | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/beautifiers/rubocop.coffee b/src/beautifiers/rubocop.coffee index c73d5e1..7b7f298 100644 --- a/src/beautifiers/rubocop.coffee +++ b/src/beautifiers/rubocop.coffee @@ -37,7 +37,8 @@ module.exports = class Rubocop extends Beautifier ) # Find or generate a config file if non exists - if !@findFile(path.dirname(fullPath), ".rubocop.yml")? + configFile = @findFile(path.dirname(fullPath), ".rubocop.yml") + if !configFile? yaml = require("yaml-front-matter") config = { "Style/IndentationWidth": From 2d595f2a20e917b68490a5d98866f8f2e7f61c9f Mon Sep 17 00:00:00 2001 From: Brian Bugh Date: Sat, 7 Oct 2017 21:08:35 -0500 Subject: [PATCH 10/13] Fixed fallback for rubocop executable --- src/beautifiers/rubocop.coffee | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/beautifiers/rubocop.coffee b/src/beautifiers/rubocop.coffee index 7b7f298..72b4522 100644 --- a/src/beautifiers/rubocop.coffee +++ b/src/beautifiers/rubocop.coffee @@ -23,7 +23,6 @@ module.exports = class Rubocop extends Beautifier [projectPath, relativePath] = atom.project.relativizePath(fullPath) # Find the rubocop path - rubocopPath = "rubocop" @Promise.all([ @which(options.rubocop_path) if options.rubocop_path @which('rubocop') @@ -31,7 +30,7 @@ module.exports = class Rubocop extends Beautifier .then((paths) => @debug('rubocop paths', paths) # Get first valid, absolute path - rubocopPath = paths.find((p) -> p and path.isAbsolute(p)) + rubocopPath = paths.find((p) -> p and path.isAbsolute(p)) or "rubocop" @verbose('rubocopPath', rubocopPath) @debug('rubocopPath', rubocopPath, paths) ) From 5f36431b1d70310a7e70ee6f1580084c875f6d8b Mon Sep 17 00:00:00 2001 From: Brian Bugh Date: Sat, 7 Oct 2017 21:26:34 -0500 Subject: [PATCH 11/13] Moved main code into then() for actual promise resolution --- src/beautifiers/rubocop.coffee | 62 +++++++++++++++++----------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/src/beautifiers/rubocop.coffee b/src/beautifiers/rubocop.coffee index 72b4522..b0dc0fb 100644 --- a/src/beautifiers/rubocop.coffee +++ b/src/beautifiers/rubocop.coffee @@ -33,35 +33,35 @@ module.exports = class Rubocop extends Beautifier rubocopPath = paths.find((p) -> p and path.isAbsolute(p)) or "rubocop" @verbose('rubocopPath', rubocopPath) @debug('rubocopPath', rubocopPath, paths) - ) - - # Find or generate a config file if non exists - configFile = @findFile(path.dirname(fullPath), ".rubocop.yml") - if !configFile? - yaml = require("yaml-front-matter") - config = { - "Style/IndentationWidth": - "Width": options.indent_size - } - tempConfig = @tempFile("rubocop-config", yaml.safeDump(config)) - - rubocopArguments = [ - "--auto-correct" - "--force-exclusion" - "--stdin", relativePath || temp.path({suffix: '.rb'}) - ] - rubocopArguments.push("--config", tempConfig) if tempConfig? - @debug("rubocop arguments", rubocopArguments) - - @run(rubocopPath, rubocopArguments, { - ignoreReturnCode: true, - cwd: projectPath, - onStdin: (stdin) -> stdin.end text - }).then((stdout) => - @debug("rubocop output", stdout) - # Rubocop output an error if stdout is empty - return text if stdout.length == 0 - - result = stdout.split("====================\n") - result[result.length - 1] + + # Find or generate a config file if non exists + configFile = @findFile(path.dirname(fullPath), ".rubocop.yml") + if !configFile? + yaml = require("yaml-front-matter") + config = { + "Style/IndentationWidth": + "Width": options.indent_size + } + tempConfig = @tempFile("rubocop-config", yaml.safeDump(config)) + + rubocopArguments = [ + "--auto-correct" + "--force-exclusion" + "--stdin", relativePath || temp.path({suffix: '.rb'}) + ] + rubocopArguments.push("--config", tempConfig) if tempConfig? + @debug("rubocop arguments", rubocopArguments) + + @run(rubocopPath, rubocopArguments, { + ignoreReturnCode: true, + cwd: projectPath, + onStdin: (stdin) -> stdin.end text + }).then((stdout) => + @debug("rubocop output", stdout) + # Rubocop output an error if stdout is empty + return text if stdout.length == 0 + + result = stdout.split("====================\n") + result[result.length - 1] + ) ) From 6f8036ba7adb058d9ce718858236f2b329e9d298 Mon Sep 17 00:00:00 2001 From: Brian Bugh Date: Sat, 7 Oct 2017 21:33:41 -0500 Subject: [PATCH 12/13] Replaced unnecessary filename/temp file with hard coded value --- src/beautifiers/rubocop.coffee | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/beautifiers/rubocop.coffee b/src/beautifiers/rubocop.coffee index b0dc0fb..86d3fb4 100644 --- a/src/beautifiers/rubocop.coffee +++ b/src/beautifiers/rubocop.coffee @@ -5,7 +5,6 @@ Requires https://github.com/bbatsov/rubocop "use strict" Beautifier = require('./beautifier') path = require('path') -temp = require('temp').track() module.exports = class Rubocop extends Beautifier name: "Rubocop" @@ -20,7 +19,7 @@ module.exports = class Rubocop extends Beautifier beautify: (text, language, options, context) -> fullPath = context.filePath - [projectPath, relativePath] = atom.project.relativizePath(fullPath) + [projectPath, _relativePath] = atom.project.relativizePath(fullPath) # Find the rubocop path @Promise.all([ @@ -47,7 +46,7 @@ module.exports = class Rubocop extends Beautifier rubocopArguments = [ "--auto-correct" "--force-exclusion" - "--stdin", relativePath || temp.path({suffix: '.rb'}) + "--stdin", "atom-beautify.rb" # filename is required but not used ] rubocopArguments.push("--config", tempConfig) if tempConfig? @debug("rubocop arguments", rubocopArguments) From d4db0089237cdd3d4bc6658c79a12127a7ae61bc Mon Sep 17 00:00:00 2001 From: Brian Bugh Date: Sun, 8 Oct 2017 07:29:26 -0500 Subject: [PATCH 13/13] Fixed deprecation warning: path.dirname requires string --- src/beautifiers/rubocop.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/beautifiers/rubocop.coffee b/src/beautifiers/rubocop.coffee index 86d3fb4..138dfb5 100644 --- a/src/beautifiers/rubocop.coffee +++ b/src/beautifiers/rubocop.coffee @@ -18,7 +18,7 @@ module.exports = class Rubocop extends Beautifier } beautify: (text, language, options, context) -> - fullPath = context.filePath + fullPath = context.filePath or "" [projectPath, _relativePath] = atom.project.relativizePath(fullPath) # Find the rubocop path