-
Notifications
You must be signed in to change notification settings - Fork 1
/
pl6-virt
executable file
·102 lines (89 loc) · 2.42 KB
/
pl6-virt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env perl6
sub MAIN (Str $dir is copy, Str :$perl6 = 'perl6') {
die "$dir already exists\n" if $dir.IO.e;
my $perlname = $perl6.IO.basename;
say "Named perl6 is $perlname";
say "Virtual environment goes to $dir";
my $bindir;
if ($perl6.IO.basename eq $perl6) {
# find it on the path
my @dirs = grep({ $_.IO.child($perlname).e }, $*SPEC.path);
die "Can't find $perl6\n" if not @dirs;
$bindir = @dirs[0];
} elsif ($perl6.IO.e and $perl6.IO.f) {
$bindir = $perl6.IO.parent.absolute;
say "Path to named perl6 is $bindir";
}
my $real_perl6;
if ($perlname ne 'perl6') {
$real_perl6 = $bindir.IO.child($perlname);
}
$dir = $dir.IO.absolute();
my $basename = $dir.IO.basename();
$dir.IO.mkdir();
$dir.IO.child("bin").mkdir();
if ($real_perl6) {
my $shim = $dir.IO.child("bin").child("perl6");
$shim.spurt("exec $real_perl6 \"\$@\"\n");
$shim.chmod($shim.mode +| 0o111);
}
my $env_bindir = $dir.IO.child("bin");
my $activate = $env_bindir.child("activate");
my $script = q:to[ACTIVATE] ;
_OLD_PERL6_VIRTUAL_PATH="$PATH"
_OLD_PERL6_VIRTUAL_PS1="$PS1"
_PL6_ENVNAME="#ENVNAME#"
PATH="#BINDIR#:$PATH"
VENV="#DIR#"
export VENV
PATH="$VENV/bin:$PATH"
if [ $PERL6LIB ]; then
_OLD_PERL6LIB="$PERL6LIB"
PERL6LIB="inst#$VENV,$PERL6LIB"
else
PERL6LIB="inst#$VENV"
fi
export PERL6LIB
deactivate () {
PATH="$_OLD_PERL6_VIRTUAL_PATH"
PS1="$_OLD_PERL6_VIRTUAL_PS1"
if [ -n "$_OLD_PERL6LIB" ] ; then
PERL6LIB="$_OLD_PERL6LIB"
unset _OLD_PERL6LIB
else
unset PERL6LIB
fi
unset _OLD_PERL6_VIRTUAL_PATH
unset _OLD_PERL6_VIRTUAL_PS1
unset _PL6_ENVNAME
unset VENV
unset -f zef
unset -f deactivate
}
PS1="($_PL6_ENVNAME) $PS1"
ACTIVATE
my $real_zef = q:x{which zef}.chomp;
my $zef = q:to[ZEF] ;
#!/bin/sh
REAL_ZEF="#ZEF#"
case "$1" in
install) LOC="--install-to=inst#$VENV";;
uninstall) LOC="--uninstall-from=inst#$VENV";;
*) LOC= ;;
esac
if [ -z "$LOC" ]; then
"$REAL_ZEF" "$@"
else
"$REAL_ZEF" "$LOC" "$@"
fi
ZEF
$script ~~ s:g/'#DIR#'/$dir/;
$script ~~ s:g/'#BINDIR#'/$bindir/;
$script ~~ s:g/'#ENVNAME#'/$basename/;
$activate.IO.spurt($script);
$activate.IO.chmod($activate.IO.mode +| 0o111);
$zef ~~ s:g/'#ZEF#'/$real_zef/;
my $zef_script = $env_bindir.child("zef").IO;
$zef_script.spurt($zef);
$zef_script.chmod($zef_script.mode +| 0o111);
}