Skip to content

Commit

Permalink
Fixing execution environment.
Browse files Browse the repository at this point in the history
Facility for loading commands from known directory.
Sample 'pwd' command added.
  • Loading branch information
Sanel Zukan committed Sep 29, 2010
1 parent 92586fa commit 1bb224b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 29 deletions.
5 changes: 5 additions & 0 deletions commands/pwd.clj
@@ -0,0 +1,5 @@

(defcommand pwd
"Print current directory."
[]
(.getCanonicalPath (java.io.File. ".")))
41 changes: 20 additions & 21 deletions src/hubris/builtin.clj
Expand Up @@ -5,29 +5,28 @@
(:use hubris.command)
(:use hubris.repl))

(defn exit
"Exit from shell."
(defn register-all
"Register all builtin commands. Done at application startup."
[]
(System/exit 0))

(defn help
"Show help."
[]
(doseq [cmd (all-commands)]
(printf " %-20s %s\n" cmd "Here should go help"))
)
(defcommand help
"Show help."
[]
(doseq [cmd (all-commands)]
(printf " %-20s %s\n" cmd (command-doc cmd))
) )

(defn clojure-mode-on
"Go into Clojure mode."
[]
(println "You are now in Clojure mode. To return back, type '(hubris.repl/clojure-mode false)'.")
(clojure-mode true))
(defcommand exit
"Exit from shell."
[]
(System/exit 0))

(defn register-all
"Register all builtin commands. Done at application startup."
[]
(make-builtin help)
(make-builtin exit)
;; commands to be executed in sandbox
;; println is special as is refered to clojure content
(make-builtin println)
(make-builtin clojure-mode-on))

(defcommand clojure-mode-on
"Go into Clojure mode."
[]
(println "You are now in Clojure mode. To return back, type '(hubris.repl/clojure-mode false)'.")
(clojure-mode true))
)
21 changes: 15 additions & 6 deletions src/hubris/command.clj
@@ -1,7 +1,11 @@
;; vim:ft=lisp:ts=3:sw=3:expandtab
(ns hubris.command
(:gen-class))
"Functions for handling hubris actions (aka commands)."
(:gen-class)
(:use clojure.contrib.with-ns))

;; here are store registered commands, but to resolve each of them make sure to
;; use 'hubris.command' namespace, as there symbols are stored by default
(def *known-commands* (ref []))

(defn command-exists?
Expand Down Expand Up @@ -35,19 +39,24 @@
[]
@*known-commands*)

(defmacro command-doc
(defn command-doc
"Return associated documentation for command or nil if not exists."
[cmd]
`(:doc (meta (var ~cmd))))
;; only 'println' is in clojure namespace
(if (= cmd 'println)
(:doc (meta (intern 'clojure.core cmd)))
(:doc (meta (intern 'hubris.builtin cmd)))
) )

(defmacro defcommand
"Creates hubris command."
[name args & body]
`(do
(register-command '~name)
(defn ~name ~args
~@body)
))
(with-ns 'hubris.builtin
(defn ~name ~args
~@body))
) )

(defmacro make-builtin
"Creates builtin command. For now, nothing too smart, but
Expand Down
28 changes: 28 additions & 0 deletions src/hubris/command_dir.clj
@@ -0,0 +1,28 @@
;; vim:ft=lisp:ts=3:sw=3:expandtab
(ns hubris.command-dir
"Code for loading commands from specific directory."
(:gen-class))

(defn load-dir [fd dir ext]
(doseq [i (.list fd)]
(let [full-path (str dir
java.io.File/separator
i)]
(when (.endsWith i ext)
(printf "Loading %s...\n" full-path)

;; so changes are kept localy
(binding [*ns* *ns*]
(in-ns 'hubris.builtin)
(load-file full-path)
) ) ) ) )

(defn load-all
"Load all files with .clj extension from given directory
and evaluate each of them."
[dir]
(let [fd (new java.io.File dir)]
(if (.exists fd)
(load-dir fd dir ".clj")
(printf "'%s' is not readable. hubris is started with only builtin commands\n" dir)
) ) )
4 changes: 3 additions & 1 deletion src/hubris/core.clj
Expand Up @@ -2,10 +2,12 @@
(ns hubris.core
(:gen-class)
(:use hubris.builtin)
(:use hubris.repl))
(:use hubris.repl)
(:use hubris.command-dir))

(defn -main [& args]
(register-all)
(load-all "commands")
(if (and args
(= "true" (first args)))
(clojure-mode true)
Expand Down
2 changes: 1 addition & 1 deletion src/hubris/repl.clj
Expand Up @@ -62,4 +62,4 @@
(if on
(clojure.main/main)
(init-plain-repl)
))
) )

0 comments on commit 1bb224b

Please sign in to comment.