-
Notifications
You must be signed in to change notification settings - Fork 1
/
install
executable file
·67 lines (55 loc) · 1.41 KB
/
install
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
#!/usr/bin/env perl
package Dotfile;
use Carp qw( croak );
use File::Basename qw( basename );
use File::Spec::Functions qw( rel2abs );
sub new {
my $self = shift;
bless { @_ }, ref $self || $self;
}
sub source {
my $self = shift;
return $self->{source};
}
sub destination {
my $self = shift;
my $name = basename $self->source;
$name =~ s/^_/./;
$name =~ s/\.TEMPLATE$//;
return "$ENV{HOME}/$name";
}
sub is_templated {
my $self = shift;
return $self->source =~ /\.TEMPLATE$/;
}
sub install_symlink {
my $self = shift;
if (-l $self->destination) {
unlink $self->destination;
}
symlink rel2abs( $self->source ), rel2abs( $self->destination )
or croak "symlink: $!";
}
sub install_template {
my $self = shift;
my $stage = $self->destination . ".$$";
require Template;
my $tt = Template->new;
$tt->process( $self->source, { env => \ %ENV }, $stage )
or croak $tt->error;
if (-f $self->destination) {
system 'diff', '-u', $self->destination, $stage;
unlink $self->destination;
}
rename $stage, $self->destination;
}
sub install {
my $self = shift;
my $src = $self->source;
my $dest = $self->destination;
print "$src => $dest\n";
$self->is_templated ? $self->install_template : $self->install_symlink;
}
for my $source (@ARGV) {
Dotfile->new( source => $source )->install;
}