Skip to content

Commit

Permalink
Ver 0.2.0 - Fixes #2 and #3
Browse files Browse the repository at this point in the history
  • Loading branch information
lenz committed Oct 20, 2019
1 parent 8a37531 commit d33c4f3
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 97 deletions.
65 changes: 49 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ is not available anymore once you deploy your app somewhere else.
It can also be useful to run a pipeline once when building, e.g. compiling a SASS file
into CSS and storing the result as a string.


[![Clojars Project](https://img.shields.io/clojars/v/say-cheez.svg)](https://clojars.org/say-cheez)
[![](https://cljdoc.xyz/badge/say-cheez)](https://cljdoc.xyz/jump/release/say-cheez)


## Example

Look at this namespace:
Expand All @@ -21,15 +26,16 @@ Look at this namespace:
The var called `BUILD` is exactly the same as if you wrote by hand:

(defonce BUILD
{:arch "x86_64",
:git-build "e4b7836/2018-11-03.14:45:31",
:osname "gnu-linux",
:project "say-cheez",
:built-at "2018-11-03.14:49:25",
:built-by "jenkins",
:on-host "jenkins18.loway.internal",
:version "0.0.2",
:build-no "107"})
{:project
{:arch "x86_64",
:git-build "e4b7836/2018-11-03.14:45:31",
:osname "gnu-linux",
:project "say-cheez",
:built-at "2018-11-03.14:49:25",
:built-by "jenkins",
:on-host "jenkins18.loway.internal",
:version "0.0.2",
:build-no "107"}})

But that would be pretty annoying to do by hand, because such information comes from different places:

Expand All @@ -40,6 +46,26 @@ But that would be pretty annoying to do by hand, because such information comes

And would not usually be available at run time.

### Creating your own DEFs

The problem with the approach above is that linters may not understand that
`BUILD` was defined at all, so they might display it as "broken" in your
IDE or raise an exception.

To make them happy, you can define `BUILD` by yourself:

(ns baab.baah
(:require [say-cheez.core :refer [current-build-env]]))
....
(def BUILD (current-build-env))

Please note that `current-build-env` has a couple of minor differences to `capture-build-env-to`:

* It does not print the captured value (so you can have a 'silent' build)
* It does not nest the captured value under a `:project` key, as it is
just a value that you can nest yourself.



### Customizing values

Expand All @@ -48,6 +74,19 @@ Of course, you can capture the exact values you need if our own chili is not to
(capture-to MYBUILD {:project (leiningen-info :project-name)
:myId (env ["MY_OWN_ID"] "?")})

or, to avoid creating a silent def:

(def ABC (capture {:project (leiningen-info :project-name)
:myId (env ["MY_OWN_ID"] "?")})
or even:

(def ABC (capture {:project (leiningen-info :project-name)
:myId (env ["MY_OWN_ID"] "?")}
"value of ABC")
where, during compile, the value computed will be printed out as "value of ABC".


You can call any function and build any valid data structure.

Expand Down Expand Up @@ -78,13 +117,7 @@ About the runtime, under `platform` there is a function to set the current threa

## Using

The library is available on Clojars:

[![Clojars Project](https://img.shields.io/clojars/v/say-cheez.svg)](https://clojars.org/say-cheez)
[![](https://cljdoc.xyz/badge/say-cheez)](https://cljdoc.xyz/jump/release/say-cheez)


Or the library can be easily referenced through Github:
The library is available on Clojars, or the library can be easily referenced through Github:

{:deps
{cli-matic
Expand Down
4 changes: 2 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject say-cheez "0.1.1"
(defproject say-cheez "0.2.0"
:description "Takes a snapshot of the environment at build time."
:url "https://github.com/l3nz/say-cheez"
:license {:name "Eclipse Public License"
Expand All @@ -20,7 +20,7 @@

:profiles {:kondo
{:dependencies [[org.clojure/clojure "1.10.1"]
[clj-kondo "2019.06.23-alpha"]]}} :main say-cheez.run
[clj-kondo "2019.10.11-alpha"]]}} :main say-cheez.run
:scm {:name "git"
;; :tag "..."
:url "https://github.com/l3nz/say-cheez"}
Expand Down
156 changes: 80 additions & 76 deletions src/say_cheez/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,27 @@
"
(:require [say-cheez.platform :as P]
[clojure.edn :as edn]))
(:require [say-cheez.platform :as P]
[clojure.edn :as edn]))

;;
;; Date/time
;;



(defn now-as
"Prints the current date/time in different ways.
"Prints the current date/time in different ways.
* :date
* :time
* :timec (compact)
* :datetime
"
[mode]
(condp = mode
:date (P/date->str "yyyy-MM-dd")
:time (P/date->str "HH:mm:ss")
:timec (P/date->str "HHmmss")
:datetime (P/date->str "yyyy-MM-dd.HH:mm:ss")
))

[mode]
(condp = mode
:date (P/date->str "yyyy-MM-dd")
:time (P/date->str "HH:mm:ss")
:timec (P/date->str "HHmmss")
:datetime (P/date->str "yyyy-MM-dd.HH:mm:ss")))

;;
;; Environment
Expand All @@ -63,60 +58,53 @@
[lEnvVars defValue]
(let [labels (if (string? lEnvVars)
[lEnvVars]
lEnvVars
)
lEnvVars)
attrs (mapv P/getenv labels)
attrs-with-default (conj attrs defValue)]

(first (filter some? attrs-with-default))))



;;
;; Leiningen project
;;


(defn read-project-clj
[]
(let [src (slurp "project.clj")]
(edn/read-string src)))

[]
(let [src (slurp "project.clj")]
(edn/read-string src)))

(defn leiningen-info
"Reads information from a leiningen project.
"Reads information from a leiningen project.
* :projectt-name
* :version
Use the one-arity version.
"
([tag]
(leiningen-info tag read-project-clj))
([tag]
(leiningen-info tag read-project-clj))

([tag fnReader]
(let [project (fnReader)
[_ name version & opts] project]
(condp = tag
:project-name
(str name)
([tag fnReader]
(let [project (fnReader)
[_ name version & _] project]
(condp = tag
:project-name
(str name)

:version
version))))
:version
version))))

;;
;; GIT
;;

(defn gitlog [parms]
(let [cmdline (str "git log --oneline -n 1 " parms)]

(let [cmdline (str "git log --oneline -n 1 " parms)]
(first (P/shellout cmdline))))


(defn git-info
"Reads data from GIT. Git must be installed (we just shell-out).
"Reads data from GIT. Git must be installed (we just shell-out).
* :commit-id
* :commit-long
Expand All @@ -127,20 +115,16 @@
"

[what]
(condp = what
:commit-id (gitlog "--pretty=%h")
:commit-long (gitlog "--pretty=%H")
:last-committer (gitlog "--pretty=%aN")

:date (gitlog "--date=format:%Y-%m-%d.%H:%M:%S --pretty=%cd")
:date-compact (gitlog "--date=format:%Y%m%d-%H%M --pretty=%cd")
:all (str (git-info :commit-id) "/"
(git-info :date))
))


[what]
(condp = what
:commit-id (gitlog "--pretty=%h")
:commit-long (gitlog "--pretty=%H")
:last-committer (gitlog "--pretty=%aN")

:date (gitlog "--date=format:%Y-%m-%d.%H:%M:%S --pretty=%cd")
:date-compact (gitlog "--date=format:%Y%m%d-%H%M --pretty=%cd")
:all (str (git-info :commit-id) "/"
(git-info :date))))

;; ======================================================
;; RUNTIME INFORMATION
Expand All @@ -163,7 +147,6 @@
[n d]
(str (safe-quot (* n 100) d) "%"))


(defn display-memory
"Displays the amount of memory available.
Expand All @@ -179,8 +162,6 @@
(asMb (+ max other)) "M "
(asPrc used max) " used"))



(defn runtime
"Inspects the runtime for stuff you may want to
print at runtime.
Expand All @@ -193,16 +174,22 @@
(condp = what
:pid (P/get-current-pid)
:vm (P/get-current-VM)
:mem (display-memory (P/get-memory-state))
))


:mem (display-memory (P/get-memory-state))))

