Skip to content

Commit

Permalink
Runner added, more roadmap TODO added
Browse files Browse the repository at this point in the history
  • Loading branch information
molnarmark committed Sep 8, 2018
1 parent 59a2c4c commit 9112364
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 68 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -9,6 +9,8 @@
## Roadmap

- [x] Add an override for `Lupin.task` to create `Lupin.src` tasks
- [ ] Add debug parameter to task creation which will `pp` all pipe outputs
- [ ] Create a LupinInputFile type that holds the file path and the file content
- [x] Add a `Lupin._debug` method to encapsulate all if DEBUG statements
- [ ] Implement `Lupin.dist`
- [ ] Implement `Lupin.watch`
Expand Down
Binary file modified assets/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions shard.lock
@@ -1,5 +1,9 @@
version: 1.0
shards:
bargs:
github: crystalrealm/bargs
version: 1.0.0

crayon:
github: crystalrealm/crayon
commit: 5486ebfeca12dd89e17ed3f96317f34990f98e22
Expand Down
2 changes: 2 additions & 0 deletions shard.yml
Expand Up @@ -11,6 +11,8 @@ targets:
dependencies:
epilog:
github: crystalrealm/epilog
bargs:
github: crystalrealm/bargs

crystal: 0.26.0

Expand Down
76 changes: 36 additions & 40 deletions src/lupin.cr
@@ -1,5 +1,3 @@
# TODO: Write documentation for `Lupin`

require "./lupin/*"
require "./lupin/plugins/*"
require "epilog"
Expand All @@ -16,29 +14,52 @@ module Lupin
task = Lupin::Task.new(name, params.at(0))
@@tasks.push(task.as(Lupin::Task))

self._debug("Task '#{name}' created.")
self.debug("Task '#{name}' created.")

task
end

# Override for tasks that do not start from nil
# This includes: commands, directory structures (files)
def self.task(name : String, param : String, mode = "w")
mode = self._get_mode(param)
mode = self.get_mode(param)
task = self.task(name, {"mode" => mode})

if mode == "directory"
task.pipable = self.src(param, mode)
self._debug("Detected directory: #{param}")
task.pipe = self.src(param, mode)
self.debug("Detected directory: #{param}")
elsif mode == "command"
task.pipable = self.command(param)
task.pipe = self.command(param)
end

task
end

# Run a task
def self.run(name : String)
@@tasks.each do |task|
if name == task.name
@@logger.info("Running Task '#{name}'..")
task.run
@@logger.success("Task '#{name}' finished successfully.")
end
end
end

# Override to run multiple tasks
# To be used for default tasks.
def self.run(tasks : Array)
tasks.each do |task|
self.run(task)
end
end

def self.set_debug(debug)
@@debug = debug
end

# Determines the task mode by the given param
def self._get_mode(param)
private def self.get_mode(param)
if param.includes?("/")
"directory"
elsif param.includes?(" ")
Expand All @@ -49,57 +70,32 @@ module Lupin
end

# Debugging utility
def self._debug(message)
private def self.debug(message)
if @@debug
@@logger.debug message
end
end

def self.set_debug(debug)
@@debug = debug
end

# Load files with the given mode, according to the given path
def self.src(path, mode)
private def self.src(path, mode)
files = Dir.glob(path).map do |file_path|
File.open(file_path, "w+")
File.read(file_path)
end

Lupin::Pipable.new files
Lupin::Pipe.new files
end

# Process and execute raw shell commands
def self.command(command)
private def self.command(command)
# TODO allow multiple commands to be chained with &&
command_args = command.split(" ")
command_name = command_args.delete_at(0)
status = Process.run(command_name, args: command_args)
Lupin::Pipable.new status.exit_code
end

# Run a task
def self.run(name : String)
@@tasks.each do |task|
if name == task.name
@@logger.info("Running Task '#{name}'..")
task.run
@@logger.success("Task '#{name}' finished successfully.")
end
end
end

# Override to run multiple tasks
# To be used for default tasks.
def self.run(tasks : Array)
tasks.each do |task|
self.run(task)
end
Lupin::Pipe.new status.exit_code
end
end

Lupin.set_debug true

# Don't run this with crystal run. Run crystal build src/lupin.cr - bin/lupin first and use the binary.
Lupin.task("command", "crystal -v").pipe(Lupin::PP.new)

Lupin.task("command", "echo test")
Lupin.run("command")
4 changes: 2 additions & 2 deletions src/lupin/pipable.cr → src/lupin/pipe.cr
@@ -1,12 +1,12 @@
module Lupin
class Pipable(T)
class Pipe(T)
getter value

def initialize(@value : T)
end

def pipe(plugin : Lupin::Plugin)
plugin.exec(@value.as(T))
plugin.run(@value.as(T))
end
end
end
2 changes: 1 addition & 1 deletion src/lupin/plugin.cr
@@ -1,6 +1,6 @@
module Lupin
abstract class Plugin
def exec
def run
end

def on(event_type : String)
Expand Down
10 changes: 3 additions & 7 deletions src/lupin/plugins/hello_world.cr
Expand Up @@ -2,13 +2,9 @@

module Lupin::Plugins
class HelloWorld < Lupin::Plugin
def exec(files)
files.as(Array(File)).each do |file|
File.write(file.path, "Hello World.")
file.close
end

files
# TODO
def run(data)
true
end
end
end
8 changes: 0 additions & 8 deletions src/lupin/pretty_print.cr

This file was deleted.

33 changes: 23 additions & 10 deletions src/lupin/task.cr
@@ -1,37 +1,50 @@
module Lupin
class Task
getter name
setter pipable
setter pipe

def initialize(@name : String, *params)
@pipe_classes = [] of Lupin::Plugin

# Task mode can be the following:
# default: Task starts out with nil
# directory: Task starts out with an array of files according to the given path
# command: Task starts out with the given command being executed and its exit code returned
# command: Task starts out with the given command being runuted and its exit code returned
task_mode = "default"
debug_task = false

if params.size > 0
task_mode = params.at(0).fetch("mode")
params_hash = params.at(0)
task_mode = params_hash.fetch("mode")

# TODO
# if params_hash.has?("debug")
# debug_task = true
# end
end

if task_mode == "directory"
@pipable = Lupin::Pipable(Array(File)).new([] of File)
@pipe = Lupin::Pipe(Array(String)).new([] of String)
elsif task_mode == "command"
@pipable = Lupin::Pipable(Int32).new(0)
@pipe = Lupin::Pipe(Int32).new(0)
else
@pipable = Lupin::Pipable(Nil).new(nil)
@pipe = Lupin::Pipe(Nil).new(nil)
end
@logger = Epilog::Logger.new
end

def run
previous_value = @pipable.value
previous_value = @pipe.value
@pipe_classes.each do |instance|
instance.on("pre_execution")
previous_value = instance.exec(previous_value)
instance.on("after_execution")
instance.on("pre_runution")
previous_value = instance.run(previous_value)

# TODO
# if debug_task
# pp previous_value
# end

instance.on("after_runution")
if previous_value.is_a?(Nil)
@logger.error("Pipe '#{instance.class.name}' failed for task '#{@name}'")
Process.exit(1)
Expand Down
3 changes: 3 additions & 0 deletions src/runner/task_runner.cr
@@ -0,0 +1,3 @@
require "../lupin"
require "../../../../lupinfile.cr"
Lupin.run(ARGV[0])

0 comments on commit 9112364

Please sign in to comment.