Skip to content

Commit 53b5a39

Browse files
clojure-cli: content enhancements
1 parent 53a1866 commit 53b5a39

File tree

3 files changed

+123
-97
lines changed

3 files changed

+123
-97
lines changed

docs/clojure-cli/built-in-commands.md

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# Clojure CLI Built-in commands
22

3-
`clojure` without any other arguments will run a REPL with a basic terminal prompt. `clj` is a wrapper script for the `clojure` command (`rlwrap` required) to add command history to the basic REPL prompt.
3+
`clojure` without any other arguments will run a REPL with a basic terminal prompt.
4+
5+
`clj` is a wrapper script for the `clojure` command (`rlwrap` required) to add command history to the basic REPL prompt.
6+
7+
`clojure -T:deps` to run one of several built-in commands to help work with Clojure CLI projects and libraries.
8+
9+
`clojure --help` list the available commands.
410

5-
`clojure -T:deps` to run one of several built-in commands to help work with Clojure CLI projects and libraries. `clojure --help` list the available commands.
611

712
`-X` execution option is used for the `:deps` aliases, running them via `clojure.exec` and limiting the class path to the current directory.
813

@@ -40,9 +45,10 @@ The list includes transitive dependencies, library dependencies of the project l
4045

4146
The `:aliases '[:alias/name(s)]'` argument will also list library dependencies from a given alias, either project alias or user alias.
4247

43-
```shell
44-
clojure -X:deps list
45-
```
48+
!!! NOTE ""
49+
```shell
50+
clojure -X:deps list
51+
```
4652

4753
??? EXAMPLE "Dependency list from Practicalli Service project template"
4854
```shell
@@ -56,7 +62,7 @@ clojure -X:deps list
5662
com.brunobonacci/mulog 0.9.0 (Apache-2.0)
5763
com.brunobonacci/mulog-adv-console 0.9.0 (Apache-2.0)
5864
com.brunobonacci/mulog-json 0.9.0 (Apache-2.0)
59-
com.cnuernber/charred 1.010
65+
com.cnuernber/charred 1.010
6066
com.cognitect/transit-clj 1.0.324 (Apache-2.0)
6167
com.cognitect/transit-java 1.0.343 (Apache-2.0)
6268
com.fasterxml.jackson.core/jackson-annotations 2.12.1 (Apache-2.0)
@@ -125,9 +131,10 @@ clojure -X:deps list
125131

126132
Use the `:aliases '[:alias/name(s)]'` option to also include the dependencies from a project or user alias. Showing the dependencies from an aliase can useful to identify conflicting dependencies when using an alias (unlikely but it could happen).
127133

128-
```shell
129-
clojure -X:deps list :aliases '[:dev/reloaded]'
130-
```
134+
!!! NOTE ""
135+
```shell
136+
clojure -X:deps list :aliases '[:dev/reloaded]'
137+
```
131138

132139
??? EXAMPLE "Dependency list from project and Practicalli :dev/reloaded alias"
133140
```shell
@@ -222,9 +229,10 @@ The tree includes transitive dependencies, library dependencies of the project l
222229

223230
The `:aliases '[:alias/name(s)]'` argument will also list library dependencies from a given alias, either project alias or user alias.
224231

225-
```shell
226-
clojure -X:deps tree
227-
```
232+
!!! NOTE ""
233+
```shell
234+
clojure -X:deps tree
235+
```
228236

