Skip to content

Commit

Permalink
feat: update cli to support generating for multi languages
Browse files Browse the repository at this point in the history
  • Loading branch information
krvital committed Aug 8, 2024
1 parent e599c33 commit 52fa89f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
61 changes: 41 additions & 20 deletions src/aidbox_sdk/cli.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
(ns aidbox-sdk.cli
(:require
[clojure.tools.cli :as cli]
[clojure.string :as str]
[aidbox-sdk.generator :as generator]))

(def cli-options
[["-a" "--auth BASE64_string" "Base64 of username:password"
[["-a" "--auth-token BASE64_string" "Base64 of username:password"
:validate [(complement nil?) "auth token is required"]]
["-o" "--output-dir Output directory" "Output directory"
:default "out"]
["-h" "--help"]])

(defn validate-args [[input output]]
(cond-> []
(nil? input) (conj "Please provide input argument")
(nil? output) (conj "Please provide output argument")))
(def supported-commands #{"generate"})
(def supported-languages #{"dotnet" "java" "typescript" "python"})

(defn validate-args [args]
(let [[command target-language input] args]
(cond-> []
(not= "generate" command)
(conj (str "Please provide one of the supported commands: "
(str/join ", " supported-commands)))

(not (contains? supported-languages target-language))
(conj (str "Please provide one of the supported target languages: "
(str/join ", " supported-languages)))

(nil? input)
(conj "Please provide input argument"))))

(defn print-errors [errors]
(binding [*out* *err*]
Expand All @@ -22,38 +37,44 @@
(println "Generate Aidbox SDK from FHIR schemas")
(println)
(println "USAGE")
(println "aidbox-sdk <input-source> <output-dir> [options]")
(println "aidbox-sdk generate <target-language> <input-source> [options]")
(println)
(println "OPTIONS")
(println summary))

(defn build [options arguments]
(let [[input output] arguments]
(println "Building FHIR SDK...")
(generator/build-all! :auth (:auth options)
:input input
:output output)
(println "Finished succesfully!")))
(defn generate [target-language input options]
(println "Building FHIR SDK...")
(generator/build-all! :input input
:target-language target-language
:auth (:auth-token options)
:output (:output-dir options))
(println "Finished succesfully!"))

(defn app [{:keys [exit]} args]
(let [{:keys [options arguments summary errors]} (cli/parse-opts args cli-options)
errors (into errors
(validate-args args))]
errors (into errors (validate-args args))
[command target-language input] arguments]
(cond
(:help options)
(do (help summary)
(exit 0))

(seq errors)
(and
(seq arguments)
(seq errors))
(do
(print-errors errors)
(exit 1))

:else
(= command "generate")
(try
(build options arguments)
(generate target-language input options)
(exit 0)

(catch Throwable e
(print-errors [e])
(exit 1))))))
(print-errors e)
(exit 1)))

:else
(do (help summary)
(exit 0)))))
14 changes: 8 additions & 6 deletions src/aidbox_sdk/generator.clj
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,10 @@
(defn resolve-reference [schemas]
(clojure.walk/postwalk
(fn [x]
(if-let [ref' (:elementReference x)]
(if-let [reference (:elementReference x)]
(-> x
(dissoc :elementReference)
(merge (find-element-by-reference schemas ref')))
(merge (find-element-by-reference schemas reference)))
x))
schemas))

Expand All @@ -687,8 +687,9 @@
(conj schema {:backbone-elements
(flat-backbones (:backbone-elements schema) [])})))))

(defmulti build-all! (fn [& {:keys [target-language]}] target-language))

(defn build-all! [& {:keys [auth input output]}]
(defmethod build-all! "dotnet" [& {:keys [auth input output]}]
(let [output (io/file output)
search-parameters-dir (io/file output "search")
all-schemas (schema/retrieve
Expand Down Expand Up @@ -760,9 +761,7 @@
(remove fhir/structure-definition? constraints)
(->> all-schemas
(prepared-schemas)
(map (fn [schema]
(conj schema {:backbone-elements
(flat-backbones (:backbone-elements schema) [])})))
(map #(assoc % :backbone-elements (flat-backbones (:backbone-elements %) [])))
(vector-to-map)))
(mapv (fn [[name' schema]]
{:name name'
Expand All @@ -779,3 +778,6 @@
(println "Generating common SDK files")
(doseq [file dotnettpl/files]
(spit (io/file output (:name file)) (:content file)))))

(defmethod build-all! :default [_]
(println "SDK generation for this language is not implemented yet"))

0 comments on commit 52fa89f

Please sign in to comment.