Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
imdrasil committed Feb 22, 2017
0 parents commit 18ceb30
Show file tree
Hide file tree
Showing 15 changed files with 605 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/doc/
/libs/
/lib/
/bin/
/.shards/
.idea

# Libraries don't need dependency lock
# Dependencies will be locked in application that uses them
/shard.lock
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: crystal
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Roman Kalnytskyi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
141 changes: 141 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Sam
> v0.2.0
Sam is a Make-like shard which allows to specify tasks like Ruby's Rake do using plain Crystal. It is thread safe but for now can't be used in production - to small test coverage.

## Installation

Add this to your application's `shard.yml`:

```yaml
dependencies:
sam:
github: imdrasil/sam.cr
```
## Usage
### Simple example
Create `sam.cr` file in your app root directory and paste next:
```crystal
# here you should load your app configuration if
# it will be needed to perform tasks
Sam.namespace "db" do
namespace "schema" do
task "load" do |t, args|
puts args["f1"]
t.invoke("1")
t.invoke("schema:1")
t.invoke("db:migrate")
t.invoke("db:db:migrate")
t.invoke("db:ping")
t.invoke("din:dong")
puts "------"
t.invoke("2", {"f2" => 1})
end
task "1" do
puts "1"
end
task "2", ["1", "db:migrate"] do |t, args|
puts args.named["f2"].as(Int32) + 3
end
end
namespace "db" do
task "migrate" do
puts "migrate"
end
end
task "ping" do
puts "ping"
end
end
Sam.help
```

To ran any of this task open prompt in root location and paste:

```shell
$> crystal sam.cr -- <your_task_path> [options]
```

Each tasks has own "path" which consists of namespace names and task name joined together by ":".

Also tasks can accept space separated arguments from prompt. To pass named argument (which have associated name) use next rules:

- `-name value`
- `-name "value with spaces"`
- `name=value`
- `name="value with spaces"`

Also just array of arguments can be passed - just past everything needed without any flags anywhere:
```shell
$> crystal sam.cr -- <your_task_path> first_raw_option "options with spaces"
```

All arguments from prompt will be parsed as `String`.

So to invoke first task from example ("load") will be used next command:

```shell
crystal sam.cr -- db:schema:load -f1 asd
```

To autoload Sam files from your dependencies - just past
```crystal
load_dependencies "./", "dep1", "dep2"`
```
where first argument is root location, next are names of your dependencies you want to load.

#### 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.

#### Task
To define task use `task` method with it's name and block. Given block could take 0..2 arguments: `Task` object and `Args` object. Also as second parameter could be provided array of dependent tasks which will be invoked before current.

Another task could be invoked from current using `invoke` method. It has next signatures:

-
- `name : String` - task path

-
- `name : String` - task path
- `args : Args` - prepared argument object

-
- `name : String` - task path
- `hash : Hash(String, String | Int32, Float32)` - hash with arguments

-
- `name : String` - task path
- `args : Tuple` - raw arguments

#### 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.

#### Args

This class represents argument set for task. It can handle named arguments and just raw array of arguments. Now it supports only `String`, `Int32` and `Float32` types. To get access to named argument you can use `[](name : String)` and `[]?(name : String)` methods. For raw attributes there are `[](index : Int32)` and `[]?(index : Int32)` as well.


## Development

For now major functionality is ready but to few tests were added. So major task for next release will be increasing test coverage.

## Contributing

1. [Fork it]( https://github.com/imdrasil/sam.cr/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request

## Contributors

- [imdrasil](https://github.com/[your-github-name]) Roman Kalnytskyi - creator, maintainer
37 changes: 37 additions & 0 deletions examples/sam.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "../src/sam"
load_dependencies "../", "asd"

Sam.namespace "db" do
namespace "schema" do
task "load" do |t, args|
puts args["f1"]
t.invoke("1")
t.invoke("schema:1")
t.invoke("db:migrate")
t.invoke("db:db:migrate")
t.invoke("db:ping")
t.invoke("din:dong")
puts "------"
t.invoke("2", {"f2" => 1})
end

task "1" do
puts "1"
end

task "2", ["1", "db:migrate"] do |t, args|
puts args.named["f2"].as(Int32) + 3
end
end

namespace "db" do
task "migrate" do
puts "migrate"
end
end

task "ping" do
puts "ping"
end
end
Sam.help
9 changes: 9 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: sam
version: 0.2.0

authors:
- Roman Kalnytskyi <moranibaca@gmail.com>

crystal: 0.20.5

license: MIT
29 changes: 29 additions & 0 deletions spec/args_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "./spec_helper"

describe Sam::Args do
describe "#initialize" do
context "without arguments" do
end

context "with array of string from prompt" do
end

context "with providing named and raw arguments" do
end
end

describe "#raw_arguments" do
end

describe "#named_arguments" do
end

describe "#size" do
end

describe "#[]" do
end

describe "#[]?" do
end
end
24 changes: 24 additions & 0 deletions spec/namespace_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "./spec_helper"

describe Sam::Namespace do
describe "#initialize" do
end

describe "#namespace" do
end

describe "#touch_namespace" do
end

describe "#task" do
end

describe "#namespaces" do
end

describe "#tasks" do
end

describe "#find" do
end
end
26 changes: 26 additions & 0 deletions spec/sam_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "./spec_helper"

describe Sam do
describe "::namespace" do
end

describe "::task" do
end

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
it "finds correct task by path" do
Sam.find("db:schema:load").should eq(tasks["load"])
end
end

describe "::help" do
end
end
42 changes: 42 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "spec"
require "../src/sam"

module Spec
@@tasks = {} of String => Sam::Task

def self.tasks
@@tasks
end
end

Sam.namespace "db" do
namespace "schema" do
Spec.tasks["load"] = task "load" do |t, args|
puts args["f1"]
t.invoke("1")
t.invoke("schema:1")
t.invoke("db:migrate")
t.invoke("db:db:migrate")
t.invoke("db:ping")
t.invoke("din:dong")
end

Spec.tasks["1"] = task "1" do
puts "1"
end
end

namespace "db" do
task "migrate" do
puts "migrate"
end
end

task "ping" do
puts "ping"
end
end

def tasks
Spec.tasks
end
12 changes: 12 additions & 0 deletions spec/task_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "./spec_helper"

describe Sam::Task do
describe "#initialize" do
end

describe "#call" do
end

describe "#invoke" do
end
end
Loading

0 comments on commit 18ceb30

Please sign in to comment.