Permalink
Browse files

Added a way to list available genomes.

  • Loading branch information...
1 parent 62de9cd commit 4de0a5b5f99235eddbb8d97ca20ff936d1311f7f @Raynes Raynes committed Nov 6, 2011
Showing with 20 additions and 11 deletions.
  1. +20 −11 src/spawn/core.clj
View
@@ -1,18 +1,14 @@
(ns spawn.core
"A code generation utility"
- (:import (java.io File)))
+ (:require [clojure.java.io :as io]))
(defn resolve-genome
"Loads a Spawn genome, assuming a .spawn.genome.<name> namespace.
Returns the var <name> in the genome namespace."
[name]
- (try
- (let [namespace (symbol (str (symbol (str "spawn.genome." name))))]
- (require namespace)
- (ns-resolve namespace (symbol name)))
- (catch Exception e
- (println "Error loading genome")
- (throw e))))
+ (let [namespace (symbol (str (symbol (str "spawn.genome." name))))]
+ (require namespace)
+ (ns-resolve namespace (symbol name))))
(defn- requires-project?
"Tests if a given genome var requires a project to run"
@@ -24,19 +20,32 @@ Returns the var <name> in the genome namespace."
"Tests if a given genome var can take a project"
[genome]
(some (fn [arglist] (= (symbol "project") (first arglist)))
- (:arglists (meta genome))))
+ (:arglists (meta genome))))
(defn spawn
"A code generation utility"
[genome & {:keys [project args]}]
- (let [genome (resolve-genome genome)
+ (let [genome (resolve-genome genome)
genome-fn (deref genome)]
(if project
(if (takes-project? genome)
(apply genome-fn project args)
(apply genome-fn args))
(if (requires-project? genome)
- (throw (Exception. "genome" "must be run in the context of an existing project"))
+ (throw
+ (Exception. (str "genome" genome "must be run in the context of an existing project")))
(apply genome-fn args)))))
+(defn- get-genome-files []
+ (remove #(.isDirectory %)
+ (.listFiles (io/file (io/resource "spawn/genome")))))
+
+(defn- drop-ext [f]
+ (.replace (first (.split f "\\.")) "_" "-"))
+(defn genomes
+ "Get a list of genomes on the classpath."
+ []
+ (for [genome (map (comp meta resolve-genome drop-ext #(.getName %))
+ (get-genome-files))]
+ [(:name genome) genome]))

0 comments on commit 4de0a5b

Please sign in to comment.