;;
;; DEFINE build env
;;


(defmacro capture
"Evals an expression, and optionally prints it out
at compile time with the label 'name' if the label
exists."
([exp-to-eval & [name]]
(let [v (eval exp-to-eval)
_ (when name
(prn (str "=== Say-cheez captured " name ":"))
(prn v))]
`~v)))

(defmacro capture-to
"Captures the compile-time evaluation of `exp-to-eval`
Expand All @@ -221,12 +208,37 @@
"
[sym exp-to-eval]
(let [v (eval exp-to-eval)
_ (prn (str "=== Say-cheez captured environment '" sym "':"))
_ (prn v)
]
`(defonce ~sym {:project ~v})))
(let [n (str "to symbol " sym)
v `(capture ~exp-to-eval ~n)]
`(defonce ~sym ~v)))

(defmacro current-build-env
"Creates the default build environment as a plain data
structure - so it is run only during macroexpansion.
Differently from [[capture-build-env-to]] and
[[capture-to]], it does not expand to a defonce for you,
as this confuses linters.
You would use this function like:
`(defonce BUILD (current-build-env))`
So that linters won't complain.
We just create a map an stick it in our source.
"
[]
(let [v {:project (leiningen-info :project-name)
:version (leiningen-info :version)
:built-at (now-as :datetime)
:on-host (env ["HOSTNAME"] "?")
:osname (env ["OSTYPE"] "?")
:arch (env ["HOSTTYPE"] "?")
:build-no (env ["BUILD_NUMBER"] "?")
:git-build (git-info :all)
:built-by (env ["USER"] "?")}]

`(capture ~v)))

(defmacro capture-build-env-to
"Captures common build environment stuff into a single map
Expand All @@ -236,10 +248,10 @@
`(capture-build-env-to BUILD)`
Generates:
`(defonce BUILD
{:project
{:arch \"x86_64\",
:git-build \"e4b7836/2018-11-03.14:45:31\",
:osname \"gnu-linux\",
Expand All @@ -248,22 +260,14 @@
:built-by \"jenkins\",
:on-host \"jenkins18.loway.internal\",
:version \"0.0.2\",
:build-no \"107\"})`
:build-no \"107\"}})`
Not all values may be present, as it actually depends on
what is available. If you need to fine-tune the contents,
use [[capture-to]]. Look at the source.
"
[sym]
`(capture-to ~sym {:project (leiningen-info :project-name)
:version (leiningen-info :version)
:built-at (now-as :datetime)
:on-host (env ["HOSTNAME"] "?")
:osname (env ["OSTYPE"] "?")
:arch (env ["HOSTTYPE"] "?")
:build-no (env ["BUILD_NUMBER"] "?")
:git-build (git-info :all)
:built-by (env ["USER"] "?")}))

`(capture-to ~sym
{:project (current-build-env)}))

0 comments on commit d33c4f3

Please sign in to comment.