From 024890fb87b1e2236c308ea730f1f59adf98ee42 Mon Sep 17 00:00:00 2001 From: Andreea Isac Date: Fri, 17 Jul 2020 09:33:30 -0700 Subject: [PATCH] Preconfigure support --- package.json | 17 ++++++++++++ src/configuration.ts | 64 +++++++++++++++++++++++++++++++++++++++----- src/extension.ts | 7 +++++ src/make.ts | 49 +++++++++++++++++++++++++++++++-- 4 files changed, 129 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index d507cea..4d1b6ff 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "onCommand:makefile.launchDebug", "onCommand:makefile.launchRun", "onCommand:makefile.configure", + "onCommand:makefile.preConfigure", "workspaceContains:makefile", "workspaceContains:Makefile" ], @@ -80,6 +81,10 @@ { "command": "makefile.configure", "title": "Makefile: Configure" + }, + { + "command": "makefile.preConfigure", + "title": "Makefile: Preconfigure" } ], "configuration": { @@ -280,6 +285,18 @@ "default": true, "description": "Automatically configure Makefile project directories after relevant operations, like change build configuration or makefile target", "scope": "resource" + }, + "makefile.preconfigureScript": { + "type": "string", + "description": "The path to the script that needs to be run at least once before configure", + "default": null, + "scope": "resource" + }, + "makefile.alwaysPreconfigure": { + "type": "boolean", + "description": "Always run the preconfigure script before configure", + "default": false, + "scope": "resource" } } } diff --git a/src/configuration.ts b/src/configuration.ts index d2c9f23..c808c8b 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -178,6 +178,35 @@ export function readExtensionLog(): void { } } +let preconfigureScript: string | undefined; +export function getPreconfigureScript(): string | undefined { return preconfigureScript; } +export function setPreconfigureScript(path: string): void { preconfigureScript = path; } + +// Read from settings the path to a script file that needs to have been run at least once +// before a sucessful configure of this project. +export function readPreconfigureScript(): void { + let workspaceConfiguration: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("makefile"); + preconfigureScript = workspaceConfiguration.get("preconfigureScript"); + if (preconfigureScript) { + preconfigureScript = util.resolvePathToRoot(preconfigureScript); + logger.message('Found pre-configure script defined as {0}', preconfigureScript); + if (!util.checkFileExistsSync(preconfigureScript)) { + logger.message("Preconfigure script not found on disk."); + } + } +} + +let alwaysPreconfigure: boolean; +export function getAlwaysPreconfigure(): boolean { return alwaysPreconfigure; } +export function setAlwaysPreconfigure(path: boolean): void { alwaysPreconfigure = path; } + +// Read from settings whether the preconfigure step is supposed to be executed +// always before the configure operation. +export function readAlwaysPreconfigure(): void { + let workspaceConfiguration: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("makefile"); + alwaysPreconfigure = workspaceConfiguration.get("alwaysPreconfigure", false); +} + let dryrunCache: string; export function getDryrunCache(): string { return dryrunCache; } export function setDryrunCache(path: string): void { dryrunCache = path; } @@ -518,6 +547,8 @@ export function initFromStateAndSettings(): void { readMakePath(); readMakefilePath(); readBuildLog(); + readPreconfigureScript(); + readAlwaysPreconfigure(); readDryRunSwitches(); readCurrentMakefileConfiguration(); readCurrentMakefileConfigurationCommand(); @@ -588,20 +619,43 @@ export function initFromStateAndSettings(): void { readDebugConfig(); } - let updatedBuildLog : string | undefined = workspaceConfiguration.get("buildLog", "./build.log"); - if (util.resolvePathToRoot(updatedBuildLog) !== buildLog) { + let updatedBuildLog : string | undefined = workspaceConfiguration.get("buildLog"); + if (updatedBuildLog) { + updatedBuildLog = util.resolvePathToRoot(updatedBuildLog); + } + if (updatedBuildLog !== buildLog) { updateConfigProviderAfterEdit = true; logger.message("makefile.buildLog setting changed."); readBuildLog(); } - let updatedExtensionLog : string | undefined = workspaceConfiguration.get("extensionLog", "./extension.log"); - if (util.resolvePathToRoot(updatedExtensionLog) !== extensionLog) { + let updatedExtensionLog : string | undefined = workspaceConfiguration.get("extensionLog"); + if (updatedExtensionLog) { + updatedExtensionLog = util.resolvePathToRoot(updatedExtensionLog); + } + if (updatedExtensionLog !== extensionLog) { // No IntelliSense update needed. logger.message("makefile.extensionLog setting changed."); readExtensionLog(); } + let updatedPreconfigureScript : string | undefined = workspaceConfiguration.get("preconfigureScript"); + if (updatedPreconfigureScript) { + updatedPreconfigureScript = util.resolvePathToRoot(updatedPreconfigureScript); + } + if (updatedPreconfigureScript !== preconfigureScript) { + // No IntelliSense update needed. + logger.message("makefile.preconfigureScript setting changed."); + readPreconfigureScript(); + } + + let updatedAlwaysPreconfigure : boolean = workspaceConfiguration.get("alwaysPreconfigure", false); + if (updatedAlwaysPreconfigure !== alwaysPreconfigure) { + // No IntelliSense update needed. + logger.message("makefile.alwaysPreconfigure setting changed."); + readAlwaysPreconfigure(); + } + let updatedDryrunCache : string | undefined = workspaceConfiguration.get("dryrunCache", "./dryrunCache.log"); if (util.resolvePathToRoot(updatedDryrunCache) !== dryrunCache) { updateConfigProviderAfterEdit = true; @@ -796,7 +850,6 @@ export async function setNewLaunchConfiguration(): Promise { await util.spawnChildProcess(configurationMakeCommand, makeArgs, vscode.workspace.rootPath || "", stdout, stderr, closing); } catch (error) { logger.message(error); - return; } } @@ -876,7 +929,6 @@ export async function setNewTarget(): Promise { await util.spawnChildProcess(configurationMakeCommand, makeArgs, vscode.workspace.rootPath || "", stdout, stderr, closing); } catch (error) { logger.message(error); - return; } } diff --git a/src/extension.ts b/src/extension.ts index 78837cb..762e1b2 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -158,9 +158,16 @@ export async function activate(context: vscode.ExtensionContext): Promise })); context.subscriptions.push(vscode.commands.registerCommand('makefile.configure', () => { + if (configuration.getAlwaysPreconfigure()) { + make.runPreconfigureScript(); + } make.parseBuildOrDryRun(); })); + context.subscriptions.push(vscode.commands.registerCommand('makefile.preConfigure', () => { + make.runPreconfigureScript(); + })); + configuration.readLoggingLevel(); configuration.readExtensionLog(); diff --git a/src/make.ts b/src/make.ts index ea3a5b9..bbb1c7f 100644 --- a/src/make.ts +++ b/src/make.ts @@ -45,7 +45,6 @@ export async function buildCurrentTarget(): Promise { } catch (error) { // No need for notification popup, since the build result is visible already in the output channel logger.message(error); - return; } } export function parseBuild(): boolean { @@ -119,6 +118,52 @@ export async function parseBuildOrDryRun(): Promise { await util.spawnChildProcess(configuration.getConfigurationMakeCommand(), makeArgs, vscode.workspace.rootPath || "", stdout, stderr, closing); } catch (error) { logger.message(error); - return; + } +} + +export async function runPreconfigureScript(): Promise { + let script: string | undefined = configuration.getPreconfigureScript(); + if (!script || !util.checkFileExistsSync(script)) { + vscode.window.showErrorMessage("Could not find pre-configure script."); + logger.message("Make sure a pre-configuration script path is defined with makefile.preconfigureScript and that it exists on disk."); + return; + } + + let scriptArgs: string[] = []; + let runCommand: string; + if (process.platform === 'win32') { + runCommand = "cmd"; + scriptArgs.push("/c"); + scriptArgs.push(script); + } else { + runCommand = "/bin/bash"; + scriptArgs.push("-c"); + scriptArgs.push(`"source ${script}"`); + } + + try { + let stdoutStr: string = ""; + let stderrStr: string = ""; + + let stdout : any = (result: string): void => { + stdoutStr += result; + }; + + let stderr : any = (result: string): void => { + stderrStr += result; + }; + + let closing : any = (retCode: number, signal: string): void => { + if (retCode === 0) { + logger.message("The preconfigure script run successfully."); + } else { + logger.message("The preconfigure script failed. This project may not configure successfully."); + logger.message(stderrStr); + } + }; + + await util.spawnChildProcess(runCommand, scriptArgs, vscode.workspace.rootPath || "", stdout, stderr, closing); + } catch (error) { + logger.message(error); } }