Skip to content

Commit

Permalink
refactor: Extract view build log into command
Browse files Browse the repository at this point in the history
  • Loading branch information
jlandersen committed Jul 11, 2017
1 parent 51956b1 commit b810761
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 105 deletions.
44 changes: 44 additions & 0 deletions src/commands/OpenBuildLogCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {window} from "vscode";
import {VstsBuildRestClient} from "../vstsbuildrestclient";
import {Settings} from "../settings";
import {BuildQuickPicker} from "../components/BuildQuickPicker";
import {BuildLogViewer} from "../components/BuildLogViewer";

export default class OpenBuildLogCommand {
private buildQuickPicker: BuildQuickPicker;
private logViewer: BuildLogViewer;

constructor(private settings: Settings, private restClient: VstsBuildRestClient) {
this.buildQuickPicker = new BuildQuickPicker(settings, restClient);
this.logViewer = new BuildLogViewer(restClient);
}

public execute() {
this.buildQuickPicker.showBuildDefinitionQuickPick("Select a build definition")
.then(result => {
if (!result) {
return;
}

if (result.length > 1) {
window.showInformationMessage(`Viewing group build is not possible, please select single build instead.`);
return;
}

return this.buildQuickPicker.showBuildQuickPick(result[0].id, "Select a build to view");
})
.then(build => {
if (!build) {
return;
}

this.logViewer.viewLog(build.id);
});
}

public dispose() {
if (this.logViewer) {
this.logViewer.dispose;
}
}
}
67 changes: 67 additions & 0 deletions src/components/BuildLogViewer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"use strict";

import {window, OutputChannel} from "vscode";
import {VstsBuildRestClient} from "../vstsbuildrestclient";


export class BuildLogViewer {
private outputChannel: OutputChannel;
private intervalTimer: NodeJS.Timer;
private updateIntervalInSeconds: number = 5;
private currentLogIndex: number = 0;

constructor(private restClient: VstsBuildRestClient) {
}

public viewLog(buildId: number) {
if (!this.outputChannel) {
this.outputChannel = window.createOutputChannel("VSTS Build Log");
}

this.outputChannel.clear();
this.outputChannel.show();

this.getNext(buildId);
}

private getNext(buildId: number) {
if (this.currentLogIndex === 0) {
this.outputChannel.appendLine("(VSTS Build Agent Status extension) Waiting for first logs...");
}

let getBuild = this.restClient.getBuild(buildId);
let getLog = getBuild.then(build => this.restClient.getLog(build.value));

Promise.all([getBuild, getLog]).then(value => {
let build = value[0];
let log = value[1];

if (!log) {
return;
}

var newLogEntries = log.value.messages.splice(this.currentLogIndex);
newLogEntries.forEach(element => {
this.outputChannel.appendLine(element);
});

this.currentLogIndex = this.currentLogIndex + newLogEntries.length;

if (build.value.status === "completed") {
clearInterval(this.intervalTimer);
this.intervalTimer = null;
this.currentLogIndex = 0;

this.outputChannel.appendLine("(VSTS Build Agent Status extension) End of build log")
} else if (build.value.status !== "completed" && !this.intervalTimer) {
this.intervalTimer = setInterval(() => this.getNext(buildId), this.updateIntervalInSeconds * 1000);
}
});
}

public dispose() {
if (this.outputChannel) {
this.outputChannel.dispose();
}
}
}
14 changes: 8 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
"use strict";

import * as vscode from 'vscode';
import {WorkspaceVstsSettings} from './settings'
import {VstsBuildStatus} from './vstsbuildstatus'
import {VstsBuildRestClientImpl} from './vstsbuildrestclient'
import OpenBuildInBrowserCommand from './commands/OpenBuildInBrowserCommand';
import * as vscode from "vscode";
import {WorkspaceVstsSettings} from "./settings";
import {VstsBuildStatus} from "./vstsbuildstatus";
import {VstsBuildRestClientImpl} from "./vstsbuildrestclient";
import OpenBuildInBrowserCommand from "./commands/OpenBuildInBrowserCommand";
import OpenBuildLogCommand from "./commands/OpenBuildLogCommand";

