Skip to content

Commit

Permalink
allow multiple task execution
Browse files Browse the repository at this point in the history
  • Loading branch information
imdrasil committed Nov 14, 2017
1 parent 6fd31ff commit 1976dbe
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -123,6 +123,14 @@ To autoload Sam files from your dependencies - just past
load_dependencies "dep1", "dep2"`
```

To execute multiple tasks at once just list them separted by `@` character:

```crystal
$ crystal sam.cr -- namespace1:task1 arg1=2 @ other_task arg1=3
```

Each task will be executed only if the previous one is successfully finished (without throwing any exception).

#### Namespace

To define namespace (for now they only used for namespacing tasks) use `Sam.namespace` (opens `root` namespace) or just `namespace` inside of it. `Sam.namespace` can be called any times - everything will be added to existing staff.
Expand Down
1 change: 1 addition & 0 deletions examples/sam.cr
Expand Up @@ -12,6 +12,7 @@ Sam.task "prepare" do
Sam.namespace "din" do
task "dong" do
puts "dong"
Container.add("din:dong")
end
end
DOC
Expand Down
31 changes: 31 additions & 0 deletions spec/sam_spec.cr
Expand Up @@ -2,9 +2,13 @@ require "./spec_helper"

describe Sam do
describe "::namespace" do
pending "add" do
end
end

describe "::task" do
pending "add" do
end
end

describe "::invoke" do
Expand All @@ -22,6 +26,33 @@ describe Sam do
end

describe "::help" do
pending "add" do
end
end

describe "::process_tasks" do
context "one task" do
it "executes given task" do
Sam.process_tasks(["db:schema"])
Container.tasks.should eq(["db:schema"])
end
end

context "multiple tasks" do
context "without arguments" do
it "executes all of them" do
Sam.process_tasks(["db:schema", "@", "db:ping"])
Container.tasks.should eq(["db:schema", "db:ping"])
end
end

context "with arguments" do
it "executes them and pass arguments" do
Sam.process_tasks(["db:schema", "@", "db:with_argument", "f1=2"])
Container.tasks.should eq(["db:schema", "db:with_argument"])
end
end
end
end

describe ".generate_makefile" do
Expand Down
34 changes: 34 additions & 0 deletions spec/spec_helper.cr
Expand Up @@ -3,6 +3,30 @@ require "../src/sam"

load_dependencies "asd"

class Container
@@executed_tasks = [] of String

def self.add(name)
@@executed_tasks << name
end

def self.tasks
@@executed_tasks
end

def self.clear
@@executed_tasks.clear
end
end

# Callbacks

Spec.before_each do
Container.clear
end

# Tasks

Sam.namespace "db" do
namespace "schema" do
task "load" do |t, args|
Expand All @@ -14,24 +38,34 @@ Sam.namespace "db" do
t.invoke("db:ping")
t.invoke("din:dong")
t.invoke("schema")
Container.add(t.path)
end

task "1" do
puts "1"
Container.add("db:schema:1")
end
end

task "with_argument" do |t, args|
puts args["f1"]
Container.add(t.path)
end

task "schema" do
puts "same as namespace"
Container.add("db:schema")
end

namespace "db" do
task "migrate" do
puts "migrate"
Container.add("db:db:migrate")
end
end

task "ping" do
puts "ping"
Container.add("db:ping")
end
end
21 changes: 19 additions & 2 deletions src/sam.cr
@@ -1,6 +1,8 @@
require "./sam/*"

module Sam
TASK_SEPARATOR = "@"

@@root_namespace = Namespace.new("root", nil)

def self.namespace(name : String)
Expand Down Expand Up @@ -49,16 +51,31 @@ module Sam

def self.help
if ARGV.size > 0
Sam.invoke(ARGV[0], ARGV[1..-1])
process_tasks(ARGV)
else
puts "Hm, nothing to do"
puts "Hm, nothing to do..."
end
rescue e
puts e.backtrace.join("\n")
puts e
exit(1)
end

def self.process_tasks(args)
separator_indexes = args.map_with_index { |a, i| i if a == TASK_SEPARATOR }.compact
sets = [] of Array(String)
previous_index = 0
separator_indexes.each do |i|
sets << args[previous_index...i]
previous_index = i + 1
end
sets << args[previous_index..-1]

sets.each do |set|
Sam.invoke(set[0], set[1..-1])
end
end

def self.pretty_print
descs = [] of String
tasks = @@root_namespace.all_tasks
Expand Down

0 comments on commit 1976dbe

Please sign in to comment.