Class: Howzit::Task
- Inherits:
-
Object
- Object
- Howzit::Task
- Defined in:
- lib/howzit/task.rb
Overview
Task object
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#optional ⇒ Object
readonly
Returns the value of attribute optional.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(attributes, optional: false, default: true) ⇒ Task
constructor
Initialize a Task object.
-
#inspect ⇒ String
Inspect.
-
#run ⇒ Object
Execute the task.
-
#run_block ⇒ Object
Execute a block type.
-
#run_copy ⇒ Object
Execute a copy task.
-
#run_include ⇒ Array
Execute an include task.
-
#run_run ⇒ Object
Execute a run task.
-
#to_list ⇒ String
Output terminal-formatted list item.
-
#to_s ⇒ String
Output string representation.
Constructor Details
#initialize(attributes, optional: false, default: true) ⇒ Task
Initialize a Task object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/howzit/task.rb', line 21 def initialize(attributes, optional: false, default: true) @prefix = "{bw}\u{25B7}\u{25B7} {x}" # arrow = "{bw}\u{279F}{x}" @arguments = attributes[:arguments] || [] @type = attributes[:type] || :run @title = attributes[:title] || nil @parent = attributes[:parent] || nil @action = attributes[:action].render_arguments || nil @optional = optional @default = default end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
6 7 8 |
# File 'lib/howzit/task.rb', line 6 def action @action end |
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
6 7 8 |
# File 'lib/howzit/task.rb', line 6 def arguments @arguments end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
6 7 8 |
# File 'lib/howzit/task.rb', line 6 def default @default end |
#optional ⇒ Object (readonly)
Returns the value of attribute optional.
6 7 8 |
# File 'lib/howzit/task.rb', line 6 def optional @optional end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
6 7 8 |
# File 'lib/howzit/task.rb', line 6 def parent @parent end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
6 7 8 |
# File 'lib/howzit/task.rb', line 6 def title @title end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/howzit/task.rb', line 6 def type @type end |
Instance Method Details
#inspect ⇒ String
Inspect
41 42 43 |
# File 'lib/howzit/task.rb', line 41 def inspect %(<#Howzit::Task @type=:#{@type} @title="#{@title}" @action="#{@action}" @arguments=#{@arguments} @block?=#{@action.split(/\n/).count > 1}>) end |
#run ⇒ Object
Execute the task
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/howzit/task.rb', line 115 def run output = [] tasks = 1 res = if @type == :block run_block else case @type when :include output, tasks = run_include when :run run_run when :copy run_copy when :open Util.os_open(@action) end end [output, tasks, res] end |
#run_block ⇒ Object
Execute a block type
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/howzit/task.rb', line 57 def run_block Howzit.console.info "#{@prefix}{bg}Running block {bw}#{@title}{x}".c if Howzit.[:log_level] < 2 block = @action script = Tempfile.new('howzit_script') begin script.write(block) script.close File.chmod(0o777, script.path) res = system(%(/bin/sh -c "#{script.path}")) ensure script.close script.unlink end res end |
#run_copy ⇒ Object
Execute a copy task
105 106 107 108 109 110 |
# File 'lib/howzit/task.rb', line 105 def run_copy title = Howzit.[:show_all_code] ? @action : @title Howzit.console.info("#{@prefix}{bg}Copied {bw}#{title}{bg} to clipboard{x}".c) Util.os_copy(@action) true end |
#run_include ⇒ Array
Execute an include task
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/howzit/task.rb', line 79 def run_include output = [] action = @action matches = Howzit.buildnote.find_topic(action) raise "Topic not found: #{action}" if matches.empty? Howzit.console.info("#{@prefix}{by}Running tasks from {bw}#{matches[0].title}{x}".c) output.concat(matches[0].run(nested: true)) Howzit.console.info("{by}End include: #{matches[0].tasks.count} tasks{x}".c) [output, matches[0].tasks.count] end |
#run_run ⇒ Object
Execute a run task
95 96 97 98 99 100 |
# File 'lib/howzit/task.rb', line 95 def run_run title = Howzit.[:show_all_code] ? @action : @title Howzit.console.info("#{@prefix}{bg}Running {bw}#{title}{x}".c) ENV['HOWZIT_SCRIPTS'] = File.('~/.config/howzit/scripts') system(@action) end |
#to_list ⇒ String
Output terminal-formatted list item
141 142 143 |
# File 'lib/howzit/task.rb', line 141 def to_list " * #{@type}: #{@title.preserve_escapes}" end |
#to_s ⇒ String
Output string representation
50 51 52 |
# File 'lib/howzit/task.rb', line 50 def to_s @title end |