Skip to content

Commit

Permalink
(GH-2) Added add sample build file command
Browse files Browse the repository at this point in the history
  • Loading branch information
gep13 committed Nov 22, 2017
1 parent 85f951e commit 2d89dd1
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 42 deletions.
57 changes: 22 additions & 35 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,48 +1,35 @@
// A launch configuration that compiles the extension and then opens it inside a new window
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"version": "0.1.0",
"configurations": [
{
"type": "PowerShell",
"name": "Launch Extension",
"type": "extensionHost",
"request": "launch",
"name": "PowerShell Launch Current File",
"script": "${file}",
"args": [],
"cwd": "${file}"
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [ "${workspaceRoot}/out/src/**/*.js" ],
"preLaunchTask": "npm"
},
{
"type": "PowerShell",
"name": "Launch Tests",
"type": "extensionHost",
"request": "launch",
"name": "PowerShell Launch Current File in Temporary Console",
"script": "${file}",
"args": [],
"cwd": "${file}",
"createTemporaryIntegratedConsole": true
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [ "${workspaceRoot}/out/test/**/*.js" ],
"preLaunchTask": "npm"
},
{
"type": "PowerShell",
"name": "Launch Package",
"type": "go",
"request": "launch",
"name": "PowerShell Launch Current File w/Args Prompt",
"script": "${file}",
"args": [
"${command:SpecifyScriptArgs}"
],
"cwd": "${file}"
},
{
"type": "PowerShell",
"request": "attach",
"name": "PowerShell Attach to Host Process",
"processId": "${command:PickPSHostProcess}",
"runspaceId": 1
},
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Interactive Session",
"cwd": "${workspaceRoot}"
"mode": "debug",
"program": "${workspaceRoot}"
}
]
}
14 changes: 8 additions & 6 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"editor.insertSpaces": true,
"editor.tabSize": 4,
"files.exclude": {
"out": false // set this to true to hide the "out" folder with the compiled JS files
},
"search.exclude": {
"?.?.?/": true
"**/node_modules": true,
"**/bower_components": true,
"logs/": true,
"out/": true
}
}
}
30 changes: 30 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team
// ${file}: the current opened file
// ${fileBasename}: the current opened file's basename
// ${fileDirname}: the current opened file's dirname
// ${fileExtname}: the current opened file's extension
// ${cwd}: the current working directory of the spawned process

