Skip to content

Commit

Permalink
Merge pull request #2 from imdrasil/add_makefile_support
Browse files Browse the repository at this point in the history
Add makefile support
  • Loading branch information
imdrasil authored Jun 21, 2017
2 parents 6996180 + 790a973 commit 009e301
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 4 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Sam.namespace "db" do
end
namespace "db" do
task "schema" do
puts "same as namespace"
end
task "migrate" do
puts "migrate"
end
Expand Down Expand Up @@ -91,6 +95,30 @@ So to invoke first task from example ("load") will be used next command:
crystal sam.cr -- db:schema:load -f1 asd
```

Makefile-like usage is supported. To autogenerate receipt just call

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

This will allow to call tasks in the next way:

```shell
$ make sam some:task raw_arg1
```

But for named argument you need to add `--`

```shell
$ make sam db:shema:load -- -f1 asd
```

By default it will try to use your samfile in the app root. To override it pass proper way as second argument

```shell
$ crystal src/sam.cr -- generate:makefile "src/sam.cr"
```

To autoload Sam files from your dependencies - just past
```crystal
load_dependencies "dep1", "dep2"`
Expand Down Expand Up @@ -122,7 +150,7 @@ Another task could be invoked from current using `invoke` method. It has next si

#### Routing

When task is invoked from other one provided path will float up through current task namespace nesting and search given path on each level.
When task is invoked from other one provided path will float up through current task namespace nesting and search given path on each level. Task could have same name as any existing namespace.

#### Args

Expand Down
40 changes: 40 additions & 0 deletions spec/sam_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,44 @@ describe Sam do

describe "::help" do
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))
# === 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

it "includes all default tasks" do
Sam.find!("help")
Sam.find!("generate:makefile")
end
end
5 changes: 5 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ Sam.namespace "db" do
t.invoke("db:db:migrate")
t.invoke("db:ping")
t.invoke("din:dong")
t.invoke("schema")
end

task "1" do
puts "1"
end
end

task "schema" do
puts "same as namespace"
end

namespace "db" do
task "migrate" do
puts "migrate"
Expand Down
7 changes: 6 additions & 1 deletion spec/task_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ describe Sam::Task do
describe "#call" do
end

describe "#invoke" do
describe "#find!" do
it "properly invokes task with same name as parent namespace" do
task = Sam.find!("db:schema:load")
task = task.find!("schema")
task.path.should eq("db:schema")
end
end
end
41 changes: 41 additions & 0 deletions src/sam.cr
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,36 @@ module Sam
puts pathes[i].ljust(max_length + 5) + task.description
end
end

def self.generate_makefile(sam_path)
declaration = <<-MAKEFILE
# === Sam shortcut
# next lines are autogenerated and any changes will be discarded after regenerating
CRYSTAL_BIN ?= `which crystal`
SAM_PATH ?= "#{sam_path}"
.PHONY: sam
sam:
\t$(CRYSTAL_BIN) run $(SAM_PATH) -- $(filter-out $@,$(MAKECMDGOALS))
# === Sam shortcut\n
MAKEFILE
file_path = File.join("../", "Makefile")
if File.exists?(file_path)
ignore = false
content = String.build do |io|
File.read_lines(file_path).each do |line|
if line.starts_with?("# === Sam shortcut")
ignore = !ignore
next
end
io << line << "\n" unless ignore
end
io << declaration
end
File.write(file_path, content)
else
File.write(file_path, declaration)
end
end
end

macro load_dependencies(*libraries)
Expand All @@ -88,3 +118,14 @@ Sam.desc("Prints description for all tasks")
Sam.task "help" do
Sam.pretty_print
end

Sam.namespace "generate" do
desc "Generates makefile extension. Now command could be executed via `make sam your:command argument`"
task "makefile" do |t, args|
if args.raw.size == 1
Sam.generate_makefile(args.raw[0].as(String))
else
Sam.generate_makefile("src/sam.cr")
end
end
end
2 changes: 1 addition & 1 deletion src/sam/args.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Sam
class Args
alias ALLOWED_HASH = Hash(String, ALLOWED_TYPES)
alias ALLOWED_TYPES = String | Int32 | Float32
alias ALLOWED_TYPES = String | Int32 | Float64

@arr = [] of ALLOWED_TYPES
@named_args = {} of String => ALLOWED_TYPES
Expand Down
2 changes: 1 addition & 1 deletion src/sam/task.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module Sam
t.not_nil!.call(Args.new(Args::ALLOWED_HASH.new, args.to_a))
end

private def find!(name)
def find!(name)
t = @parent.find(name)
raise "Task #{name} was not found" unless t
t.not_nil!
Expand Down

0 comments on commit 009e301

Please sign in to comment.