-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.rb
140 lines (119 loc) · 3.21 KB
/
commands.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
module Commands
def without_advice
old_value = SETTINGS['global']['advise_commands']
SETTINGS['global']['advise_commands'] = false
yield
SETTINGS['global']['advise_commands'] = old_value
end
end
class Command
attr_accessor :name, :flags, :description
def initialize(package, action)
@package = package
@preconditions = []
@postconditions = []
@before_advice = []
@after_advice = []
@action = action
end
def call(*args)
result = nil
advise = SETTINGS['global']['advise_commands']
@preconditions.each do |cond|
result = cond.call(@package, *args)
return if result == :skip
raise "PreCondition #{cond} failed" if result == :fail
end
@before_advice.each {|b| b.call(@package, *args)} if advise
begin
result = @action.call(*args) if @action
rescue => e
$stderr.puts "messed up #{@package.name} #@name because of #{e}"
exit 1 unless SETTINGS['global']['debug']
raise e
end
@after_advice.each {|b| b.call(@package, *args)} if advise
@postconditions.each do |cond|
result = cond.call(@package, *args)
return if result == :skip
raise "PostCondition #{cond} failed" if result == :fail
end
return result
end
def add_advice(position, advice)
case position
when :before
@before_advice << advice
when :after
@after_advice << advice
else
raise "Did not recognize position #{position} for advice"
end
end
def add_precondition(condition)
@preconditions << condition
end
def add_postcondition(condition)
@postconditions << condition
end
end
#condition results = one of [:skip, :continue, :fail]
def install_pre_condition
lambda do |package, *args|
return :skip if package.installed?
return :continue
end
end
def install_before_advice
lambda do |package, *args|
package.install_dependencies
package.download_repositories
package.create_directories
package.download_other_stuff
package.process_support_files
end
end
def install_after_advice
lambda do |package, *args|
package.install_service
end
end
def remove_pre_condition
lambda do |package, *args|
return :skip unless package.installed?
return :continue
end
end
def remove_before_advice
lambda do |package, *args|
package.remove_directories
end
end
def default_install_command(package, &action)
command = Command.new(package, action)
command.add_advice(:before, install_before_advice)
command.add_advice(:after, install_after_advice)
command.add_precondition(install_pre_condition)
return command
end
def default_remove_command(package, &action)
command = Command.new(package, action)
command.add_advice(:before, remove_before_advice)
command.add_precondition(remove_pre_condition)
return command
end
def default_installed_predicate(package, &action)
command = Command.new package, lambda { |*args|
return action.call(*args) if action
return File.exists? package.project_directory
}
return command
end
def default_reinstall_command(package, &action)
command = Command.new package, lambda { |*args|
return action.call(*args) if action
package.remove
package.install
}
return command
end