Skip to content

Instantly share code, notes, and snippets.

@burn2delete
Last active August 29, 2015 14:24
Show Gist options
  • Save burn2delete/21a7f30d16c9d06ea97d to your computer and use it in GitHub Desktop.
Save burn2delete/21a7f30d16c9d06ea97d to your computer and use it in GitHub Desktop.
boot-tasks
(ns degree9.boot-d9micromgmt.impl
{:boot/export-tasks true}
(:require [boot.core :as boot]
[boot.pod :as pod]
[boot.tmpdir :as tmpd]
[boot.util :as util]
[boot.task.built-in :as tasks]
[clj-yaml.core :as yaml]
[clojure.string :as string]
[clojure.java.io :as io]
[cheshire.core :as cheshire]
[stencil.core :as stencil]
[stencil.loader :as tmplldr]
[clojure.algo.generic.functor :as algo]
[selmer.parser :as selmer]))
(defn change-file-ext [path ext]
(string/replace path #"\.[^\.]+$" (str "." ext)))
(defn psdsc-generate
""
[x]
(let [doallx (doall x)
newmap (algo/fmap (fn rec [s] (cond
(string? s) s
(seq? s) (rec (into [] s))
(or (vector? s) (map? s)) (let [y (algo/fmap (fn [is] (rec is)) s)] y)
:else s)) x)]
(println (doall x))
(println newmap)
(println tmplpath)
(stencil/render-file "psdsc.mustache" newmap)))
(defmacro srctype-to-desttype
""
[parser generator ext]
`(boot/with-pre-wrap fileset#
(let [tmp# (boot/tmp-dir!)]
(doseq [f# (->> fileset#
boot/input-files)]
(let [in-file# (tmpd/file f#)
in-path# (tmpd/path f#)
out-path# (change-file-ext in-path# ~ext)
out-file# (io/file tmp# out-path#)
result# (-> in-file# slurp (~parser :keywords true) ~generator)]
(util/info "Converting files...\n")
(doto out-file#
io/make-parents
(spit result#))))
(-> fileset# (boot/add-resource tmp#) boot/commit!))))
(defn yaml-json
"Convert YAML to JSON."
[]
(srctype-to-desttype yaml/parse-string boot/json-generate "json"))
(defn yaml-psdsc
"Convert YAML to PowerShell Desired State Configuration."
[]
(srctype-to-desttype yaml/parse-string psdsc-generate "ps1"))
(defn import-templates
""
[]
(boot/with-pre-wrap fileset
(let []
(doseq [f (->> fileset
boot/input-files)]
(let [in-file (tmpd/file f)
in-path (tmpd/path f)
template-path (change-file-ext in-path "tmpl")]
(util/info "Importing templates...\n")
(util/info "• %s\n" (.getName template-path))))
(-> fileset boot/commit!))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment