#!/usr/bin/env nu let template_dir = ".config/template" let config_dir = ".config" let out_dir = $"(pwd)" # let out_dir = $"(pwd)/works" def main [subject: string, shortcode: string, number?: int] { let subject_lower = ($subject | str downcase) let shortcode_lower = ($shortcode | str downcase) validate-input $subject $shortcode $number let temp_dir = $"/tmp/nure-work-(random chars -l 8)" let final_dir = $"($out_dir)/($subject_lower)/($shortcode_lower)($number)" let subject_dir = $"($out_dir)/($subject_lower)" # TODO?: replace with git clone cp -r ($config_dir)/template $temp_dir generate-doc-toml $shortcode_lower $subject_lower $temp_dir $number check-mkdir $out_dir if ($final_dir | path exists) { error make -u {msg: $"Directory ($final_dir) already exists"} } check-mkdir $subject_dir; cp -r $temp_dir $final_dir; rm -rf $temp_dir print $"=> Created new work: ($final_dir)" } def check-mkdir [path: string] { if not ($path | path exists) { mkdir $path } } def validate-input [subject: string, shortcode: string, number?: int] { if not ($config_dir | path exists) { error make -u {msg: $"($config_dir) not found"} } let defaults_toml = $"($config_dir)/defaults.toml" let subjects_toml = $"($config_dir)/subjects.toml" if not ($defaults_toml | path exists) { error make -u {msg: $"defaults.toml not found in ($config_dir)"} } if not ($subjects_toml | path exists) { error make -u {msg: $"subjects.toml not found in ($config_dir)"} } if ($number != nothing and $number < 1 ) { error make -u {msg: "Work number should be greater than 0"} } let subject_lower = ($subject | str downcase) let subjects = (open $subjects_toml | columns) if $subject_lower not-in $subjects { error make -u { msg: $"Subject '($subject)' not found in configuration" help: $"Available subjects: ($subjects | str join ', ')" } } } def generate-doc-toml [shortcode: string, subject: string, target_dir: string, number?: int] { let defaults = (open $"($config_dir)/defaults.toml") let subjects = (open $"($config_dir)/subjects.toml") let subject_data = ($subjects | get $subject) let work_type = ($defaults | get work_types | try { get $shortcode } catch { $shortcode }) { university: ($defaults | get university) subject: ($subject_data | get name), type: $work_type, number: $number mentors: ($subject_data | get mentors), authors: [($defaults | get default_author)] } | compact --empty | to toml | save $"($target_dir)/src/doc.toml" --force }