Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brettatoms committed Nov 11, 2024
0 parents commit a7fbfa0
Show file tree
Hide file tree
Showing 21 changed files with 2,917 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/test-and-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Release Version

on:
push:
tags:
- "v*"

jobs:
build-and-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- name: Setup Clojure
uses: DeLaGuardo/setup-clojure@master
with:
cli: '1.12.0.1479'
- name: Cache All The Things
uses: actions/cache@v4
with:
path: |
~/.m2/repository
~/.gitlibs
~/.clojure
~/.cpcache
key: ${{ runner.os }}-${{ hashFiles('**/deps.edn') }}
- name: Run All Tests and Release
run: clojure -T:build ci
- name: Deploy Release
run: clojure -T:build deploy :snapshot false
env:
CLOJARS_PASSWORD: ${{secrets.DEPLOY_TOKEN}}
CLOJARS_USERNAME: ${{secrets.DEPLOY_USERNAME}}
44 changes: 44 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Pull Request

on:
pull_request:
push:
branches:
- "master"

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
java: [ '17', '21' ]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: ${{ matrix.java }}
- name: Setup Clojure
uses: DeLaGuardo/setup-clojure@master
with:
cli: '1.12.0.1479'
- name: Cache All The Things
uses: actions/cache@v4
with:
path: |
~/.m2/repository
~/.gitlibs
~/.clojure
~/.cpcache
key: ${{ runner.os }}-${{ hashFiles('**/deps.edn') }}
- name: Lint
run: clojure -M:clj-kondo --lint src
- name: Check formatting
run: clojure -M:cljfmt check
- name: Run All Tests
run: clojure -T:build ci :snapshot true
- name: Deploy Snapshot
run: clojure -T:build deploy :snapshot true
env:
CLOJARS_PASSWORD: ${{secrets.DEPLOY_TOKEN}}
CLOJARS_USERNAME: ${{secrets.DEPLOY_USERNAME}}
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.calva/output-window/
.classpath
.clj-kondo/.cache
.cpcache
.DS_Store
.eastwood
.factorypath
.hg/
.hgignore
.java-version
.lein-*
.lsp/.cache
.lsp/sqlite.db
.nrepl-history
.nrepl-port
.portal
.project
.rebel_readline_history
.settings
.socket-repl-port
.sw*
.vscode
*.class
*.jar
*.swp
*~
/checkouts
/classes
/target

node_modules/
.clj-kondo/
.vite/
examples/todo-app/resources/todo/assets/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Change Log

* 0.1.x -- Unreleased
* Initial public version
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Brett Adams

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Zodiac Assets

