Improved Rubocop configuration and temp files

This commit is contained in:
Brian Bugh 2017-07-23 13:49:07 -05:00
parent 788d002402
commit 4f8f4d68f8
2 changed files with 61 additions and 42 deletions

View File

@ -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))

View File

@ -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")
fs = require 'fs'
if fs.existsSync(configFile)
@debug("rubocop", config, fs.readFileSync(configFile, 'utf8'))
else
# Find or generate a config file if non exists
configFile = @findFile(path.dirname(fullPath), ".rubocop.yml")
if !configFile?
yaml = require("yaml-front-matter")
# Generate config file
config = {
"Style/IndentationWidth":
"Width": options.indent_size
}
tempConfig = @tempFile("rubocop-config", yaml.safeDump(config))
configFile = @tempFile("rubocop-config", yaml.safeDump(config))
@debug("rubocop", config, configFile)
# Check if PHP-CS-Fixer path was found
if rubocopPath?
@run(rubocopPath, [
"--auto-correct"
"--config", configFile
tempFile = @tempFile("temp", text, '.rb')
], {ignoreReturnCode: true})
@Promise.using(@createTempFile(fullPath), (tempFileName) =>
new @Promise((resolve, reject) ->
fs.writeFile(tempFileName, text, 'utf8', (err) ->
return reject(err) if err?
resolve tempFileName
)
)
.then(=>
@readFile(tempFile)
)
else
@run("rubocop", [
rubocopArguments = [
"--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))
)