229237
??? EXAMPLE "Dependency list from Practicalli Service project template"
230238
```shell
@@ -348,9 +356,10 @@ clojure -X:deps tree
348356

349357
Use the `:aliases` option with the Clojure CLI in the root of a project to show library dependencies for the project and the `:dev/reloaded` alias which could be useful if there are library conflicts when using an alias (unlikely but it could happen).
350358

351-
```shell
352-
clojure -X:deps tree :aliases '[:dev/reloaded]'
353-
```
359+
!!! NOTE ""
360+
```shell
361+
clojure -X:deps tree :aliases '[:dev/reloaded]'
362+
```
354363

355364
The `:aliases` option can be used to inspect the dependencies of a user alias, listing only dependencies from the specified aliases when run outside of a Clojure project.
356365

@@ -393,18 +402,20 @@ The `:aliases` option can be used to inspect the dependencies of a user alias, l
393402

394403
Add a jar file for a library to the local Maven repository, e.g. `~/.m2/repository`, making that library accessible to all other local projects.
395404

396-
```shell
397-
clojure -X:deps mvn-install :jar '"/path/to.jar"'`
398-
```
405+
!!! NOTE ""
406+
```shell
407+
clojure -X:deps mvn-install :jar '"/path/to.jar"'`
408+
```
399409

400410

401411
## Find Library Versions
402412

403413
Find the available versions of a given library in the form domain/library-name (domain is typically the company name or Git Service user or organisation name).
404414

405-
```clojure
406-
clojure -X:deps find-versions :lib clojure.java-time/clojure.java-time
407-
```
415+
!!! NOTE ""
416+
```clojure
417+
clojure -X:deps find-versions :lib clojure.java-time/clojure.java-time
418+
```
408419

409420

410421
## Prepare Source dependencies
@@ -413,36 +424,40 @@ Some dependencies will require a preparation step before they can be used on the
413424

414425
Projects that require preparation would have a configuration of the form:
415426

416-
```clojure
417-
{:paths ["src" "target/classes"]
418-
:deps/prep-lib {:alias :build
419-
:fn compile
420-
:ensure "target/classes"}}
421-
```
427+
!!! EXAMPLE
428+
```clojure
429+
{:paths ["src" "target/classes"]
430+
:deps/prep-lib {:alias :build
431+
:fn compile
432+
:ensure "target/classes"}}
433+
```
422434

423435
Including the top-level key `:deps/prep-lib` tells the tools.deps classpath construction that something extra is needed to prepare this lib and that can be performed by invoking the compile function in the :build alias. Once the prepare step has been done, it should create the path "target/classes" and that can be checked for completion.
424436

425437
Add a library dependency as with any other library (git or local/root):
426438

427-
```clojure
428-
{:deps {practicalli/library-name {:local/root "../needs-prep"}
429-
practicalli/library-name {:git/sha "../needs-prep"}}}
430-
```
439+
!!! EXAMPLE
440+
```clojure
441+
{:deps {practicalli/library-name {:local/root "../needs-prep"}
442+
practicalli/library-name {:git/sha "../needs-prep"}}}
443+
```
431444

432445

433446
`:deps prep` will built the library of any dependency that requires it
434447

435-
```shell
436-
clojure -X:deps prep
437-
```
448+
!!! NOTE ""
449+
```shell
450+
clojure -X:deps prep
451+
```
438452

439453
## POM file
440454

441455
Generate or update pom.xml with deps and paths
442456

443-
```shell
444-
clojure -X:deps mvn-pom
445-
```
457+
!!! NOTE ""
458+
```shell
459+
clojure -X:deps mvn-pom
460+
```
446461

447462

448463
## Resolve Git tags
@@ -451,9 +466,10 @@ clojure -X:deps mvn-pom
451466

452467
`-X:deps git-resolve-tags` updates git based dependencies in the project `deps.edn` file which use :git/tags key to the equivalent SHA commit values in the `:git/sha` key
453468

454-
```shell
455-
clojure -X:deps git-resolve-tags
456-
```
469+
!!! NOTE ""
470+
```shell
471+
clojure -X:deps git-resolve-tags
472+
```
457473

458474

459475
## References

docs/clojure-cli/defining-aliases.md

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,22 @@ Aliases can be used to :
2626
* `:mvn/local-repo` to [specify an alternative location for the Maven cache](https://github.com/practicalli/clojure-deps-edn#maven-local-repository)
2727
* `:aliases` - a map of optional libraries and tools, the key being the alias name and its value the configuration
2828

29-
3029
The installation of Clojure CLI contains a configuration
3130

3231
* adds `src` and `org.clojure/clojure` library
3332
* Maven Central & Clojars.org repository sources.
3433

35-
Configuration can be defined in a `deps.edn` file in the root of a Clojure project, applying only to that specific project.
36-
37-
A user `deps.edn` configuration can be used in any Clojure project and the `deps.edn` configuration file resides in either `$XDG_CONFIG_HOME/clojure` or `$HOME/.clojure`.
34+
Configuration available to all projects (or stand-alone tools) is defined in a user `deps.edn` configuration in either `$XDG_CONFIG_HOME/clojure` or `$HOME/.clojure`.
3835

36+
Project specific configuration is defined in a `deps.edn` file in the root of the Clojure project.
3937

4038
??? INFO "User configuration locations"
41-
User configuration is either `$XDG_CONFIG_HOME/clojure/deps.edn` or `$HOME/.clojure/deps.edn` locations and its aliases can be used for any Clojure project.
39+
If `XDG_CONFIG_HOME` environment variable is set, then the user configuration is `$XDG_CONFIG_HOME/clojure/deps.edn`
40+
41+
Otherwide the user configuration is `$HOME/.clojure/deps.edn`.
42+
43+
The `CLJ_CONFIG` environment variable will be used if set.
44+
4245

4346

4447
## Alias keys
@@ -118,7 +121,7 @@ Arguments can be nested within the `:exec-args` map, especially useful on projec
118121

119122
To run with the default arguments:
120123

121-
```
124+
```shell
122125
clojure -X:project/run
123126
```
124127

