Skip to content

Commit

Permalink
As a developer, I want to be able to perform tasks using the framework.
Browse files Browse the repository at this point in the history
- 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
eliasjpr committed Oct 21, 2017
1 parent 126b003 commit a9bcd33
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 0 deletions.
36 changes: 36 additions & 0 deletions spec/amber/cli/commands/perform_spec.cr
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
61 changes: 61 additions & 0 deletions spec/amber/tasks/runner_spec.cr
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 find ,valid 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
29 changes: 29 additions & 0 deletions src/amber/cli/commands/perform.cr
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
38 changes: 38 additions & 0 deletions src/amber/tasks/runner.cr
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
20 changes: 20 additions & 0 deletions src/amber/tasks/task.cr
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

0 comments on commit a9bcd33

Please sign in to comment.