Skip to content

Commit

Permalink
Merge pull request #22 from imdrasil/add-default-task
Browse files Browse the repository at this point in the history
Execute default task if nothing specified; fix default path where sam sample file is placed
  • Loading branch information
imdrasil authored Jul 10, 2023
2 parents 764d371 + 3cc36bd commit d405924
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 34 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Add this to your application's `shard.yml`:
dependencies:
sam:
github: imdrasil/sam.cr
version: 0.4.2
version: 0.5.0
```
After executing `shards install` Sam-file will be added to the root of your project (unless you already have one).
Expand Down Expand Up @@ -63,6 +63,19 @@ To see a list of all available tasks with their descriptions:
$ crystal sam.cr help
```

If you pass no arguments - the task `"default"` will be invoked.

```crystal
task "default" do
puts "Hi"
end
```

```shell
$ crystal sam.cr
Hi
```

#### Tasks with arguments

To pass arguments to your task just list them after it's name:
Expand Down
14 changes: 9 additions & 5 deletions examples/sam.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/bin/crystal

require "../src/sam"
require "file_utils"

class Container
def self.add(a); end
end

Sam.task "setup" do
task "setup" do
lib1 = "./lib/lib1/src/lib1"
lib2 = "./lib/lib2/src/lib2"
lib3 = "./lib/lib3/src/lib3"
Expand Down Expand Up @@ -78,11 +80,11 @@ Sam.task "setup" do
)
end

Sam.task "clear" do
task "clear" do
FileUtils.rm_r("./lib")
end

Sam.namespace "db" do
namespace "db" do
namespace "schema" do
desc "just test"
task "load" do |t, args|
Expand Down Expand Up @@ -116,6 +118,8 @@ Sam.namespace "db" do
task "ping" do
puts "ping"
end
end

Sam.help
task "with_argument" do |_, args|
puts args["f1"]
end
end
4 changes: 2 additions & 2 deletions examples/sam.template
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/crystal

require "sam"

# Here you can define your tasks
# desc "with description to be used by help command"
# task "test" do
# puts "ping"
# end

Sam.help
5 changes: 5 additions & 0 deletions examples/with_default.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "../src/sam"

task "default" do
puts "this is by default"
end
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license: MIT
development_dependencies:
ameba:
github: crystal-ameba/ameba
version: "= 0.14.3"
version: "= 1.4.3"

scripts:
postinstall: "false | [ -f ../../sam.cr ] && true || cp -i examples/sam.template ../../sam.cr 2>/dev/null"
36 changes: 29 additions & 7 deletions spec/sam_spec.cr
Original file line number Diff line number Diff line change
@@ -1,36 +1,58 @@
require "./spec_helper"

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

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

describe "::invoke" do
describe ".invoke" do
it "raises error if given task is not exists" do
expect_raises(Exception, "Task giberrish was not found") do
Sam.invoke("giberrish")
end
end
end

describe "::find" do
describe ".find" do
it "finds correct task by path" do
Sam.find!("db:schema:load").name.should eq("load")
end
end

describe "::help" do
pending "add" do
describe ".help" do
it "with multiple tasks" do
res = execute("crystal examples/sam.cr", ["db:with_argument", "f1=a", "@", "db:ping"])
res[1].should eq(<<-TEXT)
a
ping
TEXT
end

it "without a specified task but with a default task defined" do
res = execute("crystal examples/with_default.cr", %w[])
res[1].should eq(<<-TEXT)
this is by default
TEXT
end

it "without a specified task and without a default task defined" do
res = execute("crystal examples/sam.cr", %w[])
res[1].should eq(<<-TEXT)
Hm, nothing to do...
TEXT
end
end

describe "::process_tasks" do
describe ".process_tasks" do
context "one task" do
it "executes given task" do
Sam.process_tasks(["db:schema"])
Expand Down
9 changes: 9 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ require "../src/sam"
load_dependencies "lib1"
load_dependencies "lib2": "special", "lib3": ["/special"]

# Helpers

class Container
@@executed_tasks = [] of String

Expand All @@ -20,6 +22,13 @@ class Container
end
end

def execute(command, options)
io = IO::Memory.new

status = Process.run("#{command} \"${@}\"", options, shell: true, output: io, error: io).exit_status
{status, io.to_s}
end

# Callbacks

Spec.before_each do
Expand Down
18 changes: 13 additions & 5 deletions src/sam.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ require "./sam/*"
module Sam
extend Execution

VERSION = "0.4.2"
VERSION = "0.5.0"
DEFAULT_TASK_NAME = "default"

# Task separation symbol used in command line.
TASK_SEPARATOR = "@"
Expand All @@ -15,7 +16,7 @@ module Sam
@@root_namespace
end

def self.namespace(name : String)
def self.namespace(name : String, &)
n = @@root_namespace.touch_namespace(name)
with n yield
end
Expand All @@ -38,9 +39,10 @@ module Sam
end

def self.help
return puts "Hm, nothing to do..." if ARGV.empty?
return process_tasks(ARGV.clone) unless ARGV.empty?
return invoke(DEFAULT_TASK_NAME) if find(DEFAULT_TASK_NAME)

process_tasks(ARGV.clone)
puts "Hm, nothing to do..."
rescue e : NotFound
puts e.message
exit 1
Expand All @@ -51,7 +53,7 @@ module Sam

# :nodoc:
def self.process_tasks(args)
while (definition = read_task(args))
while definition = read_task(args)
invoke(*definition)
end
end
Expand All @@ -66,3 +68,9 @@ module Sam
{task, task_args}
end
end

include Sam::DSL

at_exit do
Sam.help
end
4 changes: 1 addition & 3 deletions src/sam/dsl.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Sam
# Task definition DSL. Delegates all calls to `Sam` itself.
module DSL
def namespace(name : String)
def namespace(name : String, &)
namespace = Sam.root_namespace.touch_namespace(name)
with namespace yield
end
Expand Down Expand Up @@ -40,5 +40,3 @@ module Sam
end
end
end

include Sam::DSL
11 changes: 4 additions & 7 deletions src/sam/namespace.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ module Sam
end

def path
if @parent
@parent.not_nil!.path + @name + ":"
else
# this is a root namespace
""
end
return "" if @parent.nil? # NOTE: this is a root namespace

@parent.as(Namespace).path + @name + ":"
end

# Sets description to the next defined task.
Expand All @@ -24,7 +21,7 @@ module Sam
end

# Defines nested namespace.
def namespace(name)
def namespace(name, &)
with touch_namespace(name) yield
@namespaces[name]
end
Expand Down
4 changes: 2 additions & 2 deletions src/sam/task.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ module Sam
end
end

def find(name : String)
@parent.find(name)
def find(path : String)
@parent.find(path)
end
end
end
2 changes: 1 addition & 1 deletion src/sam/tasks.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ end
namespace "generate" do
desc "Generates makefile extension. Now command could be executed via `make sam your:command argument`"
task "makefile" do |_, args|
sam_file_path = args.raw.size == 1 ? args.raw[0].as(String) : "src/sam.cr"
sam_file_path = args.raw.size == 1 ? args.raw[0].as(String) : "sam.cr"
Sam::Makefile.new(sam_file_path).generate
end
end

0 comments on commit d405924

Please sign in to comment.