-
Notifications
You must be signed in to change notification settings - Fork 1
/
install-dotfiles.pl
executable file
·566 lines (454 loc) · 14.9 KB
/
install-dotfiles.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#!/usr/bin/env perl
#
# Copyright (C) 2015-2021 Joelle Maslak
# All Rights Reserved - See License
#
use strict;
use warnings;
# use autodie; # This is incompatible with older perl
use Carp;
use Cwd;
use File::Copy qw(move);
use Symbol;
# Files to ignore
my %IGNORE_FILENAME = (
'.' => 1,
'..' => 1,
'.git' => 1,
'.gitignore' => 1,
'nytprof.out' => 1
);
my %IGNORE_SUFFIX = ( '.swp' => 1 );
MAIN: {
if ( !defined( $ENV{HOME} ) ) {
die "Please set \$HOME environmental variable";
}
if ( umask == 0 ) {
umask 0002; # Defensively set umask
}
# Get options
my %options;
foreach my $arg (@ARGV) {
if ( $arg =~ m/^--..*\=/ms ) {
my ( $name, $val ) = $arg =~ m/^--(..*)\=(.*)$/ms;
$options{ lc($name) } = $val;
} elsif ( $arg =~ m/^--..*$/ms ) {
my ($name) = $arg =~ m/^--(..*)$/ms;
$options{ lc($name) } = 1;
} else {
print STDERR "$arg is an unknown option format\n";
print STDERR "\n";
print STDERR "Aborting.\n";
exit 3;
}
}
# Validate options
my (%valid) = map { $_, 1 } ( 'skipperl', 'skippython' );
foreach my $arg ( sort keys %options ) {
if ( !exists( $valid{$arg} ) ) {
print STDERR "$arg is an unknown option\n";
print STDERR "\n";
print STDERR "Aborting.\n";
exit 2;
}
}
my $home = $ENV{HOME};
my $current = getcwd;
my $network_available = network_available();
if ( ( !-f '~/.dotfile.nogit' ) || ( !exists( $ENV{DOTFILE_NOGIT} ) ) ) {
if ($network_available) {
system "git fetch origin main >/dev/null 2>/dev/null";
my $gitrevs = `git rev-list HEAD..origin/main | wc -l`;
if ( $gitrevs > 0 ) {
print STDERR "Local directory is not synced with remote, please do a git pull.\n";
print STDERR "\n";
print STDERR "To avoid this check, set \$ENV{DOTFILE_NOGIT}\n";
print STDERR "\n";
print STDERR "Aborting.\n";
exit 1;
}
}
}
my $rename_old = 1; # We rename old files
if ( -e "$home/.dotfiles.installed" ) {
# We don't rename if we've already installed stuff
$rename_old = undef;
}
my $environment = get_environment($home);
my $copyright = get_copyright( $home, $environment );
my $fullname = get_fullname( $home, $environment );
my $email = get_email( $home, $environment );
my $personal_email = get_personal_email( $home, $environment );
if ( $environment eq 'work' ) {
assert_work_dotfiles_exist($network_available);
}
install_git_submodules() if $network_available;
install_files( $rename_old, $environment, '', 'src', ['.config'] );
install_files( $rename_old, $environment, '.config', 'src/.config', [], );
install_vim_templates( $home, $copyright );
install_git_config( $fullname, $email, $personal_email );
if ( !-d "$home/tmp" ) {
print "Creating temporary directory...";
mkdir "$home/tmp";
print "\n";
}
if ($network_available) {
system "$current/perl-setup.sh" unless exists $options{skipperl};
system "$current/raku-modules.sh" unless exists $options{skipraku};
# XXX system "$current/python-modules.sh" unless exists $options{skippython};
}
}
sub assert_work_dotfiles_exist {
my ($network_available) = @_;
my $current = getcwd;
if ( !-d "$current/../../netflix/dotfiles" ) {
print STDERR "Work dotfile directory does not exist. Please clone that repo.\n";
print STDERR "\n";
print STDERR "Aborting.\n";
exit 1;
}
if ( ( !-f '~/.dotfile.nogit' ) || ( !exists( $ENV{DOTFILE_NOGIT} ) ) ) {
chdir '../../netflix/dotfiles' or die($!);
if ($network_available) {
system "git fetch origin main >/dev/null 2>/dev/null";
my $gitrevs = `git rev-list HEAD..origin/main | wc -l`;
if ( $gitrevs > 0 ) {
print STDERR
"Work dotfile directory is not synced with remote, please do a git pull.\n";
print STDERR "\n";
print STDERR "To avoid this check, set \$ENV{DOTFILE_NOGIT}\n";
print STDERR "\n";
print STDERR "Aborting.\n";
exit 1;
}
}
chdir $current or die($!);
}
}
sub get_environment {
my ($home) = @_;
my $environment;
if ( -e "$home/.dotfiles.environment" ) {
$environment = slurp("$home/.dotfiles.environment");
chomp $environment;
}
while ( !defined($environment) ) {
print "Please enter the environment (home/work/other):\n";
local $| = 1;
print " > ";
$environment = <STDIN>;
chomp($environment);
if ( ( $environment ne 'home' )
&& ( $environment ne 'work' )
&& ( $environment ne 'other' ) )
{
# Let's try this again
$environment = undef;
next;
}
print "Creating $home/.dotfiles.environment ...";
spitout( "$home/.dotfiles.environment", $environment );
print "\n";
}
return $environment;
}
sub get_copyright {
my ( $home, $environment ) = @_;
my $copyright;
if ( -e "$home/.dotfiles.copyright" ) {
$copyright = slurp("$home/.dotfiles.copyright");
if ( $copyright =~ m/Joel\W/s ) { # We want to change this always!
$copyright = undef;
}
}
while ( !defined($copyright) ) {
if ( $environment eq 'home' ) { $copyright = 'Joelle Maslak' }
if ( $environment eq 'work' ) { $copyright = 'Netflix' }
if ( $environment eq 'other' ) {
print "Please enter the name to use for copyright in templates:\n";
local $| = 1;
print " > ";
$copyright = <STDIN>;
chomp($copyright);
}
if ( $copyright =~ m/\@/ ) {
print "ERROR: Do not include an at-sign in this field\n";
$copyright = undef;
next;
}
print "Creating $home/.dotfiles.copyright ...";
spitout( "$home/.dotfiles.copyright", $copyright );
print "\n";
}
return $copyright;
}
sub get_fullname {
my ( $home, $environment ) = @_;
my $fullname;
if ( -e "$home/.dotfiles.fullname" ) {
$fullname = slurp("$home/.dotfiles.fullname");
if ( $fullname =~ m/Joel\W/s ) { # We want to change this always!
$fullname = undef;
}
}
while ( !defined($fullname) ) {
if ( $environment eq 'home' ) { $fullname = 'Joelle Maslak' }
if ( $environment eq 'work' ) { $fullname = 'Joelle Maslak' }
if ( $environment eq 'other' ) {
print "Please enter the full name to use for git in templates:\n";
local $| = 1;
print " > ";
$fullname = <STDIN>;
chomp($fullname);
}
if ( $fullname =~ m/\@/ ) {
print "ERROR: Do not include an at-sign in this field\n";
$fullname = undef;
next;
}
print "Creating $home/.dotfiles.fullname...";
spitout( "$home/.dotfiles.fullname", $fullname );
print "\n";
}
return $fullname;
}
sub get_email {
my ( $home, $environment ) = @_;
my $email;
if ( -e "$home/.dotfiles.email" ) {
$email = slurp("$home/.dotfiles.email");
if ( $email =~ m/joel\W/is ) { # We want to change this always!
$email = undef;
}
}
while ( !defined($email) ) {
if ( $environment eq 'home' ) { $email = '[email protected]' }
if ( $environment eq 'work' ) { $email = '[email protected]' }
if ( $environment eq 'other' ) {
print "Please enter the email address to use for git in templates:\n";
local $| = 1;
print " > ";
$email = <STDIN>;
chomp($email);
}
if ( $email !~ m/\@/ ) {
print "ERROR: Email address must include an at-sign\n";
$email = undef;
next;
}
print "Creating $home/.dotfiles.email...";
spitout( "$home/.dotfiles.email", $email );
print "\n";
}
return $email;
}
sub get_personal_email {
my ( $home, $environment ) = @_;
my $email;
if ( -e "$home/.dotfiles.personal_email" ) {
$email = slurp("$home/.dotfiles.personal_email");
}
while ( !defined($email) ) {
$email = '[email protected]';
if ( $email !~ m/\@/ ) {
print "ERROR: Email address must include an at-sign\n";
$email = undef;
next;
}
print "Creating $home/.dotfiles.personal_email...";
spitout( "$home/.dotfiles.personal_email", $email );
print "\n";
}
return $email;
}
sub get_and_update_year {
my ($home) = @_;
my (@dtparts) = localtime(time);
my $year = $dtparts[5] + 1900;
my $fileyear = 1900;
if ( -e "$home/.dotfiles.year" ) {
$fileyear = slurp("$home/.dotfiles.year");
}
if ( $fileyear != $year ) {
print "Creating $home/.dotfiles.year ...";
spitout( "$home/.dotfiles.year", $year );
print "\n";
}
return $year;
}
sub install_files {
if ( scalar(@_) != 5 ) { confess 'invalid call'; }
my $rename_old = shift;
my $environment = shift;
my $newdir = shift;
my $directory = shift;
my $exclude = shift;
my $current = getcwd;
my $home = $ENV{HOME};
my $dstdir = $home;
if ( $newdir ne '' ) {
$dstdir = "$home/$newdir";
mkdir $dstdir unless ( -d $dstdir );
}
opendir( my $dh, "$current/$directory" );
my (@dirs);
push @dirs, "$current/$directory";
if ( -d "$current/../../netflix/dotfiles/$directory" ) {
my $workdir = Cwd::abs_path("$current/../../netflix/dotfiles/$directory");
push @dirs, $workdir if ( $environment eq 'work' and defined $workdir );
}
for my $dir (@dirs) {
install_files_dir( $rename_old, $environment, $dstdir, $dir, $exclude );
}
return;
}
sub install_files_dir {
if ( scalar(@_) != 5 ) { confess 'invalid call'; }
my $rename_old = shift;
my $environment = shift;
my $dstdir = shift;
my $srcdir = shift;
my $exclude = shift;
my $home = $ENV{HOME};
print "Processing $srcdir\n";
opendir( my $dh, "$srcdir" );
my @files = sort readdir($dh);
closedir $dh;
LOOP:
foreach my $file (@files) {
print "File: $file -> ";
if ( exists( $IGNORE_FILENAME{$file} ) ) {
print "ignoring\n";
next;
}
if ( scalar( grep { $_ eq $file } @$exclude ) ) {
print "excluded\n";
next LOOP;
}
foreach my $suffix ( keys %IGNORE_SUFFIX ) {
if ( $file =~ m/${suffix}$/ ) {
print "ignoring\n";
next LOOP;
}
}
if ( -e "$dstdir/$file" ) {
if ($rename_old) {
print "backing up old file ... ";
move( "$dstdir/$file", "$dstdir/$file.bak" );
} else {
print "file already exists, skipping\n";
next;
}
}
print "symlinking ... ";
symlink( "$srcdir/$file", "$dstdir/$file" );
print "done\n";
}
return;
}
sub install_vim_templates {
if ( scalar(@_) != 2 ) { confess 'invalid call'; }
my ( $home, $copyright ) = @_;
my $year = get_and_update_year($home);
my $current = getcwd;
opendir( my $dh, "$current/src/.vim/templates" );
my @files = sort readdir($dh);
closedir $dh;
LOOP:
foreach my $file (@files) {
print "Template: $file -> ";
if ( exists( $IGNORE_FILENAME{$file} ) ) {
print "ignoring\n";
next;
}
foreach my $suffix ( keys %IGNORE_SUFFIX ) {
if ( $file =~ m/${suffix}$/ ) {
print "ignoring\n";
next LOOP;
}
}
if ( !( $file =~ m/\.base$/ ) ) {
print "ignoring\n";
next LOOP;
}
my $basefile = $file;
$basefile =~ s/\.base$//;
print "Creating ... ";
my $out = slurp("$current/src/.vim/templates/$file");
$out =~ s/_COPYRIGHT_/$copyright/g;
$out =~ s/_YEAR_/$year/g;
spitout( "$home/.vim/templates/$basefile", $out );
# Make executable if needed
if ( -x "$current/src/.vim/templates/$file" ) {
chmod 0755, "$home/.vim/templates/$basefile";
}
print "done\n";
}
return;
}
sub install_git_submodules {
system("git submodule init");
system("git submodule update --init --recursive");
system("git submodule sync --recursive");
return;
}
sub install_git_config {
my ( $fullname, $email, $personal_email ) = @_;
system("git config --global user.email \"$email\"");
system("git config --global user.name \"$fullname\"");
system("git config --global log.mailmap true");
system("git config --global github.user jmaslak");
system("git config --global push.recurseSubmodules check");
system("git config --global diff.tool ccdiff");
system("git config --global difftool.prompt false");
system("git config --global init.defaultBranch main");
system("git config --global pull.rebase true");
system(
"git config --global difftool.ccdiff.cmd 'ccdiff --bg=black --old=bright_red --utf-8 -u -r \$LOCAL \$REMOTE'"
);
# Also set this repo's email
system("git config --local user.email \"${personal_email}\"");
my $ver = `git version`;
chomp($ver);
if ( $ver =~ m/git version 1\.[01234567]\./ ) {
# Version <= 1.7
# We do NOT configure the simple
} else {
# New enough git!
system("git config --global push.default simple");
}
return;
}
sub slurp {
if ( scalar(@_) != 1 ) { confess 'invalid call' }
my $fn = shift;
# Be compatible with old Perl
my $fh = gensym();
open( $fh, '<', $fn ) or die($!);
my @out = <$fh>;
close($fh);
return join( '', @out );
}
sub spitout {
if ( scalar(@_) != 2 ) { confess 'invalid call' }
my ( $fn, $val ) = @_;
my $fh = gensym();
open( $fh, '>', $fn ) or die("Couldn't create $fn - $!\n");
print $fh $val or die($!);
close($fh);
return;
}
sub network_available {
my $ret = eval { require Net::Ping };
if ( !$ret ) {
warn "Net::Ping not installed.";
return 1; # We can't guarante N:P installed.
}
my $ping = Net::Ping->new();
$ping->port_number(80);
my $result = $ping->ping( '1.1.1.1', 1 );
if ( !$result ) {
print("Network not available, skipping functions dependent upon network\n");
}
return !!$result;
}