Skip to content

Commit

Permalink
Merge pull request #178 from notquiteamonad/support-global-wakatime-i…
Browse files Browse the repository at this point in the history
…nstallation

Support global WakaTime installation
  • Loading branch information
alanhamlett authored Feb 19, 2021
2 parents becb24c + 50f5057 commit dccf48f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 13 deletions.
9 changes: 6 additions & 3 deletions package-lock.json

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

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,9 @@
"typescript": "^4.1.3",
"webpack": "^5.11.1",
"webpack-cli": "^4.3.1"
},
"dependencies": {
"@types/which": "^1.3.2",
"which": "^2.0.2"
}
}
39 changes: 37 additions & 2 deletions src/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as process from 'process';
import * as request from 'request';
import * as rimraf from 'rimraf';
import * as vscode from 'vscode';
import * as which from 'which';

import { Options } from './options';
import { Logger } from './logger';
Expand All @@ -17,17 +18,27 @@ export class Dependencies {
private logger: Logger;
private extensionPath: string;
private s3urlprefix = 'https://wakatime-cli.s3-us-west-2.amazonaws.com/';
private global: boolean;
private standalone: boolean;

constructor(options: Options, extensionPath: string, logger: Logger, standalone: boolean) {
constructor(
options: Options,
extensionPath: string,
logger: Logger,
global: boolean,
standalone: boolean,
) {
this.options = options;
this.logger = logger;
this.extensionPath = extensionPath;
this.global = global;
this.standalone = standalone;
}

public checkAndInstall(callback: () => void): void {
if (this.standalone) {
if (this.global) {
this.checkGlobalCli(callback);
} else if (this.standalone) {
this.checkAndInstallStandaloneCli(callback);
} else {
this.isPythonInstalled(isInstalled => {
Expand Down Expand Up @@ -81,6 +92,16 @@ export class Dependencies {
return path.join(this.extensionPath, 'wakatime-cli', 'wakatime-cli' + ext);
}

public getGlobalCliLocation(): string {
const binaryName = `wakatime-cli${Dependencies.isWindows() ? '.exe' : ''}`;
const pathName =
which.sync(binaryName, { nothrow: true }) ??
which.sync(binaryName.replace('-cli', ''), { nothrow: true });
if (pathName) return pathName;
this.logger.error('Could not find global cli - is it installed?');
throw new Error('Could not find global cli - is it installed?');
}

public static isWindows(): boolean {
return os.platform() === 'win32';
}
Expand Down Expand Up @@ -117,6 +138,20 @@ export class Dependencies {
}
}

private checkGlobalCli(callback: () => void): void {
const binaryName = `wakatime-cli${Dependencies.isWindows() ? '.exe' : ''}`;
which(binaryName)
.then(() => callback())
.catch(() => {
which(binaryName.replace('-cli', ''))
.then(() => callback())
.catch(() => {
this.logger.error('Could not find global installation.');
throw new Error('Could not find global installation.');
});
});
}

private findPython(locations: string[], callback: (arg0: string) => void): void {
const binary = locations.shift();
if (!binary) {
Expand Down
10 changes: 8 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ export function activate(ctx: vscode.ExtensionContext) {
if (debug === 'true') {
logger.setLevel(LogLevel.DEBUG);
}
options.getSetting('settings', 'standalone', (_err, standalone) => {
wakatime.initialize(standalone !== 'false');
options.getSetting('settings', 'global', (_err, global) => {
const isGlobal = global === 'true';
if (isGlobal) wakatime.initialize(isGlobal, false);
else {
options.getSetting('settings', 'standalone', (_err, standalone) => {
wakatime.initialize(false, standalone !== 'false');
});
}
});
});
}
Expand Down
15 changes: 9 additions & 6 deletions src/wakatime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class WakaTime {
private lastFetchToday: number = 0;
private showStatusBar: boolean;
private showCodingActivity: boolean;
private global: boolean;
private standalone: boolean;
private disabled: boolean = true;

Expand All @@ -42,12 +43,14 @@ export class WakaTime {
this.options = options;
}

public initialize(standalone: boolean): void {
public initialize(global: boolean, standalone: boolean): void {
this.global = global;
this.standalone = standalone;
this.dependencies = new Dependencies(
this.options,
this.extensionPath,
this.logger,
this.global,
this.standalone,
);
this.statusBar.command = COMMAND_DASHBOARD;
Expand Down Expand Up @@ -320,8 +323,8 @@ export class WakaTime {
private sendHeartbeat(file: string, isWrite: boolean): void {
this.hasApiKey(hasApiKey => {
if (hasApiKey) {
if (this.standalone === undefined) return;
if (this.standalone) {
if (this.global === undefined || this.standalone === undefined) return;
if (this.global || this.standalone) {
this._sendHeartbeat(file, isWrite);
} else {
this.dependencies.getPythonLocation(pythonBinary => {
Expand All @@ -338,13 +341,13 @@ export class WakaTime {

private _sendHeartbeat(file: string, isWrite: boolean, pythonBinary?: string): void {
if (this.standalone && !this.dependencies.isStandaloneCliInstalled()) return;
let cli = this.standalone
let cli = this.global ? this.dependencies.getGlobalCliLocation() : (this.standalone
? this.dependencies.getStandaloneCliLocation()
: this.dependencies.getCliLocation();
: this.dependencies.getCliLocation());
let user_agent =
this.agentName + '/' + vscode.version + ' vscode-wakatime/' + this.extension.version;
let args = ['--file', Libs.quote(file), '--plugin', Libs.quote(user_agent)];
if (!this.standalone) args.unshift(cli);
if (!(this.global || this.standalone)) args.unshift(cli);
let project = this.getProjectName(file);
if (project) args.push('--alternate-project', Libs.quote(project));
if (isWrite) args.push('--write');
Expand Down

0 comments on commit dccf48f

Please sign in to comment.