Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
Handle multiple versions of Flake8
Browse files Browse the repository at this point in the history
Check which version of Flake8 is running before using version dependent commands.
  • Loading branch information
lucasdf committed Aug 19, 2017
1 parent 6c84a01 commit 469d392
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
29 changes: 27 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { CompositeDisposable } from 'atom';
import fs from 'fs-plus';
import path from 'path';
import * as helpers from 'atom-linter';
import semver from 'semver';

// Local variables
const parseRegex = /(\d+):(\d+):\s(([A-Z])\d{2,3})\s+(.*)/g;
const execPathVersions = new Map();

const applySubstitutions = (givenExecPath, projDir) => {
let execPath = givenExecPath;
Expand Down Expand Up @@ -73,6 +75,22 @@ const generateInvalidPointTrace = async (execPath, match, filePath, textEditor,
};
};

const determineExecVersion = async (execPath) => {
const versionString = await helpers.exec(execPath, ['--version'], { ignoreExitCode: true });
const versionPattern = /^[^\s]+/g;
const match = versionString.match(versionPattern);
if (match !== null) {
execPathVersions.set(execPath, match[0]);
}
}

const getFlake8Version = async (execPath) => {
if (!execPathVersions.has(execPath)) {
await determineExecVersion(execPath);
}
return execPathVersions.get(execPath);
}

export default {
activate() {
this.idleCallbacks = new Set();
Expand Down Expand Up @@ -150,6 +168,15 @@ export default {
const projectPath = atom.project.relativizePath(filePath)[0];
const baseDir = projectPath !== null ? projectPath : path.dirname(filePath);
const configFilePath = await helpers.findCachedAsync(baseDir, this.projectConfigFile);
const execPath = fs.normalize(applySubstitutions(this.executablePath, baseDir));

// get the version of Flake8
const version = await getFlake8Version(execPath);

// stdin-display-name available since 3.0.0
if (semver.valid(version) && semver.gte(version, '3.0.0')) {
parameters.push('--stdin-display-name', filePath);
}

if (this.projectConfigFile && baseDir !== null && configFilePath !== null) {
parameters.push('--config', configFilePath);
Expand All @@ -173,11 +200,9 @@ export default {
parameters.push('--builtins', this.builtins.join(','));
}
}
parameters.push('--stdin-display-name', filePath);

parameters.push('-');

const execPath = fs.normalize(applySubstitutions(this.executablePath, baseDir));
const forceTimeout = 1000 * 60 * 5; // (ms * s * m) = Five minutes
const options = {
stdin: fileText,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
"dependencies": {
"atom-linter": "^10.0.0",
"atom-package-deps": "^4.0.1",
"fs-plus": "^3.0.0"
"fs-plus": "^3.0.0",
"semver": "^5.4.1"
},
"devDependencies": {
"eslint": "^4.3.0",
Expand Down

0 comments on commit 469d392

Please sign in to comment.