Class: Abid::Application

Inherits:
Rake::Application
  • Object
show all
Includes:
TaskManager
Defined in:
lib/abid/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TaskManager

#[], #define_mixin, #define_play, #lookup_play_class, #play_base, record_task_metadata

Constructor Details

#initializeApplication

Returns a new instance of Application.



8
9
10
11
12
13
14
15
# File 'lib/abid/application.rb', line 8

def initialize
  super
  @rakefiles = %w(abidfile Abidfile abidfile.rb Abidfile.rb) << abidfile
  @worker = Worker.new(self)

  @config = IniFile.new(content: default_config)
  @config.merge!(IniFile.load('config/abid.conf'))
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/abid/application.rb', line 6

def config
  @config
end

#workerObject (readonly)

Returns the value of attribute worker.



5
6
7
# File 'lib/abid/application.rb', line 5

def worker
  @worker
end

Instance Method Details

#abid_optionsObject

:nodoc:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/abid/application.rb', line 78

def abid_options # :nodoc:
  sort_options(
    [
      ['--retry-failed-job',
       'Retry failed job.',
       proc { options.retry_failed_job = true }
      ],
      ['--repair',
       'Run the task in repair mode.',
       proc { options.repair = true }
      ],
      ['--preview', '-p',
       'Run tasks in preview mode.',
       proc do
         options.preview = true
       end
      ],
      ['--wait-external-task',
       'Wait a task finished if it is running in externl process',
       proc do
         options.wait_external_task_interval = true
       end
      ]
    ]
  )
end

#abidfileObject

allows the built-in tasks to load without a abidfile



23
24
25
# File 'lib/abid/application.rb', line 23

def abidfile
  File.expand_path(File.join(File.dirname(__FILE__), '..', 'Abidfile.rb'))
end

#databaseObject



133
134
135
136
137
138
139
140
141
# File 'lib/abid/application.rb', line 133

def database
  return @database if @database
  if config.sections.include?('abid database')
    cfg = config['abid database'].map { |k, v| [k.to_sym, v] }.to_h
  else
    cfg = default_database_config
  end
  @database = Sequel.connect(**cfg)
end

#default_configObject



51
52
53
# File 'lib/abid/application.rb', line 51

def default_config
  {}
end

#default_database_configObject



55
56
57
58
59
60
61
# File 'lib/abid/application.rb', line 55

def default_database_config
  {
    adapter: 'sqlite',
    database: File.join(Dir.pwd, 'abid.db'),
    max_connections: 1
  }
end

#handle_optionsObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/abid/application.rb', line 105

def handle_options
  options.rakelib = %w(rakelib tasks)
  options.trace_output = $stderr

  OptionParser.new do |opts|
    opts.banner = 'See full documentation at https://github.com/ojima-h/abid.'
    opts.separator ''
    opts.separator 'Show available tasks:'
    opts.separator '    bundle exec abid -T'
    opts.separator ''
    opts.separator 'Invoke (or simulate invoking) a task:'
    opts.separator '    bundle exec abid [--dry-run | --preview] TASK'
    opts.separator ''
    opts.separator 'Abid options:'
    abid_options.each { |args| opts.on(*args) }
    opts.separator ''
    opts.separator 'Advanced options:'
    standard_rake_options.each { |args| opts.on(*args) }

    opts.on_tail('-h', '--help', '-H', 'Display this help message.') do
      puts opts
      exit
    end

    opts.environment('RAKEOPT')
  end.parse!
end

#invoke_task(task_string) ⇒ Object

:nodoc:



46
47
48
49
# File 'lib/abid/application.rb', line 46

def invoke_task(task_string) # :nodoc:
  name, args = parse_task_string(task_string)
  self[name].async_invoke(*args).wait!
end

#load_rakefileObject

load built-in tasks



28
29
30
31
32
33
34
35
# File 'lib/abid/application.rb', line 28

def load_rakefile
  standard_exception_handling do
    glob(File.expand_path('../tasks/*.rake', __FILE__)) do |name|
      Rake.load_rakefile name
    end
  end
  super
end

#runObject



17
18
19
20
# File 'lib/abid/application.rb', line 17

def run
  Abid.application = self
  super
end

#run_with_threadsObject



37
38
39
40
41
42
43
44
# File 'lib/abid/application.rb', line 37

def run_with_threads
  yield
rescue Exception => err
  worker.kill
  raise err
else
  worker.shutdown
end

#standard_rake_optionsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/abid/application.rb', line 63

def standard_rake_options
  super.each do |opt|
    case opt.first
    when '--execute-print'
      # disable short option
      opt.delete_at(1)
    when '--version'
      opt[-1] = lambda do |_value|
        puts "Abid Version: #{Abid::VERSION} (Rake Version: #{RAKEVERSION})"
        exit
      end
    end
  end
end