-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotgenlib.rb
154 lines (117 loc) · 3.85 KB
/
dotgenlib.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
################################################################################
# Helper functions.
################################################################################
def bail(msg)
STDERR.puts(msg)
exit 1
end
# Will look up the OS specific option in hash 'options'. Note that if
# this method is called, 'options' MUST contain a matching key.
def os_opt(options)
result = options[@os]
bail 'os_opt - options does not contain a recognised OS symbol.' if result.nil?
result
end
################################################################################
## Sanity tests
################################################################################
def config_enabled?(cfg)
enabled = cfg[:enabled].nil? || cfg[:enabled] == true
right_os = cfg[:inc_os].nil? || cfg[:inc_os].include?(@os)
wrong_os = !cfg[:exc_os].nil? && cfg[:exc_os].include?(@os)
warn "DISABLED: #{cfg[:name]}" unless enabled
enabled && right_os && !wrong_os
end
def correct_type?(cfg, entry_name, expected_type)
return true if cfg[entry_name].nil?
ok = cfg[entry_name].class == expected_type
warn ":#{entry_name} must be a #{expected_type} in entry '#{cfg[:name]}'" unless ok
ok
end
def run_test(cfg)
result = cfg[:test].nil? ||
system(cfg[:test], {:err => File::NULL, :out => File::NULL})
warn "FAILED TEST: #{cfg[:name]}." unless result
result
end
def valid_config?(cfg)
valid_entry = cfg.class == Hash &&
cfg[:name].class == String &&
!cfg[:name].chomp.empty?
warn 'Config entry must be a hash containing a ":name" key.' unless valid_entry
valid_entry &&
config_enabled?(cfg) &&
correct_type?(cfg, :aliases, Hash) &&
correct_type?(cfg, :paths, Array) &&
correct_type?(cfg, :manpaths, Array) &&
correct_type?(cfg, :test, String) &&
correct_type?(cfg, :vars, Hash) &&
correct_type?(cfg, :bashrc, Array) &&
correct_type?(cfg, :profile, Array) &&
correct_type?(cfg, :inc_os, Array) &&
correct_type?(cfg, :exc_os, Array) &&
run_test(cfg)
end
################################################################################
## Extract/copy 'stuff' from config
################################################################################
def extract_paths(cfg, key, prefix)
result = ["# #{cfg[:name]}"]
cfg[key].each do |p|
if p.include?("$#{prefix}")
result << "export #{prefix}=\"#{p}\""
else
result << "export #{prefix}=\"#{p}:$#{prefix}\""
end
end
result << "\n"
end
def extract_map(cfg, key, name)
result = ["# #{cfg[:name]}"]
cfg[key].each do |a, c|
result << "#{name} #{a}=\"#{c}\""
end
result << "\n"
end
def extract_aliases(cfg)
extract_map(cfg, :aliases, 'alias')
end
def extract_exports(cfg)
extract_map(cfg, :exports, 'export')
end
def extract_vars(cfg)
extract_map(cfg, :vars, 'set')
end
def extract_profile(cfg)
result = ["# #{cfg[:name]}"]
result << cfg[:profile]
result << "\n"
end
def extract_bashrc(cfg)
result = ["# #{cfg[:name]}"]
result << cfg[:bashrc]
result << "\n"
end
################################################################################
## Output 'stuff'
################################################################################
# Generates bash code for all config that has a given key, by using
# the supplied transform function.
def generate(key, &transform)
@config # for each config entry
.filter { |cfg| !cfg[key].nil? } # that has a matching key
.map { |cfg| transform.call(cfg) } # extract what we need
.flatten # and flatten the results
end
# Expects a block that returns a string.
def write_config(name)
unless block_given?
puts 'writeConfig() requires a block.'
exit 1
end
content = yield
home = ENV['HOME'] + '/'
File.open(home + name, 'w') do |file|
file.write(content)
end
end