export function activate(context: vscode.ExtensionContext) {
let settings = new WorkspaceVstsSettings(context.workspaceState);
let restClient = new VstsBuildRestClientImpl(settings);
let buildServiceStatus = new VstsBuildStatus(settings, restClient);

let openBuildInBrowserCommand = new OpenBuildInBrowserCommand(settings, restClient);
let openBuildLogCommand = new OpenBuildLogCommand(settings, restClient);

context.subscriptions.push(
vscode.commands.registerCommand('extension.openVstsBuildDefinitionSelection', () => buildServiceStatus.openBuildDefinitionSelection()));
Expand All @@ -20,7 +22,7 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('extension.openVstsBuildWebSelection', () => openBuildInBrowserCommand.execute()));

context.subscriptions.push(
vscode.commands.registerCommand('extension.openVstsBuildLogSelection', () => buildServiceStatus.openBuildLogSelection()));
vscode.commands.registerCommand('extension.openVstsBuildLogSelection', () => openBuildLogCommand.execute()));

context.subscriptions.push(
vscode.commands.registerCommand('extension.openVstsQueueBuildSelection', () => buildServiceStatus.openQueueBuildSelection()));
Expand Down
66 changes: 0 additions & 66 deletions src/vstsbuildlog.ts

This file was deleted.

33 changes: 0 additions & 33 deletions src/vstsbuildstatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {window, OutputChannel, QuickPickItem} from "vscode";
import {Settings} from "./settings";
import {VstsBuildStatusBar} from "./vstsbuildstatusbar";
import {Build, BuildDefinition, VstsBuildRestClient} from "./vstsbuildrestclient";
import {VstsBuildLogStreamHandler} from "./vstsbuildlog";
import {BuildQuickPicker} from "./components/BuildQuickPicker";

export class VstsBuildStatus {
Expand All @@ -16,15 +15,13 @@ export class VstsBuildStatus {
private settings: Settings;
private intervalTimer: NodeJS.Timer;
private restClient: VstsBuildRestClient;
private logStreamHandler: VstsBuildLogStreamHandler;

constructor(settings: Settings, restClient: VstsBuildRestClient) {
this.settings = settings;
this.statusBar = new VstsBuildStatusBar();
this.buildQuickPicker = new BuildQuickPicker(settings, restClient);
this.restClient = restClient;
this.activeDefinitions = settings.activeBuildDefinitions;
this.logStreamHandler = new VstsBuildLogStreamHandler(this.restClient);

this.settings.onDidChangeSettings(() => {
this.beginBuildStatusUpdates();
Expand Down Expand Up @@ -106,32 +103,6 @@ export class VstsBuildStatus {
});
}

public openBuildLogSelection(): void {
this.buildQuickPicker.showBuildDefinitionQuickPick("Select a build definition")
.then(result => {
if (!result) {
return;
}

if (result.length > 1) {
window.showInformationMessage(`Viewing group build is not possible, please select single build instead.`);
return;
}

return this.buildQuickPicker.showBuildQuickPick(result[0].id, "Select a build to view");
})
.then(build => {
if (!build) {
return;
}

this.logStreamHandler.streamLogs(build);
})
.catch(error => {
this.handleError();
});
}

public openQueueBuildSelection(): void {
if (!this.settings.isValid()) {
this.showSettingsMissingMessage();
Expand Down Expand Up @@ -204,9 +175,5 @@ export class VstsBuildStatus {
public dispose() {
this.statusBar.dispose();
this.settings.dispose();

if (this.logStreamHandler) {
this.logStreamHandler.dispose();
}
}
}

0 comments on commit b810761

Please sign in to comment.