Skip to content

Commit

Permalink
Merge pull request #14 from imdrasil/add-crystal-0.35-support
Browse files Browse the repository at this point in the history
Add crystal 0.35 support
  • Loading branch information
imdrasil authored Jul 2, 2020
2 parents 4d920cf + 6736e34 commit ebabed7
Show file tree
Hide file tree
Showing 18 changed files with 349 additions and 207 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
language: crystal
env:
- TERM=xterm-256color
before_script:
- crystal examples/sam.cr -- setup
- crystal examples/sam.cr setup
script: ./bin/ameba && crystal spec
30 changes: 15 additions & 15 deletions 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.3.2
version: 0.4.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 @@ -40,39 +40,35 @@ end
Sam does no magic with your `sam.cr` file - it is just a common `.cr` source file which allows you to recompile it with any possible code you want such amount of times you need. Therefore the most obvious way to execute any task is:

```shell
$ crystal sam.cr -- name
$ crystal sam.cr name
```

> `--` here means that `name` is passed as an argument to executed file not `crystal` utility.

In addition to this you are able to configure your makefile to invoke sam tasks. This allows you to use shorten variant

```shell
$ make sam name
```

> This solution still requires `--` in some cases - see the following section.

To automatically preconfigure makefile run
To automatically modify your Makefile run

```shell
$ crystal sam.cr -- generate:makefile
$ crystal sam.cr generate:makefile
```

This will modify existing Makefile or create new one. Be careful - this will silent all nonexisting makefile tasks on invocation.

To see a list of all available tasks with their descriptions:

```shell
$ crystal sam.cr -- help
$ crystal sam.cr help
```

#### Tasks with arguments

To pass arguments to your task just list them after it's name:

```shell
> crystal sam.cr -- name john rob ned
$ crystal sam.cr name john rob ned
```

They are passed to a task as a 2nd block argument.
Expand All @@ -87,25 +83,29 @@ end

> Each task has own collection of arguments; only prerequisites shares with target task same `Args` instance.

As was mentioned named argument also can be specified by the following ways:
Named argument also can be specified by the following ways:

- `-argument value`
- `-argument "value with spaces"`
- `argument=value`
- `argument="value with spaces"`

One important restriction with named arguments usage and makefile-style task invocation: `--` should be placed to explicitly specify that specified arguments belongs to compiled program not crystal compiler:
Two important restriction with named arguments usage and makefile-style task invocation:

* `--` should be placed to explicitly specify that specified named arguments belongs to task not to Makefile:

```shell
$ make sam name john
$ # but
$ make same name -- argument=john
```

More than one task can be specified (even with own arguments) - just separate them by `@` symbol:
* makefile doesn't support named arguments with `=` sign

To invoke More than one task list them one by one (including their arguments) separating them with `@` symbol:

```shell
$ crystal sam.cr -- name john @ surname argument=snow
$ crystal sam.cr name john @ surname argument=snow
```

#### Accessing tasks programmatically
Expand Down Expand Up @@ -190,7 +190,7 @@ require "./lib/lib1/tasks/sam.cr"
Before running tests call

```shell
$ crystal examples/sam.cr -- setup
$ crystal examples/sam.cr setup
```

## Contributing
Expand Down
3 changes: 2 additions & 1 deletion examples/sam.cr
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Sam.namespace "db" do
puts "1"
end

task "2", ["1", "db:migrate"] do |t, args|
task "2", ["1", "db:migrate"] do |_, args|
puts args.named["f2"].as(Int32) + 3
end
end
Expand All @@ -117,4 +117,5 @@ Sam.namespace "db" do
puts "ping"
end
end

Sam.help
9 changes: 7 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
name: sam
version: 0.3.2
version: 0.4.0

authors:
- Roman Kalnytskyi <[email protected]>

crystal: 0.30.1
crystal: 0.35.1

license: MIT

development_dependencies:
ameba:
github: crystal-ameba/ameba
version: "= 0.13.0"

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

describe Sam::Makefile do
makefile = Sam::Makefile.new("src/sam.cr")

describe "#generate" do
declaration_part = <<-FILE
# === Sam shortcut
# next lines are autogenerated and any changes will be discarded after regenerating
CRYSTAL_BIN ?= `which crystal`
SAM_PATH ?= "src/sam.cr"
.PHONY: sam
sam:
\t$(CRYSTAL_BIN) $(SAM_PATH) $(filter-out $@,$(MAKECMDGOALS))
%:
\t@:
# === Sam shortcut\n
FILE
makefile_path = "Makefile"

it "creates new makefile" do
begin
File.exists?(makefile_path).should eq(false)
makefile.generate
File.read(makefile_path).should eq(declaration_part)
ensure
File.delete(makefile_path) if File.exists?(makefile_path)
end
end

it "correctly regenerates existing file" do
begin
File.exists?(makefile_path).should eq(false)
File.write(makefile_path, "# before\n" + declaration_part + "# after\n")
makefile.generate
File.read(makefile_path).should eq("# before\n# after\n" + declaration_part)
ensure
File.delete(makefile_path) if File.exists?(makefile_path)
end
end
end
end
39 changes: 1 addition & 38 deletions spec/sam_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,50 +48,13 @@ describe Sam do

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

describe ".generate_makefile" do
desclaration_part = <<-FILE
# === Sam shortcut
# next lines are autogenerated and any changes will be discarded after regenerating
CRYSTAL_BIN ?= `which crystal`
SAM_PATH ?= "src/sam.cr"
.PHONY: sam
sam:
\t$(CRYSTAL_BIN) run $(SAM_PATH) -- $(filter-out $@,$(MAKECMDGOALS))
%:
\t@:
# === Sam shortcut\n
FILE
makefile = "Makefile"

