taskr-wannabe 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 138495b6e63f52eed5676d2ab017c39fb4cf2e4c
4
+ data.tar.gz: 56174b77f344ad62c9fd804bbf78e40493ad8c34
5
+ SHA512:
6
+ metadata.gz: aa4b9956560a7189c5c994fd2fe4ccca4fbb8e7797f4a978c2986bb2c4fb9d12f8b90f75100e3236e0d1631e0d1d1994af3f04b9a15c6ec5e6fcae61b5b9d8f9
7
+ data.tar.gz: e7d0fb096847a771c6e034ef359a2ff938238199df67e2ec77988fe45b9d76995c995a97d6496e0995804b5ae8146bfe1ec7e5842b2d62882f99bad5070a482a
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require File.expand_path('../../lib/taskr', __FILE__)
5
+ include Taskr::DSL
6
+ instance_eval(File.read('Taskrfile'))
7
+
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: taskr task_name"
10
+
11
+ opts.on('-h', '--help', 'Displays Help') do
12
+ puts opts
13
+ exit 0
14
+ end
15
+
16
+ opts.on('-t', '--tasks', 'Displays Available Tasks') do
17
+ task_manager.tasks.each do |task|
18
+ puts "#{task.name}: #{task.description}"
19
+ end
20
+ exit 0
21
+ end
22
+
23
+ unless ARGV.any?
24
+ puts opts
25
+ exit 1
26
+ end
27
+ end.parse!
28
+
29
+ task_name = ARGV.first
30
+ task_manager.run(task_name)
@@ -0,0 +1,9 @@
1
+ require File.expand_path('../taskr/task', __FILE__)
2
+ require File.expand_path('../taskr/task_manager', __FILE__)
3
+ require File.expand_path('../taskr/dsl', __FILE__)
4
+
5
+ module Taskr
6
+
7
+ class TaskNotFoundError < StandardError; end
8
+
9
+ end
@@ -0,0 +1,36 @@
1
+ module Taskr
2
+
3
+ module DSL
4
+
5
+ def task_manager
6
+ @task_manager ||= TaskManager.new
7
+ end
8
+
9
+ def desc(description)
10
+ @description = description
11
+ end
12
+
13
+ def task(task_name, &action)
14
+ deps = []
15
+ if task_name.is_a? Hash
16
+ deps = [task_name.values.first].flatten
17
+ task_name = task_name.keys.first
18
+ end
19
+ task_description = description
20
+ reset_description
21
+ task_manager.register(Task.new(task_name, action, task_description), deps)
22
+ end
23
+
24
+ private
25
+
26
+ def description
27
+ @description ||= ''
28
+ end
29
+
30
+ def reset_description
31
+ @description = ''
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,15 @@
1
+ module Taskr
2
+ class Task
3
+ attr_reader :name, :description
4
+
5
+ def initialize(name, action, description)
6
+ raise ArgumentError.new('Action is not callable') unless action.respond_to? :call
7
+ @name = name
8
+ @action = action
9
+ @description = description
10
+ end
11
+ def run
12
+ @action.call
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ module Taskr
2
+
3
+ class TaskManager
4
+
5
+ def initialize
6
+ @data = {}
7
+ end
8
+
9
+ def register(task, deps=[])
10
+ raise ArgumentError.new('Can register only instances of Taskr::Task') unless task.is_a? Task
11
+ @data[task.name] = {:task => task, :deps => deps}
12
+ true
13
+ end
14
+
15
+ def tasks
16
+ @data.values.map {|task_record| task_record[:task]}
17
+ end
18
+
19
+ def run(task_name)
20
+ task_record = find_by_name(task_name)
21
+ task_record[:deps].each { |task_dep_name| run(task_dep_name) }
22
+ task_record[:task].run
23
+ end
24
+
25
+ private
26
+
27
+ def find_by_name(task_name)
28
+ task = @data.fetch(task_name, nil)
29
+ raise TaskNotFoundError.new("Task with name #{task_name} could not be found") if task.nil?
30
+ task
31
+ end
32
+
33
+ end
34
+
35
+
36
+ end
@@ -0,0 +1,3 @@
1
+ module Taskr
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'mocha/mini_test'
3
+ require 'taskr'
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe Taskr::DSL do
5
+
6
+ include Taskr::DSL
7
+
8
+
9
+ it 'can register tasks' do
10
+ task :sample_task do
11
+ "Just a sample action"
12
+ end
13
+ task_manager.tasks.find do |task|
14
+ task.name == :sample_task
15
+ end.wont_be_nil
16
+ end
17
+
18
+ it 'can register task with a single dependency' do
19
+ task :dep1 do
20
+ "Dep1"
21
+ end
22
+ task :the_task => :dep1 do
23
+ "The task"
24
+ end
25
+ dep1 = task_manager.tasks.find {|task| task.name == :dep1}
26
+ dep1.expects(:run)
27
+ task_manager.run(:the_task)
28
+ end
29
+
30
+ it 'can register task with multilple dependencies' do
31
+ task :dep1 do
32
+ "Dep1"
33
+ end
34
+ task :dep2 do
35
+ "Dep2"
36
+ end
37
+ task :the_task => [:dep1, :dep2] do
38
+ "The task"
39
+ end
40
+ dep1 = task_manager.tasks.find {|task| task.name == :dep1}
41
+ dep2 = task_manager.tasks.find {|task| task.name == :dep2}
42
+ dep1.expects(:run)
43
+ dep2.expects(:run)
44
+ task_manager.run(:the_task)
45
+ end
46
+
47
+ it 'can add a description to a task' do
48
+ desc "sample task description"
49
+ task :sample_task_1 do
50
+ "Sample task 1"
51
+ end
52
+ task :sample_task_2 do
53
+ "Sample task 2"
54
+ end
55
+ task_manager.tasks.find do |task|
56
+ task.name == :sample_task_1
57
+ end.description.must_equal "sample task description"
58
+ task_manager.tasks.find do |task|
59
+ task.name == :sample_task_2
60
+ end.description.must_equal ""
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe Taskr::TaskManager do
5
+
6
+ let(:task_manager) { Taskr::TaskManager.new }
7
+ let(:task) { Taskr::Task.new('name', stub(:call => nil), 'desc') }
8
+
9
+ it 'can register task' do
10
+ task_manager.register(task).must_equal true
11
+ end
12
+
13
+ it 'can register tasks only' do
14
+ e = lambda do
15
+ task_manager.register('something else')
16
+ end.must_raise ArgumentError
17
+ e.message.must_equal 'Can register only instances of Taskr::Task'
18
+ end
19
+
20
+ it 'provides access to the tasks registered' do
21
+ task_manager.register(task)
22
+ task_manager.tasks.must_include task
23
+ end
24
+
25
+ it 'can find and run a task by its name' do
26
+ task_manager.register(task)
27
+ task.expects(:run)
28
+ task_manager.run(task.name)
29
+ end
30
+
31
+ it 'can register task with dependencies' do
32
+ begin
33
+ task_manager.register(task, [:one, :two, :three])
34
+ rescue ArgumentError
35
+ flunk("Should be able to register a task with its dependencies")
36
+ end
37
+ end
38
+
39
+ it 'runs the dependencies when we run the task' do
40
+ dep1 = Taskr::Task.new('dep1', stub(:call => nil), '')
41
+ dep2 = Taskr::Task.new('dep2', stub(:call => nil), '')
42
+ task_manager.register(task, [dep1.name, dep2.name])
43
+ task_manager.register(dep1)
44
+ task_manager.register(dep2)
45
+ dep1.expects(:run)
46
+ dep2.expects(:run)
47
+ task_manager.run(task.name)
48
+ end
49
+
50
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Taskr::Task do
4
+
5
+ it 'can be created with a name, callable action and description' do
6
+ begin
7
+ Taskr::Task.new('name', stub(:call => nil), 'description')
8
+ rescue ArgumentError
9
+ flunk("Should not raise ArgumentError")
10
+ end
11
+ end
12
+
13
+ it 'must have the name and the task accessible' do
14
+ t = Taskr::Task.new('name', stub(:call => nil), 'desc')
15
+ t.name.must_equal 'name'
16
+ t.description.must_equal 'desc'
17
+ end
18
+
19
+ it 'must be created with a callable action' do
20
+ e = lambda do
21
+ Taskr::Task.new('name', 'something not callable', 'desc')
22
+ end.must_raise ArgumentError
23
+ e.message.must_equal 'Action is not callable'
24
+ end
25
+
26
+ it 'must run the action when we run the task' do
27
+ action = stub(:call => nil)
28
+ action.expects(:call)
29
+ task = Taskr::Task.new('name', action, 'desc')
30
+ task.run
31
+ end
32
+
33
+ end
34
+
35
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: taskr-wannabe
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Filip Kostovski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Just a dummy task runner dsl
14
+ email:
15
+ - github.sirfilip@gmail.com
16
+ executables:
17
+ - taskr
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/taskr
22
+ - lib/taskr.rb
23
+ - lib/taskr/dsl.rb
24
+ - lib/taskr/task.rb
25
+ - lib/taskr/task_manager.rb
26
+ - lib/taskr/version.rb
27
+ - spec/spec_helper.rb
28
+ - spec/unit/dsl_spec.rb
29
+ - spec/unit/task_manager_spec.rb
30
+ - spec/unit/task_spec.rb
31
+ homepage: https://github.com/sirfilip/blogcode/tree/master/taskr
32
+ licenses:
33
+ - MIT
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 2.5.1
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Run your tasks everyday
55
+ test_files:
56
+ - spec/spec_helper.rb
57
+ - spec/unit/task_manager_spec.rb
58
+ - spec/unit/dsl_spec.rb
59
+ - spec/unit/task_spec.rb