Created
April 12, 2022 09:49
-
-
Save minimal/5d3e36931af433f993cb9ebc47c5830b to your computer and use it in GitHub Desktop.
babashka script to symlink shell.nix, .envrc etc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns user | |
(:require [babashka.fs :as fs] | |
[clojure.java.shell :refer [sh]] | |
[clojure.string :as str] | |
[clojure.tools.cli :refer [parse-opts]])) | |
(def allowed-files | |
#{"envrc" "shell.nix" "justfile" "Makefile"}) | |
(def mappings | |
{"envrc" ".envrc"}) | |
(defn map-file [f] | |
(let [f-name (fs/file-name f)] | |
(mappings f-name f-name))) | |
(def metadata-repo "~/code/scratch/nix-shells/") | |
(defn link-dir | |
[args] | |
(let [target-dir (or args ".") ;; usually run from the repo we want to add shell.nix to | |
metadata-dir (fs/path (fs/expand-home metadata-repo) (-> target-dir fs/canonicalize fs/file-name))] | |
(if-not (fs/exists? metadata-dir) | |
(do (println "No metadata exists for this dir") | |
(System/exit 1)) | |
(let [files (fs/list-dir metadata-dir) | |
filtered (filter (fn [f] (contains? allowed-files (fs/file-name f))) files) | |
targets (for [f filtered] | |
[(fs/canonicalize f) (fs/path target-dir (map-file f))])] | |
(println (format "Will link files: %s to %s " (pr-str (map str filtered)) target-dir)) | |
(doseq [[path target] targets] | |
(if (fs/exists? target) | |
(println "Already exists: " (str target)) | |
(do | |
(println (format "will create symlink from %s to %s " path target)) | |
(fs/create-sym-link target path)))))))) | |
(when (= *file* (System/getProperty "babashka.file")) | |
(link-dir *command-line-args*) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment