Class: Build::Task

Inherits:
Graph::Task
  • Object
show all
Defined in:
lib/build/task.rb

Overview

This task class serves as the base class for the environment specific task classes genearted when adding targets.

Defined Under Namespace

Classes: CommandFailure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(walker, node, group, logger: nil) ⇒ Task

Returns a new instance of Task.



51
52
53
54
55
56
# File 'lib/build/task.rb', line 51

def initialize(walker, node, group, logger: nil)
  super(walker, node)
  
  @group = group
  @logger = logger
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



58
59
60
# File 'lib/build/task.rb', line 58

def group
  @group
end

#loggerObject (readonly)

Returns the value of attribute logger.



59
60
61
# File 'lib/build/task.rb', line 59

def logger
  @logger
end

Instance Method Details

#cp(source_path, destination_path) ⇒ Object



91
92
93
94
95
96
# File 'lib/build/task.rb', line 91

def cp(source_path, destination_path)
  return unless wet?
  
  @logger&.info(self) {Console::Shell.for('cp', source_path, destination_path)}
  FileUtils.copy(source_path, destination_path)
end

#install(source_path, destination_path) ⇒ Object



114
115
116
117
118
119
# File 'lib/build/task.rb', line 114

def install(source_path, destination_path)
  return unless wet?
  
  @logger&.info(self) {Console::Shell.for('install', source_path, destination_path)}
  FileUtils.install(source_path, destination_path)
end

#invoke_rule(rule, arguments, &block) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/build/task.rb', line 134

def invoke_rule(rule, arguments, &block)
  arguments = rule.normalize(arguments, self)
  
  @logger&.debug(self) {"-> #{rule}(#{arguments.inspect})"}
  
  invoke(
    RuleNode.new(rule, arguments, &block)
  )
  
  @logger&.debug(self) {"<- #{rule}(...) -> #{rule.result(arguments)}"}
  
  return rule.result(arguments)
end

#mkpath(path) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/build/task.rb', line 105

def mkpath(path)
  return unless wet?
  
  unless File.exist?(path)
    @logger&.info(self) {Console::Shell.for('mkpath', path)}
    FileUtils.mkpath(path)
  end
end

#rm(path) ⇒ Object



98
99
100
101
102
103
# File 'lib/build/task.rb', line 98

def rm(path)
  return unless wet?
  
  @logger&.info(self) {Console::Shell.for('rm -rf', path)}
  FileUtils.rm_rf(path)
end

#run!(*arguments) ⇒ Object



80
81
82
# File 'lib/build/task.rb', line 80

def run!(*arguments)
  self.spawn(shell_environment, *arguments)
end

#shell_environmentObject



76
77
78
# File 'lib/build/task.rb', line 76

def shell_environment
  @shell_environment ||= environment.flatten.export
end

#spawn(*arguments) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/build/task.rb', line 65

def spawn(*arguments)
  if wet?
    @logger&.info(self) {Console::Shell.for(*arguments)}
    status = @group.spawn(*arguments)
    
    if status != 0
      raise CommandFailure.new(self, arguments, status)
    end
  end
end

#touch(path) ⇒ Object



84
85
86
87
88
89
# File 'lib/build/task.rb', line 84

def touch(path)
  return unless wet?
  
  @logger&.info(self) {Console::Shell.for('touch', path)}
  FileUtils.touch(path)
end

#updateObject



130
131
132
# File 'lib/build/task.rb', line 130

def update
  @node.apply!(self)
end

#wet?Boolean

Returns:



61
62
63
# File 'lib/build/task.rb', line 61

def wet?
  @node.dirty?
end

#write(path, data, mode = "w") ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/build/task.rb', line 121

def write(path, data, mode = "w")
  return unless wet?
  
  @logger&.info(self) {Console::Shell.for("write", path, "#{data.size}bytes")}
  File.open(path, mode) do |file|
    file.write(data)
  end
end