From 2d89dd1eaf0a137d73bf2e8592deaafb0552af0e Mon Sep 17 00:00:00 2001 From: Gary Ewan Park Date: Wed, 22 Nov 2017 20:47:24 +0000 Subject: [PATCH] (GH-2) Added add sample build file command --- .vscode/launch.json | 57 ++++++++++---------------- .vscode/settings.json | 14 ++++--- .vscode/tasks.json | 30 ++++++++++++++ package.json | 2 +- src/buildFile/psakeBuildFile.ts | 51 +++++++++++++++++++++++ src/buildFile/psakeBuildFileCommand.ts | 45 ++++++++++++++++++++ src/constants.ts | 4 ++ src/psakeMain.ts | 9 ++++ src/shared/index.ts | 5 +++ src/shared/log.ts | 29 +++++++++++++ src/shared/messages.ts | 1 + src/shared/utility.ts | 12 ++++++ tsconfig.json | 23 +++++++++++ 13 files changed, 240 insertions(+), 42 deletions(-) create mode 100644 .vscode/tasks.json create mode 100644 src/buildFile/psakeBuildFile.ts create mode 100644 src/buildFile/psakeBuildFileCommand.ts create mode 100644 src/constants.ts create mode 100644 src/psakeMain.ts create mode 100644 src/shared/index.ts create mode 100644 src/shared/log.ts create mode 100644 src/shared/messages.ts create mode 100644 src/shared/utility.ts create mode 100644 tsconfig.json diff --git a/.vscode/launch.json b/.vscode/launch.json index de0c692..269d76b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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}" } ] } diff --git a/.vscode/settings.json b/.vscode/settings.json index 8f0df1f..2ab04be 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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 } -} \ No newline at end of file +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..a971688 --- /dev/null +++ b/.vscode/tasks.json @@ -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" +} \ No newline at end of file diff --git a/package.json b/package.json index e740875..9b821ad 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "activationEvents": [ "onCommand:psake.buildFile" ], - "main": "./out/src/psakeMain", + "main": "./out/psakeMain", "contributes": { "snippets": [ { diff --git a/src/buildFile/psakeBuildFile.ts b/src/buildFile/psakeBuildFile.ts new file mode 100644 index 0000000..c20be99 --- /dev/null +++ b/src/buildFile/psakeBuildFile.ts @@ -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 { + 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); + } + }); + } +} diff --git a/src/buildFile/psakeBuildFileCommand.ts b/src/buildFile/psakeBuildFileCommand.ts new file mode 100644 index 0000000..c0f525b --- /dev/null +++ b/src/buildFile/psakeBuildFileCommand.ts @@ -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 { + // 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; +} diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..8f96a82 --- /dev/null +++ b/src/constants.ts @@ -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'; diff --git a/src/psakeMain.ts b/src/psakeMain.ts new file mode 100644 index 0000000..7e67a97 --- /dev/null +++ b/src/psakeMain.ts @@ -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(); + })); +} diff --git a/src/shared/index.ts b/src/shared/index.ts new file mode 100644 index 0000000..fb17f81 --- /dev/null +++ b/src/shared/index.ts @@ -0,0 +1,5 @@ +import * as logger from './log'; +import * as utils from './utility'; +import * as messages from './messages'; + +export { logger, messages, utils }; diff --git a/src/shared/log.ts b/src/shared/log.ts new file mode 100644 index 0000000..931b51b --- /dev/null +++ b/src/shared/log.ts @@ -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; +} diff --git a/src/shared/messages.ts b/src/shared/messages.ts new file mode 100644 index 0000000..dae1fc9 --- /dev/null +++ b/src/shared/messages.ts @@ -0,0 +1 @@ +export const PROMPT_SCRIPT_NAME = 'Enter the name for your new build script'; diff --git a/src/shared/utility.ts b/src/shared/utility.ts new file mode 100644 index 0000000..ebfcace --- /dev/null +++ b/src/shared/utility.ts @@ -0,0 +1,12 @@ +import { window } from 'vscode'; +import * as fs from 'fs'; + +export async function checkForExisting(path: string): Promise { + 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; +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..11fa72b --- /dev/null +++ b/tsconfig.json @@ -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" + ] +} \ No newline at end of file