Skip to content

Commit

Permalink
mhutchie#398 New command "Git Graph: Get Version Information", that d…
Browse files Browse the repository at this point in the history
…isplays basic version information of the Git Graph installation, and allows it to be easily copied to the clipboard.
  • Loading branch information
mhutchie committed Oct 18, 2020
1 parent f10111f commit f025c1a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 28 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
"category": "Git Graph",
"command": "git-graph.resumeWorkspaceCodeReview",
"title": "Resume a specific Code Review in Workspace..."
},
{
"category": "Git Graph",
"command": "git-graph.version",
"title": "Get Version Information"
}
],
"configuration": {
Expand Down
35 changes: 29 additions & 6 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as os from 'os';
import * as vscode from 'vscode';
import { AvatarManager } from './avatarManager';
import { getConfig } from './config';
Expand All @@ -6,15 +7,15 @@ import { CodeReviewData, CodeReviews, ExtensionState } from './extensionState';
import { GitGraphView } from './gitGraphView';
import { Logger } from './logger';
import { RepoManager } from './repoManager';
import { GitExecutable, UNABLE_TO_FIND_GIT_MSG, abbrevCommit, abbrevText, getPathFromUri, getRelativeTimeDiff, getRepoName, isPathInWorkspace, resolveToSymbolicPath, showErrorMessage, showInformationMessage } from './utils';
import { GitExecutable, UNABLE_TO_FIND_GIT_MSG, abbrevCommit, abbrevText, copyToClipboard, getExtensionVersion, getPathFromUri, getRelativeTimeDiff, getRepoName, isPathInWorkspace, resolveToSymbolicPath, showErrorMessage, showInformationMessage } from './utils';
import { Disposable } from './utils/disposable';
import { Event } from './utils/event';

/**
* Manages the registration and execution of Git Graph Commands.
*/
export class CommandManager extends Disposable {
private readonly extensionPath: string;
private readonly context: vscode.ExtensionContext;
private readonly avatarManager: AvatarManager;
private readonly dataSource: DataSource;
private readonly extensionState: ExtensionState;
Expand All @@ -33,9 +34,9 @@ export class CommandManager extends Disposable {
* @param onDidChangeGitExecutable The Event emitting the Git executable for Git Graph to use.
* @param logger The Git Graph Logger instance.
*/
constructor(extensionPath: string, avatarManger: AvatarManager, dataSource: DataSource, extensionState: ExtensionState, repoManager: RepoManager, gitExecutable: GitExecutable | null, onDidChangeGitExecutable: Event<GitExecutable>, logger: Logger) {
constructor(context: vscode.ExtensionContext, avatarManger: AvatarManager, dataSource: DataSource, extensionState: ExtensionState, repoManager: RepoManager, gitExecutable: GitExecutable | null, onDidChangeGitExecutable: Event<GitExecutable>, logger: Logger) {
super();
this.extensionPath = extensionPath;
this.context = context;
this.avatarManager = avatarManger;
this.dataSource = dataSource;
this.extensionState = extensionState;
Expand All @@ -50,6 +51,7 @@ export class CommandManager extends Disposable {
this.registerCommand('git-graph.endAllWorkspaceCodeReviews', () => this.endAllWorkspaceCodeReviews());
this.registerCommand('git-graph.endSpecificWorkspaceCodeReview', () => this.endSpecificWorkspaceCodeReview());
this.registerCommand('git-graph.resumeWorkspaceCodeReview', () => this.resumeWorkspaceCodeReview());
this.registerCommand('git-graph.version', () => this.version());

this.registerDisposable(
onDidChangeGitExecutable((gitExecutable) => {
Expand Down Expand Up @@ -92,7 +94,7 @@ export class CommandManager extends Disposable {
loadRepo = this.repoManager.getRepoContainingFile(getPathFromUri(vscode.window.activeTextEditor.document.uri));
}

GitGraphView.createOrShow(this.extensionPath, this.dataSource, this.extensionState, this.avatarManager, this.repoManager, this.logger, loadRepo !== null ? { repo: loadRepo, commitDetails: null } : null);
GitGraphView.createOrShow(this.context.extensionPath, this.dataSource, this.extensionState, this.avatarManager, this.repoManager, this.logger, loadRepo !== null ? { repo: loadRepo, commitDetails: null } : null);
}

/**
Expand Down Expand Up @@ -210,7 +212,7 @@ export class CommandManager extends Disposable {
}).then((item) => {
if (item) {
const commitHashes = item.codeReviewId.split('-');
GitGraphView.createOrShow(this.extensionPath, this.dataSource, this.extensionState, this.avatarManager, this.repoManager, this.logger, {
GitGraphView.createOrShow(this.context.extensionPath, this.dataSource, this.extensionState, this.avatarManager, this.repoManager, this.logger, {
repo: item.codeReviewRepo,
commitDetails: {
commitHash: commitHashes[commitHashes.length > 1 ? 1 : 0],
Expand All @@ -223,6 +225,27 @@ export class CommandManager extends Disposable {
});
}

/**
* The method run when the `git-graph.version` command is invoked.
*/
private async version() {
try {
const gitGraphVersion = await getExtensionVersion(this.context);
const information = 'Git Graph: ' + gitGraphVersion + '\nVisual Studio Code: ' + vscode.version + '\nOS: ' + os.type() + ' ' + os.arch() + ' ' + os.release() + '\nGit: ' + (this.gitExecutable !== null ? this.gitExecutable.version : '(none)');
vscode.window.showInformationMessage(information, { modal: true }, 'Copy').then((selectedItem) => {
if (selectedItem === 'Copy') {
copyToClipboard(information).then((result) => {
if (result !== null) {
showErrorMessage(result);
}
});
}
}, () => { });
} catch (_) {
showErrorMessage('An unexpected error occurred while retrieving version information.');
}
}


/* Helper Methods */

Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function activate(context: vscode.ExtensionContext) {
const avatarManager = new AvatarManager(dataSource, extensionState, logger);
const repoManager = new RepoManager(dataSource, extensionState, onDidChangeConfiguration, logger);
const statusBarItem = new StatusBarItem(repoManager.getNumRepos(), repoManager.onDidChangeRepos, onDidChangeConfiguration, logger);
const commandManager = new CommandManager(context.extensionPath, avatarManager, dataSource, extensionState, repoManager, gitExecutable, onDidChangeGitExecutable, logger);
const commandManager = new CommandManager(context, avatarManager, dataSource, extensionState, repoManager, gitExecutable, onDidChangeGitExecutable, logger);
const diffDocProvider = new DiffDocProvider(dataSource);

context.subscriptions.push(
Expand Down
22 changes: 1 addition & 21 deletions src/life-cycle/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
import { LifeCycleStage, LifeCycleState, generateNonce, getDataDirectory, getLifeCycleStateInDirectory, saveLifeCycleStateInDirectory, sendQueue } from './utils';
import { getExtensionVersion } from '../utils';

/**
* Run on startup to detect if Git Graph has been installed or updated, and if so generate an event.
Expand Down Expand Up @@ -89,27 +90,6 @@ function saveLifeCycleState(extensionContext: vscode.ExtensionContext, state: Li
]);
}

/**
* Gets the version of Git Graph.
* @param extensionContext The extension context of Git Graph.
* @returns The Git Graph version.
*/
function getExtensionVersion(extensionContext: vscode.ExtensionContext) {
return new Promise<string>((resolve, reject) => {
fs.readFile(path.join(extensionContext.extensionPath, 'package.json'), (err, data) => {
if (err) {
reject();
} else {
try {
resolve(JSON.parse(data.toString()).version);
} catch (_) {
reject();
}
}
});
});
}

/**
* Get a nonce generated for this installation of Git Graph.
* @returns A 256 bit cryptographically strong pseudo-random nonce.
Expand Down
21 changes: 21 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,27 @@ export function getRelativeTimeDiff(unixTimestamp: number) {
return diff + ' ' + unit + (diff !== 1 ? 's' : '') + ' ago';
}

/**
* Gets the version of Git Graph.
* @param extensionContext The extension context of Git Graph.
* @returns The Git Graph version.
*/
export function getExtensionVersion(extensionContext: vscode.ExtensionContext) {
return new Promise<string>((resolve, reject) => {
fs.readFile(path.join(extensionContext.extensionPath, 'package.json'), (err, data) => {
if (err) {
reject();
} else {
try {
resolve(JSON.parse(data.toString()).version);
} catch (_) {
reject();
}
}
});
});
}

/**
* Randomly generate a nonce.
* @returns The nonce.
Expand Down

0 comments on commit f025c1a

Please sign in to comment.