clojureでtodo cliをつくること


最近、関数プログラミング言語であるクロジュールを学ぶことにしました.このブログで私はクロゼアを使用してスタンドアローンのTODOクライアントを作成する私の経験を文書化しました.
私は新しいプロジェクトを作成することから始めましたLeiningen .

Leiningen is the easiest way to use Clojure. With a focus on project automation and declarative configuration, it gets out of your way and lets you focus on your code.


$ lein new app todo
上記のコマンドは、アプリケーションプロジェクトテンプレートに基づいて新しいプロジェクトを作成するためにLeiningenを要求します.

フォルダ構造
プロジェクトフォルダ構造を見てみましょう
.
├── CHANGELOG.md
├── doc
│   └── intro.md
├── example.txt
├── hello.txt
├── LICENSE
├── project.clj
├── README.md
├── resources
├── src
│   └── todo
│       └── core.clj
└── test
    └── todo
        └── core_test.clj
主なアプリケーションロジックはsrc/todo/coreの下にあります.CLJ
(ns todo.core
  (:gen-class)
  (:require [clojure.string :refer [split]])
  (:require [clojure.java.io :refer [writer reader]]))

(use 'clojure.pprint)

(def file-location (System/getenv "TODO_LOCATION"))

(defn now [] (new java.util.Date))

(defn add-content
  "appends content to todo file"
  [file-location text-content]
  (with-open [file (writer file-location :append true)]
    (.write file (str text-content "\t" (now) "\n"))))

(defn print-helper
  "Converts line content to a row obj"
  [line-content]
  (let [[todo created_at] (split line-content #"\t")]
    {:todo todo :created_at (or created_at "UNKNOWN")}))

(defn read-content
  "reads content from todo file"
  [file-location]
  (with-open [file (reader file-location)]
    (let [file-content (slurp file)]
      (print-table
       (map print-helper
            (split file-content #"\n"))))))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (if (nil? file-location)
    (throw (AssertionError. "empty $TODO_LOCATION")))
  (case (first args)
    "a" (do
          (add-content file-location (second args))
          (read-content file-location))
    "ls" (read-content file-location)
    (println "Choose either a or ls")))
各関数の意味に入る前に、アプリを行うことができます参照してください
$ lein run a "practise clojure"

|            :todo |                  :created_at |
|------------------+------------------------------|
| practise clojure | Fri Jul 24 17:51:27 BST 2020 |

$ lein run ls

|            :todo |                  :created_at |
|------------------+------------------------------|
| practise clojure | Fri Jul 24 17:51:27 BST 2020 |

アプリケーションの実行
走るlein run-main 関数とコマンドライン引数を渡します.メイン関数に渡すことができる2つの操作可能なコマンドがあります
テキストコンテンツを追加する
リスト:すべてのto doを
それぞれの関数が何をするかを見てみましょう.私は、アプリケーションの主なユースケースを扱う2つの機能を持っています

コンテンツを追加
関数はファイル位置とテキスト内容を引数として受け入れる.それから、それは内容を加えられた時間とともにファイルにテキスト内容を書きます.内容はタブで区切られ、最後に改行が追加されます.

内容を読む
関数はファイルの場所を引数として受け入れる.その後、すべてのファイルの内容を読み取るslurp . すべての行を分割し、結果を出力する
私はヘルパー機能のカップルを持って

現在
関数は新しい日付オブジェクトを

プリントヘルパー
機能は、私がテーブル形式で私のto - do内容を印刷することができるように、地図にテキスト内容を変えるのを助けます

アプリのビルド
プロジェクトファイルと依存関係をjarファイルにパッケージ化するには、実行できます
$ lein uberjar
Compiling todo.core
Created /mnt/d/todo-cli/target/uberjar/todo-0.1.0-SNAPSHOT.jar
Created /mnt/d/todo-cli/target/uberjar/todo-0.1.0-SNAPSHOT-standalone.jar
その後、アプリケーションを使用して実行することができます
$ java -jar target/uberjar/todo-0.1.0-SNAPSHOT-standalone.jar
私はそれがかなりのように構築されたアプリを実行するには退屈だった気づいた.我々は我々のアプリを実行することができればそれはかなりクールだろうjava -jar 毎回.
私はこれを可能にし、見つけられる解決策を探しましたlein-bin .

Leiningen plugin for producing standalone console executables that work on OS X, Linux, and Windows.
It basically just takes your uberjar and stuffs it in another file with some fancy magical execution stuff.


私はleinビンを私の~/.プロフィール.CLJ .
{:user {:plugins [[lein-bin "0.3.5"]]}}
修正マイプロジェクトCLJは以下のようになります
(defproject todo "0.1.0-SNAPSHOT"
  :description "a todo cli"
  :url "https://github.com/PrasannaGnanaraj/todo-cli"
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url "https://www.eclipse.org/legal/epl-2.0/"}
  :dependencies [[org.clojure/clojure "1.10.1"]]
  :main ^:skip-aot todo.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}}
  :bin {:name "todo"
        :bin-path "~/bin"
        :bootclasspath true})
binキーを使用すると、スタンドアロン実行可能ファイル名を指定できます.実行可能ファイルを配置するパス
あなたが走るならば
$ lein bin                                                                                                                                                                                  ok | at 18:24:21
Compiling todo.core
Created /mnt/d/todo-cli/target/uberjar/todo-0.1.0-SNAPSHOT.jar
Created /mnt/d/todo-cli/target/uberjar/todo-0.1.0-SNAPSHOT-standalone.jar
Creating standalone executable: /mnt/d/todo-cli/target/default/todo
Copying binary to #object[java.io.File 0x7c40ffef /home/user/bin]
その後、スタンドアロン実行可能なようなアプリケーションを使用できるようになります
$ todo a "practise clojure"
|            :todo |                  :created_at |
|------------------+------------------------------|
| practise clojure | Fri Jul 24 17:51:27 BST 2020 |
全体的に私はClojureとその機能的プログラミングパラダイムを見つけました
  • 責任の観点から明確な分離
  • 簡単に変更/リファクタリング/の上に構築
  • 理解しやすい/デバッグ
  • 私はクロゼアを実際の生活のソフトウェアの問題に適用しようとすることによって学習し、調査を続ける.
    Githubのソースコードを見つけることができますhere
    また、Iveは非常に私のclojure学習パスで有用なこの本を見つけた.
    Learn to Program the World's Most Bodacious Language with Clojure for the Brave and True