From a18a4d8150be7bdc710912a01b140cf388d5d1a6 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Fri, 23 Jul 2021 14:58:36 -0600 Subject: [PATCH] Variadic joinPath. Close #20 --- kiss/src/kiss/Kiss.hx | 1 + kiss/src/kiss/Prelude.hx | 9 ++++++- projects/kiss-vscode/config/KissConfig.kiss | 4 +-- projects/kiss-vscode/src/Main.kiss | 26 +++++++++---------- .../nat-archive-tool/src/nat/Archive.kiss | 10 +++---- projects/nat-archive-tool/src/nat/Lib.kiss | 2 +- 6 files changed, 30 insertions(+), 22 deletions(-) diff --git a/kiss/src/kiss/Kiss.hx b/kiss/src/kiss/Kiss.hx index 496b0dc9..5977941e 100644 --- a/kiss/src/kiss/Kiss.hx +++ b/kiss/src/kiss/Kiss.hx @@ -60,6 +60,7 @@ class Kiss { "pairs" => Symbol("Prelude.pairs"), // TODO test pairs "reversed" => Symbol("Prelude.reversed"), // TODO test reversed "memoize" => Symbol("Prelude.memoize"), // TODO test memoize + "joinPath" => Symbol("Prelude.joinPath"), "symbolName" => Symbol("Prelude.symbolName"), "symbolNameValue" => Symbol("Prelude.symbolNameValue"), "symbol" => Symbol("Prelude.symbol"), diff --git a/kiss/src/kiss/Prelude.hx b/kiss/src/kiss/Prelude.hx index 963d7021..8e316ebb 100644 --- a/kiss/src/kiss/Prelude.hx +++ b/kiss/src/kiss/Prelude.hx @@ -12,6 +12,7 @@ import js.node.Buffer; import sys.io.Process; #end import uuid.Uuid; +import haxe.io.Path; using StringTools; using uuid.Uuid; @@ -239,7 +240,7 @@ class Prelude { // Ranges with a min, exclusive max, and step size, just like Python. public static function range(min, max, step):Iterator { - if (step <= 0) + if (step <= 0 || max < min) throw "(range...) can only count up"; var count = min; return { @@ -254,6 +255,12 @@ class Prelude { }; } + static function _joinPath(parts:Array) { + return Path.join([for (part in parts) cast(part, String)]); + } + + public static var joinPath:Function = Reflect.makeVarArgs(_joinPath); + public static dynamic function truthy(v:T) { return switch (Type.typeof(v)) { case TNull: false; diff --git a/projects/kiss-vscode/config/KissConfig.kiss b/projects/kiss-vscode/config/KissConfig.kiss index cc4d2710..2f9c8c91 100644 --- a/projects/kiss-vscode/config/KissConfig.kiss +++ b/projects/kiss-vscode/config/KissConfig.kiss @@ -250,9 +250,9 @@ currentFileDirectory (Path.directory currentFile) haxeFile - (Path.join [currentFileDirectory "${className}.hx"]) + (joinPath currentFileDirectory "${className}.hx") kissFile - (Path.join [currentFileDirectory "${className}.kiss"]) + (joinPath currentFileDirectory "${className}.kiss") // Try to use the same package statement from the first line of the // currently open Kiss class's .hx file pkg diff --git a/projects/kiss-vscode/src/Main.kiss b/projects/kiss-vscode/src/Main.kiss index 28d2e35f..64282f07 100644 --- a/projects/kiss-vscode/src/Main.kiss +++ b/projects/kiss-vscode/src/Main.kiss @@ -1,7 +1,7 @@ (defun userConfigDir [] - (Path.join [ + (joinPath (or (Sys.getEnv "MSYSHOME") (Sys.getEnv "HOME") (Sys.getEnv "UserProfile")) - ".kiss"])) + ".kiss")) (defvar &mut activeConfigDir "") (defvar &mut lastConfigDir "") @@ -22,12 +22,12 @@ (let [customConfigDir (#if test // When running unit tests, build the example config - (Path.join [builtinConfigDir "example"]) + (joinPath builtinConfigDir "example") // When running for real, try the user's config directory (if (FileSystem.exists (userConfigDir)) (userConfigDir) // Supply the default (empty) config if the user doesn't have one - (Path.join [builtinConfigDir "default"]))) + (joinPath 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? @@ -37,13 +37,13 @@ // 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]))) + (joinPath builtinConfigDir file) + (joinPath 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]))) + (joinPath customConfigDir file) + (joinPath 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: @@ -55,8 +55,8 @@ // Require the config.js package. // But since Node.require() caches modules by filename, // copy it to a unique path first so hot-reloading works properly. - (let [activeConfigFile (Path.join [activeConfigDir "config.js"]) - uniqueConfigFile (Path.join [activeConfigDir "$(.toShort (Uuid.v4)).js"])] + (let [activeConfigFile (joinPath activeConfigDir "config.js") + uniqueConfigFile (joinPath activeConfigDir "$(.toShort (Uuid.v4)).js")] (File.copy activeConfigFile uniqueConfigFile) (set config (the KissConfig .KissConfig (Node.require uniqueConfigFile))) // (FileSystem.deleteFile uniqueConfigFile) @@ -116,9 +116,9 @@ // TODO overload Prelude.print to use showInformationMessage - (set builtinConfigDir (Path.join [context.extensionPath "config"])) - (set activeConfigDir (Path.join [context.extensionPath "_activeConfig"])) - (set lastConfigDir (Path.join [context.extensionPath "_lastActiveConfig"])) + (set builtinConfigDir (joinPath context.extensionPath "config")) + (set activeConfigDir (joinPath context.extensionPath "_activeConfig")) + (set lastConfigDir (joinPath context.extensionPath "_lastActiveConfig")) (tryLoadConfig))) (defun :Void main [] diff --git a/projects/nat-archive-tool/src/nat/Archive.kiss b/projects/nat-archive-tool/src/nat/Archive.kiss index 4a1ab7c9..9a924bbe 100644 --- a/projects/nat-archive-tool/src/nat/Archive.kiss +++ b/projects/nat-archive-tool/src/nat/Archive.kiss @@ -3,12 +3,12 @@ [:Array systems [] :Map entries - (let [entryDir (Path.join [archiveDir "entries"]) - componentDir (Path.join [archiveDir "components"])] + (let [entryDir (joinPath archiveDir "entries") + componentDir (joinPath archiveDir "components")] (FileSystem.createDirectory entryDir) (FileSystem.createDirectory componentDir) (let [entryFiles (FileSystem.readDirectory entryDir)] - (for file entryFiles =>(file.withoutExtension) (the Entry (Json.parse (File.getContent (Path.join [archiveDir "entries" file])))))))]) + (for file entryFiles =>(file.withoutExtension) (the Entry (Json.parse (File.getContent (joinPath archiveDir "entries" file)))))))]) (defmethod addSystem [:System system] // Assign entries to the Systems that care about them @@ -33,11 +33,11 @@ (defmethod _saveEntry [:Entry e] (File.saveContent - (Path.join [archiveDir "entries" (e.id.withExtension "json")]) + (joinPath archiveDir "entries" (e.id.withExtension "json")) (Json.stringify e))) (defmethod componentData [:Entry e :String componentType] - (haxe.Json.parse (File.getContent (haxe.io.Path.join [archiveDir "components" "$(dictGet e.components componentType).json"])))) + (haxe.Json.parse (File.getContent (joinPath archiveDir "components" "$(dictGet e.components componentType).json")))) (defmethod fullData [:Entry e] (object diff --git a/projects/nat-archive-tool/src/nat/Lib.kiss b/projects/nat-archive-tool/src/nat/Lib.kiss index 93b6db79..843de187 100644 --- a/projects/nat-archive-tool/src/nat/Lib.kiss +++ b/projects/nat-archive-tool/src/nat/Lib.kiss @@ -5,7 +5,7 @@ `(.exists .components ,e ,(symbolName componentType))) (defmacro _componentPath [archive e componentType] - `(haxe.io.Path.join [.archiveDir (the nat.Archive ,archive) "components" (+ (dictGet (the Map .components ,e) ,(symbolName componentType)) ".json")])) + `(joinPath .archiveDir (the nat.Archive ,archive) "components" (+ (dictGet (the Map .components ,e) ,(symbolName componentType)) ".json"))) // Changes to the object returned by (readComponent) will not be saved! Use (withWritableComponents) for making changes (defmacro readComponent [archive e componentType]