|
1 | 1 | (ns metafacture-playground.process
|
2 | 2 | (:require
|
3 | 3 | [clojure.string :as clj-str]
|
| 4 | + [clojure.java.io :as io] |
4 | 5 | [clojure.tools.logging :as log])
|
5 | 6 | (:import
|
6 | 7 | (java.io File)
|
|
20 | 21 | (log/trace "Content" content)
|
21 | 22 | file-path))))
|
22 | 23 |
|
| 24 | +(defn- remove-temp-files [temp-files] |
| 25 | + (doseq [temp-file temp-files] |
| 26 | + (try |
| 27 | + (let [file (io/file temp-file)] |
| 28 | + (when (.exists file) |
| 29 | + (io/delete-file file false))) |
| 30 | + (catch Exception e |
| 31 | + (log/warn "Could not delete temp file:" temp-file e))))) |
23 | 32 |
|
24 | 33 | (defn process [flux data transformation]
|
25 |
| - (let [inputfile (content->tempfile-path data ".data") |
26 |
| - transformationFile (content->tempfile-path transformation ".fix") |
| 34 | + (let [input-file (content->tempfile-path data ".data") |
| 35 | + transformation-file (content->tempfile-path transformation ".fix") |
27 | 36 | out-path (content->tempfile-path "" ".txt")
|
28 | 37 | output (str "|write(\"" out-path "\");")
|
29 |
| - flux (-> (str "default inputFile = \"" inputfile "\";\n" |
30 |
| - "default transformationFile = \"" transformationFile "\";\n" |
| 38 | + flux (-> (str "default inputFile = \"" input-file "\";\n" |
| 39 | + "default transformationFile = \"" transformation-file "\";\n" |
31 | 40 | flux)
|
32 | 41 | (clj-str/replace #"\|(\s*|\n*)write\(\".*\"\)(\s*|\n*);" output)
|
33 |
| - (clj-str/replace #"\|(\s*|\n*)print(\s*|\n*);" output))] |
34 |
| - (Flux/main (into-array [(content->tempfile-path flux ".flux")])) |
35 |
| - (log/info "Executed flux file with Flux/main. Result in" out-path) |
36 |
| - (slurp out-path))) |
| 42 | + (clj-str/replace #"\|(\s*|\n*)print(\s*|\n*);" output)) |
| 43 | + flux-file (content->tempfile-path flux ".flux")] |
| 44 | + (try |
| 45 | + (Flux/main (into-array [flux-file])) |
| 46 | + (log/info "Executed flux file with Flux/main. Result in" out-path) |
| 47 | + (slurp out-path) |
| 48 | + (finally |
| 49 | + (remove-temp-files [input-file transformation-file flux-file out-path]))))) |
0 commit comments