-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall
executable file
·58 lines (44 loc) · 1.29 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
#!/usr/bin/env perl
use strict;
use warnings;
foreach my $app (@ARGV) {
my $orig_dir = "$ENV{PWD}/$app";
my $link_dir = read_target($app);
-d $link_dir
or mkdir($link_dir)
or die "unable to create directory $link_dir";
foreach my $orig_file (<$orig_dir/*>) {
my $orig_file_name = basename($orig_file);
my $link_file = $link_dir . "/" . expand_dotfile($orig_file_name);
if ($orig_file_name eq "TARGET") {
next;
}
symlink($orig_file, $link_file)
or die "unable to link $link_file";
print("$orig_file -> $link_file\n");
}
}
sub read_target {
my ($app) = @_;
my $target = "$app/TARGET";
open(my $fd, "<", $target)
or die "unable to open $target";
my $content = do { undef $\; <$fd> };
close($fd)
or die "unable to close $target";
$content =~ s/^\s+|\s+$//g; # trim whitespace
$content =~ s/~/$ENV{HOME}/g; # expand tilde (~)
return $content;
}
# get last component of file path
sub basename {
my ($file_path) = @_;
(my $basename = $file_path) =~ s/.+\/(.+)$/$1/;
return $basename
}
# transform strings of the form "dot.zshrc" -> ".zshrc"
sub expand_dotfile {
my ($file_path) = @_;
$file_path =~ s/dot\.(.+)$/\.$1/;
return $file_path;
}