Skip to content

Commit

Permalink
#79 Added builder pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
lucsomers101 committed Feb 19, 2024
1 parent 7721049 commit 5270f26
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 71 deletions.
1 change: 1 addition & 0 deletions src/builders/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./terminalBuilder";
33 changes: 33 additions & 0 deletions src/builders/terminalBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as vscode from 'vscode';
import { terminalInfo } from "../models/terminal";
import { fileUtils } from '../utils';

export class terminalBuilder
{
private terminalInfoBuild:terminalInfo = new terminalInfo;

private reset() {
this.terminalInfoBuild = new terminalInfo;
}

public getTerminal(): vscode.Terminal {
const toReturn = vscode.window.createTerminal({name:this.terminalInfoBuild.terminalName,cwd:fileUtils.generateWorkspacePath(this.terminalInfoBuild.workingDir)});
this.reset();
return toReturn;
}

public setName(terminalName:string) : terminalBuilder {
this.terminalInfoBuild.terminalName = terminalName;
return this;
}

public setVisibility(terminalVisible:boolean) : terminalBuilder {
this.terminalInfoBuild.terminalVisible = terminalVisible;
return this;
}

public setWorkingDir(workingDir:string[]) : terminalBuilder {
this.terminalInfoBuild.workingDir = workingDir;
return this;
}
}
12 changes: 9 additions & 3 deletions src/commands/generateAtlasCommand.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import * as vscode from 'vscode';
import { config, fileUtils, terminalUtils } from "../utils";
import { terminalBuilder } from '../builders';

