From f313efe86eda16a873b8094b5e1b6d569f24c99a Mon Sep 17 00:00:00 2001 From: Sangelo Date: Tue, 7 Mar 2023 20:20:21 +0100 Subject: [PATCH] Started work on config implementation --- config/config.toml | 20 +++--- config/templates/nim/init.toml | 12 ++++ config/{ => templates/nim}/launch.json | 0 config/{ => templates/nim}/tasks.json | 0 install.sh | 2 +- src/niminit.nim | 93 ++++++++++++++++---------- 6 files changed, 80 insertions(+), 47 deletions(-) create mode 100644 config/templates/nim/init.toml rename config/{ => templates/nim}/launch.json (100%) rename config/{ => templates/nim}/tasks.json (100%) diff --git a/config/config.toml b/config/config.toml index 9cd9676..7f9b24d 100644 --- a/config/config.toml +++ b/config/config.toml @@ -3,15 +3,13 @@ # (c) Sangelo - 2023 # ####################### [general] -# Should the home directory appended before the config directory? -appendHome = true -# The path to the config file (no trailing slashes!) -configDirectory = "/.config/niminit" +# Should compatibility warnings be suppressed? +suppressWarnings = false -[files] -# The directory to be created inside the project -directory = ".vscode" -files = [ - "launch.json", - "tasks.json" -] \ No newline at end of file +[project] +# This is the directory inside your home folder containing all of your templates. +sourceDirectory = "/.config/niminit/templates" +# The directory to be created inside the project, and to copy the selected project template into. (for example: utils) +targetDirectory = ".vscode" +# The default template to use when not explicitly specified. (for example: java) +defaultTemplate = "nim" \ No newline at end of file diff --git a/config/templates/nim/init.toml b/config/templates/nim/init.toml new file mode 100644 index 0000000..da266dd --- /dev/null +++ b/config/templates/nim/init.toml @@ -0,0 +1,12 @@ +######################## +# niminit Nim Template # +# (c) Sangelo - 2023 # +######################## + +[general] +# Should a script be ran after initialising? +runScript = false +# What script should be executed? +scriptPath = "" +# Should a git repo be initialised? +initGitRepo = true \ No newline at end of file diff --git a/config/launch.json b/config/templates/nim/launch.json similarity index 100% rename from config/launch.json rename to config/templates/nim/launch.json diff --git a/config/tasks.json b/config/templates/nim/tasks.json similarity index 100% rename from config/tasks.json rename to config/templates/nim/tasks.json diff --git a/install.sh b/install.sh index 8e3c207..a29a6e6 100755 --- a/install.sh +++ b/install.sh @@ -26,7 +26,7 @@ mkdir -p $HOME/.local/bin && cp bin/niminit $HOME/.local/bin/niminit && # Create config directory & copy files mkdir -p $HOME/.config/niminit && -cp config/* $HOME/.config/niminit && +cp -r config/* $HOME/.config/niminit && # Print info message to export local bin if not already printf "\n" diff --git a/src/niminit.nim b/src/niminit.nim index e1c3696..6df3f20 100644 --- a/src/niminit.nim +++ b/src/niminit.nim @@ -5,13 +5,31 @@ import parsetoml # TODO: Make .nimble creator -# OS detection +# Windows detection when defined(windows): echo "Warning: Windows is currently unsupported!" quit 1 + +# Initialise config +let homeDir = getEnv("HOME") +let config = parseToml.parseFile(joinPath(homeDir, + "/.config/niminit/config.toml")) + +# Set necessary variables (home directory, config path) +let projectTarget = config["project"]["targetDirectory"].getStr(".vscode") +# var projectTarget = config["project"].getStr("targetDirectory", ".vscode") +var projectSource = config["project"]["sourceDirectory"].getStr("/.config") +var projectTemplate = config["project"]["defaultTemplate"].getStr("nim") + +# let tasksPath = joinPath(homeDir, "/.config/niminit/tasks.json") +# let launchPath = joinPath(homeDir, "/.config/niminit/launch.json") + +# Set configuration variables +let suppressWarnings = config["general"]["suppressWarnings"].getBool(false) + when defined(macosx): - if "-s" in (commandLineParams()): - echo "Suppressing experimental support warning" + if "-s" or suppressWarnings == true in (commandLineParams()): + echo "Suppressing experimental support warning." else: echo "Warning: macOS is currently untested. Would you like to continue? [y/N]" var input = stdin.readLine() @@ -22,24 +40,6 @@ when defined(macosx): echo "Cancelling..." quit 0 -# Check for files in directory -var count = 0 -for entry in walkDir("."): - count += 1 -# If there are files, ask for confirmation, otherwise continue. -if count == 0: - echo "Initialising..." -else: - echo "This directory is not empty." - echo "Continue initialising? [y/N]" - var input = stdin.readLine() - case input.toLower - of "yes", "y", "z", "j": - echo "Initialising non-empty directory..." - else: - echo "Cancelling..." - quit 0 - # Define how to handle failed copies proc copyFile(src, dest: string): bool = try: @@ -73,24 +73,47 @@ proc configureGitRepo(gitRemote, gitBranch: string) = else: echo &"Failed to set branch {gitBranch}" -#-------------------------------------------------------------------------------# +#--# -# Set necessary variables (home directory, config path) -let homeDir = getEnv("HOME") -let vscodeDir = ".vscode" -let tasksPath = joinPath(homeDir, "/.config/niminit/tasks.json") -let launchPath = joinPath(homeDir, "/.config/niminit/launch.json") +# Check for files in directory +var count = 0 +for entry in walkDir("."): + count += 1 +# If there are files, ask for confirmation, otherwise continue. +if count == 0: + echo "Initialising..." +else: + echo "This directory is not empty." + echo "Continue initialising? [y/N]" + var input = stdin.readLine() + case input.toLower + of "yes", "y", "z", "j": + echo "Initialising non-empty directory..." + else: + echo "Cancelling..." + quit 0 -# Create .vscode directory and copy files into it -let vscDirCreated = createDir(vscodeDir) -let tasksCopied = copyFile(tasksPath, joinPath(vscodeDir, "tasks.json")) -let launchCopied = copyFile(launchPath, joinPath(vscodeDir, "launch.json")) +# Create project directory and copy files into it +let projectDirCreated = createDir(projectTarget) + +# Get a list of all the files in the source directory +let srcDir = +let files = walkDir(joinPath(projectSource, "/", projectTemplate)) + +# Loop through each file and copy it to the destination directory +for file in files: + let srcFile = joinPath(srcDir, file) + let destFile = joinPath(destDir, file) + if not isDir(srcFile): + copyFile(srcFile, destFile) + let copiedFiles = copyFile(joinPath(projectSource, "/", projectTemplate, + "/*"), projectTarget) # Check for success and quit if command errored -if vscDirCreated and tasksCopied and launchCopied: - echo "All files copied successfully" +if projectDirCreated and copiedFiles: # and tasksCopied and launchCopied: + echo "All files created successfully" else: - echo "Failed to copy one or more files" + echo "Failed to create one or more files" quit 1 # Handle -g parameter for git repos. @@ -114,4 +137,4 @@ if "-g" in (commandLineParams()): else: echo: "Failed creating local git repository." -echo "Done!" \ No newline at end of file +echo "Done!"