-
Notifications
You must be signed in to change notification settings - Fork 2
/
perl-migrate-modules.zsh
executable file
·109 lines (86 loc) · 2.39 KB
/
perl-migrate-modules.zsh
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
103
104
105
106
107
108
109
#!/bin/zsh
set -eu
emulate -L zsh
progFile=$0
function usage {
((! ARGC)) || print 1>&2 $*
cat 1>&2 <<EOF
Usage: ${progFile:t} [-p] [-S] [--SUBCOMMAND] ARGS...
Available Subcommands are:
--migrate FROM_PERL TO_PERL
--list-modules-from PERL
--install-modules-to PERL MODULE_NAMES...
Options:
-p use cpm instead of cpanm
-S use sudo
EOF
exit 1
}
function die { echo 1>&2 $*; exit 1 }
#==========================
o_sudo=() sudo=()
o_cpm=()
o_xtrace=()
zparseopts -D -K S=o_sudo x=o_xtrace p=o_cpm
#==========================
binDir=$(cd $0:h && print $PWD)
xbDir=$binDir:h
function xb_perl {
local spec=$1 dn
if [[ -x $spec && $spec:t == perl && $spec:h:t == bin && -d $spec:h:h/lib/site_perl ]]; then
print $spec
elif [[ -d $spec && -x $spec/bin/perl && -d $spec/lib/site_perl ]]; then
print $spec:a/bin/perl
elif dn=$xbDir/perl-$spec; [[ -d $dn && -x $dn/bin/perl && -d $dn/lib/site_perl ]]; then
print $dn/bin/perl
else
die "Can't find acutal perl path for $spec"
fi
}
#==========================
function cmd-migrate {
((ARGC == 2)) || usage "migrate FROM TO"
local from=$1 to=$2
local pkgs; pkgs=($(cmd-list-modules-from $from)) || return 1
cmd-install-modules-to $to $pkgs
}
#==========================
# Stolen and splitted from plenv's migrate-modules
function cmd-list-modules-from {
((ARGC)) || usage "list-modules-from FROM_PERL"
local from=$1
local fromPerl; fromPerl=$(xb_perl $from) || return 1
$fromPerl -MExtUtils::Installed -e 'for (ExtUtils::Installed->new(skip_cwd => 1)->modules) {next if /\APerl\z/; print $_, "\n";}'
}
function cmd-install-modules-to {
((ARGC)) || usage "install-modules-to TO_PERL"
local to=$1; shift
local toPerl; toPerl=$(xb_perl $to) || return 1
local cpm=$toPerl:h/cpm cpanm=$toPerl:h/cpanm
if (($#o_cpm)) && ! [[ -x $cpm ]]; then
$sudo $cpanm App::cpm
fi
if [[ -x $cpm ]]; then
$sudo $cpm install -g "$@"
elif [[ -x $cpanm ]]; then
$sudo $cpanm "$@"
else
die "Neither cpm nor cpanm found, stopped!"
fi
}
#==========================
((!$#o_xtrace)) || set -x
((!$#o_sudo)) || sudo=(sudo)
#==========================
if ((ARGC)) && [[ $1 == --* ]]; then
if cmd=cmd-${1#--}; (($+functions[$cmd])); then
shift
elif cmd=${1#--}; (($+functions[$cmd])); then
shift
else
usage "No such subcommand: $1"
fi
else
cmd=cmd-migrate
fi
$cmd "$@"