-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As a developer, I want to be able to perform tasks using the framework.
- Adds Perform command to execute tasks - Tasks should be inherited from **Amber::Tasks::Task** - The task should declare a `perform` method. This is the method that gets called.
- Loading branch information
Showing
5 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require "../../../spec_helper" | ||
require "../../tasks/**" | ||
|
||
class FakeTask < Amber::Tasks::Task | ||
def description | ||
"Fake command task" | ||
end | ||
|
||
def perform | ||
"Fake task completed!" | ||
end | ||
end | ||
|
||
module Amber::CLI | ||
describe MainCommand::Perform do | ||
it "performs a tasks via command line" do | ||
runner = MainCommand.run ["perform", "faketask"] | ||
|
||
runner.should eq "Fake task completed!" | ||
end | ||
|
||
it "performs a tasks via command line alias" do | ||
runner = MainCommand.run ["p", "faketask"] | ||
|
||
runner.should eq "Fake task completed!" | ||
end | ||
|
||
it "shows task description" do | ||
runner = MainCommand.run ["p", "-l", "faketask"] | ||
|
||
runner.should eq( | ||
%(FirstFakeTask\t\t #First fake task\nSecond::FakeTask\t\t #Second fake task\nFakeTask\t\t #Fake command task\n) | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require "../../spec_helper" | ||
require "../../../src/amber/tasks/**" | ||
|
||
class FirstFakeTask < Amber::Tasks::Task | ||
def description | ||
"First fake task" | ||
end | ||
|
||
def perform | ||
"First Fake Task" | ||
end | ||
end | ||
|
||
class Second::FakeTask < Amber::Tasks::Task | ||
def perform | ||
"Second Fake Task" | ||
end | ||
|
||
def description | ||
"Second fake task" | ||
end | ||
end | ||
|
||
module Amber::Tasks | ||
describe Runner do | ||
describe "#tasks" do | ||
it "returns a list of defined tasks" do | ||
Runner.tasks.should be_a Array(Task) | ||
Runner.tasks.size.should eq 2 | ||
end | ||
end | ||
|
||
describe "#definitions" do | ||
it "outputs tasks information" do | ||
Runner.definitions.should eq( | ||
%(FirstFakeTask\t\t #First fake task\nSecond::FakeTask\t\t #Second fake task\n) | ||
) | ||
end | ||
end | ||
|
||
describe "#perform" do | ||
it "executes the tasks operation" do | ||
runner = Runner.new("firstfaketask") | ||
|
||
runner.perform.should eq "First Fake Task" | ||
end | ||
|
||
it "does not find invalid task name" do | ||
runner = Runner.new("invalid_task") | ||
|
||
runner.perform.should be_nil | ||
end | ||
|
||
it "performs namespaced task" do | ||
runner = Runner.new("second::faketask") | ||
|
||
runner.perform.should eq "Second Fake Task" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require "icr" | ||
require "cli" | ||
|
||
module Amber::CLI | ||
class MainCommand < ::Cli::Supercommand | ||
command "p", aliased: "perform" | ||
|
||
class Perform < ::Cli::Command | ||
command_name "perform" | ||
|
||
class Options | ||
bool ["-l", "--list"], desc: "# Displays all tasks available", default: false | ||
arg "task", desc: "Name of the task to execute", required: true | ||
end | ||
|
||
class Help | ||
caption "# It run and performs tasks within the amber application scope" | ||
end | ||
|
||
def run | ||
if options.l? | ||
Amber::Tasks::Runner.definitions | ||
else | ||
Amber::Tasks::Runner.perform(args.task) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module Amber::Tasks | ||
class Runner | ||
class_property tasks = [] of Amber::Tasks::Task | ||
getter task : String | ||
|
||
def self.perform(task) | ||
new(task).perform | ||
end | ||
|
||
def self.definitions | ||
String.build do |str| | ||
tasks.each do |task| | ||
str << task.class.name | ||
str << "\t\t #" | ||
str << task.description | ||
str << "\n" | ||
end | ||
end | ||
end | ||
|
||
def initialize(@task : String) | ||
end | ||
|
||
def tasks | ||
self.class.tasks | ||
end | ||
|
||
def perform | ||
find(task).perform | ||
end | ||
|
||
private def find(lookup_task) | ||
self.class.tasks.find(Amber::Tasks::NullTask.new) do |task| | ||
task.class.name.downcase == lookup_task.downcase | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Amber::Tasks | ||
abstract class Task | ||
macro inherited | ||
Amber::Tasks::Runner.tasks << self.new | ||
end | ||
|
||
abstract def description | ||
abstract def perform | ||
end | ||
|
||
class NullTask | ||
def description | ||
"No Description" | ||
end | ||
|
||
def perform | ||
nil | ||
end | ||
end | ||
end |