一个 Clojure 项目构建了几个相关的小工具
A Clojure project building several related small tools
我正在开发一个具有通用功能库的 Clojure 项目,但我想通过许多小型命令行工具使用该库。
我们称它们为工具 1、工具 2、工具 3。
这些工具中的每一个都包装了大部分库。
我应该如何组织我的 Clojure 源代码以及我的 project.clj 文件中需要什么?
现在我的代码在
src/projectname/core.cljc ; a default executable
cljc/projectname/lib1.cljc
cljc/projectname/lib2.cljc ; cljc because I want to use from clojurescript too later
等等
我应该把 tool1.clj、tool2.clj 等放在哪里?
我的 defproject 是
(defproject
....
:aot [projectname.core]
:main project.core )
我需要输入什么来告诉它我想让这三个独立的可执行工具?
你可以做到这一点using the lein-exec library。
首先,像这样(或类似的)设置 ~/bin:
> ls -ldF ~/bin/l*
lrwxrwxrwx 1 alan alan 26 Oct 31 08:29 /home/alan/bin/lein -> /home/alan/cool/tools/lein*
lrwxrwxrwx 1 alan alan 31 Oct 31 08:29 /home/alan/bin/lein-exec -> /home/alan/cool/tools/lein-exec*
lrwxrwxrwx 1 alan alan 33 Oct 31 08:29 /home/alan/bin/lein-exec-p -> /home/alan/cool/tools/lein-exec-p*
我有指向实际文件的符号链接,但您可以直接复制它们。当然,请确保 ~/bin 在您的路径中。
然后像下面这样写一个可执行的clojure "script"。当然,必须是可执行的:
> ls -l say-hello
-rwxrwxr-x 1 alan alan 212 Nov 2 09:10 say-hello
> cat say-hello
#!/usr/bin/env lein-exec
(defn say-hello [name]
(println (format "Hello from the command line, %s!" name)))
(do
; *command-line-args* = <cmd> <arg1> <arg2> ...
(say-hello (second *command-line-args*)))
我们出发吧!
> ./say-hello buckaroo
Hello from the command line, buckaroo!
至于项目组织,我会从简单开始(与任何项目一样!)。也许从只有 1 个源文件开始,每个 "script" 有不同的函数作为入口点。随着项目的发展,你会更容易看到你想在哪里突破不同 namespaces/files.
更新
你也可以用lein uberjar
,然后直接调用java:
(ns clj.core
(:gen-class))
(defn say-hello [name]
(println (format "Hello from the command line, %s!" name)))
(defn -main [& args]
(say-hello (first args)))
> lein uberjar
Compiling 1 source files to /home/alan/clj/target/uberjar/classes
Compiling clj.core
Created /home/alan/clj/target/uberjar/clj-0.1.0-SNAPSHOT.jar
Created /home/alan/clj/target/uberjar/clj-0.1.0-SNAPSHOT-standalone.jar
> java -jar /home/alan/clj/target/uberjar/clj-0.1.0-SNAPSHOT-standalone.jar pardner
Hello from the command line, pardner!
所以只需将 java
命令放入 shell 脚本中即可启动。另请注意,:gen-class
是必需的,args
不再包含脚本名称。当然,您必须同时部署脚本文件和uberjar。
更新#2
你也可以使用这个版本:
来源:
(ns clj.core
(:gen-class))
(defn say-howdy [args]
(println (format "Howdy, %s!" (first args))))
(defn give-reply [args]
(println (format "Back at ya, %s!" (first args))))
(defn -main [& args]
(let [method-name (first args)
message (second args) ]
(cond
(= method-name "say-howdy" ) (say-howdy (rest args))
(= method-name "give-reply") (give-reply (rest args))
:else (throw (NoSuchMethodException. (str "clj.core: invalid method='" method-name \')))))
)
脚本:
> ls -l say*
-rwxrwxr-x 1 alan alan 212 Nov 2 16:24 say-hello
-rwxrwxr-x 1 alan alan 104 Nov 2 16:35 say-howdy
-rwxrwxr-x 1 alan alan 105 Nov 2 16:35 say-reply
> cat ./say-howdy
#!/bin/bash
java -jar /home/alan/clj/target/uberjar/clj-0.1.0-SNAPSHOT-standalone.jar say-howdy pardner
> cat ./say-reply
#!/bin/bash
java -jar /home/alan/clj/target/uberjar/clj-0.1.0-SNAPSHOT-standalone.jar give-reply
运行 工具:
~/clj > ./say-howdy
Howdy, pardner!
~/clj > ./say-reply
Back at ya, null!
~/clj > ./say-reply buckaroo
Back at ya, buckaroo!
使脚本更智能并决定如何部署 N 个脚本和单个 JAR 文件留作 reader.
的练习。
;)
我正在开发一个具有通用功能库的 Clojure 项目,但我想通过许多小型命令行工具使用该库。
我们称它们为工具 1、工具 2、工具 3。
这些工具中的每一个都包装了大部分库。
我应该如何组织我的 Clojure 源代码以及我的 project.clj 文件中需要什么?
现在我的代码在
src/projectname/core.cljc ; a default executable
cljc/projectname/lib1.cljc
cljc/projectname/lib2.cljc ; cljc because I want to use from clojurescript too later
等等
我应该把 tool1.clj、tool2.clj 等放在哪里?
我的 defproject 是
(defproject
....
:aot [projectname.core]
:main project.core )
我需要输入什么来告诉它我想让这三个独立的可执行工具?
你可以做到这一点using the lein-exec library。
首先,像这样(或类似的)设置 ~/bin:
> ls -ldF ~/bin/l*
lrwxrwxrwx 1 alan alan 26 Oct 31 08:29 /home/alan/bin/lein -> /home/alan/cool/tools/lein*
lrwxrwxrwx 1 alan alan 31 Oct 31 08:29 /home/alan/bin/lein-exec -> /home/alan/cool/tools/lein-exec*
lrwxrwxrwx 1 alan alan 33 Oct 31 08:29 /home/alan/bin/lein-exec-p -> /home/alan/cool/tools/lein-exec-p*
我有指向实际文件的符号链接,但您可以直接复制它们。当然,请确保 ~/bin 在您的路径中。
然后像下面这样写一个可执行的clojure "script"。当然,必须是可执行的:
> ls -l say-hello
-rwxrwxr-x 1 alan alan 212 Nov 2 09:10 say-hello
> cat say-hello
#!/usr/bin/env lein-exec
(defn say-hello [name]
(println (format "Hello from the command line, %s!" name)))
(do
; *command-line-args* = <cmd> <arg1> <arg2> ...
(say-hello (second *command-line-args*)))
我们出发吧!
> ./say-hello buckaroo
Hello from the command line, buckaroo!
至于项目组织,我会从简单开始(与任何项目一样!)。也许从只有 1 个源文件开始,每个 "script" 有不同的函数作为入口点。随着项目的发展,你会更容易看到你想在哪里突破不同 namespaces/files.
更新
你也可以用lein uberjar
,然后直接调用java:
(ns clj.core
(:gen-class))
(defn say-hello [name]
(println (format "Hello from the command line, %s!" name)))
(defn -main [& args]
(say-hello (first args)))
> lein uberjar
Compiling 1 source files to /home/alan/clj/target/uberjar/classes
Compiling clj.core
Created /home/alan/clj/target/uberjar/clj-0.1.0-SNAPSHOT.jar
Created /home/alan/clj/target/uberjar/clj-0.1.0-SNAPSHOT-standalone.jar
> java -jar /home/alan/clj/target/uberjar/clj-0.1.0-SNAPSHOT-standalone.jar pardner
Hello from the command line, pardner!
所以只需将 java
命令放入 shell 脚本中即可启动。另请注意,:gen-class
是必需的,args
不再包含脚本名称。当然,您必须同时部署脚本文件和uberjar。
更新#2
你也可以使用这个版本:
来源:
(ns clj.core
(:gen-class))
(defn say-howdy [args]
(println (format "Howdy, %s!" (first args))))
(defn give-reply [args]
(println (format "Back at ya, %s!" (first args))))
(defn -main [& args]
(let [method-name (first args)
message (second args) ]
(cond
(= method-name "say-howdy" ) (say-howdy (rest args))
(= method-name "give-reply") (give-reply (rest args))
:else (throw (NoSuchMethodException. (str "clj.core: invalid method='" method-name \')))))
)
脚本:
> ls -l say*
-rwxrwxr-x 1 alan alan 212 Nov 2 16:24 say-hello
-rwxrwxr-x 1 alan alan 104 Nov 2 16:35 say-howdy
-rwxrwxr-x 1 alan alan 105 Nov 2 16:35 say-reply
> cat ./say-howdy
#!/bin/bash
java -jar /home/alan/clj/target/uberjar/clj-0.1.0-SNAPSHOT-standalone.jar say-howdy pardner
> cat ./say-reply
#!/bin/bash
java -jar /home/alan/clj/target/uberjar/clj-0.1.0-SNAPSHOT-standalone.jar give-reply
运行 工具:
~/clj > ./say-howdy
Howdy, pardner!
~/clj > ./say-reply
Back at ya, null!
~/clj > ./say-reply buckaroo
Back at ya, buckaroo!
使脚本更智能并决定如何部署 N 个脚本和单个 JAR 文件留作 reader.
的练习。;)