cryogen-project / cryogen-core

Cryogen's core
Eclipse Public License 1.0
69 stars 62 forks source link

Overriding Config #141

Closed Folcon closed 4 years ago

Folcon commented 4 years ago

Hey, would it be possible to do some sort of config override?

I tend to draft posts in the future and I've not figured out how to preview them for editing other than manually toggling :hide-future-posts?.

I'd much rather prefer to create a lein alias so I can just get all the drafts / future posts visible for when I'm writing.

There's a couple of ways this could be done, but the easiest is probably adding an optional arg or env var capture a path to a config here?

I'd rather not pull in new deps (unless you think it's warranted) so probably just use:

(System/getenv "CRYOGEN_CONFIG")

Which specifies a project relative path, eg, the default is: "content/config.edn" and then apply that?

How does that sound? =)...

lacarmen commented 4 years ago

This should help :) You can do what you suggested to read in a different config and then pass that to compile-assets-timed

Folcon commented 4 years ago

So one thing that's unclear, do I need to modify them both? Or is modifying the src/cryogen/server.clj for when I'm editing, and modifying the src/cryogen/core.clj for stuff that always runs?

lacarmen commented 4 years ago

The one in server.clj is for when you're editing (lein ring server) and the one in the -main is for lein run (ie. Compiling once for deployment). Hope that clears things up!

Folcon commented 4 years ago

It might be worthwhile mentioning that in the docs? 🤷 Thanks a ton for the help =)...

Folcon commented 4 years ago

Managed to figure out a decent enough solution, documented here if anyone else wants do this =)...

In project.clj:

(defproject cryogen "0.1.0"
  :description "Simple static site generator"
  :url "https://github.com/lacarmen/cryogen"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [...
                 [environ "1.1.0"]]  ;; <-- added in environ
  :plugins [[lein-ring "0.12.5"]
            [lein-environ "1.1.0"]]   ;; <-- + the lein plugin
  :main cryogen.core
  :ring {:init cryogen.server/init
         :handler cryogen.server/handler}
  :aliases {"preview" ["ring" "server-headless"]  ;; <-- defined some preview/writing aliases
            "write"   ["with-profile" "write" "ring" "server-headless"]}
  :profiles {:write {:env {:config-path "content/drafting.edn"}}})

In src/cryogen/server.clj, mainly adjusted init:

(ns cryogen.server
  (:require 
   ...
   [environ.core :refer [env]]))

(defn init []
  (load-plugins)
  (let [custom-config-path (env :config-path)
        config-override (fn [] (when custom-config-path (read-string (slurp custom-config-path))))
        compile-assets-timed-fn (if config-override
                                 (comp compile-assets-timed config-override)
                                 compile-assets-timed)]
   (compile-assets-timed-fn)
   (let [ignored-files (-> (resolve-config) :ignored-files)]
     (start-watcher! "content" ignored-files compile-assets-timed-fn)
     (start-watcher! "themes" ignored-files compile-assets-timed-fn))))

This maintains the pleasant property that editing the supplied config while the server is running content/drafting.edn in this case, reloads the config =)...

Works great, thanks!

lacarmen commented 4 years ago

Nice! Glad you found a solution :)