Catch and report more errors in Kiss-vscode tryLoadConfig

This commit is contained in:
2021-10-13 20:11:32 -04:00
parent e9d5b4f65f
commit 9016870d65
2 changed files with 99 additions and 84 deletions

View File

@@ -5,19 +5,27 @@
// TODO pass these aliases to the KissState of "eval kiss expression" // TODO pass these aliases to the KissState of "eval kiss expression"
// output // output
(defalias &call infoMessage Vscode.window.showInformationMessage) (defAlias &call infoMessage Vscode.window.showInformationMessage)
(defalias &call warningMessage Vscode.window.showWarningMessage) (defAlias &call warningMessage Vscode.window.showWarningMessage)
(defalias &call errorMessage Vscode.window.showErrorMessage) (defAlias &call errorMessage Vscode.window.showErrorMessage)
// input // input
(defalias &call inputBox Vscode.window.showInputBox) (defAlias &call inputBox Vscode.window.showInputBox)
(defalias &call quickPick Vscode.window.showQuickPick) (defAlias &call quickPick Vscode.window.showQuickPick)
// commands // commands
(defalias &call executeCommand Vscode.commands.executeCommand) (defAlias &call executeCommand Vscode.commands.executeCommand)
(function repeatCommand [command times]
(let [iteration
->[&opt _] (executeCommand command)
&mut promise
(iteration)]
(doFor i (range (- times 1))
(set promise (promise.then iteration)))
promise))
// ui // ui
(defalias &ident activeTextEditor Vscode.window.activeTextEditor) (defAlias &ident activeTextEditor Vscode.window.activeTextEditor)
/** /**
* Helper functions * Helper functions

View File

@@ -9,9 +9,32 @@
(var &mut builtinConfigDir "") (var &mut builtinConfigDir "")
(var &mut :KissConfig config null) (var &mut :KissConfig config null)
// This has to be a macro so it can return from tryLoadConfig
(defMacro trySpawnSync [command args options onError]
`(let [command ,command
args ,args
result (ChildProcess.spawnSync command args ,options)]
(if result.error
(throw "Error $result.error from $command $args: $result.stdout $result.stderr")
(case result.status
(0 null)
(errCode
(,onError "Error code $errCode from $command $args: $result.stdout $result.stderr")
(return))))))
(function :Void tryLoadConfig [&opt :String text] (function :Void tryLoadConfig [&opt :String text]
// TODO if a config object is active and a shortcut panel is open, dispose the panel before we lose the handle in the current config object // TODO if a config object is active and a shortcut panel is open, dispose the panel before we lose the handle in the current config object
(let [handleConfigFailure
->errorMessage {
(FileSystem.deleteDirectory activeConfigDir)
(when (FileSystem.exists lastConfigDir)
(FileSystem.rename lastConfigDir activeConfigDir))
(#if test
// If there's a build error when testing, throw a test failure
(throw errorMessage)
// If there's a build error at runtime, tell the user
(Vscode.window.showErrorMessage errorMessage))
}]
// If a backup exists, delete it // If a backup exists, delete it
(when (FileSystem.exists lastConfigDir) (when (FileSystem.exists lastConfigDir)
(FileSystem.deleteDirectory lastConfigDir)) (FileSystem.deleteDirectory lastConfigDir))
@@ -46,16 +69,15 @@
(FileSystem.createDirectory (FileSystem.createDirectory
(joinPath activeConfigDir folder))) (joinPath activeConfigDir folder)))
// install all Haxe dependencies: // install all Haxe dependencies:
(assert (= 0 .status (ChildProcess.spawnSync "haxelib" ["install" "all"] (object cwd activeConfigDir)))) (trySpawnSync "haxelib" ["install" "all" "--always"] (object cwd activeConfigDir) handleConfigFailure)
// install all Node dependencies: // install all Node dependencies:
(when (FileSystem.exists (joinPath activeConfigDir "package.json")) (when (FileSystem.exists (joinPath activeConfigDir "package.json"))
(assert (= 0 .status (if (= "Windows" (Sys.systemName)) (if (= "Windows" (Sys.systemName))
(ChildProcess.spawnSync "cmd.exe" ["/c" "npm" "install"] (object cwd activeConfigDir)) (trySpawnSync "cmd.exe" ["/c" "npm" "install"] (object cwd activeConfigDir) handleConfigFailure)
(ChildProcess.spawnSync "npm" ["install"] (object cwd activeConfigDir)))))) (trySpawnSync "npm" ["install"] (object cwd activeConfigDir) handleConfigFailure)))
// Run the haxe compiler: // Run the haxe compiler:
(let [buildResult (trySpawnSync "haxe" ["build.hxml"] (object cwd activeConfigDir) handleConfigFailure)
(ChildProcess.spawnSync "haxe" ["build.hxml"] (object cwd activeConfigDir))]
(if (and !buildResult.error (= 0 buildResult.status))
// Successful compilation! // Successful compilation!
(#if test (#if test
// When testing: // When testing:
@@ -74,22 +96,7 @@
(config.prepareInterp) (config.prepareInterp)
// User-defined init: // User-defined init:
(config.init) (config.init)
(Vscode.window.showInformationMessage "Config loaded successfully!"))) (Vscode.window.showInformationMessage "Config loaded successfully!"))))))
// Failed compilation!
(begin
(FileSystem.deleteDirectory activeConfigDir)
(when (FileSystem.exists lastConfigDir)
(FileSystem.rename lastConfigDir activeConfigDir))
(let [errorMessage
(+ "Config failed to compile: "
(if buildResult.error
#| "" + buildResult.error|#
#| "" + buildResult.stderr |#))]
(#if test
// If there's a build error when testing, throw a test failure
(throw errorMessage)
// If there's a build error at runtime, tell the user
(Vscode.window.showErrorMessage errorMessage))))))))
(#unless test (#unless test
(function _activate [:ExtensionContext context] (function _activate [:ExtensionContext context]