Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

communicate host/port/project from repls to cljsh.core

communicate true env and cmd-line args/options from cljsh to cljsh.core in inheritable-thread-local
  • Loading branch information...
commit d7660bc0f12acfded54aa30542d4cb16c36543c4 1 parent 29117ac
@franks42 authored
View
6 README.md
@@ -37,9 +37,9 @@ The "repls" plugin is installed thru the standard Leiningen mechanism:
Lastly, you will have to download the cljsh shell script and put it somewhere on your path:
- $ curl https://raw.github.com/franks42/lein-repls/master/bin/cljsh.sh > cljsh
- $ chmod +x cljsh
- $ mv cljsh /somewhere-on-your-path/cljsh
+ $ curl https://raw.github.com/franks42/lein-repls/stable/bin/cljsh > /tmp/cljsh
+ $ chmod +x /tmp/cljsh
+ $ mv /tmp/cljsh /somewhere-on-your-path/cljsh
Alternatively, you can clone the github repo: "https://github.com/franks42/lein-repls"
View
118 bin/cljsh
@@ -50,7 +50,6 @@ if type -p curl >/dev/null 2>&1; then
fi
-
# util functions
function fullFilePath()
@@ -143,53 +142,51 @@ function startRepls ()
function upgradeCljsh ()
{
-if [ "$CLJSH_DIR" != "" ]; then
- displayAlert "ERROR: The cljsh upgrade is not meant to be run from a checkout."
- exit 1
-fi
-if [ "$SNAPSHOT" = "YES" ]; then
- displayAlert "The upgrade task is only meant for stable releases."
- exit 1
-fi
-if [ ! -w "$SCRIPT" ]; then
- displayAlert "ERROR: You do not have permission to upgrade the installation in $SCRIPT"
- exit 1
-else
- echo "Downloading cljsh stable version number..."
- TARGET="/tmp/cljsh-$$-upgrade"
- CLJSH_SCRIPT_URL="https://raw.github.com/franks42/lein-repls/stable/bin/cljsh"
- $HTTP_CLIENT "$TARGET" "$CLJSH_SCRIPT_URL"
- CLJSH_STABLE_VERSION="$( cat "$TARGET" | grep CLJSH_VERSION=\" )"
- if [ "$CLJSH_STABLE_VERSION" == "" ]; then
- displayAlert "ERROR: unable to obtain stable cljsh version info from github."
- exit 1;
+ if [ "$CLJSH_DIR" != "" ]; then
+ displayAlert "ERROR: The cljsh upgrade is not meant to be run from a checkout."
+ exit 1
+ fi
+ if [ "$SNAPSHOT" = "YES" ]; then
+ displayAlert "The upgrade task is only meant for stable releases."
+ exit 1
fi
- echo "cljsh at \"$SCRIPT\" has version \"$CLJSH_VERSION\" and the latest stable version is $CLJSH_STABLE_VERSION."
- /bin/echo -n "Do you want to upgrade to stable version $CLJSH_STABLE_VERSION [y/N]? "
- read RESP
- case "$RESP" in
+ if [ ! -w "$SCRIPT" ]; then
+ displayAlert "ERROR: You do not have permission to upgrade the installation in $SCRIPT"
+ exit 1
+ else
+ echo "Downloading cljsh stable version number..."
+ TARGET="/tmp/cljsh-$$-upgrade"
+ CLJSH_SCRIPT_URL="https://raw.github.com/franks42/lein-repls/stable/bin/cljsh"
+ $HTTP_CLIENT "$TARGET" "$CLJSH_SCRIPT_URL"
+ CLJSH_STABLE_VERSION="$( cat "$TARGET" | grep CLJSH_VERSION=\" )"
+ if [ "$CLJSH_STABLE_VERSION" == "" ]; then
+ displayAlert "ERROR: unable to obtain stable cljsh version info from github."
+ exit 1;
+ fi
+ echo "cljsh at \"$SCRIPT\" has version \"$CLJSH_VERSION\" and the latest stable version is $CLJSH_STABLE_VERSION."
+ /bin/echo -n "Do you want to upgrade to stable version $CLJSH_STABLE_VERSION [y/N]? "
+ read RESP
+ case "$RESP" in
y|Y)
- echo
- echo "Upgrading cljsh..."
- mv "$TARGET" "$SCRIPT" \
- && chmod +x "$SCRIPT" \
- && "$SCRIPT" -v
- exit $?;;
+ echo "Upgrading cljsh..."
+ mv "$TARGET" "$SCRIPT" \
+ && chmod +x "$SCRIPT" \
+ && "$SCRIPT" -v
+ exit $?;;
*)
- echo "Cljsh upgrade aborted."
- exit 1;;
- esac
-fi
+ echo "Cljsh upgrade aborted."
+ exit 1;;
+ esac
+ fi
}
# up/down/side-grade lein-repls plugin
# lein_repls_version="`cljsh -c '(println cljsh.core/lein-repls-version)'`"
-
function upgradeLeinRepls ()
{
- echo "Determining if lein-repls is installed..."
+ echo "Determine if the lein-repls plugin is installed..."
if [ "`lein help | grep 'repls'`" != "" ]; then
CURRENT_LEIN_REPLS_VERSION="`cljsh -c '(println cljsh.core/lein-repls-version)'`"
if [ "$CURRENT_LEIN_REPLS_VERSION" == "" ]; then
@@ -209,7 +206,6 @@ function upgradeLeinRepls ()
read RESP;
if [ "$RESP" != "" ]; then
echo "Installing/Upgrading-to lein-repls version "$RESP"...";
- set -x
if [ "$CURRENT_LEIN_REPLS_VERSION" != "" ]; then
lein plugin uninstall lein-repls "$CURRENT_LEIN_REPLS_VERSION" && lein plugin install lein-repls "$RESP"
else
@@ -220,10 +216,29 @@ function upgradeLeinRepls ()
echo "lein-repls upgrade aborted.";
exit 1;
fi
-
}
+# Cljsh args and env preparation
+
+export CLJSH_OPT="$*"
+export CLJSH_OPT2="$@"
+export CLJSH_ARGS
+for iarg in "$@"
+do
+ CLJSH_ARGS="$CLJSH_ARGS \"`echo "$iarg" | sed 's/\"/\\\\\"/g'`\""
+done
+
+export CLJSH_PID="$$"
+export CLJSH__="$_"
+export CLJSH_0="$0"
+export CLJSH_HOSTNAME="$HOSTNAME"
+export CLJSH_HOSTTYPE="$HOSTTYPE"
+export CLJSH_OSTYPE="$OSTYPE"
+export CLJSH_PWD="$PWD"
+export CLJSH_UID="$UID"
+
+
# Clojure code snippets
CLJ_EVAL_PRINT_CODE='(cljsh.core/set-repl-result-print prn)'
@@ -235,7 +250,7 @@ CLJ_EXIT_REPLS_CODE='(System/exit 0)'
LEIN_REPL_KILL_SWITCH=':leiningen.repl/exit'
# rlwrap's clojure word completion in the repl use the following file
-RLWRAP_CLJ_WORDS_FILE=${RLWRAP_CLJ_WORDS_FILE:-"$HOME/.clj_completions"}
+export RLWRAP_CLJ_WORDS_FILE=${RLWRAP_CLJ_WORDS_FILE:-"$HOME/.clj_completions"}
# determine the directory of the associated project.clj, or $HOME if none.
export LEIN_PROJECT_DIR=$(leinProjectDir)
@@ -258,7 +273,7 @@ export CLJSH_MAXTIME=${CLJSH_MAXTIME:-"600"}
CLJ_TMP_FNAME=`basename $0`
# determine what is connected to the stdin of this script
-CLJSH_STDIN="REDIRECTED"
+export CLJSH_STDIN="REDIRECTED"
if [[ -p /dev/stdin ]]; then CLJSH_STDIN="PIPE"; fi
if [[ -t 0 ]]; then CLJSH_STDIN="TERM"; fi
@@ -270,6 +285,15 @@ CLJ_CODE=`mktemp -t ${CLJ_TMP_FNAME}` || exit 1
CLJ_CODE_BEFORE=${CLJ_CODE}
CLJ_CODE_AFTER=
+# communicate env and cmd-line args/options to repls-server
+
+export CLJSH_ENV=$( env )
+CLJSH_ENV=`echo "$CLJSH_ENV" | sed 's/\"/\\\\\"/g'`
+
+echo '(cljsh.core/register-cljsh-env "'"${CLJSH_ENV}"'")' >> ${CLJ_CODE}
+echo '(cljsh.core/register-cljsh-command-line-args ['"$CLJSH_ARGS"'])' >> ${CLJ_CODE}
+
+
# command line option processing
while [ "${!OPTIND}" == "-" ] || getopts ":darwplLPgGhtuUvVm:c:s:e:f:i:" opt; do
case $opt in
@@ -345,17 +369,18 @@ while [ "${!OPTIND}" == "-" ] || getopts ":darwplLPgGhtuUvVm:c:s:e:f:i:" opt; do
;;
u) # upgrade lein-repls
upgradeLeinRepls
- exit 0
+ exit $?
;;
U) # upgrade cljsh
upgradeCljsh
- exit 0
+ exit $?
;;
v) # version information
- displayAlert Clojure Shell version: "${CLJSH_VERSION}";
+ CURRENT_LEIN_REPLS_VERSION="`cljsh -c '(println cljsh.core/lein-repls-version)'`"
+ displayAlert cljsh version: "${CLJSH_VERSION}" and lein-repls version: "${CURRENT_LEIN_REPLS_VERSION}";
exit 0
;;
- V) # version information
+ V) # version information only for script-usage
echo "${CLJSH_VERSION}";
exit 0
;;
@@ -387,6 +412,8 @@ while [ "${!OPTIND}" == "-" ] || getopts ":darwplLPgGhtuUvVm:c:s:e:f:i:" opt; do
${CLJSH_PRG} -p # -p print eval results to stdout (discarded by default)
${CLJSH_PRG} -l # -l starts the lein-repls server (when needed)
${CLJSH_PRG} -L # -L Stop/exit the lein-repls server
+ ${CLJSH_PRG} -u # -u install/upgrade lein-repls plugin from clojars
+ ${CLJSH_PRG} -U # -U upgrade cljsh script from github
${CLJSH_PRG} -w # -w refresh the clojure completion-words for rlwrap within context
${CLJSH_PRG} -g # -g pickup "global" project's repls-server coordinates
${CLJSH_PRG} -G # -G set current project coordinates for the "global" repls-server
@@ -449,6 +476,7 @@ if [ -n "$CLJFILE" ]; then
echo '(load-file "'"${CLJFILEFP}"'")' >> ${CLJ_CODE}
fi
+
# special cases for options
# no options, no clj-file and connected to the terminal => assume interactive repl
View
14 examples/BBEdit/clj-eval.sh
@@ -4,13 +4,6 @@
# BB_DOC_SELSTART and BB_DOC_SELEND yield character index for start and end of selection.
# If start and end are equal, then no selection and it designates the cursor position
-# ensure that document is saved, otherwise we work with out-of-date data
-osascript <<-END
- tell application "BBEdit"
- save document 1
- end tell
-END
-
# use applescript to get selection from current bbedit window
DOC_SELECTION=$( osascript <<-END
tell application "BBEdit"
@@ -25,9 +18,14 @@ END
if [ "${DOC_SELECTION}" != "" ]; then
# write selection to a newly created temp-file
CLJ_FILE=`mktemp -t bbedit_cljsh`.clj || exit 1
- # write selection thru some head&tail juggling to tmp-file
echo "${DOC_SELECTION}" > "${CLJ_FILE}"
else
+ # ensure that document is saved, otherwise we work with out-of-date data
+ osascript <<-END
+ tell application "BBEdit"
+ save document 1
+ end tell
+ END
# no selection, so send whole clj-file for eval
CLJ_FILE="${BB_DOC_PATH}"
fi
View
2  project.clj
@@ -1,4 +1,4 @@
-(defproject lein-repls "1.9.0"
+(defproject lein-repls "1.9.4-SNAPSHOT"
:description "A leiningen plugin to start a persistent repl server for use with cljsh"
:dependencies [ [org.clojure/clojure "1.3.0"]
[hiccup "0.3.8"]
View
13 src/cljsh/core.clj
@@ -11,11 +11,20 @@
[clojure.string]
[clojure.pprint]
[cljsh.utils]
+ [clojure.java.shell]
;[clojure.tools.cli]
))
-;; note that we have to keep this in sync with the project.clj entry
-(def lein-repls-version "1.6.0")
+;; some set at lein-repls start in leiningen.repls/repl-server
+(def ^:dynamic cljsh.core/lein-repls-name "lein-repls")
+(def ^:dynamic cljsh.core/lein-repls-version "1.9.4-SNAPSHOT")
+(def ^:dynamic cljsh.core/lein-project-name "")
+(def ^:dynamic cljsh.core/lein-project-version "")
+(def ^:dynamic cljsh.core/lein-repls-host "")
+(def ^:dynamic cljsh.core/lein-repls-port "")
+(def ^:dynamic cljsh.core/lein-repls-pid (:out (clojure.java.shell/sh "bash" "-c" (str "echo -n ${PPID}"))))
+
+
(defn ping [] (println "pong"))
View
20 src/leiningen/repls.clj
@@ -95,10 +95,19 @@
(if ~*trampoline?*
(clojure.main/repl ~@options)
- (do (when-not ~*interactive?*
- (println "## Clojure" (clojure-version) "- \"lein-repls\" console and server started on project" (str "\"" ~(:name project) " " ~(:version project) "\"") (str "(pid/host/port:" (binding [*ns* (find-ns (quote clojure.java.shell))] (eval (quote (:out (clojure.java.shell/sh "bash" "-c" (str "echo -n ${PPID}")))))) "/" ~host "/" ~port ") ##"))
- ;; block to avoid shutdown-agents
- @(promise)))))))
+ (do
+ (let [lein-project-version# ~(:version project)
+ lein-project-name# ~(:name project)
+ lein-repls-host# ~host
+ lein-repls-port# ~port]
+ (binding [*ns* (find-ns 'cljsh.core)] (eval `(do (def ^:dynamic cljsh.core/lein-project-version ~lein-project-version#)
+ (def ^:dynamic cljsh.core/lein-project-name ~lein-project-name#)
+ (def ^:dynamic cljsh.core/lein-repls-host ~lein-repls-host#)
+ (def ^:dynamic cljsh.core/lein-repls-port ~lein-repls-port#)))))
+ (when-not ~*interactive?*
+ (println "## Clojure" (clojure-version) "- \"lein-repls\" console and server started on project" (str "\"" ~(:name project) " " ~(:version project) "\"") (str "(pid/host/port:" (binding [*ns* (find-ns (quote clojure.java.shell))] (eval (quote (:out (clojure.java.shell/sh "bash" "-c" (str "echo -n ${PPID}")))))) "/" ~host "/" ~port ") ##"))
+ ;; block to avoid shutdown-agents
+ @(promise)))))))
(defn copy-out-loop [reader]
(let [buffer (make-array Character/TYPE 1000)]
@@ -179,7 +188,8 @@ See \"https://github.com/franks42/lein-repls\" for details and docs."
out5 (:out (clojure.java.shell/sh "bash" "-c" (str "echo export LEIN_PROJECT_NAME='" (:name project) "'" " >> " pwd "/.lein_repls")))
out6 (:out (clojure.java.shell/sh "bash" "-c" (str "echo export LEIN_PROJECT_VERSION='" (:version project) "'" " >> " pwd "/.lein_repls")))
]
- (println "hi" (:name project)))
+ ;(binding [*ns* (find-ns 'cljsh.core)] (eval `(def ^:dynamic cljsh.core/lein-repls-version ~(:version project)))))
+ (binding [*ns* (find-ns 'cljsh.core)] (eval `(def ^:dynamic cljsh.core/lein-repls-version "jaja"))))
(if *trampoline?*
(eval-in-project project server-form)
(do (future (if (empty? project)
Please sign in to comment.
Something went wrong with that request. Please try again.