forked from mzp/Git-Hooks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Install.PL
151 lines (134 loc) · 3.45 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
#! /usr/bin/env perl
use warnings;
use strict;
use File::Copy;
use IO::File;
use File::Basename;
use Getopt::Long;
my $verbose = 0;
sub ln {
return if (-e $_[1]);
if ($verbose) {
print("ln -s @_\n");
}
symlink $_[0], $_[1] or die;
}
sub cp {
if ($verbose) {
print("cp @_\n");
}
copy @_ or die;
}
sub exec_cmd {
my $cmd = shift;
if ($verbose) {
print($cmd, "\n");
}
chomp(my $res = `$cmd`);
$res;
}
my $prefix;
{
my $res = GetOptions(
'verbose' => \$verbose,
'prefix=s' => \$prefix,
);
}
my $count = @ARGV;
if ($count == 0) {
my @envs = `ls configs/Install-*.pl`;
foreach my $e (@envs) {
$e =~ /Install-(.+).pl/;
print $1, "\n";
}
exit 0;
}
my $env = shift;
unless (-f "configs/Install-${env}.pl") {
print "configs/Install-${env}.pl does not exist.\n";
exit 1;
}
my %conf = %{+eval{require "configs/Install-${env}.pl"}};
my $use_prefix = 0;
if (defined($prefix) and -d $prefix) {
$use_prefix = 1;
$prefix =~ s|/+$||;
foreach my $k (keys(%conf)) {
my $v = $conf{$k};
$v =~ s|^/usr(/)?|$prefix$1|;
$conf{$k} = $v;
}
}
my $gitcmddir = $conf{gitcmddir};
my $gitconfig = $count == 2 ? shift : $conf{gitconfig};
my $templatedir = $conf{templatedir};
my $githookshome = $conf{githookshome};
my $usesymlink = $conf{usesymlink} eq 'true';
unless ($usesymlink) {
no warnings 'redefine';
*main::ln = \&cp;
}
$_ = $gitconfig;
s/~/$ENV{HOME}/;
$gitconfig = $_;
# なぜかtouchしておかないとディレクトリが作られるので回避
if ($env eq 'msys') {
`touch "${gitconfig}"`;
}
# 自分自身をgithookshomeにすべてコピー
my $dirname = dirname($gitconfig);
unless (-d $dirname) {
exec_cmd("mkdir -p '$dirname'");
}
unless (-d "${githookshome}/.git") {
if ($verbose) {
print("cp -fpR \"./\" \"$githookshome\"", "\n");
}
unless (-d $githookshome) {
exec_cmd("mkdir -p '$githookshome'");
}
`cp -fpR "./" "$githookshome"`;
chmod(0755, $githookshome);
}
# インストールに使用した設定を保存
{
my $save_config_filename = "${githookshome}/install-config";
if ($use_prefix) {
open my $fh, '>', $save_config_filename or die;
use Data::Dumper ();
local $Data::Dumper::Terse = 1;
print $fh Data::Dumper->Dump([\%conf]);
} else {
cp("configs/Install-${env}.pl", $save_config_filename);
}
}
unless (-d $gitcmddir) {
exec_cmd("mkdir -p '$gitcmddir'");
}
# サブコマンドのコピー
foreach my $i (`ls ${githookshome}/commands/* 2>/dev/null`) {
chomp(my $orig = $i);
chomp(my $link = $gitcmddir . '/' . basename($i));
ln($orig, $link);
}
# GIT_HOOKS_HOMEの設定
my $home = $ENV{GIT_HOOKS_HOME} || '';
unless ($home eq $githookshome) {
if ($verbose) {
print('echo \'GIT_HOOKS_HOME=\"' . $githookshome . '\"; export GIT_HOOKS_HOME\' >> ' . $gitconfig, "\n");
}
open my $fh, '>>', $gitconfig or die;
print $fh 'GIT_HOOKS_HOME="' . $githookshome . '"; export GIT_HOOKS_HOME', "\n";
}
# テンプレートのコピー
unless (-d $templatedir) {
exec_cmd("mkdir -p '$templatedir'");
}
cp("${githookshome}/templates/dispatcher", "${templatedir}/dispatcher");
chmod(0755, "${templatedir}/dispatcher");
foreach my $h (`cat "${githookshome}/configs/hooks-list"`) {
chomp($h);
unless ($h eq 'ci') {
ln("${githookshome}/templates/common", "${templatedir}/$h");
}
}