Skip to content

Commit edc18be

Browse files
committed
Improvements to the virtual env functionality.
1 parent bd71d33 commit edc18be

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

bin/janet-pm

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
string search. If no search pattern is given, prints the
3535
entire package listing.
3636
37+
env name
38+
Create an environment with which one can install Janet dependencies and scripts in an isolated manner.
39+
3740
new-project name
3841
Create a new Janet project in a directory `name`.
3942
@@ -236,7 +239,9 @@
236239
(defn new-venv
237240
"Create a new virtual environment"
238241
[path]
239-
(pm/scaffold-pm-shell path))
242+
(pm/scaffold-pm-shell path)
243+
(print "created project shell environment at " path)
244+
(print "run `source " path "/bin/activate` to enter the new environment, then `deactivate` to exit."))
240245

241246
(defn quickbin
242247
"Create an executable file"
@@ -264,7 +269,7 @@
264269
"help" help
265270
"deps" deps
266271
"hook" do-hook
267-
"new-venv" new-venv
272+
"env" new-venv
268273
"new-project" new-project
269274
"new-simple-project" new-simple-project
270275
"new-c-project" new-c-project

spork/pm.janet

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@
633633

634634
(deftemplate enter-shell-template
635635
````
636-
# source bin/enter_shell
636+
# source bin/activate
637637
_OLD_JANET_PATH="$$JANET_PATH";
638638
_OLD_PATH="$$PATH";
639639
_OLD_PS1="$$PS1";
@@ -643,7 +643,7 @@
643643
export PATH;
644644
PS1="("$name") $${PS1:-}"
645645
export PS1;
646-
exit_shell() {
646+
deactivate() {
647647
PATH="$$_OLD_PATH";
648648
JANET_PATH="$$_OLD_JANET_PATH";
649649
PS1="$$_OLD_PS1";
@@ -660,20 +660,24 @@
660660

661661
(deftemplate enter-cmd-template
662662
````
663-
@rem bin\enter_shell.bat
664-
@set _OLD_JANET_PATH="%JANET_PATH%"
665-
@set _OLD_PATH="%PATH%"
666-
@set JANET_PATH="$abspath"
663+
@rem bin\activate.bat
664+
@set _OLD_JANET_PATH=%JANET_PATH%
665+
@set _OLD_PATH=%PATH%
666+
@set _OLD_PROMPT=%PROMPT%
667+
@set JANET_PATH=$abspath
667668
@set PATH=%JANET_PATH%\bin:%PATH%
669+
@set PROMPT=($path) %PROMPT%
668670
````)
669671

670672
(deftemplate exit-cmd-template
671673
````
672-
@rem bin\exit_shell.bat
674+
@rem bin\deactivate.bat
673675
@set JANET_PATH=%_OLD_JANET_PATH%
674676
@set PATH=%_OLD_PATH%
677+
@set PROMPT=%_OLD_PROMPT%
675678
@set _OLD_JANET_PATH=%PATH%
676679
@set _OLD_PATH=%PATH%
680+
@set _OLD_PROMPT=%PROMPT%
677681
````)
678682

679683
(defn scaffold-pm-shell
@@ -683,6 +687,6 @@
683687
(os/mkdir (path/join path "bin"))
684688
(os/mkdir (path/join path "man"))
685689
(def opts {:path path :abspath (path/abspath path) :name (path/basename path)})
686-
(spit (path/join path "bin" "enter_shell") (enter-shell-template opts))
687-
(spit (path/join path "bin" "enter_shell.bat") (enter-cmd-template opts))
688-
(spit (path/join path "bin" "exit_shell.bat") (enter-cmd-template opts)))
690+
(spit (path/join path "bin" "activate") (enter-shell-template opts))
691+
(spit (path/join path "bin" "activate.bat") (enter-cmd-template opts))
692+
(spit (path/join path "bin" "deactivate.bat") (exit-cmd-template opts)))

spork/sh.janet

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,26 @@
178178
[& args]
179179
(string/join (map shell-quote args) " "))
180180

181+
(defn which
182+
"Search for the full path to a program, like the `which` command on unix or the `where` command on Windows."
183+
[name &opt paths]
184+
(default paths
185+
(let [o (os/which)]
186+
(if (or (= o :windows) (= o :mingw))
187+
(string/split ";" (os/getenv "Path"))
188+
(string/split ":" (os/getenv "PATH")))))
189+
(prompt :result
190+
(each p paths
191+
(when (= (os/stat p :mode) :directory)
192+
(def fp (path/join p name))
193+
(when (= (os/stat fp :mode) :file)
194+
(return :result fp))))))
195+
181196
(defn self-exe
182197
"Get path to the janet executable"
183198
[]
184199
(def janet (dyn *executable* "janet"))
185-
(defn which [cmd] (try (exec-slurp cmd janet) ([e] janet)))
186200
(case (os/which)
187-
:windows (which "where")
188-
:mingw (which "where")
189201
:linux (os/readlink "/proc/self/exe")
190202
# default
191-
(which "which")))
203+
(which janet)))

0 commit comments

Comments
 (0)