Class: Patcmd::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/patcmd/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes = {}) ⇒ Task

Returns a new instance of Task.



7
8
9
10
11
12
13
14
# File 'lib/patcmd/task.rb', line 7

def initialize(name, attributes = {})
  @name        = name
  @description = attributes["description"] || ""
  @group       = attributes["group"] || "default"
  @command     = attributes["command"]
  @args        = attributes["args"] || []
  @env         = attributes["env"] || {}
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



5
6
7
# File 'lib/patcmd/task.rb', line 5

def args
  @args
end

#commandObject

Returns the value of attribute command.



5
6
7
# File 'lib/patcmd/task.rb', line 5

def command
  @command
end

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/patcmd/task.rb', line 5

def description
  @description
end

#envObject

Returns the value of attribute env.



5
6
7
# File 'lib/patcmd/task.rb', line 5

def env
  @env
end

#groupObject

Returns the value of attribute group.



5
6
7
# File 'lib/patcmd/task.rb', line 5

def group
  @group
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/patcmd/task.rb', line 5

def name
  @name
end

Instance Method Details

#executeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/patcmd/task.rb', line 16

def execute
  # Save original environment variables
  original_env = ENV.to_hash
  # Set the specified environment variables
  @env.each { |key, value| ENV[key] = value }

  # Build and run the command string
  full_command = [@command, *@args].join(" ")
  system(full_command)

  # Restore original environment.
  ENV.replace(original_env)
end