From 247b32838dc2901d430b78d959c41f6884ccfd70 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sat, 25 Jan 2025 14:58:54 -0600 Subject: [PATCH] fix new-project command --- src/kiss/Main.hx | 86 +++++++++++-------- template/.haxerc | 4 + template/README.md | 2 + template/haxe_libraries/haxe-strings.hxml | 5 ++ template/haxe_libraries/hscript.hxml | 5 ++ template/haxe_libraries/kiss.hxml | 12 +++ template/haxe_libraries/tink_core.hxml | 3 + template/haxe_libraries/tink_json.hxml | 4 + template/haxe_libraries/tink_macro.hxml | 4 + template/haxe_libraries/tink_priority.hxml | 3 + template/haxe_libraries/tink_syntaxhub.hxml | 6 ++ template/haxe_libraries/tink_typecrawler.hxml | 4 + template/haxe_libraries/uuid.hxml | 3 + 13 files changed, 106 insertions(+), 35 deletions(-) create mode 100644 template/.haxerc create mode 100644 template/README.md create mode 100644 template/haxe_libraries/haxe-strings.hxml create mode 100644 template/haxe_libraries/hscript.hxml create mode 100644 template/haxe_libraries/kiss.hxml create mode 100644 template/haxe_libraries/tink_core.hxml create mode 100644 template/haxe_libraries/tink_json.hxml create mode 100644 template/haxe_libraries/tink_macro.hxml create mode 100644 template/haxe_libraries/tink_priority.hxml create mode 100644 template/haxe_libraries/tink_syntaxhub.hxml create mode 100644 template/haxe_libraries/tink_typecrawler.hxml create mode 100644 template/haxe_libraries/uuid.hxml diff --git a/src/kiss/Main.hx b/src/kiss/Main.hx index 66cfbb5..772b375 100644 --- a/src/kiss/Main.hx +++ b/src/kiss/Main.hx @@ -6,6 +6,7 @@ import haxe.macro.Expr; import haxe.macro.Context; import haxe.macro.Type; import kiss.Kiss; +import kiss.Helpers; import kiss.Reader; import kiss.Stream; @@ -20,12 +21,21 @@ import sys.io.Process; import sys.FileSystem; using StringTools; +using haxe.io.Path; class Main { static function main() { macroMain(); } + static function libPath(lib:String) { + #if macro + return Helpers.libPath(lib); + #end + throw 'Tried to get libPath outside of macroMain()'; + return ""; + } + // When called from the command-line, Kiss has various subcommands, some of which can only run in macro context static macro function macroMain():Expr { var args = Sys.args(); @@ -92,7 +102,7 @@ class Main { return input; } - static function _makeFileForNewProject(templateDir:String, templateFile:Array, workingDir:String, projectName:String, pkg:String) { + static function _makeFileForNewProject(templateDir:String, templateFile:Array, workingDir:String, projectName:String, description:String, pkg:String) { // Expand this list when making new templates with different binary extensions var extensionsForBytes = [ "png" @@ -108,14 +118,21 @@ class Main { var fullTemplateFilePath = Path.join([templateDir, "template"].concat(templateFile)); var newFileContent:Dynamic = getContent(fullTemplateFilePath); - if (replaceStringTemplate) - newFileContent = StringTools.replace(newFileContent, "template", pkg); + if (replaceStringTemplate) { + var projectOrPackageName = if (['hx', 'hxml', 'kiss'].contains(fullTemplateFilePath.extension())) { + pkg; + } else { + projectName; + } + newFileContent = StringTools.replace(newFileContent, "template", projectOrPackageName); + newFileContent = StringTools.replace(newFileContent, "{{description}}", pkg); + } var templateFileInNewProject = [for (part in templateFile) if (part == "template") pkg else part]; var newFilePath = Path.join([workingDir, projectName].concat(templateFileInNewProject)); saveContent(newFilePath, newFileContent); } - static function _makeFolderForNewProject(templateDir:String, templateFolder:Array, workingDir:String, projectName:String, pkg:String) { + static function _makeFolderForNewProject(templateDir:String, templateFolder:Array, workingDir:String, projectName:String, description:String, pkg:String) { var fullTemplateFolderPath = Path.join([templateDir, "template"].concat(templateFolder)); var templateFolderInNewProject = [for (part in templateFolder) if (part == "template") pkg else part]; var newFolderPath = Path.join([workingDir, projectName].concat(templateFolderInNewProject)); @@ -123,30 +140,32 @@ class Main { for (fileOrFolder in FileSystem.readDirectory(fullTemplateFolderPath)) { if (FileSystem.isDirectory(Path.join([fullTemplateFolderPath, fileOrFolder]))) { - _makeFolderForNewProject(templateDir, templateFolder.concat([fileOrFolder]), workingDir, projectName, pkg); + _makeFolderForNewProject(templateDir, templateFolder.concat([fileOrFolder]), workingDir, projectName, description, pkg); } else { - _makeFileForNewProject(templateDir, templateFolder.concat([fileOrFolder]), workingDir, projectName, pkg); + _makeFileForNewProject(templateDir, templateFolder.concat([fileOrFolder]), workingDir, projectName, description, pkg); } } } static function newProject(args:Array) { - var kissLibPath = new Process("haxelib", ["libpath", "kiss"]).stdout.readAll().toString().trim(); + var kissLibPath = libPath("kiss"); var name = promptFor("name"); - // TODO put the prompted name and description in a README.md var pkg = name.toLowerCase().replace("-", "_"); + var description = ""; var haxelibJson = { "name": name, "contributors": promptFor("authors (comma-separated)").split(",").map(StringTools.trim), - // TODO can make the default URL actually point to the projects subdirectory... but only want that functionality if the working dir is in kisslang/projects - "url": promptFor("url", "https://github.com/NQNStudios/kisslang"), + "url": promptFor("url", 'https://github.com/kiss-lang/${name}'), "license": promptFor("license", "LGPL"), "tags": { var t = promptFor("tags (comma-separated)", "").split(",").map(StringTools.trim); t.remove(""); t; }, - "description": promptFor("description", ""), + "description": { + description = promptFor("description", ""); + description; + }, "version": "0.0.0", "releasenote": "", "classPath": "src/", @@ -155,11 +174,13 @@ class Main { "kiss": "" } }; - var workingDir = Sys.args().pop(); - var makeFileForNewProject:haxe.Constraints.Function = _makeFileForNewProject.bind(kissLibPath, _, workingDir, name, pkg); - FileSystem.createDirectory(Path.join([workingDir, name, "src", pkg])); - makeFileForNewProject(["src", "template", "Main.hx"]); - makeFileForNewProject(["src", "template", "Main_.kiss"]); + var workingDir = Sys.getCwd(); + var makeFileForNewProject:haxe.Constraints.Function = _makeFileForNewProject.bind(kissLibPath, _, workingDir, name, description, pkg); + var makeFolderForNewProject:haxe.Constraints.Function = _makeFolderForNewProject.bind(kissLibPath, _, workingDir, name, description, pkg); + makeFolderForNewProject(["haxe_libraries"]); + makeFolderForNewProject(["src"]); + makeFileForNewProject([".haxerc"]); + makeFileForNewProject(["build.hxml"]); makeFileForNewProject(["build.hxml"]); makeFileForNewProject(["test.sh"]); File.saveContent(Path.join([workingDir, name, 'haxelib.json']), Json.stringify(haxelibJson, null, "\t")); @@ -173,7 +194,7 @@ class Main { var background = promptFor("background color", "#000000"); var kissFlixelLibPath = new Process("haxelib", ["libpath", "kiss-flixel"]).stdout.readAll().toString().trim(); - var workingDir = Sys.args().pop(); + var workingDir = Sys.getCwd(); FileSystem.createDirectory(Path.join([workingDir, title])); // Substitute the specified values into the Project.xml: @@ -191,8 +212,8 @@ class Main { firstWindowElement.set("background", background); File.saveContent(Path.join([workingDir, title, 'Project.xml']), projectXml.toString()); - var makeFileForNewProject:haxe.Constraints.Function = _makeFileForNewProject.bind(kissFlixelLibPath, _, workingDir, title, ""); - var makeFolderForNewProject:haxe.Constraints.Function = _makeFolderForNewProject.bind(kissFlixelLibPath, _, workingDir, title, ""); + var makeFileForNewProject:haxe.Constraints.Function = _makeFileForNewProject.bind(kissFlixelLibPath, _, workingDir, title, "", ""); + var makeFolderForNewProject:haxe.Constraints.Function = _makeFolderForNewProject.bind(kissFlixelLibPath, _, workingDir, title, "", ""); makeFolderForNewProject([".vscode"]); makeFolderForNewProject(["assets"]); makeFolderForNewProject(["source"]); @@ -204,12 +225,12 @@ class Main { var title = promptFor("title (lower-case!)").toLowerCase(); var pkg = title.replace("-", "_"); var kissExpressLibPath = new Process("haxelib", ["libpath", "kiss-express"]).stdout.readAll().toString().trim(); - var workingDir = Sys.args().pop(); + var workingDir = Sys.getCwd(); var projectDir = Path.join([workingDir, title]); FileSystem.createDirectory(projectDir); - var makeFileForNewProject:haxe.Constraints.Function = _makeFileForNewProject.bind(kissExpressLibPath, _, workingDir, title, pkg); - var makeFolderForNewProject:haxe.Constraints.Function = _makeFolderForNewProject.bind(kissExpressLibPath, _, workingDir, title, pkg); + var makeFileForNewProject:haxe.Constraints.Function = _makeFileForNewProject.bind(kissExpressLibPath, _, workingDir, title, "", pkg); + var makeFolderForNewProject:haxe.Constraints.Function = _makeFolderForNewProject.bind(kissExpressLibPath, _, workingDir, title, "", pkg); makeFolderForNewProject(["src", "template"]); makeFileForNewProject([".gitignore"]); makeFileForNewProject(["build.hxml"]); @@ -225,27 +246,22 @@ class Main { static function newVscodeProject(args:Array) { var title = promptFor("title (lower-case!)").toLowerCase(); + var description = promptFor("description"); var pkg = title.replace("-", "_"); var kissVscodeApiLibPath = new Process("haxelib", ["libpath", "kiss-vscode-api"]).stdout.readAll().toString().trim(); - var workingDir = Sys.args().pop(); + var workingDir = Sys.getCwd(); var projectDir = Path.join([workingDir, title]); FileSystem.createDirectory(projectDir); - var makeFileForNewProject:haxe.Constraints.Function = _makeFileForNewProject.bind(kissVscodeApiLibPath, _, workingDir, title, pkg); - var makeFolderForNewProject:haxe.Constraints.Function = _makeFolderForNewProject.bind(kissVscodeApiLibPath, _, workingDir, title, pkg); + var makeFileForNewProject:haxe.Constraints.Function = _makeFileForNewProject.bind(kissVscodeApiLibPath, _, workingDir, title, description, pkg); + var makeFolderForNewProject:haxe.Constraints.Function = _makeFolderForNewProject.bind(kissVscodeApiLibPath, _, workingDir, title, description, pkg); makeFolderForNewProject(["src"]); makeFolderForNewProject([".vscode"]); makeFileForNewProject([".gitignore"]); makeFileForNewProject([".vscodeignore"]); makeFileForNewProject(["README.md"]); makeFileForNewProject(["build.hxml"]); - { - makeFileForNewProject(["package.json"]); - var packageFile = Path.join([projectDir, "package.json"]); - var packageJson = Json.parse(File.getContent(packageFile)); - packageJson.name = title; - File.saveContent(packageFile, Json.stringify(packageJson, null, "\t")); - } + makeFileForNewProject(["package.json"]); makeFileForNewProject(["test.sh"]); } @@ -261,12 +277,12 @@ class Main { urlPatterns.push(nextPattern); } var kissFirefoxLibPath = new Process("haxelib", ["libpath", "kiss-firefox"]).stdout.readAll().toString().trim(); - var workingDir = Sys.args().pop(); + var workingDir = Sys.getCwd(); var projectDir = Path.join([workingDir, title]); FileSystem.createDirectory(projectDir); - var makeFileForNewProject:haxe.Constraints.Function = _makeFileForNewProject.bind(kissFirefoxLibPath, _, workingDir, title, pkg); - var makeFolderForNewProject:haxe.Constraints.Function = _makeFolderForNewProject.bind(kissFirefoxLibPath, _, workingDir, title, pkg); + var makeFileForNewProject:haxe.Constraints.Function = _makeFileForNewProject.bind(kissFirefoxLibPath, _, workingDir, title, description, pkg); + var makeFolderForNewProject:haxe.Constraints.Function = _makeFolderForNewProject.bind(kissFirefoxLibPath, _, workingDir, title, description, pkg); makeFolderForNewProject(["src"]); makeFolderForNewProject(["icons"]); makeFileForNewProject([".gitignore"]); diff --git a/template/.haxerc b/template/.haxerc new file mode 100644 index 0000000..dc3cec2 --- /dev/null +++ b/template/.haxerc @@ -0,0 +1,4 @@ +{ + "version": "4.3.1", + "resolveLibs": "scoped" +} \ No newline at end of file diff --git a/template/README.md b/template/README.md new file mode 100644 index 0000000..0837336 --- /dev/null +++ b/template/README.md @@ -0,0 +1,2 @@ +# template +{{description}} \ No newline at end of file diff --git a/template/haxe_libraries/haxe-strings.hxml b/template/haxe_libraries/haxe-strings.hxml new file mode 100644 index 0000000..003c2de --- /dev/null +++ b/template/haxe_libraries/haxe-strings.hxml @@ -0,0 +1,5 @@ +# @install: lix --silent download "haxelib:/haxe-strings#7.0.3" into haxe-strings/7.0.3/haxelib +-cp ${HAXE_LIBCACHE}/haxe-strings/7.0.3/haxelib/src/ +-D haxe-strings=7.0.3 +--macro hx.strings.internal.Macros.addDefines() +--macro hx.strings.internal.Macros.configureNullSafety() diff --git a/template/haxe_libraries/hscript.hxml b/template/haxe_libraries/hscript.hxml new file mode 100644 index 0000000..106e1f8 --- /dev/null +++ b/template/haxe_libraries/hscript.hxml @@ -0,0 +1,5 @@ +# @install: lix --silent download "haxelib:/hscript#2.5.0" into hscript/2.5.0/haxelib +# @run: haxelib run-dir hscript "${HAXE_LIBCACHE}/hscript/2.5.0/haxelib" +-cp ${HAXE_LIBCACHE}/hscript/2.5.0/haxelib/ +-D hscript=2.5.0 +--macro keep('IntIterator') \ No newline at end of file diff --git a/template/haxe_libraries/kiss.hxml b/template/haxe_libraries/kiss.hxml new file mode 100644 index 0000000..56c708c --- /dev/null +++ b/template/haxe_libraries/kiss.hxml @@ -0,0 +1,12 @@ +# @install: lix --silent download "gh://github.com/kiss-lang/kiss#877b965812be5013c514d03d6f94a074495a53fa" into kiss/0.0.1/github/877b965812be5013c514d03d6f94a074495a53fa +# @run: haxelib run-dir kiss "${HAXE_LIBCACHE}/kiss/0.0.1/github/877b965812be5013c514d03d6f94a074495a53fa" +-lib haxe-strings +-lib hscript +-lib tink_json +-lib tink_macro +-lib tink_syntaxhub +-lib uuid +-cp ${HAXE_LIBCACHE}/kiss/0.0.1/github/877b965812be5013c514d03d6f94a074495a53fa/src +-D kiss=0.0.1 +-w -WUnusedPattern +--macro kiss.KissFrontend.use() \ No newline at end of file diff --git a/template/haxe_libraries/tink_core.hxml b/template/haxe_libraries/tink_core.hxml new file mode 100644 index 0000000..37a0d96 --- /dev/null +++ b/template/haxe_libraries/tink_core.hxml @@ -0,0 +1,3 @@ +# @install: lix --silent download "haxelib:/tink_core#2.1.0" into tink_core/2.1.0/haxelib +-cp ${HAXE_LIBCACHE}/tink_core/2.1.0/haxelib/src +-D tink_core=2.1.0 \ No newline at end of file diff --git a/template/haxe_libraries/tink_json.hxml b/template/haxe_libraries/tink_json.hxml new file mode 100644 index 0000000..b167de7 --- /dev/null +++ b/template/haxe_libraries/tink_json.hxml @@ -0,0 +1,4 @@ +# @install: lix --silent download "haxelib:/tink_json#0.11.0" into tink_json/0.11.0/haxelib +-lib tink_typecrawler +-cp ${HAXE_LIBCACHE}/tink_json/0.11.0/haxelib/src +-D tink_json=0.11.0 \ No newline at end of file diff --git a/template/haxe_libraries/tink_macro.hxml b/template/haxe_libraries/tink_macro.hxml new file mode 100644 index 0000000..1e712e7 --- /dev/null +++ b/template/haxe_libraries/tink_macro.hxml @@ -0,0 +1,4 @@ +# @install: lix --silent download "gh://github.com/kiss-lang/tink_macro#8b60a484b1141d1176b34ba3af9ac65b499079ff" into tink_macro/1.0.3/github/8b60a484b1141d1176b34ba3af9ac65b499079ff +-lib tink_core +-cp ${HAXE_LIBCACHE}/tink_macro/1.0.3/github/8b60a484b1141d1176b34ba3af9ac65b499079ff/src +-D tink_macro=1.0.3 \ No newline at end of file diff --git a/template/haxe_libraries/tink_priority.hxml b/template/haxe_libraries/tink_priority.hxml new file mode 100644 index 0000000..98cfa80 --- /dev/null +++ b/template/haxe_libraries/tink_priority.hxml @@ -0,0 +1,3 @@ +-D tink_priority=0.1.3 +# @install: lix --silent download "gh://github.com/haxetink/tink_priority#ea736d31dc788aae703a2aa415c25d5b80d0e7d1" into tink_priority/0.1.3/github/ea736d31dc788aae703a2aa415c25d5b80d0e7d1 +-cp ${HAXE_LIBCACHE}/tink_priority/0.1.3/github/ea736d31dc788aae703a2aa415c25d5b80d0e7d1/src diff --git a/template/haxe_libraries/tink_syntaxhub.hxml b/template/haxe_libraries/tink_syntaxhub.hxml new file mode 100644 index 0000000..e3c7d2b --- /dev/null +++ b/template/haxe_libraries/tink_syntaxhub.hxml @@ -0,0 +1,6 @@ +# @install: lix --silent download "gh://github.com/haxetink/tink_syntaxhub#b6ea4966bbdee4d176ac8dd5d2d8ae3b362b2f86" into tink_syntaxhub/0.6.0/github/b6ea4966bbdee4d176ac8dd5d2d8ae3b362b2f86 +-lib tink_macro +-lib tink_priority +-cp ${HAXE_LIBCACHE}/tink_syntaxhub/0.6.0/github/b6ea4966bbdee4d176ac8dd5d2d8ae3b362b2f86/src +-D tink_syntaxhub=0.6.0 +--macro tink.SyntaxHub.use() \ No newline at end of file diff --git a/template/haxe_libraries/tink_typecrawler.hxml b/template/haxe_libraries/tink_typecrawler.hxml new file mode 100644 index 0000000..373aee9 --- /dev/null +++ b/template/haxe_libraries/tink_typecrawler.hxml @@ -0,0 +1,4 @@ +# @install: lix --silent download "haxelib:/tink_typecrawler#0.7.0" into tink_typecrawler/0.7.0/haxelib +-lib tink_macro +-cp ${HAXE_LIBCACHE}/tink_typecrawler/0.7.0/haxelib/src +-D tink_typecrawler=0.7.0 \ No newline at end of file diff --git a/template/haxe_libraries/uuid.hxml b/template/haxe_libraries/uuid.hxml new file mode 100644 index 0000000..ff1269b --- /dev/null +++ b/template/haxe_libraries/uuid.hxml @@ -0,0 +1,3 @@ +# @install: lix --silent download "haxelib:/uuid#2.4.1" into uuid/2.4.1/haxelib +-cp ${HAXE_LIBCACHE}/uuid/2.4.1/haxelib/src +-D uuid=2.4.1 \ No newline at end of file