it "creates new makefile" do
begin
File.exists?(makefile).should eq(false)
Sam.generate_makefile("src/sam.cr")
File.read(makefile).should eq(desclaration_part)
ensure
File.delete(makefile) if File.exists?(makefile)
end
end

it "correctly regenerates existing file" do
begin
File.exists?(makefile).should eq(false)
File.write(makefile, "# before\n" + desclaration_part + "# after\n")
Sam.generate_makefile("src/sam.cr")
File.read(makefile).should eq("# before\n# after\n" + desclaration_part)
ensure
File.delete(makefile) if File.exists?(makefile)
end
end
end

describe "%load_dependencies" do
context "given as splat array" do
it "properly loads tasks from dependencies" do
Expand Down
37 changes: 37 additions & 0 deletions spec/shell_table_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "./spec_helper"

describe Sam::ShellTable do
describe "#generate" do
it "generates correct table" do
fail "terminal should has width 80" if `tput cols`.to_i != 80

namespace = Sam::Namespace.new("name", nil)
Sam::ShellTable.new([
Sam::Task.new(
-> {},
%w[],
namespace,
"short_name",
"but very long description, such long that it requires multiple lines to be written"
),
Sam::Task.new(
-> {},
%w[],
namespace,
"very_long_task_name_such_long_that_it_requires_multiple_lines_to_be_written",
"and short description"
),
]).generate.should eq(
<<-TEXT
Name Description
-------------------------------------- | ---------------------------------------
short_name | but very long description, such long th
| at it requires multiple lines to be wri
| tten
very_long_task_name_such_long_that_it_ | and short description
requires_multiple_lines_to_be_written | \n
TEXT
)
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ end

Spec.before_each do
Container.clear
Sam.root_namespace.all_tasks.each(&.reenable)
end

# Tasks
Expand Down
28 changes: 15 additions & 13 deletions spec/task_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ describe Sam::Task do
end
arr.empty?.should eq(true)
end

it "invokes dependencies with arguments" do
count = 0
namespace.task("t1") { |t, args|
count += args[0].as(Int32)
count += args["count"].as(Int32) }
namespace.task("t2", ["t1"]) { |t, args|
count += 1}.call(Sam::Args.new({"count" => 1}, [1]))
namespace.task("t1") do |_, args|
count += args[0].as(Int32)
count += args["count"].as(Int32)
end
namespace.task("t2", ["t1"]) { count += 1 }
.call(Sam::Args.new({"count" => 1}, [1]))
count.should eq(3)
end
end
Expand Down Expand Up @@ -99,28 +101,28 @@ describe Sam::Task do

it "accepts tuple at the end" do
count = 0
namespace.task("t1") { |t, args| count += args[0].as(Int32) }
namespace.task("t1") { |_, args| count += args[0].as(Int32) }
namespace.task("t2") { |t| t.invoke("t1", 1) }.call(empty_args)
count.should eq(1)
end

it "accepts hash" do
count = 0
namespace.task("t1") { |t, args| count += args["count"].as(Int32) }
namespace.task("t1") { |_, args| count += args["count"].as(Int32) }
namespace.task("t2") { |t| t.invoke("t1", {"count" => 2}) }.call(empty_args)
count.should eq(2)
end

it "accepts arg object" do
count = 0
namespace.task("t1") { |t, args| count += args["count"].as(Int32) }
namespace.task("t1") { |_, args| count += args["count"].as(Int32) }
namespace.task("t2") { |t, args| t.invoke("t1", args) }.call(Sam::Args.new({"count" => 2}))
count.should eq(2)
end

it "accepts hash and array" do
count = 0
namespace.task("t1") { |t, args| count += args["count"].as(Int32) + args[0].as(Int32) }
namespace.task("t1") { |_, args| count += args["count"].as(Int32) + args[0].as(Int32) }
namespace.task("t2") { |t| t.invoke("t1", {"count" => 2}, [1]) }.call(empty_args)
count.should eq(3)
end
Expand All @@ -143,28 +145,28 @@ describe Sam::Task do

it "accepts tuple at the end" do
count = 0
namespace.task("t1") { |t, args| count += args[0].as(Int32) }
namespace.task("t1") { |_, args| count += args[0].as(Int32) }
namespace.task("t2") { |t| t.execute("t1", 1) }.call(empty_args)
count.should eq(1)
end

it "accepts hash" do
count = 0
namespace.task("t1") { |t, args| count += args["count"].as(Int32) }
namespace.task("t1") { |_, args| count += args["count"].as(Int32) }
namespace.task("t2") { |t| t.execute("t1", {"count" => 2}) }.call(empty_args)
count.should eq(2)
end

it "accepts arg object" do
count = 0
namespace.task("t1") { |t, args| count += args["count"].as(Int32) }
namespace.task("t1") { |_, args| count += args["count"].as(Int32) }
namespace.task("t2") { |t, args| t.execute("t1", args) }.call(Sam::Args.new({"count" => 2}))
count.should eq(2)
end

it "accepts hash and array" do
count = 0
namespace.task("t1") { |t, args| count += args["count"].as(Int32) + args[0].as(Int32) }
namespace.task("t1") { |_, args| count += args["count"].as(Int32) + args[0].as(Int32) }
namespace.task("t2") { |t| t.execute("t1", {"count" => 2}, [1]) }.call(empty_args)
count.should eq(3)
end
Expand Down
Loading

0 comments on commit ebabed7

Please sign in to comment.