Browse Source

Change the whole system to work with Chestnut

master
parent
commit
4cc8e027a3
28 changed files with 314 additions and 138 deletions
  1. +13
    -15
      .gitignore
  2. +9
    -0
      README.md
  3. +27
    -0
      env/dev/clj/character_suite/dev.clj
  4. +14
    -0
      env/dev/cljs/character_suite/main.cljs
  5. +13
    -0
      env/prod/clj/character_suite/dev.clj
  6. +4
    -0
      env/prod/cljs/character_suite/main.cljs
  7. +24
    -0
      env/test/js/polyfill.js
  8. +42
    -0
      env/test/js/unit-test.js
  9. +6
    -0
      env/test/unit-test.html
  10. +68
    -75
      project.clj
  11. +7
    -5
      resources/index.html
  12. +3
    -6
      resources/public/devcards/index.html
  13. +0
    -0
      src/clj/character_suite/character.clj
  14. +0
    -0
      src/clj/character_suite/pdf.clj
  15. +39
    -17
      src/clj/character_suite/server.clj
  16. +0
    -0
      src/cljs/character_suite/character.cljs
  17. +15
    -14
      src/cljs/character_suite/core.cljs
  18. +0
    -0
      src/cljs/character_suite/devcards.cljs
  19. +0
    -0
      src/cljs/character_suite/dice.cljs
  20. +0
    -0
      src/cljs/character_suite/views.cljs
  21. +0
    -0
      src/cljs/character_suite/views/distribute.cljs
  22. +0
    -0
      src/cljs/character_suite/views/free_form.cljs
  23. +6
    -6
      src/cljs/character_suite/views/free_form_distribute.cljs
  24. +0
    -0
      src/cljs/character_suite/views/pick.cljs
  25. +0
    -0
      src/cljs/character_suite/views/utils.cljs
  26. +5
    -0
      test/clj/character_suite/example_test.clj
  27. +6
    -0
      test/cljs/character_suite/core-test.cljs
  28. +13
    -0
      test/cljs/character_suite/test-runner.cljs

+ 13
- 15
.gitignore View File

