Browse Source

maps detected paths to openapi spec

master
anonymous 1 year ago
parent
commit
79a8ca43d1
2 changed files with 67 additions and 42 deletions
  1. +5
    -20
      src/main/clj/gen/core.clj
  2. +62
    -22
      src/main/clj/scan/lang/php/laravel5.clj

+ 5
- 20
src/main/clj/gen/core.clj View File

@@ -7,8 +7,7 @@
^{}
run
(fn [args]
(scan.lang.php.laravel5/routes (first args))
#_(println
(println
(yaml/generate-string
{"openapi" "3.0.0"
:info
@@ -17,31 +16,17 @@
:license
{:name "MIT"}}
:servers
[{:url "thing"}]
[
{:url "thing"}]
:paths
{"/pets"
{:get
{:parameters
[{:name "limit"
:in "query"
:description "aaaaaaaa"
:required false
:schema
{:type "integer"
:format "int32"}}]}}
"/lists"
{:get
{:responses
{"200"
{:description "ok"}}}}}
:spec {:lorem "ipsum" :thing true}}
(scan.lang.php.laravel5/routes (first args))}
:dumper-options {:indent 2
:indicator-indent 1
:flow-style :block}))))

(comment
)
)

(defn -main [& argv]
(prn (run argv)))

+ 62
- 22
src/main/clj/scan/lang/php/laravel5.clj View File

@@ -4,6 +4,12 @@

(def
^{}
omit-comments
(fn [source]
(str/replace source #"\/\*.+\*\/" "")))

(def
^{}
load-method-signature!
(fn [route]
(cond (.exists (io/file (:ctl route)))
@@ -21,27 +27,61 @@

(def
^{}
to-openapi
(fn [routes]
(reduce
(fn [acc el]
(assoc-in
acc
[(:path el)
(:verb el)]
{:description (:act el)
:parameters
(into
[]
(map
(fn [e]
(if (< 0 (count e))
(let [[type name] (str/split e #" ")]
{:name (re-find #"\w" name)
:in "query"
:required true ;; depends on param optionality
:schema
{:type type}})
[]))
(:signature el)))}))
{}
routes)))

(def
^{}
routes
(fn [project-path]
(map (fn [route]
(assoc route
:signature
(load-method-signature! route)))
(let [route-file (str project-path "/routes/web.php")]
(filter
(fn [e]
(not (nil? e)))
(map
(fn [hit]
(let [[_ verb raw-params] hit
params (map (fn [e] (str/trim (str/escape e {\' ""}))) (str/split raw-params #","))
[route ctl] params]
(if ctl
{:verb verb
:path route
:ctl (str project-path "/app/Http/Controllers/" (str/replace (str/replace ctl #"\@(.*)" ".php") "\\" "/"))
:act (str/replace ctl #"(.*)\@" "")}
nil)))
(re-seq
#"router->(.*)\((.*)\)"
(slurp route-file))))))))
(to-openapi
(map (fn [route]
(assoc route
:signature
(load-method-signature! route)))
(let [route-file (str project-path "/routes/web.php")]
(filter
(fn [e]
(not (nil? e)))
(map
(fn [hit]
(let [[_ verb raw-params] hit
params (map (fn [e] (str/trim (str/escape e {\' ""}))) (str/split raw-params #","))
[route ctl] params]
(if ctl
{:verb verb
:path route
:ctl (str project-path "/app/Http/Controllers/" (str/replace (str/replace ctl #"\@(.*)" ".php") "\\" "/"))
:act (str/replace ctl #"(.*)\@" "")}
nil)))
(re-seq
#"router->(.*)\((.*)\)"
(omit-comments (slurp route-file))))))))))

(comment
)

Loading…
Cancel
Save