niminit/niminit.nim

98 lines
3.1 KiB
Nim
Raw Normal View History

2023-03-06 16:08:41 +00:00
import os
import strutils
2023-03-06 16:27:29 +00:00
import strformat
2023-03-06 16:08:41 +00:00
2023-03-06 18:14:14 +00:00
# Check for files in directory
2023-03-06 16:08:41 +00:00
var count = 0
for entry in walkDir("."):
count += 1
2023-03-06 18:14:14 +00:00
# If there are files, ask for confirmation, otherwise continue.
2023-03-06 16:08:41 +00:00
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
2023-03-06 18:14:14 +00:00
# Define how to handle failed copies
2023-03-06 16:08:41 +00:00
proc copyFile(src, dest: string): bool =
try:
os.copyFile(src, dest)
echo "Successfully copied ", src, " to ", dest
return true
except:
echo "Failed to copy ", src, " to ", dest
return false
2023-03-06 18:14:14 +00:00
# Define how to handle failed directory creations
2023-03-06 16:08:41 +00:00
proc createDir(dir: string): bool =
try:
os.createDir(dir)
echo "Successfully created ", dir
return true
except:
echo "Failed to create ", dir
return false
2023-03-06 18:14:14 +00:00
# Define how to handle configuring a git repo
proc configureGitRepo(gitRemote, gitBranch: string) =
let setupGitRepo = execShellCmd(&"git remote add origin {gitRemote}")
let setGitBranch = execShellCmd(&"git branch -m {gitBranch}")
if setupGitRepo == 0:
echo &"Successfully added remote {gitRemote}"
else:
echo &"Failed to add remote {gitRemote}"
if setGitBranch == 0:
echo &"Successfully set branch to {gitBranch}"
else:
echo &"Failed to set branch {gitBranch}"
#-------------------------------------------------------------------------------#
# Set necessary variables (home directory, config path)
2023-03-06 16:08:41 +00:00
let homeDir = getEnv("HOME")
let vscodeDir = ".vscode"
let tasksPath = joinPath(homeDir, "/.config/niminit/tasks.json")
let launchPath = joinPath(homeDir, "/.config/niminit/launch.json")
2023-03-06 18:14:14 +00:00
# Create .vscode directory and copy files into it
2023-03-06 16:08:41 +00:00
let vscDirCreated = createDir(vscodeDir)
let tasksCopied = copyFile(tasksPath, joinPath(vscodeDir, "tasks.json"))
let launchCopied = copyFile(launchPath, joinPath(vscodeDir, "launch.json"))
2023-03-06 18:14:14 +00:00
# Check for success and quit if command errored
2023-03-06 16:08:41 +00:00
if vscDirCreated and tasksCopied and launchCopied:
echo "All files copied successfully"
else:
echo "Failed to copy one or more files"
quit 1
2023-03-06 18:14:14 +00:00
# Handle -g parameter for git repos.
2023-03-06 16:08:41 +00:00
if "-g" in (commandLineParams()):
let createdRepo = execShellCmd("git init")
if createdRepo == 0:
2023-03-06 16:27:29 +00:00
echo "Successfully initialised local git repository!"
echo "Would you like to configure it? [Y/n]"
var input = stdin.readLine()
case input.toLower
of "yes", "y", "z", "j", "":
echo "Enter the git remote:"
let gitRemote = stdin.readLine()
echo "Enter your desired branch name:"
let gitBranch = stdin.readLine()
2023-03-06 18:14:14 +00:00
echo "Configuring Git repo..."
configureGitRepo(gitRemote, gitBranch)
2023-03-06 16:27:29 +00:00
else:
2023-03-06 18:14:14 +00:00
echo "Skipping repository configuration..."
2023-03-06 16:27:29 +00:00
echo "Successfully created local git repository!"
2023-03-06 16:08:41 +00:00
else:
echo: "Failed creating local git repository."
echo "Done!"