@@ -11,65 +11,42 @@ def packages
11
11
resolver . packages
12
12
end
13
13
14
- def apply ( command_name )
14
+ def execute ( command_name )
15
+ @node . log command_name , packages . map ( &:name ) . join ( ', ' ) . yellow
15
16
packages . each do |pkg |
16
- next unless pkg . provides_command? ( command_name )
17
- @node . log pkg . name , command_name . to_s . yellow
18
- context = @perform ? Hull ::ExecutionContext . new ( @node ) : Hull ::MockExecutionContext . new ( @node )
19
- cmd = pkg . command ( command_name )
20
- context . apply ( cmd )
17
+ next unless should_run? ( pkg , command_name )
18
+ exec ( pkg , command_name )
19
+ validate! ( pkg ) if command_name == :apply
21
20
end
22
21
end
23
22
24
- def demonstrate ( command_name )
25
- @perform = false
26
- apply ( command_name )
27
- @perform = true
28
- end
29
- end
30
-
31
-
32
- class Hull ::ExecutionContext
33
- def initialize ( node )
34
- @node = node
35
- end
36
-
37
- def apply ( blk )
38
- instance_eval ( &blk )
23
+ def should_run? ( pkg , command_name )
24
+ return false unless pkg . provides_command? ( command_name )
25
+ return true unless @perform
26
+ return true unless command_name == :apply || command_name == :remove
27
+ return true unless pkg . provides_command? ( :validate )
28
+ is_present = exec ( pkg , :validate )
29
+ return !is_present if command_name == :apply
30
+ return is_present
39
31
end
40
32
41
- def run ( cmd )
42
- @node . execute ( cmd )
33
+ def validate! ( pkg )
34
+ return true unless @perform
35
+ return unless pkg . provides_command? ( :validate )
36
+ return if exec ( pkg , :validate )
37
+ raise "Package #{ pkg . name } failed validation"
43
38
end
44
39
45
- def trigger ( action , *args )
46
- pkg_name , action_name = *action . split ( ':' , 2 )
47
- pkg = Hull ::PackageIndex . default . get ( pkg_name )
48
- action = pkg . actions [ action_name ]
49
- instance_exec ( *args , &action )
50
- end
51
- end
52
-
53
- class Hull ::MockExecutionContext < Hull ::ExecutionContext
54
- def run ( cmd )
55
- @node . log 'mock-execute' , cmd
56
- end
57
- end
58
-
59
-
60
- class Hull ::Resolver
61
- attr_reader :packages
62
- def initialize ( package )
63
- @package = package
64
- @packages = [ @package ]
40
+ def demonstrate ( command_name )
41
+ @perform = false
42
+ apply ( command_name )
43
+ @perform = true
65
44
end
66
45
67
- def resolve
68
- dependancies = @package . dependancies . map { |d | Hull ::PackageIndex . default . get ( d ) }
69
- @packages += dependancies . map { |d | Hull ::Resolver . new ( d ) . resolve . packages }
70
- @packages . flatten!
71
- @packages . reverse!
72
- @packages . uniq!
73
- self
46
+ def exec ( pkg , command_name )
47
+ @node . log pkg . name , command_name . to_s . yellow
48
+ context = @perform ? Hull ::ExecutionContext . new ( @node ) : Hull ::MockExecutionContext . new ( @node )
49
+ cmd = pkg . command ( command_name )
50
+ context . apply ( cmd )
74
51
end
75
52
end
0 commit comments