-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall
executable file
·195 lines (171 loc) · 5.45 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
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
#! /usr/bin/env ruby
require 'yaml'
require 'fileutils'
$argv = {}; arg=nil
$argv[:help] = %w'-h --help'.any?{|x| $*.delete(x)}
$argv[:pull] = !%w'--no-pull'.any?{|x| $*.delete(x)}
$argv[:uninstall] = %w'-u --uninstall'.any?{|x| $*.delete(x)}
$argv[:verbose] = %w'-v --verbose'.any?{|x| $*.delete(x)}
$argv[:silent] = %w'-s --silent'.any?{|x| $*.delete(x)}
$argv[:test] = %w'-t --test'.any?{|x| $*.delete(x)}
$argv[:silent] = false if $argv[:verbose]
$argv[:src] = $*.reject!{|x|x=~/^--src=(.*)$/&&arg=$1} && arg || $argv[:src]
$argv[:src] = File.expand_path($argv[:src]||File.dirname($0))
$argv[:dir] = $*.reject!{|x|x=~/^--dir=(.*)$/&&arg=$1} && arg || $argv[:dir]
$argv[:dir] = File.expand_path($argv[:dir]||'~')
$argv[:conf] = []
$*.each{|x| $argv[:conf].push($1) if x =~ /^--conf=(.*)$/}
$argv[:conf] = %w'install.yml local.install.yml' if $argv[:conf].empty?
if $argv[:help]
print <<"EOM"
Usage: #{$0} [OPTIONS]
Options:
--no-pull Suppress pull action. (#{!$argv[:pull]})
-u, --uninstall Remove generated links. (#{$argv[:uninstall]})
-v, --verbose Verbose messages. (#{$argv[:verbose]})
-s, --silent Supress messages. (#{$argv[:silent]})
-t, --test Show what is going to be done. (#{$argv[:test]})
--src=<dir> Source directory. (#{$argv[:src]})
--dir=<dir> Target directory. (#{$argv[:dir]})
--conf=<file> YAML configuration file. (#{File.expand_path('{'+$argv[:conf].join(',')+'}', $argv[:src])})
EOM
exit
end
def try(&block)
begin
block.call
rescue => e
$info.warn(e.message)
end
end
class Info
def initialize(silent, verbose, test) @s=silent; @v=verbose; @t=test end
def err(msg) Kernel.warn(msg) end
def warn(msg, c=nil) c==false || @s || (Kernel.warn(msg)||true); c end
def debug(msg) Kernel.puts(msg) unless @v end
def puts(msg) Kernel.puts(msg) unless @s end
def ask(msg) return @s ? false : (print(msg) || $stdin.gets =~ /^y/) end
def test?; return @t end
end
$info = Info.new($argv[:silent], $argv[:verbose], $argv[:test])
class Hash
def deep_merge(rhs)
self.merge(rhs){|k,v1,v2| v1.is_a?(Hash) ? v1.deep_merge(v2) : v2}
end
end
class File
def self.to_rel(basedir, target)
sep = /#{File::SEPARATOR}+/o
base = File.expand_path(basedir).split(sep)
target = File.expand_path(target).split(sep)
while (base.first == target.first)
base.shift
target.shift
end
return File.join(['..']*base.size + target)
end
end
class Linker
class Link
attr_reader :dir, :link, :target
def initialize(dir, link, target)
@dir = dir
@link = link
@target = target
end
def make
t = Linker.info.test?
FileUtils.mkdir_p(@dir) unless File.exist?(@dir)
return unless File.directory?(@dir)
return unless target
Dir.chdir(@dir) do
return if !t && (File.exist?(@link) || File.symlink?(@link))
target = File.to_rel(@dir, @target)
Linker.info.puts(" #{@link} -> #{target}")
try { FileUtils.ln_s(target, link) } unless t
end
end
def remove
t = Linker.info.test?
return unless File.directory?(@dir)
Dir.chdir(@dir) do
msg = "'#{@link}' is not symlink. Remove it? "
return unless File.exist?(@link) || File.symlink?(@link)
return unless File.symlink?(@link) || Linker.info.ask(msg)
Linker.info.puts(" rm #{@link}")
try { FileUtils.rm_r(@link) } unless t
end
end
end
def initialize(dst, hash)
@dst = dst
@conf = hash
end
def link(src) each(src, proc{|a,b|a[0]<=>b[0]}, proc{|l|l.make}) end
def unlink(src) each(src, proc{|a,b|b[0]<=>a[0]}, proc{|l|l.remove}) end
def each(src, comp, action)
dir = ''
links(src).sort(&comp).each do |d,l|
@@info.puts("In #{l.dir}") if l.dir != dir
dir = l.dir
action.call(l)
end
end
def links(src)
r = []
@conf.each do |k, v|
next unless v.is_a?(Hash)
dir = File.expand_path(k, src)
v.each do |l, f|
if f.is_a?(Hash)
sep = File::SEPARATOR
l = l.gsub(/(?:^|#{sep})<src>(?:$|(?=#{sep}))/, @dst)
f.each do |ll,ff|
ff = File.expand_path(ff, l) if ff
link = Link.new(dir, ll, ff)
r.push([ File.expand_path(ll, dir), link ])
end
else
f = File.expand_path(f, dir) if f
link = Link.new(dir, l, f)
r.push([ File.expand_path(l, dir), link ])
end
end
end
return r
end
def self.from_files(dir, files)
Dir.chdir(dir) do
conf = files.map do |f|
File.expand_path(f)
end.reject do |f|
@@info.warn("'#{f}': no such file", !File.exist?(f))
end.map{|f|YAML.load_file(f)}.inject({}){|r,h|r.deep_merge(h)}
return self.new(dir, conf)
end
end
def self.set_info(info) @@info = info end
def self.info; return @@info end
end
Linker.set_info($info)
# check directory
[ :src, :dir ].map{|s|$argv[s]}.each do |d|
$argv[s] = $info.err("'#{d}': no such directory") unless File.directory?(d)
end.any?{|x| exit(1) if x===nil}
# remove links
Linker.from_files($argv[:src], $argv[:conf]).unlink($argv[:dir])
exit if $argv[:uninstall]
# pull
if $argv[:pull]
$info.puts('')
Dir.chdir($argv[:src]) do
$info.puts('pull')
try do
submod = %w'sync init update'.map{|x|'git submodule '+x}.join(' && ')
$info.puts(`git pull && #{submod}`)
end unless $info.test?
end
$info.puts('')
end
# new links
Linker.from_files($argv[:src], $argv[:conf]).link($argv[:dir])