Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix resolve of protolint.path if configured relative #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as path from 'path';
import * as vscode from 'vscode';
import * as cp from 'child_process';

const DEFAULT_PROTO_LINT_PATH = 'protolint';

let config: { protoLintPath?: string } = {
protoLintPath: undefined
};

export function reloadConfig(): boolean {
config.protoLintPath = resolveProtoLintPath();

// Verify that protolint can be successfully executed on the host machine by running the version command.
// In the event the binary cannot be executed, tell the user where to download protolint from.
const result = cp.spawnSync(config.protoLintPath, ['version']);
if (result.status !== 0) {
vscode.window.showErrorMessage("protolint was not detected using path `" + config.protoLintPath + "`. Download from: https://github.com/yoheimuta/protolint");
return false;
}

return true;
}

export function getConfig() {
return config;
}

function resolveProtoLintPath(): string {
let p = vscode.workspace.getConfiguration('protolint').get<string>('path');

if (!p || p === DEFAULT_PROTO_LINT_PATH) {
return DEFAULT_PROTO_LINT_PATH;
} else if (!path.isAbsolute(p) && vscode.workspace.workspaceFolders) {
return path.join(vscode.workspace.workspaceFolders[0].uri.fsPath, p);
} else {
return p;
}
}
19 changes: 8 additions & 11 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode';
import * as cp from 'child_process';
import Linter, { LinterError } from './linter';
import { reloadConfig } from './config';

const diagnosticCollection = vscode.languages.createDiagnosticCollection("protolint");

Expand All @@ -17,24 +17,21 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.executeCommand('protolint.lint');
});

// Verify that protolint can be successfully executed on the host machine by running the version command.
// In the event the binary cannot be executed, tell the user where to download protolint from.
let protoLintPath = vscode.workspace.getConfiguration('protolint').get<string>('path');
if (!protoLintPath) {
protoLintPath = "protolint"
}
vscode.workspace.onDidChangeConfiguration((e: vscode.ConfigurationChangeEvent) => {
if (e.affectsConfiguration('protolint')) {
reloadConfig();
}
});

const result = cp.spawnSync(protoLintPath, ['version']);
if (result.status !== 0) {
vscode.window.showErrorMessage("protolint was not detected using path `" + protoLintPath + "`. Download from: https://github.com/yoheimuta/protolint");
if (!reloadConfig()) {
return;
}
}

function runLint() {
let editor = vscode.window.activeTextEditor;
if (!editor) {
return;
return;
}

// We only want to run protolint on documents that are known to be
Expand Down
7 changes: 2 additions & 5 deletions src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as vscode from 'vscode';
import * as util from 'util';
import * as path from 'path';

import { getConfig } from './config';
import { ProtoError, parseProtoError } from './protoError';

export interface LinterError {
Expand Down Expand Up @@ -44,11 +45,7 @@ export default class Linter {
let currentFile = this.codeDocument.uri.fsPath;
let currentDirectory = path.dirname(currentFile);

let protoLintPath = vscode.workspace.getConfiguration('protolint').get<string>('path');
if (!protoLintPath) {
protoLintPath = "protolint"
}

let { protoLintPath } = getConfig();
const cmd = `${protoLintPath} lint "${currentFile}"`;

// Execute the protolint binary and store the output from standard error.
Expand Down