@@ -1,16 +1,14 @@
/resources/public/js/compiled/**
figwheel_server.log
/target
/classes
/checkouts
pom.xml
*jar
/lib/
/classes/
/out/
/target/
.lein-deps-sum
.lein-repl-history
.lein-failures
.lein-plugins/
.repl
.nrepl-port
compiled/
characters/*
pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
/resources/public/js/out
/resources/public/js/app.js
/out
/.repl
characters/

+ 9
- 0
README.md View File

@@ -14,3 +14,12 @@ The idea is to have an application that aids in the tedious,
unimportant steps when creating a character for an RPG. Of course, the
character creation rules must be extensible and theoretically support
any rule-system, so the system doesn't try to make any assumptions.

Running the development version
-------------------------------

Type `lein repl`, and then `(run)` in the prompt that will appear. You
can also type `(browser-repl)` after you have a connected browser if
you want a browser-connected REPL on the command-line.

Then, open a browser with http://localhost:10555.

+ 27
- 0
env/dev/clj/character_suite/dev.clj View File

@@ -0,0 +1,27 @@
(ns character-suite.dev
(:require [environ.core :refer [env]]
[cemerick.piggieback :as piggieback]
[weasel.repl.websocket :as weasel]
[figwheel-sidecar.auto-builder :as fig-auto]
[figwheel-sidecar.core :as fig]
[clojurescript-build.auto :as auto]
[clojure.java.shell :refer [sh]]))

(def is-dev? (env :is-dev))

(defn browser-repl []
(let [repl-env (weasel/repl-env :ip "0.0.0.0" :port 9001)]
(piggieback/cljs-repl :repl-env repl-env)))

(defn start-figwheel []
(let [server (fig/start-server { :css-dirs ["resources/public/css"] })
config {:builds [{:id "dev"
:source-paths ["src/cljs" "env/dev/cljs"]
:compiler {:output-to "resources/public/js/app.js"
:output-dir "resources/public/js/out"
:source-map true
:optimizations :none
:source-map-timestamp true
:preamble ["react/react.min.js"]}}]
:figwheel-server server}]
(fig-auto/autobuild* config)))

+ 14
- 0
env/dev/cljs/character_suite/main.cljs View File

@@ -0,0 +1,14 @@
(ns character-suite.main
(:require [character-suite.core :as core]
[figwheel.client :as figwheel :include-macros true]
[cljs.core.async :refer [put!]]
[weasel.repl :as weasel]))

(figwheel/watch-and-reload
:websocket-url "ws://localhost:3449/figwheel-ws"
:jsload-callback (fn []
(core/main)))

(weasel/connect "ws://localhost:9001" :verbose true :print #{:repl :console})

(core/main)

+ 13
- 0
env/prod/clj/character_suite/dev.clj View File

@@ -0,0 +1,13 @@
(ns character-suite.dev
(:require [environ.core :refer [env]]))

(if (env :is-dev)
(throw (Exception. (str "Production environment code is being loaded while the dev environment is active. "
"You likely have compiled class files lying around from an uberjar build. "
"Remove the target/ directory and try again."))))

(def is-dev? false)
(defn browser-repl []
(throw (Exception. "Browser connected REPL is not available in prod mode")))
(defn start-figwheel []
(throw (Exception. "Figwheel is not available in prod mode")))

+ 4
- 0
env/prod/cljs/character_suite/main.cljs View File

@@ -0,0 +1,4 @@
(ns character-suite.main
(:require [character-suite.core :as core]))

(core/main)

+ 24
- 0
env/test/js/polyfill.js View File

@@ -0,0 +1,24 @@
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}

var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};

fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();

return fBound;
};
}

+ 42
- 0
env/test/js/unit-test.js View File

@@ -0,0 +1,42 @@
var page = require('webpage').create();
var url;

if (phantom.args) {
url = phantom.args[0];
} else {
url = require('system').args[1];
}

page.onConsoleMessage = function (message) {
console.log(message);
};

function exit(code) {
setTimeout(function(){ phantom.exit(code); }, 0);
phantom.onError = function(){};
}

console.log("Loading URL: " + url);

page.open(url, function (status) {
if (status != "success") {
console.log('Failed to open ' + url);
phantom.exit(1);
}

console.log("Running test.");

var result = page.evaluate(function() {
return character_suite.test_runner.runner();
});

if (result != 0) {
console.log("*** Test failed! ***");
exit(1);
}
else {
console.log("Test succeeded.");
exit(0);
}

});

+ 6
- 0
env/test/unit-test.html View File

@@ -0,0 +1,6 @@
<html>
<body>
<script src="js/polyfill.js" type="text/javascript"></script>
<script src="../../resources/public/js/app_test.js" type="text/javascript"></script>
</body>
</html>

+ 68
- 75
project.clj View File

@@ -1,88 +1,81 @@
(defproject character-suite "0.1.0-SNAPSHOT"
:description "Small tool to create RPG characters"
:url "http://github.com/emanchado/character-suite"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}

:source-paths ["src/clj"]

:resource-paths ["lib/itextpdf-5.5.6.jar" "resources"]

:test-paths ["test/clj"]

:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/clojurescript "0.0-3211"]
[org.clojure/clojurescript "0.0-2760" :scope "provided"]
[org.clojure/core.async "0.1.346.0-17112a-alpha"]
[sablono "0.3.4"]
[org.omcljs/om "0.8.8"]
[org.omcljs/om "0.9.0"]
[ring "1.3.2"]
[ring/ring-defaults "0.1.4"]
[devcards "0.1.2-SNAPSHOT"]
[cljs-ajax "0.3.13"]
[compojure "1.3.4"]
[prone "0.8.2"]
[http-kit "2.1.16"]
[compojure "1.1.8"]
[ring/ring-core "1.3.2"]
[ring/ring-devel "1.3.2"]]
[environ "1.0.0"]]

:plugins [[lein-cljsbuild "1.0.5"]
[lein-figwheel "0.3.3"]]

:source-paths ["src"]

:resource-paths ["lib/itextpdf-5.5.6.jar" "resources"]

:clean-targets ^{:protect false} ["resources/public/js/compiled" "target"]

:main character-suite.server

:cljsbuild {
:builds [{:id "dev"
:source-paths ["src"]

:figwheel { :on-jsload "character-suite.core/on-js-reload" }

:compiler {:main character-suite.core
:asset-path "js/compiled/out"
:output-to "resources/public/js/compiled/character_suite.js"
:output-dir "resources/public/js/compiled/out"
:source-map-timestamp true }}
{:id "dc"
:source-paths ["src"]

:figwheel { :on-jsload "character-suite.devcards/on-js-reload" }

:compiler {:main character-suite.devcards
:asset-path "js/compiled/out-devcards"
:output-to "resources/public/devcards/js/compiled/character_suite_devcards.js"
:output-dir "resources/public/devcards/js/compiled/out-devcards"
:source-map-timestamp true }}
{:id "min"
:source-paths ["src"]
:compiler {:output-to "resources/public/js/compiled/character_suite.js"
:main character-suite.core
:optimizations :advanced
:pretty-print false}}]}

:figwheel {
;; :http-server-root "public" ;; default and assumes "resources"
;; :server-port 3449 ;; default
:css-dirs ["resources/public/css"] ;; watch and update CSS

;; Start an nREPL server into the running figwheel process
;; :nrepl-port 7888

;; Server Ring Handler (optional)
;; if you want to embed a ring handler into the figwheel http-kit
;; server, this is for simple ring servers, if this
;; doesn't work for you just run your own server :)
:ring-handler character-suite.server/app

;; To be able to open files in your editor from the heads up display
;; you will need to put a script on your path.
;; that script will have to take a file path and a line number
;; ie. in ~/bin/myfile-opener
;; #! /bin/sh
;; emacsclient -n +$2 $1
;;
;; :open-file-command "myfile-opener"

;; if you want to disable the REPL
;; :repl false

;; to configure a different figwheel logfile path
;; :server-logfile "tmp/logs/figwheel-logfile.log"
})
[lein-environ "1.0.0"]]

:min-lein-version "2.5.0"

:uberjar-name "character-suite.jar"

:cljsbuild {:builds {:app {:source-paths ["src/cljs"]
:compiler {:output-to "resources/public/js/app.js"
:output-dir "resources/public/js/out"
:source-map "resources/public/js/out.js.map"
:preamble ["react/react.min.js"]
:optimizations :none
:pretty-print true}}}}

:profiles {:dev {:source-paths ["env/dev/clj"]
:test-paths ["test/clj"]

:dependencies [[figwheel "0.2.5"]
[figwheel-sidecar "0.2.5"]
[com.cemerick/piggieback "0.1.5"]
[weasel "0.6.0"]]

:repl-options {:init-ns character-suite.server
:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]}

:plugins [[lein-figwheel "0.2.5"]]

:figwheel {:http-server-root "public"
:server-port 3449
:css-dirs ["resources/public/css"]
:ring-handler character-suite.server/http-handler}

:env {:is-dev true}

:cljsbuild {:test-commands { "test" ["phantomjs" "env/test/js/unit-test.js" "env/test/unit-test.html"] }
:builds {:app {:source-paths ["env/dev/cljs"]}
:test {:source-paths ["src/cljs" "test/cljs"]
:compiler {:output-to "resources/public/js/app_test.js"
:output-dir "resources/public/js/test"
:source-map "resources/public/js/test.js.map"
:preamble ["react/react.min.js"]
:optimizations :whitespace
:pretty-print false}}}}}

:uberjar {:source-paths ["env/prod/clj"]
:hooks [leiningen.cljsbuild]
:env {:production true}
:omit-source true
:aot :all
:main character-suite.server
:cljsbuild {:builds {:app
{:source-paths ["env/prod/cljs"]
:compiler
{:optimizations :advanced
:pretty-print false}}}}}})

resources/public/index.html → resources/index.html View File

@@ -22,7 +22,7 @@


<div id="app" class="row marketing">
<h2>Figwheel template</h2>
<h2>Problem initialising app</h2>
</div>

<!-- div id="test-markup" class="row marketing">
@@ -36,11 +36,13 @@
<p>&copy; Company 2014</p>
</footer -->

<script src="//fb.me/react-0.9.0.js"></script> --
<!-- <script src="//fb.me/react-0.9.0.js"></script> -- -->
<!-- <script src="//code.jquery.com/jquery-1.11.0.js"></script> -- -->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/compiled/character_suite.js" type="text/javascript"></script>
<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/out/goog/base.js" type="text/javascript"></script>
<script src="/js/app.js" type="text/javascript"></script>
<script type="text/javascript">goog.require('character_suite.main')</script>
</div> <!-- /container -->
</body>
</html>

+ 3
- 6
resources/public/devcards/index.html View File

@@ -2,14 +2,13 @@
<html>
<head>
<meta charset="utf-8" />
<script src="//fb.me/react-0.9.0.js"></script>
<script src="//code.jquery.com/jquery-1.11.0.js"></script>
<!-- This showdown has been modified a tiny bit -->
<script src="//rigsomelight.com/devcards/devcards-assets/showdown.js"></script>

<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"
rel="stylesheet" type="text/css">
<link href="//rigsomelight.com/devcards/devcards-assets/devcards.css"
<link href="//rigsomelight.com/devcards/devcards-assets/devcards.css"
rel="stylesheet" type="text/css">
<link href="//rigsomelight.com/devcards/devcards-assets/rendered_edn.css"
rel="stylesheet" type="text/css">
@@ -19,10 +18,8 @@
<body>
<div id="devcards-main"></div>
<div id="app" style="display: none"></div>
<script src="js/compiled/out-devcards/goog/base.js" type="text/javascript"></script>
<script src="../js/jquery.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="js/compiled/character_suite_devcards.js" type="text/javascript"></script>
<script src="/js/out/goog/base.js" type="text/javascript"></script>
<script src="/js/app.js" type="text/javascript"></script>
<script type="text/javascript">goog.require("character_suite.devcards");</script>
</body>
</html>

src/character_suite/character.clj → src/clj/character_suite/character.clj View File


src/character_suite/pdf.clj → src/clj/character_suite/pdf.clj View File


src/character_suite/server.clj → src/clj/character_suite/server.clj View File

@@ -1,24 +1,30 @@
(ns character-suite.server
(:use org.httpkit.server)
(:use [compojure.core :only (defroutes GET POST)])
(:import java.io.ByteArrayInputStream
java.io.File
java.net.URL
java.net.URLDecoder
java.net.URLEncoder
java.util.Date
java.util.UUID)
(:require [character-suite.pdf :as pdf]
java.util.UUID
org.apache.commons.io.IOUtils)
(:require [clojure.java.io :as io]
[character-suite.dev :refer [is-dev? browser-repl start-figwheel]]
[compojure.core :refer [GET POST defroutes]]
[compojure.route :refer [resources not-found]]
[ring.middleware.reload :as reload]
[environ.core :refer [env]]
[ring.adapter.jetty :refer [run-jetty]]

[character-suite.pdf :as pdf]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str]
[compojure.route :as route]
[prone.middleware :refer [wrap-exceptions]]
[ring.middleware.file :refer [wrap-file]]
[ring.middleware.params :refer [wrap-params]]
[ring.middleware.reload :refer [wrap-reload]]
[ring.middleware.session :refer [wrap-session]]
[ring.util.response :refer [file-response resource-response]]))
[ring.util.response :refer [file-response resource-response]])
(:gen-class))

(def ^:dynamic *character-path* "characters")

@@ -57,7 +63,7 @@

(defn post-character-handler [request]
(let [raw-body (:body request)
incoming-edn (if raw-body (String. (.bytes raw-body)) "{}")
incoming-edn (if raw-body (IOUtils/toString raw-body "utf-8") "{}")
incoming-character (edn/read-string incoming-edn)
character-uuid (str (UUID/randomUUID))]
(save-character character-uuid incoming-character)
@@ -73,18 +79,34 @@
(GET "/character" [] get-character-handler)
(GET "/character/:id" [id] load-character-handler)
(POST "/character" [] post-character-handler)
(GET "/" [] (resource-response "index.html" {:root "public"}))
(route/resources "/")
(route/not-found "404 Not Found"))
(GET "/" [] (io/resource "index.html"))
(resources "/")
(resources "/react" {:root "react"})
(not-found "404 Not Found"))

(def app
(-> app-routes
wrap-params
wrap-session
wrap-exceptions
wrap-reload))
wrap-exceptions))

(def http-handler
(if is-dev?
(reload/wrap-reload app)
app))

(defn run-web-server [& [port]]
(let [port (Integer. (or port (env :port) 10555))]
(println (format "Starting web server on port %d." port))
(run-jetty http-handler {:port port :join? false})))

(defn run-auto-reload [& [port]]
(start-figwheel))

(defn run [& [port]]
(when is-dev?
(run-auto-reload))
(run-web-server port))

(defn -main []
(let [port 9090]
(println (str "Starting CS server on http://localhost:" port))
(run-server app {:port port})))
(defn -main [& [port]]
(run port))

src/character_suite/character.cljs → src/cljs/character_suite/character.cljs View File


src/character_suite/core.cljs → src/cljs/character_suite/core.cljs View File

@@ -1,14 +1,14 @@
(ns ^:figwheel-always character-suite.core
(:require [ajax.core :refer [GET POST]]
[character-suite.character :as c]
[character-suite.views :as views]
[cljs.core.async :refer [chan <!]]
[cljs.reader :as reader]
[clojure.set :as set]
[clojure.string :as str]
[om.core :as om :include-macros true]
[om.dom :as dom :include-macros true])
(:require-macros [cljs.core.async.macros :refer [go]]))
(ns character-suite.core
(:require [ajax.core :refer [GET POST]]
[character-suite.character :as c]
[character-suite.views :as views]
[cljs.core.async :refer [chan <!]]
[cljs.reader :as reader]
[clojure.set :as set]
[clojure.string :as str]
[om.core :as om :include-macros true]
[om.dom :as dom :include-macros true])
(:require-macros [cljs.core.async.macros :refer [go]]))

(enable-console-print!)

@@ -86,9 +86,10 @@
:edit character-edit-view)]
(om/build view app)))))

(om/root app-view
app-state
{:target (.getElementById js/document "app")})
(defn main []
(om/root app-view
app-state
{:target (.getElementById js/document "app")}))


(defn on-js-reload []

src/character_suite/devcards.cljs → src/cljs/character_suite/devcards.cljs View File


src/character_suite/dice.cljs → src/cljs/character_suite/dice.cljs View File


src/character_suite/views.cljs → src/cljs/character_suite/views.cljs View File


src/character_suite/views/distribute.cljs → src/cljs/character_suite/views/distribute.cljs View File


src/character_suite/views/free_form.cljs → src/cljs/character_suite/views/free_form.cljs View File


src/character_suite/views/free_form_distribute.cljs → src/cljs/character_suite/views/free_form_distribute.cljs View File

@@ -72,7 +72,7 @@
(om/update! step-spec [:answers label] value)
(om/update! step-spec [:next-answer] {:label "" :value ""}))
(.preventDefault e)
(.focus (om/get-node owner :new-label)))))
(.focus (om/get-node owner "new-label")))))

(defn- on-new-label-change [step-spec owner]
#(handle-change %
@@ -88,14 +88,14 @@

(defn- new-destination-box [step-spec owner]
(reify
om/IWillMount
(will-mount [_]
om/IDidMount
(did-mount [_]
(go
(while true
(let [new-suggestion (<! (om/get-state owner :channel))]
(om/update! step-spec [:next-answer] {:label new-suggestion
:value ""})
(.focus (om/get-node owner :new-value))))))
(.focus (om/get-node owner "new-value"))))))
om/IRenderState
(render-state [_ {:keys [forbidden]}]
(let [new-label (get-in step-spec [:next-answer :label])
@@ -103,14 +103,14 @@
(dom/div
#js {:className "form-inline opposed-set"}
(dom/span #js {:className "opposed-item"}
(dom/input #js {:ref :new-label
(dom/input #js {:ref "new-label"
:className (mark-if-invalid forbidden
new-label)
:onChange (on-new-label-change step-spec
owner)
:value new-label}))
(dom/span #js {:className "opposed-value"}
(dom/input #js {:ref :new-value
(dom/input #js {:ref "new-value"
:size 3
:onChange #(handle-change %
step-spec

src/character_suite/views/pick.cljs → src/cljs/character_suite/views/pick.cljs View File


src/character_suite/views/utils.cljs → src/cljs/character_suite/views/utils.cljs View File


+ 5
- 0
test/clj/character_suite/example_test.clj View File

@@ -0,0 +1,5 @@
(ns character_suite.example-test
(:require [clojure.test :refer :all]))

(deftest example-passing-test
(is (= 1 1)))

+ 6
- 0
test/cljs/character_suite/core-test.cljs View File

@@ -0,0 +1,6 @@
(ns character_suite.core-test
(:require-macros [cljs.test :refer (is deftest testing)])
(:require [cljs.test]))

(deftest example-passing-test
(is (= 1 1)))

+ 13
- 0
test/cljs/character_suite/test-runner.cljs View File

@@ -0,0 +1,13 @@
(ns character_suite.test-runner
(:require
[cljs.test :refer-macros [run-tests]]
[character_suite.core-test]))

(enable-console-print!)

(defn runner []
(if (cljs.test/successful?
(run-tests
'character_suite.core-test))
0
1))

Loading…
Cancel
Save