Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Amber Task Runner #318

Merged
merged 1 commit into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 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
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