singularity_dsl 1.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rubocop.yml +10 -0
- data/.singularityrc +12 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +52 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +24 -0
- data/bin/singularity_runner +5 -0
- data/lib/singularity_dsl/application.rb +76 -0
- data/lib/singularity_dsl/cli/cli.rb +125 -0
- data/lib/singularity_dsl/cli/table.rb +32 -0
- data/lib/singularity_dsl/dsl/batch.rb +23 -0
- data/lib/singularity_dsl/dsl/changeset.rb +39 -0
- data/lib/singularity_dsl/dsl/components.rb +7 -0
- data/lib/singularity_dsl/dsl/dsl.rb +71 -0
- data/lib/singularity_dsl/dsl/event_store.rb +34 -0
- data/lib/singularity_dsl/dsl/registry.rb +60 -0
- data/lib/singularity_dsl/dsl/runner.rb +67 -0
- data/lib/singularity_dsl/dsl/utils.rb +28 -0
- data/lib/singularity_dsl/errors.rb +35 -0
- data/lib/singularity_dsl/files.rb +20 -0
- data/lib/singularity_dsl/git_helper.rb +94 -0
- data/lib/singularity_dsl/runstate.rb +30 -0
- data/lib/singularity_dsl/stdout.rb +16 -0
- data/lib/singularity_dsl/task.rb +30 -0
- data/lib/singularity_dsl/tasks/rake.rb +31 -0
- data/lib/singularity_dsl/tasks/rspec.rb +32 -0
- data/lib/singularity_dsl/tasks/rubocop.rb +54 -0
- data/lib/singularity_dsl/tasks/shell_task.rb +90 -0
- data/lib/singularity_dsl/version.rb +6 -0
- data/lib/singularity_dsl.rb +9 -0
- data/singularity_dsl.gemspec +31 -0
- data/spec/singularity_dsl/application_spec.rb +92 -0
- data/spec/singularity_dsl/dsl/batch_spec.rb +30 -0
- data/spec/singularity_dsl/dsl/changeset_spec.rb +54 -0
- data/spec/singularity_dsl/dsl/dsl_spec.rb +45 -0
- data/spec/singularity_dsl/dsl/event_store_spec.rb +44 -0
- data/spec/singularity_dsl/dsl/registry_spec.rb +39 -0
- data/spec/singularity_dsl/dsl/runner_spec.rb +39 -0
- data/spec/singularity_dsl/dsl/stubs/tasks/dummy_task.rb +4 -0
- data/spec/singularity_dsl/dsl/utils_spec.rb +33 -0
- data/spec/singularity_dsl/git_helper_spec.rb +72 -0
- data/spec/singularity_dsl/runstate_spec.rb +73 -0
- data/spec/singularity_dsl/task_spec.rb +54 -0
- data/spec/singularity_dsl/tasks/shell_task_spec.rb +119 -0
- metadata +231 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
Yzc0YjUwZWE2NmJhNGEzMzkyMzQyMjllY2VlOTk4Yzg3ZjMwYzgxZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YThjODFlMDNhMTUwYTI3N2YxMDlmOWRhMGNjNzQ5MjE4ZDExNDczMg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MmY2NWFhZWY3ZTA5ZWRjZmU1MjVjYjUwNmI4MmM2YWNkNWI0YmRkYmJjNmNj
|
10
|
+
ZTljYjM3NmIwYzBmMTM4YjNlNjk0OTBkOWY5N2MwM2M4MTEwYTA4OTljNzY2
|
11
|
+
ZjJlMGZmN2ZmYTJhNWNiOWZiMjVmNTM0ZDdjNTliZDhlMjdmZWU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YzlkYmNiMDM2YzI2MjQzNzY1NWEwMWQ5OGQ2MDJjOTYyMDg0MzQ2YmJhY2E2
|
14
|
+
YTliNzYxNjU0YmNmMzhjNDA3NmRmMDUwODg4NzhkOGE3YzdjN2JhYjNlNDk1
|
15
|
+
YzJjMzUwNDkwMWZiZTNjYjE3NGJiYTJiYjEyZjM0ZThiZmY0ZmE=
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.singularityrc
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
singularity_dsl (1.2.5)
|
5
|
+
mixlib-shellout (~> 1.4)
|
6
|
+
rainbow (~> 2.0.0)
|
7
|
+
rake (~> 10.3)
|
8
|
+
rspec (~> 2.6)
|
9
|
+
rubocop (~> 0.23.0)
|
10
|
+
terminal-table (~> 1.4)
|
11
|
+
thor (~> 0.19)
|
12
|
+
|
13
|
+
GEM
|
14
|
+
remote: https://rubygems.org/
|
15
|
+
specs:
|
16
|
+
ast (2.0.0)
|
17
|
+
diff-lcs (1.2.5)
|
18
|
+
gem-release (0.7.3)
|
19
|
+
json (1.8.1)
|
20
|
+
mixlib-shellout (1.4.0)
|
21
|
+
parser (2.1.9)
|
22
|
+
ast (>= 1.1, < 3.0)
|
23
|
+
slop (~> 3.4, >= 3.4.5)
|
24
|
+
powerpack (0.0.9)
|
25
|
+
rainbow (2.0.0)
|
26
|
+
rake (10.3.2)
|
27
|
+
rspec (2.99.0)
|
28
|
+
rspec-core (~> 2.99.0)
|
29
|
+
rspec-expectations (~> 2.99.0)
|
30
|
+
rspec-mocks (~> 2.99.0)
|
31
|
+
rspec-core (2.99.1)
|
32
|
+
rspec-expectations (2.99.2)
|
33
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
34
|
+
rspec-mocks (2.99.2)
|
35
|
+
rubocop (0.23.0)
|
36
|
+
json (>= 1.7.7, < 2)
|
37
|
+
parser (~> 2.1.9)
|
38
|
+
powerpack (~> 0.0.6)
|
39
|
+
rainbow (>= 1.99.1, < 3.0)
|
40
|
+
ruby-progressbar (~> 1.4)
|
41
|
+
ruby-progressbar (1.5.1)
|
42
|
+
slop (3.6.0)
|
43
|
+
terminal-table (1.4.5)
|
44
|
+
thor (0.19.1)
|
45
|
+
|
46
|
+
PLATFORMS
|
47
|
+
ruby
|
48
|
+
|
49
|
+
DEPENDENCIES
|
50
|
+
bundler (~> 1.3)
|
51
|
+
gem-release
|
52
|
+
singularity_dsl!
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 chr0n1x
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# SingularityDsl
|
2
|
+
|
3
|
+
A DSL lib for your [SingularityCI](https://github.com/behance/singularity) instance.
|
4
|
+
Inspired by TravisCI's `.travis.yml`
|
5
|
+
|
6
|
+
...Except with the full power of Ruby, its gems & your awesome JenkinsCI machine(s)
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'singularity_dsl'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install singularity_dsl
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
TODO: cause there are a lot of things that I want to do
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
1. Fork it
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create new Pull Request
|
33
|
+
|
34
|
+
Make sure you run the tests!
|
35
|
+
|
36
|
+
```
|
37
|
+
bundle exec rake test:all
|
38
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/user/bin/env rake
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'bundler/gem_tasks'
|
5
|
+
require 'rubocop/rake_task'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
|
8
|
+
namespace :test do
|
9
|
+
|
10
|
+
RuboCop::RakeTask.new(:rubocop)
|
11
|
+
|
12
|
+
RSpec::Core::RakeTask.new(:rspec) do |t|
|
13
|
+
t.rspec_opts = '--format documentation --color'
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Run RSpec with code coverage'
|
17
|
+
task :coverage do
|
18
|
+
ENV['COVERAGE'] = 'true'
|
19
|
+
Rake::Task['test:rspec'].execute
|
20
|
+
end
|
21
|
+
|
22
|
+
desc 'Run rubocop and rspec test'
|
23
|
+
task all: [:rubocop, :rspec]
|
24
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'singularity_dsl/dsl/runner'
|
4
|
+
require 'singularity_dsl/errors'
|
5
|
+
require 'rainbow'
|
6
|
+
|
7
|
+
module SingularityDsl
|
8
|
+
# application singleton - environment container for script
|
9
|
+
class Application
|
10
|
+
include SingularityDsl::Errors
|
11
|
+
|
12
|
+
attr_reader :runner, :dsl
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@runner = Dsl::Runner.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def load_script(script)
|
19
|
+
@runner.load_ex_script script
|
20
|
+
end
|
21
|
+
|
22
|
+
def load_tasks(path)
|
23
|
+
@runner.dsl.load_tasks_in_path path
|
24
|
+
end
|
25
|
+
|
26
|
+
def run(batch = false, pass_errors = false)
|
27
|
+
begin
|
28
|
+
@runner.execute batch, pass_errors
|
29
|
+
# resource failed, :all_tasks not specified
|
30
|
+
rescue ResourceFail => failure
|
31
|
+
log_resource_fail failure
|
32
|
+
# resource actually failed & threw error
|
33
|
+
rescue ResourceError => error
|
34
|
+
log_resource_error error
|
35
|
+
ensure
|
36
|
+
post_task_runner_actions
|
37
|
+
end
|
38
|
+
@runner.state.exit_code
|
39
|
+
end
|
40
|
+
|
41
|
+
def post_task_runner_actions
|
42
|
+
script_warn @runner.state.failures if @runner.state.failed
|
43
|
+
script_error @runner.state.errors if @runner.state.error
|
44
|
+
@runner.post_actions
|
45
|
+
end
|
46
|
+
|
47
|
+
def change_list(list)
|
48
|
+
list = list.split("\n") if list.is_a? String
|
49
|
+
list = [*list]
|
50
|
+
list.sort!
|
51
|
+
@runner.dsl.changeset = list
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def log_resource_fail(failure)
|
57
|
+
script_warn 'Script run failed!'
|
58
|
+
script_warn failure.message
|
59
|
+
failure.backtrace.each { |line| puts line }
|
60
|
+
end
|
61
|
+
|
62
|
+
def log_resource_error(error)
|
63
|
+
script_error 'Script run error!'
|
64
|
+
script_error error.message
|
65
|
+
error.backtrace.each { |line| puts line }
|
66
|
+
end
|
67
|
+
|
68
|
+
def script_warn(message)
|
69
|
+
puts Rainbow(message).yellow
|
70
|
+
end
|
71
|
+
|
72
|
+
def script_error(message)
|
73
|
+
puts Rainbow(message).red
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'singularity_dsl/application'
|
4
|
+
require 'singularity_dsl/cli/table'
|
5
|
+
require 'singularity_dsl/dsl/dsl'
|
6
|
+
require 'singularity_dsl/errors'
|
7
|
+
require 'singularity_dsl/git_helper'
|
8
|
+
require 'singularity_dsl/stdout'
|
9
|
+
require 'thor'
|
10
|
+
|
11
|
+
module SingularityDsl
|
12
|
+
# CLI
|
13
|
+
module Cli
|
14
|
+
# CLI Thor app
|
15
|
+
class Cli < Thor
|
16
|
+
include Errors
|
17
|
+
include Stdout
|
18
|
+
include Table
|
19
|
+
|
20
|
+
def initialize(*args)
|
21
|
+
super
|
22
|
+
@diff_list = nil
|
23
|
+
@git = GitHelper.new
|
24
|
+
end
|
25
|
+
|
26
|
+
class_option :task_path,
|
27
|
+
aliases: '-t',
|
28
|
+
type: :string,
|
29
|
+
desc: 'Directory where custom tasks are defined',
|
30
|
+
default: './.singularity'
|
31
|
+
class_option :all_tasks,
|
32
|
+
aliases: '-a',
|
33
|
+
type: :boolean,
|
34
|
+
desc: 'Do not stop on task failure(s), collect all results'
|
35
|
+
class_option :script,
|
36
|
+
aliases: '-s',
|
37
|
+
type: :string,
|
38
|
+
desc: 'Specify path to a .singularityrc file',
|
39
|
+
default: './.singularityrc'
|
40
|
+
|
41
|
+
# TASKS COMMAND
|
42
|
+
desc 'tasks', 'Available tasks.'
|
43
|
+
def tasks
|
44
|
+
dsl = Dsl::Dsl.new
|
45
|
+
dsl.load_tasks_in_path tasks_path if ::File.exist? tasks_path
|
46
|
+
table = task_table
|
47
|
+
dsl.task_list.each do |task|
|
48
|
+
table.add_row task_row(dsl, task)
|
49
|
+
end
|
50
|
+
puts table
|
51
|
+
end
|
52
|
+
|
53
|
+
# TEST COMMAND
|
54
|
+
desc 'test', 'Run singularity script.'
|
55
|
+
def test(app = nil)
|
56
|
+
app ||= setup_app(Application.new, singularity_script, tasks_path)
|
57
|
+
exit(app.run false, options[:all_tasks])
|
58
|
+
end
|
59
|
+
|
60
|
+
# BATCH COMMAND
|
61
|
+
desc 'batch BATCH_NAME', 'Run single task batch in the script.'
|
62
|
+
def batch(batch, app = nil)
|
63
|
+
app ||= setup_app(Application.new, singularity_script, tasks_path)
|
64
|
+
exit(app.run batch, options[:all_tasks])
|
65
|
+
end
|
66
|
+
|
67
|
+
# TEST-MERGE COMMAND
|
68
|
+
desc 'testmerge FORK BRANCH INTO_BRANCH [INTO_FORK]',
|
69
|
+
'Perform a test merge into the local repo.'
|
70
|
+
option :run_task,
|
71
|
+
aliases: '-r',
|
72
|
+
type: :string,
|
73
|
+
desc: 'Run a batch after. If nothing given, script is run as-is',
|
74
|
+
default: ''
|
75
|
+
def testmerge(git_fork, branch, base_branch, base_fork = nil)
|
76
|
+
test_merge git_fork, branch, base_branch, base_fork
|
77
|
+
info 'File changesets'
|
78
|
+
puts @git.diff_remote base_branch, base_fork, '--name-status'
|
79
|
+
@diff_list = @git.diff_remote base_branch, base_fork, '--name-only'
|
80
|
+
@git.remove_remote git_fork
|
81
|
+
@git.remove_remote base_fork
|
82
|
+
batch target_run_task if options[:run_task]
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def test_merge(git_fork, branch, base_branch, base_fork)
|
88
|
+
@git.clean_reset
|
89
|
+
@git.add_remote base_fork
|
90
|
+
@git.checkout_remote base_branch, base_fork
|
91
|
+
@git.add_remote git_fork
|
92
|
+
@git.merge_remote branch, git_fork
|
93
|
+
end
|
94
|
+
|
95
|
+
def target_run_task
|
96
|
+
target = options[:run_task]
|
97
|
+
target = false if target.eql? ''
|
98
|
+
target
|
99
|
+
end
|
100
|
+
|
101
|
+
def setup_app(app, singularity_script, tasks_path)
|
102
|
+
if File.exist? tasks_path
|
103
|
+
info "Loading tasks from #{tasks_path}"
|
104
|
+
app.load_tasks tasks_path
|
105
|
+
end
|
106
|
+
unless @diff_list.nil?
|
107
|
+
info 'Running with diff-list'
|
108
|
+
puts @diff_list
|
109
|
+
app.change_list @diff_list
|
110
|
+
end
|
111
|
+
info "Loading CI script from #{singularity_script} ..."
|
112
|
+
app.load_script singularity_script
|
113
|
+
app
|
114
|
+
end
|
115
|
+
|
116
|
+
def singularity_script
|
117
|
+
File.expand_path options[:script]
|
118
|
+
end
|
119
|
+
|
120
|
+
def tasks_path
|
121
|
+
File.expand_path options[:task_path]
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rainbow'
|
4
|
+
require 'terminal-table'
|
5
|
+
|
6
|
+
module SingularityDsl
|
7
|
+
# CLI
|
8
|
+
module Cli
|
9
|
+
# util CLI table formatting fxs
|
10
|
+
module Table
|
11
|
+
private
|
12
|
+
|
13
|
+
def task_row(dsl, task_class)
|
14
|
+
name = dsl.task_name task_class
|
15
|
+
task_name = dsl.task task_class
|
16
|
+
desc = task_class.new.description
|
17
|
+
[name, task_name, desc]
|
18
|
+
end
|
19
|
+
|
20
|
+
def task_table
|
21
|
+
headers = [
|
22
|
+
Rainbow('Task').yellow,
|
23
|
+
Rainbow('Task Function').yellow,
|
24
|
+
Rainbow('Description').yellow
|
25
|
+
]
|
26
|
+
table = Terminal::Table.new headings: headers
|
27
|
+
table.style = { border_x: '', border_y: '', border_i: '' }
|
28
|
+
table
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'singularity_dsl/task'
|
4
|
+
|
5
|
+
module SingularityDsl
|
6
|
+
# DSL classes & fxs
|
7
|
+
module Dsl
|
8
|
+
# wrapper for a batch of tasks
|
9
|
+
class Batch < Task
|
10
|
+
attr_reader :name
|
11
|
+
|
12
|
+
def initialize(name, context, &block)
|
13
|
+
@name = name.to_sym
|
14
|
+
@context = context
|
15
|
+
@proc = block
|
16
|
+
end
|
17
|
+
|
18
|
+
def execute
|
19
|
+
@context.instance_eval(&@proc)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module SingularityDsl
|
4
|
+
# DSL classes & fxs
|
5
|
+
module Dsl
|
6
|
+
# methods & data to store & describe changesets
|
7
|
+
module Changeset
|
8
|
+
attr_writer :changeset
|
9
|
+
|
10
|
+
@changeset = []
|
11
|
+
@existing = nil
|
12
|
+
|
13
|
+
def files_changed?(types)
|
14
|
+
types = [*types]
|
15
|
+
@changeset ||= []
|
16
|
+
types.any? do |type|
|
17
|
+
@changeset.any? { |file| file.match("\.#{type}$") }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def changed_files(types)
|
22
|
+
types = [*types]
|
23
|
+
@changeset ||= []
|
24
|
+
types.flat_map do |type|
|
25
|
+
existing_files.select { |file| file.match("\.#{type}$") }
|
26
|
+
end.sort
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def existing_files
|
32
|
+
if @existing.nil?
|
33
|
+
@existing = @changeset.select { |file| ::File.exist?(file) }
|
34
|
+
end
|
35
|
+
@existing
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'singularity_dsl/files'
|
4
|
+
require 'singularity_dsl/dsl/changeset'
|
5
|
+
require 'singularity_dsl/dsl/event_store'
|
6
|
+
require 'singularity_dsl/dsl/registry'
|
7
|
+
require 'singularity_dsl/dsl/utils'
|
8
|
+
|
9
|
+
module SingularityDsl
|
10
|
+
# DSL classes & fxs
|
11
|
+
module Dsl
|
12
|
+
# wrapper for DSL
|
13
|
+
class Dsl
|
14
|
+
include Changeset
|
15
|
+
include EventStore
|
16
|
+
include Files
|
17
|
+
include Utils
|
18
|
+
|
19
|
+
attr_reader :registry
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
super
|
23
|
+
@registry = Registry.new
|
24
|
+
load_tasks_in_path default_task_dir
|
25
|
+
end
|
26
|
+
|
27
|
+
def define_task(klass)
|
28
|
+
raise_task_def_error klass if task_defined klass
|
29
|
+
# because define_method is private
|
30
|
+
define_singleton_method(task klass) do |&block|
|
31
|
+
@registry.add_task klass.new(&block)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def load_tasks_in_path(path)
|
36
|
+
base_tasks = task_list
|
37
|
+
files_in_path(path).each do |file|
|
38
|
+
SingularityDsl.module_eval(::File.read file)
|
39
|
+
end
|
40
|
+
load_tasks(task_list - base_tasks)
|
41
|
+
end
|
42
|
+
|
43
|
+
def invoke_batch(name)
|
44
|
+
@registry.add_batch_to_runlist name
|
45
|
+
end
|
46
|
+
|
47
|
+
def batch(name, &block)
|
48
|
+
@registry.batch(name, self, &block)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def raise_task_def_error(klass)
|
54
|
+
fail "task name clash for #{klass}"
|
55
|
+
end
|
56
|
+
|
57
|
+
def task_defined(klass)
|
58
|
+
singleton_methods(false).include?(task klass)
|
59
|
+
end
|
60
|
+
|
61
|
+
def load_tasks(list)
|
62
|
+
list.each { |klass| define_task klass }
|
63
|
+
end
|
64
|
+
|
65
|
+
def default_task_dir
|
66
|
+
dir = ::File.dirname __FILE__
|
67
|
+
::File.join dir, '..', 'tasks'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module SingularityDsl
|
4
|
+
# DSL classes & fxs
|
5
|
+
module Dsl
|
6
|
+
# default methods to for DSL objects
|
7
|
+
module EventStore
|
8
|
+
attr_reader :error_proc, :fail_proc, :success_proc, :always_proc
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@error_proc = proc {}
|
12
|
+
@fail_proc = proc {}
|
13
|
+
@success_proc = proc {}
|
14
|
+
@always_proc = proc {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def on_error(&block)
|
18
|
+
@error_proc = Proc.new(&block)
|
19
|
+
end
|
20
|
+
|
21
|
+
def on_fail(&block)
|
22
|
+
@fail_proc = Proc.new(&block)
|
23
|
+
end
|
24
|
+
|
25
|
+
def on_success(&block)
|
26
|
+
@success_proc = Proc.new(&block)
|
27
|
+
end
|
28
|
+
|
29
|
+
def always(&block)
|
30
|
+
@always_proc = Proc.new(&block)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'singularity_dsl/dsl/batch'
|
4
|
+
require 'singularity_dsl/task'
|
5
|
+
|
6
|
+
module SingularityDsl
|
7
|
+
# DSL classes & fxs
|
8
|
+
module Dsl
|
9
|
+
# wrapper for DSL
|
10
|
+
class Registry
|
11
|
+
def initialize
|
12
|
+
@task_list = []
|
13
|
+
@batch_list = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_task(task)
|
17
|
+
fail_non_type('Task', task) unless task.is_a? Task
|
18
|
+
fail_raw_type('Task', task) unless task.class < Task
|
19
|
+
@task_list.push task
|
20
|
+
end
|
21
|
+
|
22
|
+
def batch(name, context, &block)
|
23
|
+
batch_exists(name) if @batch_list.key? name.to_sym
|
24
|
+
batch = Batch.new(name.to_sym, context, &block)
|
25
|
+
@batch_list[name.to_sym] = batch
|
26
|
+
end
|
27
|
+
|
28
|
+
def add_batch_to_runlist(name)
|
29
|
+
batch_dne name unless @batch_list[name.to_sym]
|
30
|
+
@batch_list[name.to_sym].execute
|
31
|
+
end
|
32
|
+
|
33
|
+
def run_list(batch = false)
|
34
|
+
if batch
|
35
|
+
@task_list = []
|
36
|
+
add_batch_to_runlist batch
|
37
|
+
end
|
38
|
+
@task_list
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def batch_dne(name)
|
44
|
+
fail "Cannot invoke batch '#{name}', does not exist"
|
45
|
+
end
|
46
|
+
|
47
|
+
def batch_exists(name)
|
48
|
+
fail "A task batch with the name '#{name}' already exists"
|
49
|
+
end
|
50
|
+
|
51
|
+
def fail_non_type(type, task)
|
52
|
+
fail "Non-#{type} given - #{task}"
|
53
|
+
end
|
54
|
+
|
55
|
+
def fail_raw_type(type, task)
|
56
|
+
fail "Cannot use raw #{type} objects - #{task}"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|