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?
(when (FileSystem.exists activeConfigDir)
(FileSystem.rename activeConfigDir lastConfigDir))
// Choose where to find the custom config
(let [customConfigDir (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
(Path.join [builtinConfigDir "default"])))
// TODO this isn't recursive, so it doesn't allow the user to organize their config with folders
// TODO it would also attempt to copy over any documentation cache in the .kiss directory, but does File.copy work on directories?
customConfigFiles customConfigFiles
(FileSystem.readDirectory customConfigDir)] (FileSystem.readDirectory customConfigDir)]
// Copy the custom config files to the active config directory (FileSystem.createDirectory activeConfigDir)
// Copy the boilerplate config files to the active config directory
(doFor file ["build.hxml" "KissConfig.hx" "KissConfig.kiss"]
(File.copy
(Path.join [builtinConfigDir file])
(Path.join [activeConfigDir file])))
// Copy the user's custom config files to the active config directory
(doFor file customConfigFiles (doFor file customConfigFiles
(File.copy (File.copy
(Path.join [customConfigDir file]) (Path.join [customConfigDir file])
(Path.join [activeConfigDir 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 (let [buildResult
(ChildProcess.spawnSync "haxe" ["build.hxml"] (object cwd activeConfigDir))] (ChildProcess.spawnSync "haxe" ["build.hxml"] (object cwd activeConfigDir))]
(if (and !buildResult.error (= 0 buildResult.status)) (if (and !buildResult.error (= 0 buildResult.status))
// Successful compilation! require the config.js package. // 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,19 +64,27 @@
(config.registerCommand "[r]eload Kiss config" tryLoadConfig) (config.registerCommand "[r]eload Kiss config" tryLoadConfig)
(config.prepareInterp) (config.prepareInterp)
// User-defined init: // User-defined init:
(#unless test
(config.init) (config.init)
(Vscode.window.showInformationMessage "Config loaded successfully!")) (Vscode.window.showInformationMessage "Config loaded successfully!"))))
// If there's a build error, restore the config.js backup // Failed compilation!
(begin (begin
(when (FileSystem.exists backupConfigPath) (FileSystem.deleteDirectory activeConfigDir)
(FileSystem.rename backupConfigPath activeConfigPath)) (when (FileSystem.exists lastConfigDir)
(Vscode.window.showErrorMessage (FileSystem.rename lastConfigDir activeConfigDir))
(let [errorMessage
(+ "Config failed to compile: " (+ "Config failed to compile: "
(if buildResult.error (if buildResult.error
#| "" + buildResult.error|# #| "" + buildResult.error|#
#| "" + buildResult.stderr |#))))))))) #| "" + 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
(defun _activate [:ExtensionContext context]
(context.subscriptions.push (context.subscriptions.push
(Vscode.commands.registerCommand (Vscode.commands.registerCommand
"kiss.reloadConfig" "kiss.reloadConfig"
@@ -87,5 +116,15 @@
// 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"