@@ -211,19 +214,27 @@ Additional arguments can be sent when running the command, e.g. `clojure -X:proj
211214

212215
The command line can over-ride the `:exec-fn` function configuration, allowing for a default configuration that can be easily over-ridden.
213216

214-
```clojure
215-
:project/new
216-
{:replace-deps {seancorfield/clj-new {:mvn/version "1.1.226"}}
217-
:main-opts ["-m" "clj-new.create"] ;; deprecated
218-
:ns-default clj-new
219-
:exec-fn create
220-
:exec-args {:template lib :name practicalli/playground}
221-
}
222-
```
217+
!!! EXAMPLE
218+
```clojure
219+
:project/new
220+
{:replace-deps {seancorfield/clj-new {:mvn/version "1.1.226"}}
221+
:main-opts ["-m" "clj-new.create"] ;; deprecated
222+
:ns-default clj-new
223+
:exec-fn create
224+
:exec-args {:template lib :name practicalli/playground}
225+
}
226+
```
223227

224228
!!! HINT "Keyword naming"
225-
Legal Clojure keyword names can be used for an alias. Multiple aliases can be chained together with the `clojure` command. For example in this command we are combining three aliases:
226-
`clojure -M:task/path:task/deps:build/options`
229+
Alias names are a Clojure keyword, which can be qualified to provide context, e.g. `:project/create`.
230+
231+
Aliases are composable (chained together) and their path and deps configurations merged:
232+
233+
```shell
234+
clojure -M:task/path:task/deps:build/options
235+
```
236+
237+
When multiple aliases contain a `:main-opts` configurations they are not merged, the configuration in the last alias is used
227238

228239

229240
## Resources

docs/clojure-cli/execution-options.md

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,48 @@
22

33
Execution options (`-A` `-M` `-P` `-T` `-X`) define how aliases are used with the Clojure CLI. Aliases are included via one of these execution options and each option can affect how the alias is used.
44

5-
Since the [first documented released](https://clojure.org/releases/tools#v1.10.1.510) which exclusively used the `-A` execution option to include aliases, the design has evolved to provide specific execution options to run code via clojure.main (`-M`) and clojure.exec (`-X`). In July 2021, the ability to run tools (`-T`) independent from the Clojure project classpath was also introduced.
5+
??? INFO "Clojure CLI design evolution"
6+
The [first documented released](https://clojure.org/releases/tools#v1.10.1.510) used the `-A` execution option to include aliases.
67

7-
The exec-opts command line flags have evolved to enable these features, so lets explore those flags and show how each flag is typically used.
8+
The design has evolved to provide specific execution options to run code via clojure.main (`-M`) and clojure.exec (`-X`).
89

9-
??? INFO "Understanding Aliases"
10-
[Understand aliases in more detail](https://practical.li/blog/posts/clojure-cli-tools-understanding-aliases/)
10+
In July 2021 the ability to run tools (`-T`) independent from the Clojure project classpath was introduced.
1111

1212

1313
## Quick summary
1414

15-
Use `-M` for code that should be called via the `-main` function in a specified namespace, passing string-based arguments.
15+
`-M` uses `clojure.main` to call the `-main` function of the specified namespace, passing string-based arguments.
1616

17-
Use `-X` to run a specified function, passing arguments as key and value pairs (not hash-maps yet)
17+
`-X` uses `clojure.exec` to call a fully qualified function, passing arguments as key and value pairs
1818

19-
Use `-T` to run a tool independently from any libraries defined in a project. Only the libraries in the alias are included in the ClassPath. The path is defined as `"."` by default.
19+
`-T` runs a tool independent from project dependencies. Only the libraries in the alias are included in the Class Path. The path is defined as `"."` by default.
2020

21-
Use `-P` to download libraries on the class path, including those from specified aliases
21+
`-P` downloads library dependencies, including those from specified aliases
2222

23-
Use `-A` in the specific case of running a basic terminal UI REPL with the `clojure` command (or `clj` wrapper).
23+
`-A` in the specific case of running a basic terminal UI REPL with the `clojure` command or `clj` wrapper.
2424

25-
Common aliases will be used to explain the use of these execution options in more detail.
2625

26+
## clojure.main
2727

28-
## Form of clojure.exec command line arguments
28+
`-M` flag instructs Clojure CLI tools to use `clojure.main` to run Clojure code.
2929

30-
Clojure.exec command takes Key/value pairs read as EDN values (extensible data notation that is the base syntax of Clojure).
31-
32-
Number values and keywords can be parsed from the command line
33-
34-
Arguments that are vectors and hash maps should be wrapped in single quotes to avoid the command line shell splitting arguments at spaces, e.g. `'[:a :b]'`, `'{:c 1}'`.
35-
36-
The double quotes in an EDN string must be wrapped by single quotes, along with vectors and hash-maps
37-
38-
- `'"strings in double quotes surround by single quotes"'`
39-
- `'[:vectors :with-single-quotes]'`
40-
- `'{:hash-maps :with-single-quotes}'`
30+
The `--main` or `-m` flag is an argument to `clojure.main` which specifies the namespace to search for a `-main` function.
4131

32+
`clojure.main/main` function searches for a `-main` function in the given namespace, e.g. `--main pracicalli.gameboard.service`
4233

43-
## Run clojure.main project
44-
45-
`-M` execution option uses `clojure.main` to run Clojure code.
34+
> If the -main function is not found or the namespace is not specified, then the `clojure` command will run a REPL session.
4635
4736
Run a project with the main namespace `practicalli.sudoku-solver`, without any additional aliases on the command line
4837

49-
```shell
50-
clojure -M -m practicalli.sudoku-solver
51-
```
52-
53-
`-M` flag instructs Clojure CLI tools to use `clojure.main` to run the Clojure code.
54-
55-
`-m` flag is an argument to `clojure.main` which specifies the namespace to search for a `-main` function.
56-
38+
!!! NOTE ""
39+
```shell
40+
clojure -M -m practicalli.sudoku-solver
41+
```
5742

58-
Adding a `:project/run` alias to the project `deps.edn` file provides a simpler way to run the project on the command line
43+
Add `:project/run` alias to the project `deps.edn` file to provide a simpler way to run the project on the command line
5944

6045
```clojure
61-
:project/run {:main-opts ["-m" "practicalli.sudoku-solver"]}
46+
:project/run {:main-opts ["--main" "practicalli.sudoku-solver"]}
6247
```
6348

6449
Now the project code can be run using the simple command line form
@@ -68,13 +53,10 @@ clojure -M:project/run
6853
```
6954

