Kiss-vscode build the example config when testing

This commit is contained in:
2021-07-19 15:10:13 -06:00
parent 6ee2b92b62
commit 755660dfdc
7 changed files with 110 additions and 69 deletions

View File

@@ -149,6 +149,7 @@ class Kiss {
public static function load(kissFile:String, k:KissState, ?loadingDirectory:String) { public static function load(kissFile:String, k:KissState, ?loadingDirectory:String) {
if (loadingDirectory == null) if (loadingDirectory == null)
loadingDirectory = k.loadingDirectory; loadingDirectory = k.loadingDirectory;
var fullPath = Path.join([loadingDirectory, kissFile]); var fullPath = Path.join([loadingDirectory, kissFile]);
if (k.loadedFiles.exists(fullPath)) { if (k.loadedFiles.exists(fullPath)) {
return; return;

View File

@@ -1,6 +1,4 @@
bin bin
*.vsix *.vsix
config/config*.js _activeConfig/
config/args.hxml _lastActiveConfig/
config/Config.kiss
config/import.hx

View File

@@ -8,4 +8,4 @@
-D analyzer-optimize -D analyzer-optimize
-D js-es=6 -D js-es=6
-debug -debug
Main --main Main

View File

@@ -1 +0,0 @@
*.js

View File

@@ -1,4 +1,6 @@
#if !test
import vscode.*; import vscode.*;
#end
import Sys; import Sys;
import sys.io.File; import sys.io.File;
import sys.FileSystem; import sys.FileSystem;
@@ -26,8 +28,10 @@ typedef KissConfig = {
@:build(kiss.Kiss.build()) @:build(kiss.Kiss.build())
class Main { class Main {
// TODO support EMeta(s:MetadataEntry, e:Expr) via Kiss so this signature can be moved to Main.kiss // TODO support EMeta(s:MetadataEntry, e:Expr) via Kiss so this signature can be moved to Main.kiss
#if !test
@:expose("activate") @:expose("activate")
static function activate(context:ExtensionContext) { static function activate(context:ExtensionContext) {
_activate(context); _activate(context);
} }
#end
} }

View File

@@ -3,35 +3,56 @@
(or (Sys.getEnv "MSYSHOME") (Sys.getEnv "HOME") (Sys.getEnv "UserProfile")) (or (Sys.getEnv "MSYSHOME") (Sys.getEnv "HOME") (Sys.getEnv "UserProfile"))
".kiss"])) ".kiss"]))
(defun timeStamp []
(.replace (.replace (.toString (Date.now)) ":" "-") " " "-"))
(defvar &mut activeConfigDir "") (defvar &mut activeConfigDir "")
(defvar &mut lastConfigDir "")
(defvar &mut builtinConfigDir "")
(defvar &mut :KissConfig config null) (defvar &mut :KissConfig config null)
(defun :Void tryLoadConfig [&opt :String text] (defun :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 [activeConfigPath (Path.join [activeConfigDir "config.js"])
backupConfigPath (Path.join [activeConfigDir (+ "config" (timeStamp) ".js")])] // If a backup exists, delete it
// Backup existing config.js (when (FileSystem.exists lastConfigDir)
(when (FileSystem.exists activeConfigPath) (FileSystem.deleteDirectory lastConfigDir))
(FileSystem.rename activeConfigPath backupConfigPath)) // Backup currently active config
// Supply the default (empty) config if the user doesn't have one // TODO maybe also expose that backup to the user via a "rollback .kiss" command so they can rollback their config without using Git?
(let [customConfigDir (when (FileSystem.exists activeConfigDir)
(FileSystem.rename activeConfigDir lastConfigDir))
// Choose where to find the custom config
(let [customConfigDir
(#if test
// When running unit tests, build the example config
(Path.join [builtinConfigDir "example"])
// When running for real, try the user's config directory
(if (FileSystem.exists (userConfigDir)) (if (FileSystem.exists (userConfigDir))
(userConfigDir) (userConfigDir)
(Path.join [activeConfigDir "default"])) // Supply the default (empty) config if the user doesn't have one
customConfigFiles (Path.join [builtinConfigDir "default"])))
(FileSystem.readDirectory customConfigDir)]
// Copy the custom config files to the active config directory // TODO this isn't recursive, so it doesn't allow the user to organize their config with folders
(doFor file customConfigFiles // TODO it would also attempt to copy over any documentation cache in the .kiss directory, but does File.copy work on directories?
(File.copy customConfigFiles
(Path.join [customConfigDir file]) (FileSystem.readDirectory customConfigDir)]
(Path.join [activeConfigDir file]))) (FileSystem.createDirectory activeConfigDir)
(let [buildResult // Copy the boilerplate config files to the active config directory
(ChildProcess.spawnSync "haxe" ["build.hxml"] (object cwd activeConfigDir))] (doFor file ["build.hxml" "KissConfig.hx" "KissConfig.kiss"]
(if (and !buildResult.error (= 0 buildResult.status)) (File.copy
// Successful compilation! require the config.js package. (Path.join [builtinConfigDir file])
(Path.join [activeConfigDir file])))
// Copy the user's custom config files to the active config directory
(doFor file customConfigFiles
(File.copy
(Path.join [customConfigDir file])
(Path.join [activeConfigDir file])))
// When running from unit tests, install all dependencies in the example config:
(#when test (ChildProcess.spawnSync "haxelib" ["install" "all"] (object cwd activeConfigDir)))
// Run the haxe compiler:
(let [buildResult
(ChildProcess.spawnSync "haxe" ["build.hxml"] (object cwd activeConfigDir))]
(if (and !buildResult.error (= 0 buildResult.status))
// Successful compilation!
(#unless test
// Require the config.js package.
// But since Node.require() caches modules by filename, // But since Node.require() caches modules by filename,
// copy it to a unique path first so hot-reloading works properly. // copy it to a unique path first so hot-reloading works properly.
(let [activeConfigFile (Path.join [activeConfigDir "config.js"]) (let [activeConfigFile (Path.join [activeConfigDir "config.js"])
@@ -43,49 +64,67 @@
(config.registerCommand "[r]eload Kiss config" tryLoadConfig) (config.registerCommand "[r]eload Kiss config" tryLoadConfig)
(config.prepareInterp) (config.prepareInterp)
// User-defined init: // User-defined init:
(config.init) (#unless test
(Vscode.window.showInformationMessage "Config loaded successfully!")) (config.init)
// If there's a build error, restore the config.js backup (Vscode.window.showInformationMessage "Config loaded successfully!"))))
(begin // Failed compilation!
(when (FileSystem.exists backupConfigPath) (begin
(FileSystem.rename backupConfigPath activeConfigPath)) (FileSystem.deleteDirectory activeConfigDir)
(Vscode.window.showErrorMessage (when (FileSystem.exists lastConfigDir)
(+ "Config failed to compile: " (FileSystem.rename lastConfigDir activeConfigDir))
(if buildResult.error (let [errorMessage
#| "" + buildResult.error|# (+ "Config failed to compile: "
#| "" + buildResult.stderr |#))))))))) (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))))))))
(defun _activate [:ExtensionContext context] (#unless test
(context.subscriptions.push (defun _activate [:ExtensionContext context]
(Vscode.commands.registerCommand (context.subscriptions.push
"kiss.reloadConfig" (Vscode.commands.registerCommand
tryLoadConfig)) "kiss.reloadConfig"
tryLoadConfig))
(context.subscriptions.push (context.subscriptions.push
(Vscode.commands.registerCommand (Vscode.commands.registerCommand
"kiss.runCommand" "kiss.runCommand"
(lambda :Void [] (lambda :Void []
(if config (if config
(.runCommand (the KissConfig config)) (.runCommand (the KissConfig config))
(Vscode.window.showErrorMessage "Can't run commands! No config is loaded."))))) (Vscode.window.showErrorMessage "Can't run commands! No config is loaded.")))))
(context.subscriptions.push (context.subscriptions.push
(Vscode.commands.registerCommand (Vscode.commands.registerCommand
"kiss.runLastCommand" "kiss.runLastCommand"
(lambda :Void [] (lambda :Void []
(if config (if config
(.runLastCommand (the KissConfig config)) (.runLastCommand (the KissConfig config))
(Vscode.window.showErrorMessage "Can't run commands! No config is loaded."))))) (Vscode.window.showErrorMessage "Can't run commands! No config is loaded.")))))
(context.subscriptions.push (context.subscriptions.push
(Vscode.commands.registerCommand (Vscode.commands.registerCommand
"kiss.runKeyboardShortcut" "kiss.runKeyboardShortcut"
(lambda :Void [] (lambda :Void []
(if config (if config
(.runKeyboardShortcut (the KissConfig config)) (.runKeyboardShortcut (the KissConfig config))
(Vscode.window.showErrorMessage "Can't run commands! No config is loaded."))))) (Vscode.window.showErrorMessage "Can't run commands! No config is loaded.")))))
// TODO overload Prelude.print to use showInformationMessage // TODO overload Prelude.print to use showInformationMessage
(set activeConfigDir (Path.join [context.extensionPath "config"])) (set builtinConfigDir (Path.join [context.extensionPath "config"]))
(tryLoadConfig)) (set activeConfigDir (Path.join [context.extensionPath "_activeConfig"]))
(set lastConfigDir (Path.join [context.extensionPath "_lastActiveConfig"]))
(tryLoadConfig)))
(defun :Void main []
(#when test
// TODO
(set builtinConfigDir "config")
(set activeConfigDir "_activeConfig")
(set lastConfigDir "_lastActiveConfig")
(tryLoadConfig)))

View File

@@ -1,3 +1,3 @@
#! /bin/bash #! /bin/bash
haxe build.hxml haxe build.hxml && haxe -D test build.hxml -cmd "node bin/extension.js"