Improved Rubocop configuration and temp files
This commit is contained in:
parent
788d002402
commit
4f8f4d68f8
|
@ -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))
|
||||
|
|
|
@ -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})
|
||||
.then(=>
|
||||
@readFile(tempFile)
|
||||
@Promise.using(@createTempFile(fullPath), (tempFileName) =>
|
||||
new @Promise((resolve, reject) ->
|
||||
fs.writeFile(tempFileName, text, 'utf8', (err) ->
|
||||
return reject(err) if err?
|
||||
resolve tempFileName
|
||||
)
|
||||
else
|
||||
@run("rubocop", [
|
||||
"--auto-correct"
|
||||
"--config", configFile
|
||||
tempFile = @tempFile("temp", text, '.rb')
|
||||
], {ignoreReturnCode: true})
|
||||
.then(=>
|
||||
@readFile(tempFile)
|
||||
)
|
||||
)
|
||||
.then(=>
|
||||
rubocopArguments = [
|
||||
"--auto-correct"
|
||||
"--force-exclusion"
|
||||
tempFileName
|
||||
]
|
||||
rubocopArguments.push("--config", tempConfig) if tempConfig?
|
||||
@run(rubocopPath, rubocopArguments, {ignoreReturnCode: true, cwd: projectPath})
|
||||
)
|
||||
.then(=> @readFile(tempFileName))
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue