GithubHelp home page GithubHelp logo

Comments (5)

teodorlu avatar teodorlu commented on August 17, 2024

I get correct indents when I have zero aliases other than :kaocha, but wrong indents when I have other aliases.

Good behavior:

$ cat deps.edn 
{:deps {}}
$ neil-dev add kaocha
"adding kaocha"
If you wish to create a `bin/kaocha` file, copy and run the following:

mkdir -p bin && \
echo '#!/usr/bin/env bash
clojure -M:kaocha "$@"' > bin/kaocha && \
chmod +x bin/kaocha
$ cat deps.edn 
{:deps {}
 :aliases
 {:kaocha ;; added by neil
  {:extra-deps {lambdaisland/kaocha {:mvn/version "1.91.1392"}}
   :main-opts ["-m" "kaocha.runner"]}}}

Bad behavior:

$ cat deps.edn 
{:deps {}
 :aliases
 {:dev {}}}
$ neil-dev add kaocha
"adding kaocha"
If you wish to create a `bin/kaocha` file, copy and run the following:

mkdir -p bin && \
echo '#!/usr/bin/env bash
clojure -M:kaocha "$@"' > bin/kaocha && \
chmod +x bin/kaocha
$ cat deps.edn 
{:deps {}
 :aliases
 {:dev {}

 :kaocha ;; added by neil
 {:extra-deps {lambdaisland/kaocha {:mvn/version "1.91.1392"}}
  :main-opts ["-m" "kaocha.runner"]}}}

from neil.

teodorlu avatar teodorlu commented on August 17, 2024

babashka.neil/add-alias appears to be the function to understand:

neil/src/babashka/neil.clj

Lines 131 to 166 in 9a79582

(defn add-alias [opts alias-kw alias-contents]
(ensure-deps-file opts)
(let [edn-string (edn-string opts)
edn-nodes (edn-nodes edn-string)
edn (edn/read-string edn-string)
alias (or (:alias opts)
alias-kw)
existing-aliases (get-in edn [:aliases])
alias-node (r/parse-string
(str (when (seq existing-aliases) "\n ")
alias
" ;; added by neil"))]
(if-not (get existing-aliases alias)
(let [s (-> (if-not (seq existing-aliases)
; If there are no existing aliases, we assoc an empty map
; before updating to prevent borkdude.rewrite-edn/update
; from removing the newline preceding the :aliases key.
(r/assoc edn-nodes :aliases {})
edn-nodes)
(r/update :aliases
(fn [aliases]
(let [s (rw/indent alias-contents 1)
alias-nodes (r/parse-string s)
aliases' (r/assoc aliases alias-node alias-nodes)]
(if-not (seq existing-aliases)
; If there are no existing aliases, add an
; explicit newline after the :aliases key.
(r/parse-string (str "\n" (rw/indent (str aliases') 1)))
aliases'))))
str)
s (rw/clean-trailing-whitespace s)
s (str s "\n")]
(spit (:deps-file opts) s))
(do (println (format "[neil] Project deps.edn already contains alias %s" (str alias ".")))
::update))))

from neil.

teodorlu avatar teodorlu commented on August 17, 2024

@borkdude I feel tempted to drop the previous logic that tried to compute the correct in favor of a dumb approach: repeatedly using rewrite-edn/assoc-in, and accepting the result. If we go this way, newline behavior will change.

Code before and after

Previous add-alias impl relies on a hard-coded 1 value for indention, which I think is causing problems:

https://github.com/teodorlu/neil/blob/f1ec6afac0f666e068b13ec7b4fd72e8793d98be/src/babashka/neil.clj#L131-L166

Proposal: instead, assume the user already has a deps.edn file they are okay with, and that we can add content from the new map by adding one key + value per line:

https://github.com/teodorlu/neil/blob/e3dd00618a174938194509794b64f5aa46303ef1/src/babashka/neil.clj#L130-L177

Behavior example

No existing deps.edn file.

Existing behavior:

$ neil add kaocha
If you wish to create a `bin/kaocha` file, copy and run the following:

mkdir -p bin && \
echo '#!/usr/bin/env bash
clojure -M:kaocha "$@"' > bin/kaocha && \
chmod +x bin/kaocha
$ cat deps.edn 
{:deps {}
 :aliases
 {:kaocha ;; added by neil
  {:extra-deps {lambdaisland/kaocha {:mvn/version "1.91.1392"}}
   :main-opts ["-m" "kaocha.runner"]}}}

Proposed behavior:

$ rm deps.edn 
$ neil-dev add kaocha
If you wish to create a `bin/kaocha` file, copy and run the following:

mkdir -p bin && \
echo '#!/usr/bin/env bash
clojure -M:kaocha "$@"' > bin/kaocha && \
chmod +x bin/kaocha
$ cat deps.edn       
{:deps {}
 :aliases {:kaocha {:extra-deps {lambdaisland/kaocha {:mvn/version "1.91.1392"}}
                    :main-opts ["-m" "kaocha.runner"]}}}

Thoughts?

Would you like to keep the current newline behavior?

from neil.

borkdude avatar borkdude commented on August 17, 2024

Sounds good. Can you also test this with an already existing deps.edn file?

from neil.

teodorlu avatar teodorlu commented on August 17, 2024

Sounds good.

👍

Can you also test this with an already existing deps.edn file?

I will 👍

from neil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.