From 9556e48e34ba1f1d815e1ce046cce90a4a32bfbc Mon Sep 17 00:00:00 2001 From: Joel Holdbrooks Date: Tue, 13 Aug 2013 18:20:57 -0700 Subject: [PATCH] Begin work on command line version (cljs) --- bin/.gitkeep | 0 project.clj | 22 ++++++++++++++++-- src/{ => clj}/frak.clj | 11 +++++---- src/cljs/frak/cli.cljs | 52 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 bin/.gitkeep rename src/{ => clj}/frak.clj (89%) create mode 100644 src/cljs/frak/cli.cljs diff --git a/bin/.gitkeep b/bin/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/project.clj b/project.clj index 1f68073..521e26d 100644 --- a/project.clj +++ b/project.clj @@ -3,6 +3,24 @@ :url "http://github.com/noprompt/frak" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} - :dependencies [[org.clojure/clojure "1.4.0"]] + :dependencies [[org.clojure/clojure "1.5.1"]] + :plugins [[lein-cljsbuild "0.3.2"]] + :source-paths ["src/clj"] :profiles {:dev {:dependencies [[criterium "0.4.1"]]}} - :main frak.cli ) + :cljsbuild {:crossovers [frak] + :crossover-path "crossovers" + :crossover-jar true + :builds [{:id "dev" + :source-paths ["src/clj" "src/cljs"] + :compiler {:output-to "bin/frak.dev.js" + :optimizations :simple + :pretty-print true + :target :nodejs}} + + {:id "prod" + :source-paths ["src/clj" "src/cljs"] + :compiler {:output-to "bin/frak.prod.js" + :optimizations :simple + :pretty-print false + :target :nodejs}}]} + :main frak.cli) diff --git a/src/frak.clj b/src/clj/frak.clj similarity index 89% rename from src/frak.clj rename to src/clj/frak.clj index 44cac86..928859f 100644 --- a/src/frak.clj +++ b/src/clj/frak.clj @@ -87,8 +87,11 @@ (defn pattern "Construct a regular expression from a collection of strings." ([strs] - (pattern strs false)) - ([strs capture?] + (pattern strs {:capture? false, :exact? false})) + ([strs opts] {:pre [(every? string? strs)]} - (binding [*capture* capture?] - (-> strs build-trie render-trie str re-pattern)))) + (let [pattern (binding [*capture* (:capture? opts)] + (-> strs build-trie render-trie str))] + (re-pattern (if (:exact? opts) + (str "^" pattern "$") + pattern))))) diff --git a/src/cljs/frak/cli.cljs b/src/cljs/frak/cli.cljs new file mode 100644 index 0000000..478a21e --- /dev/null +++ b/src/cljs/frak/cli.cljs @@ -0,0 +1,52 @@ +(ns frak.cli + (:require [clojure.string :as s] + [frak])) + +(def FLAGS + [[["-e" "--exact"] "Generated pattern requires an exact match"] + [["-c" "--capture"] "Generated pattern captures"] + [["-h" "--help"] "Display this help message"]]) + +(def flag-re (frak/pattern (mapcat first FLAGS))) + +(defn parse-args [args] + (split-with #(re-matches flag-re %) args)) + +(defn flag-val [s] + (condp re-seq s + #"-(?:e|-exact)" {:exact? true} + #"-(?:c|-capture)" {:capture? true} + #"-(?:h|-help)" {:help? true} + {})) + +(defn flags->opts [flags] + (reduce + (fn [m flag] + (merge m (flag-val flag))) + {} + flags)) + +(def summary + (reduce + (fn [message [flags info]] + (format "%s\t%s\t%s\n" message (s/join ", " flags) info)) + "Usage: frak \n\n" + FLAGS)) + +(defn exit [code] + (.exit js/process code)) + +(defn help [] + (println summary) + (exit 0)) + +(defn start + [& args] + (let [[flags words] (parse-args args) + opts (flags->opts flags)] + (if (or (empty? args) (:help? opts)) + (help) + (let [pat (str (frak/pattern words opts))] + (println (subs pat 1 (dec (count pat)))))))) + +(set! *main-cli-fn* start) -- 2.25.1