forked from tangledhelix/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.pl
executable file
·299 lines (247 loc) · 8.33 KB
/
install.pl
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env perl
use strict;
use warnings;
use lib 'lib';
use Cwd 'abs_path';
use File::Basename;
use File::Path 'remove_tree';
my %files = (
bash => [ 'bash', 'bash_profile', 'bashrc' ],
git => [ 'gitconfig', 'gitignore' ],
misc => [ 'cvsrc', 'emacs', 'hgrc', 'ircrc', 'pryrc', 'screenrc', 'tcshrc', 'perldb',
'terminfo', 'tmux.conf', 'perltidyrc', 'inputrc', 'psqlrc', 'colordiffrc' ],
vim => [ 'vim', 'vimrc' ],
zsh => [ 'zlogin', 'zlogout', 'zshenv', 'zshrc' ],
);
# note: bash is not installed by default!
my @files_all;
foreach my $list ('zsh', 'vim', 'git', 'misc') {
foreach my $item (@{$files{$list}}) {
push @files_all, $item;
}
}
# For github repos, use shorthand "user/repo-name".
# For non-github repos, use a full URL.
# Only git repos are supported currently.
my %vim_bundles = (
'abolish' => 'tpope/vim-abolish',
'ack' => 'mileszs/ack.vim',
'clam' => 'sjl/clam.vim',
'ctrlp' => 'kien/ctrlp.vim',
'fugitive' => 'tpope/vim-fugitive',
'gitv' => 'gregsexton/gitv',
'gundo' => 'sjl/gundo.vim',
'my-ackmore' => 'tangledhelix/vim-ackmore',
'my-endwise' => 'tangledhelix/vim-endwise',
'nerdtree' => 'scrooloose/nerdtree',
'octopress' => 'tangledhelix/vim-octopress',
'pathogen' => 'tpope/vim-pathogen',
'perl' => 'vim-perl/vim-perl',
'pgsql' => 'exu/pgsql.vim',
'puppet' => 'puppetlabs/puppet-syntax-vim',
'quickrun' => 'thinca/vim-quickrun',
'repeat' => 'tpope/vim-repeat',
'signify' => 'mhinz/vim-signify',
'snipmate' => 'msanders/snipmate.vim',
'snipmate-snippets' => 'tangledhelix/snipmate-snippets',
'solarized' => 'altercation/vim-colors-solarized',
'statline' => 'millermedeiros/vim-statline',
'surround' => 'tpope/vim-surround',
'syntastic' => 'scrooloose/syntastic',
'tabular' => 'godlygeek/tabular',
'tcomment' => 'tomtom/tcomment_vim',
'textobj-rubyblock' => 'nelstrom/vim-textobj-rubyblock',
'textobj-user' => 'kana/vim-textobj-user',
'unimpaired' => 'tpope/vim-unimpaired'
);
my $replace_all = 0;
my $vim_do_updates = 0;
my $basedir = dirname(abs_path($0));
chdir $basedir;
$ENV{PATH} = '/usr/local/bin:/usr/bin:/bin';
print_help() unless defined($ARGV[0]);
foreach my $action (@ARGV) {
if ($action eq 'bash') {
foreach my $file (@{$files{bash}}) {
determine_action($file, 'dotfile');
}
} elsif ($action eq 'zsh') {
foreach my $file (@{$files{zsh}}) {
determine_action($file, 'dotfile');
}
omz_cloner();
} elsif ($action eq 'update:zsh') {
omz_updater();
} elsif ($action eq 'omz') {
omz_cloner();
} elsif ($action eq 'vim') {
foreach my $file (@{$files{vim}}) {
determine_action($file, 'dotfile');
}
vim_bundle_installer();
} elsif ($action eq 'update:vim') {
vim_bundle_cleanup();
vim_bundle_updater();
} elsif ($action eq 'cleanup:vim') {
vim_bundle_cleanup();
} elsif ($action eq 'update:all') {
foreach my $file (@files_all) {
determine_action($file, 'dotfile');
}
scripts_installer();
omz_updater();
vim_bundle_cleanup();
vim_bundle_updater();
} elsif ($action eq 'git') {
foreach my $file (@{$files{git}}) {
determine_action($file, 'dotfile');
}
} elsif ($action eq 'scripts') {
scripts_installer();
} elsif ($action eq 'all') {
foreach my $file (@files_all) {
determine_action($file, 'dotfile');
}
scripts_installer();
vim_bundle_installer();
omz_cloner();
} else {
print "*** ERROR: Unknown action $action\n";
print_help();
}
}
sub print_help {
print <<EOF;
Usage: $0 <target>
all - Install everything
bash - Install bash files only
git - Install git files only
zsh - Install zsh files only
vim - Install vim files and bundles only
scripts - Install my motley collection of scripts
update:vim - Update vim bundles
update:zsh - Update oh-my-zsh and its submodules
update - Update both vim and zsh
update:all - Install everything, update both vim and zsh
EOF
exit;
}
sub determine_action {
my $file = shift;
my $type = shift;
my $src_path;
my $link_path;
if ($type eq 'dotfile') {
$src_path = "$basedir/$file";
$link_path = "$ENV{HOME}/.$file";
} elsif ($type eq 'script') {
$src_path = "$basedir/scripts/$file";
$link_path = "$ENV{HOME}/bin/$file";
} else {
die "*** determine_action(): Type is unknown!\n";
}
if (-l $link_path and (readlink($link_path) eq $src_path)) {
print " skipping $link_path (already linked)\n";
return;
}
if (-d $link_path) {
warn "** $link_path is a directory, skipping!\n";
return;
}
if (-f $link_path or -l $link_path) {
if ($replace_all) {
replace_file($src_path, $link_path);
} else {
print "Overwrite ~/.$file? [yNaq] ";
chomp(my $choice = <STDIN>);
if ($choice eq 'a') {
$replace_all = 1;
replace_file($src_path, $link_path);
} elsif ($choice eq 'y') {
replace_file($src_path, $link_path);
} elsif ($choice eq 'q') {
exit;
} else {
print " skipping $link_path\n";
}
}
} else {
link_file($src_path, $link_path);
}
}
sub link_file {
my $src_path = shift;
my $link_path = shift;
print " linking $link_path\n";
symlink $src_path, $link_path or warn "Unable to link $link_path\n";
}
sub replace_file {
my $src_path = shift;
my $link_path = shift;
print " removing old $link_path\n";
unlink $link_path or warn "Could not remove $link_path";
link_file($src_path, $link_path);
}
# clone my omz repository
sub omz_cloner {
my $omz_path = "$basedir/oh-my-zsh";
my $repo_url = 'https://github.com/tangledhelix/oh-my-zsh.git';
if (-f $omz_path or -d $omz_path) {
print " $omz_path already exists, skipping\n";
print "To reinstall OMZ, rename or remove $omz_path and try again.\n";
return;
}
system "git clone $repo_url $omz_path";
system "cd $omz_path && git submodule update --init --recursive";
}
# update the omz repository
sub omz_updater {
my $omz_path = "$basedir/oh-my-zsh";
system "cd $omz_path && git pull && git submodule update --init --recursive";
}
# install or update vim bundles
sub vim_bundle_installer {
my $bundle_path = "$ENV{HOME}/.vim/bundle";
mkdir $bundle_path unless -d $bundle_path;
foreach my $bundle (keys %vim_bundles) {
my $repo = $vim_bundles{$bundle};
unless ($repo =~ m{^(https?|git)://}) {
$repo = "https://github.com/$repo.git";
}
my $this_bundle_path = "$bundle_path/$bundle";
if (-d $this_bundle_path) {
if ($vim_do_updates) {
print " updating vim bundle $bundle\n";
system "cd $this_bundle_path && git pull";
} else {
print " skipping vim bundle $bundle (already exists)\n";
}
} else {
print " cloning vim bundle $bundle\n";
system "git clone $repo $this_bundle_path";
}
}
}
sub vim_bundle_updater {
$vim_do_updates = 1;
vim_bundle_installer();
}
# clean out old vim bundles
sub vim_bundle_cleanup {
my $bundle_path = "$ENV{HOME}/.vim/bundle";
foreach my $dir (glob "$bundle_path/*") {
my $basename = basename $dir;
unless ($vim_bundles{$basename}) {
print " cleaning up bundle $basename\n";
remove_tree($dir);
}
}
}
# install script symlinks
sub scripts_installer {
my $install_path = "$ENV{HOME}/bin";
mkdir $install_path, 0700 unless -d $install_path;
foreach my $script (glob "$basedir/scripts/*") {
determine_action(basename($script), 'script');
}
}