7055

71-
### How clojure.main works
56+
### Using clojure.main
7257

73-
`clojure.main/main` function searches for a `-main` function in the given namespace (`-m fully-qualified.namespace`)
7458

75-
If no -main function is found or the namespace is not specified, then a REPL session is run. This is the approach Leiningen has always used and still uses today.
76-
77-
> [`clojure.main` namespace](https://clojure.org/reference/repl_and_main) has been the way Clojure code was run (including a REPL) for most of its history. This is now evolving with the addition of [clojure.exec](#clojure-exec--X).
59+
> [`clojure.main` namespace](https://clojure.org/reference/repl_and_main) has been the way Clojure code was run (including a REPL) for most of its history. This is now evolving with the addition of [clojure.exec](#clojureexec).
7860
> [clojure.main has other features, as covered in the REPL and main entrypoints article](https://clojure.org/reference/repl_and_main)) on clojure.org.
7961
8062

@@ -137,6 +119,23 @@ If the command line includes the `-m` flag with a namespace, then that namespace
137119

138120
Any function on the class path can be called and is passed a hash-map as an argument. The argument hash-map is either specified in an alias using `:exec-args` or assembled into a hash-map from key/value pairs on the command line. Key/values from the command line are merged into the `:exec-args` map if it exists, with the command line key/values taking precedence.
139121

122+
### clojure.exec arguments
123+
124+
Clojure.exec command takes key value pairs read as EDN values (extensible data notation that is the base syntax of Clojure).
125+
126+
Number values and keywords can be parsed from the command line
127+
128+
Arguments that are vectors and hash maps should be wrapped in single quotes to avoid the command line shell splitting arguments at spaces, e.g. `'[:a :b]'`, `'{:c 1}'`.
129+
130+
The double quotes in an EDN string must be wrapped by single quotes, along with vectors and hash-maps
131+
132+
- `'"strings in double quotes surround by single quotes"'`
133+
- `'[:vectors :with-single-quotes]'`
134+
- `'{:hash-maps :with-single-quotes}'`
135+
136+
137+
### clojure.exec examples
138+
140139
Call the `status` function from the namespace `practicalli.service`, which is on the classpath in the practicalli.service project
141140

142141
```shell

0 commit comments

Comments
 (0)