// A task runner that calls a custom npm script that compiles the extension.
{
"version": "0.1.0",

// we want to run npm
"command": "npm",

// the command is a shell script
"isShellCommand": true,

// show the output window only if unrecognized errors occur.
"showOutput": "silent",

// we run the custom script "compile" as defined in package.json
"args": ["run", "compile", "--loglevel", "silent"],

// The tsc compiler is started in watching mode
"isBackground": true,

// use the standard tsc in watch mode problem matcher to find compile problems in the output.
"problemMatcher": "$tsc-watch"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"activationEvents": [
"onCommand:psake.buildFile"
],
"main": "./out/src/psakeMain",
"main": "./out/psakeMain",
"contributes": {
"snippets": [
{
Expand Down
51 changes: 51 additions & 0 deletions src/buildFile/psakeBuildFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { DEFAULT_SCRIPT_NAME } from "../constants";

export class psakeBuildFile {

constructor(public scriptName: string = DEFAULT_SCRIPT_NAME) { }

public getTargetPath(): string {
if (vscode.workspace.rootPath) {
return path.join(vscode.workspace.rootPath, this.scriptName);
}

return "";
}

public create(): Thenable<boolean> {
return new Promise((resolve, reject) => {
try {
let buildFile = fs.createWriteStream(this.getTargetPath(), {
flags: 'a'
});

buildFile.write('Task default -Depends Test\n');
buildFile.write('\n');
buildFile.write('Task Test -Depends Compile, Clean {\n');
buildFile.write('\t"This is a test"\n');
buildFile.write('}\n');
buildFile.write('\n');
buildFile.write('Task Compile -Depends Clean {\n');
buildFile.write('\t"Compile"\n');
buildFile.write('}\n');
buildFile.write('\n');
buildFile.write('Task Clean {\n');
buildFile.write('\t"Clean"\n');
buildFile.write('}\n');
buildFile.end();

buildFile.on('finish', function() {
vscode.workspace.openTextDocument(buildFile.path.toString()).then((document) => {
vscode.window.showTextDocument(document);
});
resolve(true);
});
} catch (error) {
reject(false);
}
});
}
}
45 changes: 45 additions & 0 deletions src/buildFile/psakeBuildFileCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { window, workspace } from 'vscode';
import { psakeBuildFile } from './psakeBuildFile';
import { messages, utils } from "../shared";
import { DEFAULT_SCRIPT_NAME, CANCEL } from "../constants";

export async function installBuildFileCommand() {
// Check if there is an open folder in workspace
if (workspace.rootPath === undefined) {
window.showErrorMessage('You have not yet opened a folder.');
return;
}

var name = await window.showInputBox({
placeHolder: messages.PROMPT_SCRIPT_NAME,
value: DEFAULT_SCRIPT_NAME
});

if (!name) {
window.showWarningMessage('No script name provided! Try again and make sure to provide a file name.');
return;
}

var result = await installBuildFile(name);

if (result) {
window.showInformationMessage("Sample psake build file successfully created.");
} else {
window.showErrorMessage("Error creating sample psake buile file.");
}
}

export async function installBuildFile(fileName: string): Promise<boolean> {
// Create the buildFile object
let buildFile = new psakeBuildFile(fileName);

var targetPath = buildFile.getTargetPath();
var ready = await utils.checkForExisting(targetPath);

if (!ready) {
Promise.reject(CANCEL);
}

var result = await buildFile.create();
return result;
}
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const CANCEL = '__CANCEL__';
export const DEFAULT_SCRIPT_NAME = 'psakefile.ps1';
export const ERROR_NO_WORKSPACE = 'You have not yet opened a folder.';
export const OUTPUT_CHANNEL_NAME = 'psake Workspace';
9 changes: 9 additions & 0 deletions src/psakeMain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as vscode from 'vscode';
import { installBuildFileCommand } from './buildFile/psakeBuildFileCommand';

export function activate(context: vscode.ExtensionContext): void {
// Register the build file command.
context.subscriptions.push(vscode.commands.registerCommand('psake.buildFile', async () => {
installBuildFileCommand();
}));
}
5 changes: 5 additions & 0 deletions src/shared/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as logger from './log';
import * as utils from './utility';
import * as messages from './messages';

export { logger, messages, utils };
29 changes: 29 additions & 0 deletions src/shared/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { window, OutputChannel } from 'vscode';
import { OUTPUT_CHANNEL_NAME } from "../constants";

let channel: OutputChannel;

export function logToOutput(...items: string[]): void {
var channel = getChannel(OUTPUT_CHANNEL_NAME);
items.forEach(item => {
channel.appendLine(item);
});
}

export function logError(error: string, notify: boolean = true) {
var channel = getChannel(OUTPUT_CHANNEL_NAME);
channel.appendLine('Error encountered during psake operation.')
channel.appendLine(`E: ${error}`);

if (notify) {
window.showErrorMessage(error);
}
}

function getChannel(name: string): OutputChannel {
if (!channel) {
channel = window.createOutputChannel(name);
}

return channel;
}
1 change: 1 addition & 0 deletions src/shared/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const PROMPT_SCRIPT_NAME = 'Enter the name for your new build script';
12 changes: 12 additions & 0 deletions src/shared/utility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { window } from 'vscode';
import * as fs from 'fs';

export async function checkForExisting(path: string): Promise<boolean> {
if (fs.existsSync(path)) {
var message = `Overwrite the existing \'${path}\' file in this folder?`;
var option = await window.showWarningMessage(message, 'Overwrite');
return option === 'Overwrite';
}

return true;
}
23 changes: 23 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "out",
"lib": [
"es6"
],
"sourceMap": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"noFallthroughCasesInSwitch": true,
"allowUnreachableCode": false,
"noImplicitAny": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"noImplicitThis": true,
"strict": true
},
"exclude": [
"node_modules"
]
}

0 comments on commit 2d89dd1

Please sign in to comment.