Skip to content

Instantly share code, notes, and snippets.

@mooreryan
Created October 5, 2019 02:23
Show Gist options
  • Save mooreryan/9e077a3447dcca15876df2ee5d9aa6d3 to your computer and use it in GitHub Desktop.
Save mooreryan/9e077a3447dcca15876df2ee5d9aa6d3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
Signal.trap("PIPE", "EXIT")
require "abort_if"
require "fileutils"
require "optimist"
include AbortIf
opts = Optimist.options do
banner <<-EOS
Set up a skeleton directory for ClojureScript projects.
Options:
EOS
opt(:name,
"Project name (separated by '-')",
type: :string)
opt(:dir,
"Dir in which the project root will live",
type: :string,
default: ".")
opt(:reagent, "Use reagent library")
end
name = opts[:name]
dir = opts[:dir]
FileUtils.mkdir_p dir
proj_dir = File.join dir, name
name_us = name.gsub("-", "_")
module_name = "core"
abort_if Dir.exist?(proj_dir), "#{proj_dir} already exists!"
FileUtils.mkdir_p [
File.join(proj_dir, "resources", "public", "css"),
File.join(proj_dir, "src", name_us),
File.join(proj_dir, "target"),
File.join(proj_dir, "bin"),
File.join(proj_dir, "test", name_us)
]
`touch #{File.join(proj_dir, "resources", "public", "css", "style.css")}`
# Figwheel main opts
File.open File.join(proj_dir, "figwheel-main.edn"), "w" do |f|
f.puts %q~{:auto-testing true
:watch-dirs ["src" "test"]}
~
end
# An example runner
compile_it_fname = File.join proj_dir, "bin", "compile_it"
File.open compile_it_fname, "w" do |f|
f.puts "clojure -m figwheel.main --build #{module_name} --repl"
end
`chmod 755 #{compile_it_fname}`
deps_fname = File.join proj_dir, "deps.edn"
File.open deps_fname, "w" do |f|
if opts[:reagent]
f.puts %q~{:deps {com.bhauman/figwheel-main {:mvn/version "0.2.0"}
com.bhauman/rebel-readline-cljs {:mvn/version "0.1.4"}
reagent {:mvn/version "0.8.1"}}
:paths ["src" "test" "target" "resources"]}
~
else
f.puts %q~{:deps {com.bhauman/figwheel-main {:mvn/version "0.2.0"}
com.bhauman/rebel-readline-cljs {:mvn/version "0.1.4"}}
:paths ["src" "test" "target" "resources"]}
~
end
end
File.open File.join(proj_dir, "#{module_name}.cljs.edn"), "w" do |f|
f.puts "{:main #{name}.#{module_name}}"
end
# Home page
index_fname = File.join proj_dir, "resources", "public", "index.html"
File.open index_fname, "w" do |f|
f.puts %Q~<!DOCTYPE html>
<html>
<head>
<!-- this refers to resources/public/css/style.css -->
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>The #{name} App</h1>
<p>Hi! I'm the template for your main html page! Find me in #{index_fname}</p>
<div id="app"></div>
<!-- this refers to target/public/cljs-out/main.js -->
<script src="cljs-out/#{module_name}-main.js"></script>
</body>
</html>
~
end
# Src file
core_fname = File.join proj_dir, "src", name_us, "#{module_name}.cljs"
File.open core_fname, "w" do |f|
if opts[:reagent]
f.puts %Q~(ns #{name}.#{module_name}
(:require
[reagent.core :as r]))
(defn say-hi []
"Hi from #{module_name}.cljs!")
;;;; HTML IDs
(def html-id-app "app")
;;;; Components
(defn app-scaffold []
[:div
[:p "I'm the app!"]])
;;;; Rendering
(defn render-app []
(r/render [app-scaffold]
(.getElementById js/document html-id-app)))
(render-app)
~
else
f.puts %Q~(ns #{name}.#{module_name})
(defn say-hi []
"Hi from #{module_name}.cljs!")
~
end
end
File.open File.join(proj_dir, "test", name_us, "#{module_name}_test.cljs"), "w" do |f|
f.puts %Q~;;;; Open this link to see the tests: http://localhost:9500/figwheel-extra-main/auto-testing
(ns #{name}.#{module_name}-test
(:require [cljs.test :refer-macros [deftest is testing run-tests]]
[#{name}.#{module_name} :as #{module_name}]))
(deftest say-hi-test
(is (= "Hi from #{module_name}.cljs!" (#{module_name}/say-hi))))
(run-tests)
~
end
gitignore_fname = File.join proj_dir, ".gitignore"
File.open gitignore_fname, "w" do |f|
f.puts %q~target
.cpcache
.figwheel-main.edn
.rebel_readline_history
~
end
puts `tree #{proj_dir}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment