Class: RepoManager::TaskManager

Inherits:
Object
  • Object
show all
Defined in:
lib/repo_manager/tasks/task_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ TaskManager

Returns a new instance of TaskManager.



11
12
13
14
15
# File 'lib/repo_manager/tasks/task_manager.rb', line 11

def initialize(config={})
  @configuration = config.deep_clone
  options = @configuration[:options]
  self.color = options ? options[:color] : true
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



9
10
11
# File 'lib/repo_manager/tasks/task_manager.rb', line 9

def configuration
  @configuration
end

Instance Method Details

#colorObject



114
115
116
# File 'lib/repo_manager/tasks/task_manager.rb', line 114

def color
  @color
end

#color=(value) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/repo_manager/tasks/task_manager.rb', line 118

def color=(value)
  @color = value
  if value
    ::Thor::Base.shell = Thor::Shell::Color
  else
    ::Thor::Base.shell = Thor::Shell::Basic
  end
end

#find_by_namespace(name) ⇒ Class, String

@examples:

find_by_namespace(sweep:screenshots)
find_by_namespace(repo_manager:sweep:screenshots)

returns:

    RepoManager::Sweep, screenshots

Returns:

  • (Class, String)

    the Thor class and the task



27
28
29
30
31
32
33
34
35
# File 'lib/repo_manager/tasks/task_manager.rb', line 27

def find_by_namespace(name)
  names = name.to_s.split(':')
  raise "invalid task namespace" unless names.any?

  namespace = names.join(":")

  #puts "searching for task #{namespace}"
  klass, task = ::Thor::Util.find_class_and_task_by_namespace(namespace, fallback = false)
end

#invoke(name, args = ARGV) ⇒ Object

@examples:

invoke(sweep:screenshots)
invoke(update:models)
invoke(generate:init, ["."], nil)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/repo_manager/tasks/task_manager.rb', line 44

def invoke(name, args=ARGV)
  logger.debug "invoke name: #{name}, args #{args.inspect}, configuration defined: #{configuration ? 'yes' : 'no'}"
  args = args.dup
  load_tasks

  logger.debug "find_by_namespace: #{name}"
  klass, task  = find_by_namespace(name)

  if klass
    config = {}
    config[:shell] ||= shell
    klass.send(:dispatch, task, args, nil, config) do |instance|
      if defined?(instance.configuration)
        instance.configuration = configuration.deep_clone
      end
    end
    logger.debug "after invoke"
    result = 0
  else
    puts "Could not find task #{name}"
    result = 1
  end
  result
end

#list_bare_tasksObject

display a list of tasks for CLI completion



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/repo_manager/tasks/task_manager.rb', line 166

def list_bare_tasks
  load_tasks

  Thor::Base.subclasses.each do |klass|
    unless klass == Thor
      klass.tasks.each do |t|
        puts "#{klass.namespace}:#{t[0]}"
      end
    end
  end
end

#list_tasksObject

display a list of tasks for user display



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/repo_manager/tasks/task_manager.rb', line 147

def list_tasks
  load_tasks

  # set '$thor_runner' to true to display full namespace
  $thor_runner = true

  list = [] #Thor.printable_tasks(all = true, subcommand = true)
  Thor::Base.subclasses.each do |klass|
    list += klass.printable_tasks(false) unless klass == Thor
  end
  list.sort!{ |a,b| a[0] <=> b[0] }

  title = "repo_manager tasks"
  shell.say shell.set_color(title, :blue, bold=true)
  shell.say "-" * title.size
  shell.print_table(list, :ident => 2, :truncate => true)
end

#load_tasksObject

load all the tasks in this gem plus the user’s own repo_manager task folder

NOTE: doesn’t load any default tasks or non-RepoManager tasks



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
# File 'lib/repo_manager/tasks/task_manager.rb', line 72

def load_tasks
  return if @loaded

  # By convention, the '*_helper.rb' files are helpers and need to be loaded first. Load
  # them into the Thor::Sandbox namespace
  Dir.glob( File.join(File.dirname(__FILE__), '**', '*.rb')  ).each do |task|
    if task.match(/_helper\.rb$/)
      #logger.debug "load_thorfile helper: #{task}"
      ::Thor::Util.load_thorfile task
    end
  end

  # Now load the thor files
  Dir.glob( File.join(File.dirname(__FILE__), '**', '*.rb')  ).each do |task|
    unless task.match(/_helper\.rb$/)
      #logger.debug "load_thorfile: #{task}"
      ::Thor::Util.load_thorfile task
    end
  end

  # load user tasks
  if user_tasks_folder
    Dir.glob( File.join([user_tasks_folder, '**', '*.{rb,thor}'])  ).each { |task| ::Thor::Util.load_thorfile task if task.match(/_helper\.rb$/) }
    Dir.glob( File.join([user_tasks_folder, '**', '*.{rb,thor}'])  ).each { |task| ::Thor::Util.load_thorfile task unless task.match(/_helper\.rb$/) }
  end

  @loaded = true
end

#shellObject



127
128
129
130
131
# File 'lib/repo_manager/tasks/task_manager.rb', line 127

def shell
  return @shell if @shell

  @shell = @color ? ::Thor::Shell::Color.new : ::Thor::Shell::Basic.new
end

#task_help(name) ⇒ Object

display help for the given task



135
136
137
138
139
140
141
142
143
144
# File 'lib/repo_manager/tasks/task_manager.rb', line 135

def task_help(name)
  load_tasks

  klass, task  = find_by_namespace(name)

  # set '$thor_runner' to true to display full namespace
  $thor_runner = true

  klass.task_help(shell , task)
end

#user_tasks_folderObject



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/repo_manager/tasks/task_manager.rb', line 101

def user_tasks_folder
  return unless configuration

  folder = configuration[:folders] ?  configuration[:folders][:tasks] : nil
  return unless folder
  return folder if Pathname.new(folder).absolute?

  if configuration[:configuration_filename]
    base_folder = File.dirname(configuration[:configuration_filename])
    folder = File.join(base_folder, folder)
  end
end