-
Notifications
You must be signed in to change notification settings - Fork 11
/
install.rb
executable file
·228 lines (189 loc) · 6.38 KB
/
install.rb
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
#!/usr/bin/env ruby
require './lib/diakonos/version.rb'
require 'fileutils'
require 'pp'
require 'rbconfig'
module Diakonos
class Installer
def strip_prefix( str )
str.sub( %r{^#{Regexp.escape( @prefix )}/*}, '' )
end
def initialize( argv = ARGV.dup )
rconfig = RbConfig::CONFIG
bang_ruby = File.join( rconfig[ 'bindir' ], rconfig[ 'ruby_install_name' ] )
@shebang = "#!#{bang_ruby}"
want_help = false
@verbose = false
@pretend = false
@dest_dir = '/'
@lib_dir = nil
@conf_dir = nil
@bin_dir = nil
@doc_dir = nil
@prefix = rconfig[ 'prefix' ]
@bin_suffix = strip_prefix( rconfig[ 'bindir' ] )
@lib_suffix = strip_prefix( rconfig[ 'sitelibdir' ] )
@conf_dir = rconfig[ 'sysconfdir' ]
@doc_suffix = strip_prefix( rconfig[ 'docdir' ].sub( /\$\(PACKAGE\)/, "diakonos-#{Diakonos::VERSION}" ) )
@data_suffix = strip_prefix( File.join(rconfig[ 'datadir' ], 'diakonos') )
while argv.any?
arg = argv.shift
case arg
when '--dest-dir'
@dest_dir = argv.shift
when '--prefix'
@prefix = argv.shift
when '--bin-dir'
@bin_dir = argv.shift
when '--conf-dir'
@conf_dir = argv.shift
when '--doc-dir'
@doc_dir = argv.shift
when '--help-dir'
@help_dir = argv.shift
when '--lib-dir'
@lib_dir = argv.shift
when '-v', '--verbose'
@verbose = true
when '-p', '--pretend', '--dry-run'
@pretend = true
when '-h', '--help'
want_help = true
end
end
@bin_dir ||= File.join( @prefix, @bin_suffix )
@lib_dir ||= File.join( @prefix, @lib_suffix )
@doc_dir ||= File.join( @prefix, @doc_suffix )
@help_dir ||= File.join( @prefix, @data_suffix, 'help' )
if want_help
print_usage_and_exit
end
if @pretend
puts "(Dry run only; not actually writing any files or directories)"
puts
end
if @verbose && @pretend
self.extend FileUtils::DryRun
elsif @verbose
self.extend FileUtils::Verbose
elsif @pretend
self.extend FileUtils::NoWrite
else
self.extend FileUtils
end
@installed_files = []
@installed_dirs = []
end
def print_usage_and_exit
puts "#{$0} [options]"
puts " -h / --help show usage (can be used with other options to preview paths)"
puts " -v / --verbose print each step"
puts " -p / --pretend don't actually do anything"
puts " --prefix <path> set installation prefix (default #{@prefix})"
puts " --dest-dir <path> set installation sandbox dir (default #{@dest_dir})"
puts " --bin-dir <path> set executable installation dir (default <prefix>/#{@bin_suffix})"
puts " --doc-dir <path> set documentation installation dir (default <prefix>/#{@doc_suffix})"
puts " --conf-dir <path> set configuration installation dir (default #{@conf_dir})"
puts " --lib-dir <path> set library installation dir (default <prefix>/#{@lib_suffix})"
exit 2
end
def write_installation_settings
installation_file = File.join( @lib_dir, 'diakonos', 'installation.rb' )
dest_file = File.join( @dest_dir, installation_file )
if @verbose
puts "Writing installation settings to #{dest_file}"
end
return if @pretend
@installed_files << installation_file
File.open( dest_file, 'w' ) do |f|
f.puts %|
module Diakonos
INSTALL_SETTINGS = {
:prefix => #{@prefix.dump},
:bin_dir => #{@bin_dir.dump},
:doc_dir => #{@doc_dir.dump},
:help_dir => #{@help_dir.dump},
:conf_dir => #{@conf_dir.dump},
:lib_dir => #{@lib_dir.dump},
:installed => {
:files => #{@installed_files.pretty_inspect.strip},
:dirs => #{@installed_dirs.pretty_inspect.strip},
},
}
end
|
end
end
def cp_( source, dest )
Array( source ).each do |item|
if File.directory? item
dir = "#{dest}/#{File.basename( item )}"
mkdir_ dir
cp_ Dir[ "#{item}/*" ], dir
else
cp item, File.join( @dest_dir, dest )
dest_dir_installed_file = File.expand_path( File.join( @dest_dir, dest, File.basename( item ) ) )
chmod File.stat( item ).mode, dest_dir_installed_file
installed_file = File.expand_path( File.join( dest, File.basename( item ) ) )
@installed_files << installed_file
end
end
end
def mkdir_( dir )
begin
mkdir_p File.join( @dest_dir, dir ), :mode => 0755
@installed_dirs << dir
rescue Errno::EEXIST
# Don't panic if the directory already exists
end
end
def install_( source, dest )
dest_file = File.join( dest, File.basename( source ) )
dest_dir_file = File.join( @dest_dir, dest_file )
install source, dest_dir_file, :mode => 0755
# Rewrite shebang line
command = "sed -i_ '1s|.*|#{@shebang}|' #{dest_dir_file}"
if @verbose
puts command
end
if ! @pretend
system command
rm "#{dest_dir_file}_"
end
@installed_files << dest_file
end
def run
::Diakonos.check_ruby_version
# Libraries
mkdir_ @lib_dir
cp_ 'lib/diakonos.rb', @lib_dir
dir = "#{@lib_dir}/diakonos"
mkdir_ dir
lib_files = Dir[ 'lib/diakonos/*' ].reject { |f| f =~ /installation\.rb/ }
cp_ lib_files, dir
# Configuration
mkdir_ @conf_dir
cp_ %w( diakonos.conf diakonos-256-colour.conf ), @conf_dir
# Executables
mkdir_ @bin_dir
install_ 'bin/diakonos', @bin_dir
# Documentation
mkdir_ @help_dir
mkdir_ @doc_dir
cp_ %w( README.rdoc CHANGELOG LICENCE ), @doc_dir
cp_ Dir[ 'help/*' ], @help_dir
write_installation_settings
end
end
end
installer = Diakonos::Installer.new
installer.run
puts %{
Diakonos #{Diakonos::VERSION} (#{Diakonos::LAST_MODIFIED}) installed.
If Diakonos has been installed with a Linux distro's package manager,
uninstall using that package manager.
If Diakonos has been installed independently of a package manager,
uninstall by running:
diakonos --uninstall
Thank you for installing Diakonos. Have a stupendous day! :)
}