export class generateAtlasCommand {
static GenerateAtlasCommand()
private builder : terminalBuilder = new terminalBuilder();

public GenerateAtlasCommand()
{
if(config.mainScriptSetting === undefined || config.folderSetting === undefined)
return;
Expand All @@ -12,7 +15,10 @@ export class generateAtlasCommand {
if(currentActiveFilePath === undefined)
return;

terminalUtils.RunCommandInNewTerminal("ampersand generate atlas",
`ampersand population --output-dir='./' --build-recipe Grind --output-format json --verbosity warn ${currentActiveFilePath}`)
const terminal = this.builder.setName("Ampersand generate functional spec")
.getTerminal();

terminalUtils.RunCommandsInExistingTerminal(terminal,
[`ampersand population --output-dir='./' --build-recipe Grind --output-format json --verbosity warn ${currentActiveFilePath}`])
}
}
13 changes: 10 additions & 3 deletions src/commands/generateFunctionalSpecCommand.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import * as vscode from 'vscode';
import { config, fileUtils, terminalUtils } from '../utils';
import { terminalBuilder } from '../builders';

export class generateFunctionalSpecCommand{
static GenerateFunctionalSpecCommand()
private builder : terminalBuilder = new terminalBuilder();

public GenerateFunctionalSpecCommand()
{
if(config.mainScriptSetting === undefined || config.folderSetting === undefined)
return;

const mainScriptPath: string = fileUtils.generateWorkspacePath([config.folderSetting, config.mainScriptSetting]);

terminalUtils.RunCommandsInNewTerminal("Ampersand generate functional spec",
const terminal = this.builder.setName("Ampersand generate functional spec")
.setWorkingDir(['ampersand'])
.getTerminal();

terminalUtils.RunCommandsInExistingTerminal(terminal,
[`ampersand documentation --no-text --format docx ${mainScriptPath}`,
`ampersand documentation ${mainScriptPath} --format docx --no-graphics --language=NL --ConceptualAnalysis --verbosity debug`],
['ampersand']);
);
}
}
111 changes: 55 additions & 56 deletions src/commands/generatePrototypeCommand.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,75 @@
import { config, fileUtils, terminalUtils, zipUtils } from "../utils";
import * as vscode from 'vscode';
import * as path from 'path';
import { terminalBuilder } from "../builders/terminalBuilder";
import { manifest } from '../models/manifest';

export class generatePrototypeCommand {
private static portForwardTerminalPID: vscode.Terminal | undefined;
private portForwardTerminal: vscode.Terminal | undefined;
private builder : terminalBuilder = new terminalBuilder();

static GeneratePrototypeCommand(context: vscode.ExtensionContext)
private manifestFile : manifest;

constructor(context: vscode.ExtensionContext)
{
//Get extension path
if(context === undefined)
return;
this.manifestFile = new manifest(context.extensionPath);
}

const extensionPath: string = context.extensionPath;
public GeneratePrototypeCommand()
{
this.tryKillPortForwardedProcessAndTerminal();

const encodedZipContent:string|undefined = zipUtils.zipFolder(extensionPath);
vscode.workspace.fs.readFile(this.manifestFile.templateFileUri).then((data: Uint8Array) => this.replaceMarkers(data));
}

if(encodedZipContent === undefined)
private tryKillPortForwardedProcessAndTerminal()
{
if(this.portForwardTerminal === undefined)
return;

const encodedMainScript: string = btoa(config.mainScriptSetting as string);
const templateFileUri: vscode.Uri = vscode.Uri.file(path.join(extensionPath, 'assets', 'prototype-template.yaml'));
const manifestFileName: string = fileUtils.generateWorkspacePath(['ampersand', 'prototype.yaml']);

const manifestFileUri: vscode.Uri = vscode.Uri.file(manifestFileName);

tryKillPortForwardedProcessAndTerminal(this.portForwardTerminalPID);

vscode.workspace.fs.readFile(templateFileUri).then((data: Uint8Array) => replaceMarkers(data,encodedZipContent));
const buildTerminal = this.builder.setName("default name")
.setVisibility(false)
.getTerminal();

function tryKillPortForwardedProcessAndTerminal(terminalToKill : vscode.Terminal | undefined)
{
if(terminalToKill === undefined)
return;

//get the processID from the terminal that needs to be killed
terminalToKill.processId.then((terminalToKillPID: number | undefined) => {
let killerTerminal = terminalUtils.RunCommandsInNewTerminal("Kill processes",
[`PID=$(ps -ef | grep 'kubectl port-forward' | grep -v grep | awk '{print $2}')`,
`kill $PID`,
(`kill -9 ${terminalToKillPID}`)],
false);
//get the processID from the terminal that needs to be killed
this.portForwardTerminal.processId.then((terminalToKillPID: number | undefined) => {
terminalUtils.RunCommandsInExistingTerminal(buildTerminal,
[`PID=$(ps -ef | grep 'kubectl port-forward' | grep -v grep | awk '{print $2}')`,
`kill $PID`,
(`kill -9 ${terminalToKillPID}`)])

//Get own terminal PID to kill it later
killerTerminal.processId.then((killerTerminalPID: number|undefined) => {

//Kill self to cleanup
terminalUtils.RunCommandsInExistingTerminal(killerTerminal,[(`kill -9 ${killerTerminalPID}`)]);
});
//Get own terminal PID to kill it later
buildTerminal.processId.then((killerTerminalPID: number|undefined) => {

//Kill self to cleanup
terminalUtils.RunCommandsInExistingTerminal(buildTerminal,[(`kill -9 ${killerTerminalPID}`)]);
});
}
});
}

function replaceMarkers(data: Uint8Array, encodedZipContent:string)
{
const newData: Uint8Array = fileUtils.replaceMarkers(data, new Map<string, string>(
[
['{{zipFileContent}}', encodedZipContent],
['{{mainScript}}', encodedMainScript]
]
));
vscode.workspace.fs.writeFile(manifestFileUri, newData).then(runPrototypeCommand)
}
private replaceMarkers(data: Uint8Array)
{
const newData: Uint8Array = fileUtils.replaceMarkers(data, new Map<string, string>(
[
['{{zipFileContent}}', this.manifestFile.encodedZipContent],
['{{mainScript}}', this.manifestFile.encodedMainScript]
]
));

vscode.workspace.fs.writeFile(this.manifestFile.fileUri, newData).then(this.runPrototypeCommand)
}

function runPrototypeCommand()
{
const deployment: string = 'prototype';
const service: string = 'prototype';
private runPrototypeCommand()
{
const deployment: string = 'prototype';
const service: string = 'prototype';

this.portForwardTerminal = this.builder.setName("Run prototype in minikube")
.getTerminal();

generatePrototypeCommand.portForwardTerminalPID = terminalUtils.RunCommandsInNewTerminal("Run prototype in minikube",
[`kubectl apply -f ${manifestFileUri.fsPath}`,
`kubectl rollout status deployment/${deployment} --timeout=300s`,
`kubectl port-forward svc/${service} -n default 8000:80`,]);
}
terminalUtils.RunCommandsInExistingTerminal(this.portForwardTerminal,
[`kubectl apply -f ${this.manifestFile.fileUri.fsPath}`,
`kubectl rollout status deployment/${deployment} --timeout=300s`,
`kubectl port-forward svc/${service} -n default 8000:80`,]);
}
}
12 changes: 8 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ export function activate(context: vscode.ExtensionContext) {
ampersandVersionChecker.checkVersion();

watcherUtils.setupLastRunningWatcher(context);


const generatePrototypeCommandInstance = new generatePrototypeCommand(context);
const generateFunctionalSpecCommandInstance = new generateFunctionalSpecCommand();
const generateAtlasCommandInstance = new generateAtlasCommand();

pushDisposable(context, "extension.checkVersion", () => checkVersionCommand.checkVersionCommand())
pushDisposable(context, "extension.generateFunctionalSpec", () => generateFunctionalSpecCommand.GenerateFunctionalSpecCommand())
pushDisposable(context, "extension.generateAtlas", () => generateAtlasCommand.GenerateAtlasCommand())
pushDisposable(context, "extension.generatePrototype", () => generatePrototypeCommand.GeneratePrototypeCommand(context))
pushDisposable(context, "extension.generateFunctionalSpec", () => generateFunctionalSpecCommandInstance.GenerateFunctionalSpecCommand())
pushDisposable(context, "extension.generateAtlas", () => generateAtlasCommandInstance.GenerateAtlasCommand())
pushDisposable(context, "extension.generatePrototype", () => generatePrototypeCommandInstance.GeneratePrototypeCommand())

generateWorkingFolders();
createAndFillGitIgnore();
Expand Down
2 changes: 2 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./terminal";
export * from "./manifest";
25 changes: 25 additions & 0 deletions src/models/manifest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as vscode from 'vscode';
import { config, fileUtils, terminalUtils, zipUtils } from "../utils";
import * as path from 'path';

export class manifest{
private readonly _encodedZipContent: string | undefined;

public get encodedZipContent(): string {
return (this._encodedZipContent === undefined) ? "" : this._encodedZipContent;
}

public readonly encodedMainScript: string;
public readonly templateFileUri: vscode.Uri;
public readonly fileUri: vscode.Uri;

constructor(extensionPath: string)
{
this._encodedZipContent = zipUtils.zipFolder(extensionPath);
this.encodedMainScript = btoa(config.mainScriptSetting as string);
this.templateFileUri = vscode.Uri.file(path.join(extensionPath, 'assets', 'prototype-template.yaml'));

const fileName = fileUtils.generateWorkspacePath(['ampersand', 'prototype.yaml']);
this.fileUri = vscode.Uri.file(fileName)
}
}
7 changes: 7 additions & 0 deletions src/models/terminal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class terminalInfo
{
public terminalName: string = "default";

public terminalVisible: boolean = true;
public workingDir : string[] = [``];
}
11 changes: 6 additions & 5 deletions src/utils/terminalUtils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import vscode, { Terminal } from 'vscode';
import { fileUtils } from './fileUtils';
import { terminalInfo } from '../models/terminal';

export class terminalUtils{
static RunCommandsInNewTerminal(terminalName : string, runAmpersandCommands : string[], showTerminal: boolean = true, workingDir? : string[]) : Terminal
static RunCommandsInNewTerminal(terminalInfo : terminalInfo, runAmpersandCommands : string[]) : Terminal
{
if(workingDir === undefined)
workingDir = [''];
if(terminalInfo.workingDir === undefined)
terminalInfo.workingDir = [''];

let terminal = vscode.window.createTerminal({name:terminalName,cwd:fileUtils.generateWorkspacePath(workingDir)});
let terminal = vscode.window.createTerminal({name:terminalInfo.terminalName,cwd:fileUtils.generateWorkspacePath(terminalInfo.workingDir)});

this.RunCommandsInExistingTerminal(terminal,runAmpersandCommands);

if(showTerminal)
if(terminalInfo.terminalVisible)
terminal.show();

return terminal;
Expand Down

0 comments on commit 5270f26

Please sign in to comment.