Started work on config implementation

This commit is contained in:
Sangelo 2023-03-07 20:20:21 +01:00
parent 0dfe2bbe69
commit f313efe86e
6 changed files with 80 additions and 47 deletions

View file

@ -3,15 +3,13 @@
# (c) Sangelo - 2023 # # (c) Sangelo - 2023 #
####################### #######################
[general] [general]
# Should the home directory appended before the config directory? # Should compatibility warnings be suppressed?
appendHome = true suppressWarnings = false
# The path to the config file (no trailing slashes!)
configDirectory = "/.config/niminit"
[files] [project]
# The directory to be created inside the project # This is the directory inside your home folder containing all of your templates.
directory = ".vscode" sourceDirectory = "/.config/niminit/templates"
files = [ # The directory to be created inside the project, and to copy the selected project template into. (for example: utils)
"launch.json", targetDirectory = ".vscode"
"tasks.json" # The default template to use when not explicitly specified. (for example: java)
] defaultTemplate = "nim"

View file

@ -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

View file

@ -26,7 +26,7 @@ mkdir -p $HOME/.local/bin &&
cp bin/niminit $HOME/.local/bin/niminit && cp bin/niminit $HOME/.local/bin/niminit &&
# Create config directory & copy files # Create config directory & copy files
mkdir -p $HOME/.config/niminit && 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 # Print info message to export local bin if not already
printf "\n" printf "\n"

View file

@ -5,13 +5,31 @@ import parsetoml
# TODO: Make .nimble creator # TODO: Make .nimble creator
# OS detection # Windows detection
when defined(windows): when defined(windows):
echo "Warning: Windows is currently unsupported!" echo "Warning: Windows is currently unsupported!"
quit 1 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): when defined(macosx):
if "-s" in (commandLineParams()): if "-s" or suppressWarnings == true in (commandLineParams()):
echo "Suppressing experimental support warning" echo "Suppressing experimental support warning."
else: else:
echo "Warning: macOS is currently untested. Would you like to continue? [y/N]" echo "Warning: macOS is currently untested. Would you like to continue? [y/N]"
var input = stdin.readLine() var input = stdin.readLine()
@ -22,24 +40,6 @@ when defined(macosx):
echo "Cancelling..." echo "Cancelling..."
quit 0 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 # Define how to handle failed copies
proc copyFile(src, dest: string): bool = proc copyFile(src, dest: string): bool =
try: try:
@ -73,24 +73,47 @@ proc configureGitRepo(gitRemote, gitBranch: string) =
else: else:
echo &"Failed to set branch {gitBranch}" echo &"Failed to set branch {gitBranch}"
#-------------------------------------------------------------------------------# #--#
# Set necessary variables (home directory, config path) # Check for files in directory
let homeDir = getEnv("HOME") var count = 0
let vscodeDir = ".vscode" for entry in walkDir("."):
let tasksPath = joinPath(homeDir, "/.config/niminit/tasks.json") count += 1
let launchPath = joinPath(homeDir, "/.config/niminit/launch.json") # 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 # Create project directory and copy files into it
let vscDirCreated = createDir(vscodeDir) let projectDirCreated = createDir(projectTarget)
let tasksCopied = copyFile(tasksPath, joinPath(vscodeDir, "tasks.json"))
let launchCopied = copyFile(launchPath, joinPath(vscodeDir, "launch.json")) # 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 # Check for success and quit if command errored
if vscDirCreated and tasksCopied and launchCopied: if projectDirCreated and copiedFiles: # and tasksCopied and launchCopied:
echo "All files copied successfully" echo "All files created successfully"
else: else:
echo "Failed to copy one or more files" echo "Failed to create one or more files"
quit 1 quit 1
# Handle -g parameter for git repos. # Handle -g parameter for git repos.