Skip to content

Commit

Permalink
Cached results of environment resolution of the RuboCop command.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmecham committed Feb 22, 2020
1 parent 9429aeb commit ae0d51e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
5 changes: 5 additions & 0 deletions RuboCop.novaextension/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## UNRELEASED

- Improved the speed of executing RuboCop by caching the environment checks
that determine which RuboCop installation to use.

## Version 0.3.0

- Added documentation describing how we attempt to locate and run RuboCop in
Expand Down
6 changes: 3 additions & 3 deletions Source/Scripts/Linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Linter {

constructor() {
this.issues = new IssueCollection();
this.process = new RuboCopProcess();
}

async lintDocument(document) {
Expand All @@ -23,12 +24,11 @@ class Linter {
}

async lintString(string, uri) {
const process = new RuboCopProcess(uri, string);
process.onComplete(offenses => {
this.process.onComplete(offenses => {
this.issues.set(uri, offenses.map(offense => offense.issue));
});

process.execute();
this.process.execute(string, uri);
}

removeIssues(uri) {
Expand Down
62 changes: 35 additions & 27 deletions Source/Scripts/RuboCopProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,22 @@
//

const Offense = require("./Offense");
var isUserNotifiedOfMissingCommand = false;

class RuboCopProcess {

constructor(path, content) {
this.path = path;
this.content = content;
this.defaultArguments = [
"rubocop",
"--format=json",
"--no-display-cop-names",
"--stdin",
this.path
];
constructor() {
this.isNotified = false;
}

get isBundled() {
if (!nova.workspace.contains("Gemfile")) return Promise.resolve(false);
if (typeof this._isBundled !== "undefined") {
return Promise.resolve(this._isBundled);
}

if (!nova.workspace.contains("Gemfile")) {
this._sisBundled = false;
return Promise.resolve(false);
}

return new Promise(resolve => {
const process = new Process("/usr/bin/env", {
Expand All @@ -37,9 +35,9 @@ class RuboCopProcess {
process.onDidExit(status => {
if (status === 0) {
console.log(`Found RuboCop ${output} (Bundled)`);
resolve(true);
resolve(this._isBundled = true);
} else {
resolve(false);
resolve(this._isBundled = false);
}
});

Expand All @@ -48,6 +46,10 @@ class RuboCopProcess {
}

get isGlobal() {
if (typeof this._isGlobal !== "undefined") {
return Promise.resolve(this._isGlobal);
}

return new Promise(resolve => {
const process = new Process("/usr/bin/env", {
args: ["rubocop", "--version"],
Expand All @@ -60,37 +62,43 @@ class RuboCopProcess {
process.onDidExit(status => {
if (status === 0) {
console.log(`Found RuboCop ${output} (Global)`);
resolve(true);
resolve(this._isGlobal = true);
} else {
resolve(false);
resolve(this._isGlobal = false);
}
});

process.start();
});
}

async process() {
if (this._process) return this._process;

async process(commandArguments) {
if (await this.isBundled) {
this.defaultArguments.unshift("bundle", "exec");
commandArguments.unshift("bundle", "exec");
} else if (!(await this.isGlobal)) {
this.notifyUserOfMissingCommand();
return false;
}

const process = new Process("/usr/bin/env", {
args: this.defaultArguments,
args: commandArguments,
cwd: nova.workspace.path,
stdio: "pipe"
});

return (this._process = process);
return process;
}

async execute() {
const process = await this.process();
async execute(content, uri) {
const defaultArguments = [
"rubocop",
"--format=json",
"--no-display-cop-names",
"--stdin",
uri
];

const process = await this.process(defaultArguments);
if (!process) return;

let output = "";
Expand All @@ -107,7 +115,7 @@ class RuboCopProcess {
const writer = channel.getWriter();

writer.ready.then(() => {
writer.write(this.content);
writer.write(content);
writer.close();
});
}
Expand All @@ -131,7 +139,7 @@ class RuboCopProcess {
}

notifyUserOfMissingCommand() {
if (isUserNotifiedOfMissingCommand) { return; }
if (this.isNotified) return;

const request = new NotificationRequest("rubocop-not-found");
request.title = nova.localize("RuboCop Not Found");
Expand All @@ -146,7 +154,7 @@ class RuboCopProcess {
}).catch((error) => {
console.error(error);
}).finally(() => {
isUserNotifiedOfMissingCommand = true;
this.isNotified = true;
});
}

Expand Down

0 comments on commit ae0d51e

Please sign in to comment.