[![Clojars Project](https://img.shields.io/clojars/v/com.github.brettatoms/zodiac-assets.svg)](https://clojars.org/com.github.brettatoms/zodiac-assets)

A extension for [Zodiac](https://github.com/brettatoms/zodiac) to build and
serve static assets.

This extension is uses [Vite](https://vite.dev/) and doesn't try to abstract
away the features of Vite or the JS platform tools. Instead this extension
embraces JS as a platform for building static assets and provides very simple
wrappers for serving the static assets.

For example this extension only allows you to set the Vite config file path for
the build step and any other Vite configuration options should be set in the
Vite config file. If you want to run Vite in watch mode you will need to set
[build.watch](https://vite.dev/config/build-options.html#build-watch) to true in
t `build.watch` he Vite config file.

For an example of how to use this extension see [examples/todo-app](examples/todo-app).


### Getting started

``` clojure
(ns myapp
(:require [zodiac.core :as z]
[zodiac.ext.assets :as z.assets]))

(defn handler [{:keys [::z/context]}]
;; The assets function in the request context can be used
;; to get the url path to built assets from the Vite manifest.
(let [{:keys [assets]} context]
[:html
[:head
[:script {:src (assets "src/myapp.ts")}]]
[:body
[:div "hello world"]]]))

(defn routes []
["/" {:get #'handler}])

(let [project-root (-> *file* fs/parent fs/parent str)
assets-ext (z.assets/init {;; The config file is used by the vite command
;; so it needs to be an absolute path on the
;; filesystem, e.g. not in a jar.
:config-file (str (fs/path project-root "vite.config.js"))
;; The manifest path is the relative resource
;; path to the output manifest file. This value doesn't override the build
;; time value for the output path of the manifest file.
:manifest-path "myapp/.vite/manifest.json"
;; The resource path the the built assets.
:asset-resource-path "myapp/assets"})]
(z/start {:extensions [assets-ext]
:routes routes})
```

### Configuring Vite

You will need to provide a vite config file to run the `vite build` command with this extension.

The very basic config file will need an `outDir` which holds the relative path for where to save the build assets and manifest file.

``` javascript
import { defineConfig } from "vite"

export default defineConfig({
build: {
outDir: "resources/myapp",
manifest: true,
rollupOptions: {
input: [
"src/myapp.ts",
"src/myapp.css",
],
},
},
})
```


### Options

The `zodiac.ext.assets/init` accepts the following options:

- `:manifest-path`: The resource path to the Vite manifest.json json. Required.
- `:asset-resource-path`. The resource path the build assets. This should be the
same os the `asset-dir` Vite config. Required.
- `:config-file`: The absolute path to the vite config file. This file is
required if `:build?` is true.
- `:build?`: Whether to run `vite build` to build the assets. Set to false if
you only want to use this extension to lookup assets from the vite manifest.
Defaults to `true`.
- `:cache-manifest?`: Set to true to cache the manifest file the first time it
is read instead of reading it on every request. This will improve the
performance of getting the build asset paths performance when `vite build` to
build the assets. Set to `false` if running vite in watch mode. Set to `true`
in production. Defaults to `false`.
83 changes: 83 additions & 0 deletions build.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
(ns build
(:refer-clojure :exclude [test])
(:require [clojure.tools.build.api :as b]
[deps-deploy.deps-deploy :as dd]))

(def lib 'com.github.brettatoms/zodiac-assets)

(defn- version-base [patch] (format "0.1.%s" patch))
(def version (version-base (b/git-count-revs nil)))
(def snapshot (version-base "9999-SNAPSHOT"))
(def class-dir "target/classes")
(def jar-file (format "target/%s-%s.jar" (name lib) version))

;; delay to defer side effects (artifact downloads)
(def basis (delay (b/create-basis {:project "deps.edn"})))

(defn clean [_]
(b/delete {:path "target"}))

(defn test "Run all the tests." [opts]
(b/process {:command-args ["clojure" "-M:test"]})
(b/process {:command-args ["clojure" "-M:clj-kondo" "--lint" "src"]})
(b/process {:command-args ["clojure" "-M:cljfmt" "check"]})
opts)

(defn- pom-template [version]
[[:description "A Zodiac extension to help manage static assets"]
[:url "https://github.com/brettatoms/zodiac-assets"]
[:licenses
[:license
[:name "MIT License"]
[:url "https://mit-license.org/license.txt"]]]
[:developers
[:developer
[:name "Brett Adams"]]]
[:scm
[:url "https://github.com/brettatoms/zodiac-assets"]
[:connection "scm:git:https://github.com/brettatoms/zodiac-assets.git"]
[:developerConnection "scm:git:ssh:git@github.com:brettatoms/zodiac-assets.git"]
[:tag (str "v" version)]]])

(defn- jar-opts [opts]
(let [version (if (:snapshot opts) snapshot version)]
(println "\nVersion:" version)
(assoc opts
:lib lib :version version
:jar-file (format "target/%s-%s.jar" lib version)
:basis (b/create-basis {})
:class-dir class-dir
:target "target"
:src-dirs ["src"]
:pom-data (pom-template version))))

(defn jar [opts]
(b/write-pom {:class-dir class-dir
:lib lib
:version version
:basis @basis
:src-dirs ["src"]})
(b/copy-dir {:src-dirs ["src" "resources"]
:target-dir class-dir})
(b/jar (jar-opts opts)))


(defn ci "Run the CI pipeline of tests (and build the JAR)." [opts]
(test opts)
(b/delete {:path "target"})
(let [opts (jar-opts opts)]
(println "\nWriting pom.xml...")
(b/write-pom opts)
(println "\nCopying source...")
(b/copy-dir {:src-dirs ["resources" "src"]
:target-dir class-dir})
(println "\nBuilding" (:jar-file opts) "...")
(b/jar opts))
opts)

(defn deploy "Deploy the JAR to Clojars." [opts]
(let [{:keys [jar-file] :as opts} (jar-opts opts)]
(dd/deploy {:installer :remote
:artifact (b/resolve-path jar-file)
:pom-file (b/pom-path (select-keys opts [:lib :class-dir]))}))
opts)
13 changes: 13 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.12.0"}

integrant/integrant {:mvn/version "0.13.1"}
babashka/fs {:mvn/version "0.4.19"}
org.clojure/data.json {:mvn/version "2.4.0"}}
:aliases {:build {:deps {io.github.seancorfield/build-clj
{:git/tag "v0.6.4" :git/sha "c21cfde"}}
:ns-default build}
:test {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "1.1.1"}
io.github.cognitect-labs/test-runner
{:git/tag "v0.5.0" :git/sha "48c3c67"}}}}}
3 changes: 3 additions & 0 deletions doc/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction to zodiac-assets

TODO: write [great documentation](http://jacobian.org/writing/what-to-write/)
21 changes: 21 additions & 0 deletions examples/todo-app/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.12.0"}

com.github.brettatoms/zodiac {:mvn/version "0.3.34"}
com.github.brettatoms/zodiac-assets {:local/root "../../"}

babashka/fs {:mvn/version "0.4.19"}
com.taoensso/telemere {:mvn/version "1.0.0-RC1"}
com.taoensso/telemere-slf4j {:mvn/version "1.0.0-RC1"}
org.clojure/tools.logging {:mvn/version "1.3.0"}

integrant/integrant {:mvn/version "0.13.1"}}

:aliases {:main {:main-opts ["-m" "todo"]}
;; Builds with clojure -X:uberjar
;; Run with java -jar targets/todo.jar
:uberjar {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.303"}}
:exec-fn hf.depstar/uberjar
:exec-args {:aot true
:jar "todo.jar"
:main-class "todo"}}}}
Loading

0 comments on commit a7fbfa0

Please sign in to comment.