Skip to content

Commit

Permalink
Merge pull request #91 from gandarez/master
Browse files Browse the repository at this point in the history
New features
  • Loading branch information
alanhamlett authored Apr 17, 2019
2 parents b156182 + b4fbe70 commit f9f5a1b
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 81 deletions.
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 15 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,39 @@
"vscode:prepublish": "webpack --mode production",
"compile": "webpack --mode none",
"compile-dev": "tsc -p ./",
"watch": "webpack --mode none --watch",
"watch": "webpack --mode none --watch",
"postinstall": "node ./node_modules/vscode/bin/install",
"test": "npm run compile-dev && node ./node_modules/vscode/bin/test"
},
"contributes": {
"commands": [
{
"command": "wakatime.apikey",
"title": "WakaTime API Key"
"title": "WakaTime: Api Key"
},
{
"command": "wakatime.proxy",
"title": "WakaTime Proxy"
"title": "WakaTime: Set Proxy"
},
{
"command": "wakatime.debug",
"title": "WakaTime Debug"
"title": "WakaTime: Set Debug"
},
{
"command": "wakatime.status_bar_icon",
"title": "WakaTime Status Bar Icon"
"title": "WakaTime: Status Bar Icon"
},
{
"command": "wakatime.dashboard",
"title": "WakaTime Dashboard"
"title": "WakaTime: Open Dashboard"
},
{
"command": "wakatime.config_file",
"title": "WakaTime: Open Config File..."
},
{
"command": "wakatime.log_file",
"title": "WakaTime: Open Log File..."
}
]
},
Expand All @@ -77,6 +85,7 @@
"@types/mocha": "^5.2.6",
"@types/node": "^11.13.4",
"@types/request": "^2.48.1",
"@types/rimraf": "^2.0.2",
"@types/webpack": "^4.4.27",
"adm-zip": "0.4.13",
"del-cli": "^1.1.0",
Expand Down
48 changes: 20 additions & 28 deletions src/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as vscode from 'vscode';
import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';
Expand Down Expand Up @@ -49,7 +50,7 @@ export class Dependencies {
if (this.cachedPythonLocation) return callback(this.cachedPythonLocation);

let locations: string[] = [
this.extensionPath + path.sep + 'python' + path.sep + 'pythonw',
path.join(this.extensionPath, 'python', 'pythonw'),
'python3',
'pythonw',
'python',
Expand All @@ -60,8 +61,8 @@ export class Dependencies {
];
for (var i = 39; i >= 27; i--) {
if (i >= 30 && i <= 32) continue;
locations.push('\\python' + i + '\\pythonw');
locations.push('\\Python' + i + '\\pythonw');
locations.push(`\\python${i}\\pythonw`);
locations.push(`\\Python${i}\\pythonw`);
}

this.findPython(locations, python => {
Expand All @@ -71,14 +72,7 @@ export class Dependencies {
}

public getCoreLocation(): string {
let dir =
this.extensionPath +
path.sep +
'wakatime-master' +
path.sep +
'wakatime' +
path.sep +
'cli.py';
let dir = path.join(this.extensionPath, 'wakatime-master', 'wakatime', 'cli.py');
return dir;
}

Expand All @@ -89,21 +83,21 @@ export class Dependencies {
private findPython(locations: string[], callback: (arg0: string) => void): void {
const binary = locations.shift();
if (!binary) {
callback("");
callback('');
return;
}

this.logger.debug('Looking for python at: ' + binary);
this.logger.debug(`Looking for python at: ${binary}`);

const args = ['--version'];
child_process.execFile(binary, args, (error, stdout, stderr) => {
const output: string = stdout.toString() + stderr.toString();
if (!error && this.isSupportedPythonVersion(binary, output)) {
this.cachedPythonLocation = binary;
this.logger.debug('Valid python version: ' + output);
this.logger.debug(`Valid python version: ${output}`);
callback(binary);
} else {
this.logger.debug('Invalid python version: ' + output);
this.logger.debug(`Invalid python version: ${output}`);
this.findPython(locations, callback);
}
});
Expand All @@ -120,15 +114,15 @@ export class Dependencies {
child_process.execFile(pythonBinary, args, (error, _stdout, stderr) => {
if (!(error != null)) {
let currentVersion = stderr.toString().trim();
this.logger.debug('Current wakatime-core version is ' + currentVersion);
this.logger.debug(`Current wakatime-core version is ${currentVersion}`);

this.logger.debug('Checking for updates to wakatime-core...');
this.getLatestCoreVersion(latestVersion => {
if (currentVersion === latestVersion) {
this.logger.debug('wakatime-core is up to date');
if (callback) callback(true);
} else if (latestVersion) {
this.logger.debug('Found an updated wakatime-core v' + latestVersion);
this.logger.debug(`Found an updated wakatime-core v${latestVersion}`);
if (callback) callback(false);
} else {
this.logger.debug('Unable to find latest wakatime-core version from GitHub');
Expand Down Expand Up @@ -172,26 +166,26 @@ export class Dependencies {
private installCore(callback: () => void): void {
this.logger.debug('Downloading wakatime-core...');
let url = 'https://github.com/wakatime/wakatime/archive/master.zip';
let zipFile = this.extensionPath + path.sep + 'wakatime-master.zip';
let zipFile = path.join(this.extensionPath, 'wakatime-master.zip');

this.downloadFile(url, zipFile, () => {
this.extractCore(zipFile, callback);
});
}

private extractCore(zipFile: string, callback: () => void): void {
this.logger.debug('Extracting wakatime-core into "' + this.extensionPath + '"...');
this.logger.debug(`Extracting wakatime-core into "${this.extensionPath}"...`);
this.removeCore(() => {
this.unzip(zipFile, this.extensionPath, callback);
this.logger.debug('Finished extracting wakatime-core.');
});
}

private async removeCore(callback: () => void): Promise<void> {
if (fs.existsSync(this.extensionPath + path.sep + 'wakatime-master')) {
if (fs.existsSync(path.join(this.extensionPath, 'wakatime-master'))) {
try {
const rimraf = await import('rimraf');
rimraf(this.extensionPath + path.sep + 'wakatime-master', () => {
rimraf(path.join(this.extensionPath, 'wakatime-master'), () => {
if (callback != null) {
return callback();
}
Expand Down Expand Up @@ -255,19 +249,17 @@ export class Dependencies {
let url = 'https://www.python.org/ftp/python/' + ver + '/python-' + ver + '-embed-' + arch + '.zip';

this.logger.debug('Downloading python...');
let zipFile = this.extensionPath + path.sep + 'python.zip';
let zipFile = path.join(this.extensionPath, 'python.zip');
this.downloadFile(url, zipFile, () => {
this.logger.debug('Extracting python...');
this.unzip(zipFile, this.extensionPath + path.sep + 'python', callback);
this.unzip(zipFile, path.join(this.extensionPath, 'python'), callback);
this.logger.debug('Finished installing python.');

callback();
});
} else {
this.logger.error(
'WakaTime depends on Python. Install it from https://python.org/downloads then restart VS Code',
);
// window.alert('WakaTime depends on Python. Install it from https://python.org/downloads then restart VSCode.');
let error_msg = 'WakaTime depends on Python. Install it from https://python.org/downloads then restart VS Code';
this.logger.warn(error_msg);
vscode.window.showWarningMessage(error_msg);
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ export function activate(ctx: vscode.ExtensionContext) {
wakatime.openDashboardWebsite();
}),
);

ctx.subscriptions.push(
vscode.commands.registerCommand('wakatime.config_file', function() {
wakatime.openConfigFile();
}),
);

ctx.subscriptions.push(
vscode.commands.registerCommand('wakatime.log_file', function() {
wakatime.openLogFile();
}),
);

ctx.subscriptions.push(wakatime);

Expand Down
4 changes: 2 additions & 2 deletions src/libs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export class Libs {
public static quote(str): string {
if (str.includes(' ')) return '"' + str.replace('"', '\\"') + '"';
public static quote(str: string): string {
if (str.includes(' ')) return `"${str.replace('"', '\\"')}"`;
return str;
}
}
6 changes: 3 additions & 3 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ export class Logger {
if (level in this.levels) {
this.level = level;
} else {
throw new TypeError('Invalid level: ' + level);
throw new TypeError(`Invalid level: ${level}`);
}
}

public log(level: string, msg: string): void {
if (!(level in this.levels)) throw new TypeError('Invalid level: ' + level);
if (!(level in this.levels)) throw new TypeError(`Invalid level: ${level}`);

const current: number = this.levels[level];
const cutoff: number = this.levels[this.level];

if (current >= cutoff) {
msg = '[WakaTime] [' + level.toUpperCase() + '] ' + msg;
msg = `[WakaTime][${level.toUpperCase()}] ${msg}`;
if (level == 'debug') console.log(msg);
if (level == 'info') console.info(msg);
if (level == 'warn') console.warn(msg);
Expand Down
15 changes: 12 additions & 3 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import * as fs from 'fs';
import { Dependencies } from './dependencies';

export class Options {
private configFile = path.join(this.getWakaHome(), '.wakatime.cfg');
private logFile = path.join(this.getWakaHome(), '.wakatime.log');
configFile: string;
logFile: string;

constructor() {
let wakaHome = this.getWakaHome();
this.configFile = path.join(wakaHome, '.wakatime.cfg');
this.logFile = path.join(wakaHome, '.wakatime.log');
}

private getWakaHome(): string {
let home = process.env.WAKATIME_HOME;
Expand All @@ -19,7 +25,7 @@ export class Options {
public getSetting(section: string, key: string, callback: (string, any) => void): void {
fs.readFile(this.getConfigFile(), 'utf-8', (err: NodeJS.ErrnoException, content: string) => {
if (err) {
if (callback) callback(new Error('could not read ' + this.getConfigFile()), null);
if (callback) callback(new Error(`could not read ${this.getConfigFile()}`), null);
} else {
let currentSection = '';
let lines = content.split('\n');
Expand Down Expand Up @@ -105,6 +111,9 @@ export class Options {
}

public getUserHomeDir(): string {
if (process.env['VSCODE_PORTABLE'])
return process.env['VSCODE_PORTABLE'] as string;

return process.env[Dependencies.isWindows() ? 'USERPROFILE' : 'HOME'] || '';
}

Expand Down
Loading

0 comments on commit f9f5a1b

Please sign in to comment.