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

Merge Runfile::Exec into Runfile #13

Merged
merged 3 commits into from
May 7, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ doc
gems
Gemfile.lock
*.gem
.yardoc
6 changes: 4 additions & 2 deletions Runfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ action :pretest do
File.rename "#{Dir.home}/runfile_", "#{Dir.home}/runfile"
say "Global runfiles !txtgrn!enabled"
end

end


help "Run YARD server"
action :yard
run "yard server -p3000 -B0.0.0.0 -r"
end
20 changes: 8 additions & 12 deletions examples/r_exec/Runfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
include Runfile::Exec

# this example uses the Runfile::Exec extension which provides
# additional commands such as 'run' and 'run_bg' for executing external
# commands
# this example uses the run* utility commands to run external commands
#
# run 'run -h' to see how it works

Expand All @@ -19,9 +15,9 @@ action :pid do
end

action :piddir do
Exec.pid_dir = 'tmp'
Runfile.pid_dir = 'tmp'
run_bg "ls", pid: 'pidfile'
Exec.pid_dir = nil # just reset to default for other tests
Runfile.pid_dir = nil # just reset to default for other tests
end

action :log do
Expand All @@ -42,18 +38,18 @@ action :error do
end

action :quiet do
Exec.quiet = true
Runfile.quiet = true
run "echo quietly"
Exec.quiet = false
Runfile.quiet = false
end

action :'block-config' do
Exec.setup do |config|
Runfile.setup do |config|
config.pid_dir = 'what-a-lovely-folder'
end
say "pid_dir = #{Exec.pid_dir}"
say "pid_dir = #{Runfile.pid_dir}"

Exec.pid_dir = nil # just reset to default for other tests
Runfile.pid_dir = nil # just reset to default for other tests
end

# Optional Hooks
Expand Down
5 changes: 3 additions & 2 deletions lib/runfile.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require 'runfile/version'
require 'runfile/settings'
require 'runfile/setup'
require 'runfile/docopt_helper'
require 'runfile/runfile_helper'
require 'runfile/action'
require 'runfile/runner'
require 'runfile/dsl'

require 'runfile/extensions/exec'
require 'runfile/exec'
require 'runfile/deprecations'
14 changes: 14 additions & 0 deletions lib/runfile/deprecations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Runfile

# The Runfile::Exec module is derecated. It is kept here so that those
# who include it in the past will know what to do.
module Exec
def self.included(base)
say! "!txtred!Runfile::Exec is deprecated. You should change your Runfile:"
say! "!txtred! 1. There is no need to include Runfile::Exec, it is already included."
say! "!txtred! 2. Change any configuration from Runfile::Exec.pid_dir to Runfile.pid_dir"
abort
end
end

end
60 changes: 59 additions & 1 deletion lib/runfile/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,117 @@
# All commands are immediately handed over to the Runner instance
# for handling.

# Smells of :reek:UtilityFunction
module Runfile
# Set the name of your Runfile program
# name 'My Runfile'
def name(name)
Runner.instance.name = name
end

# Set the version of your Runfile program
# version '0.1.0'
def version(ver)
Runner.instance.version = ver
end

# Set the one line summary of your Runfile program
# summary 'Utilities for my server'
def summary(text)
Runner.instance.summary = text
end

# Set the usage pattern for the next action
# usage 'server [--background]'
def usage(text)
Runner.instance.last_usage = text
end

# Set the help message for the next action
# help 'Starts the server in the foreground or background'
def help(text)
Runner.instance.last_help = text
end

# Add an option/flag to the next action (can be called multiple
# times)
# option '-b --background', 'Start in the background'
def option(flag, text, scope=nil)
Runner.instance.add_option flag, text, scope
end

# Set an example command (can be called multiple times)
# example 'server --background'
def example(text)
Runner.instance.add_example text
end

# Define the action
# action :server do |args|
# run 'rails server'
# end
def action(name, altname=nil, &block)
Runner.instance.add_action name, altname, &block
end

# Define a new command namespace
# command 'server'
# # ... define actions here
# endcommand
def command(name=nil)
Runner.instance.namespace = name
end

# Cross-call another action
# call 'other_action'
def call(command_string)
Runner.instance.cross_call command_string
end

# Run a command, wait until it is done and continue
# run 'rails server'
def run(*args)
ExecHandler.instance.run *args
end

# Run a command, wait until it is done, then exit
# run! 'rails server'
def run!(*args)
ExecHandler.instance.run! *args
end

# Run a command in the background, optionally log to a log file and save
# the process ID in a pid file
# run_bg 'rails server', pid: 'rails', log: 'tmp/log.log'
def run_bg(*args)
ExecHandler.instance.run_bg *args
end

# Stop a command started with 'run_bg'. Provide the name of he pid file you
# used in 'run_bg'
# stop_bg 'rails'
def stop_bg(*args)
ExecHandler.instance.stop_bg *args
end

# Set a block to be called before each run. The block should return
# the command to run, since this is intended to let the block modify
# the command if it needs to.
# before_run do |command|
# puts "BEFORE #{command}"
# command
# end
def before_run(&block)
ExecHandler.instance.before_run &block
end

# Set a block to be called after each run
# before_run do |command|
# puts "AFTER #{command}"
# end
def after_run(&block)
ExecHandler.instance.after_run &block
end

# Also allow to use 'endcommand' instead of 'command' to end
# a command namespace definition
alias_method :endcommand, :command
Expand Down
33 changes: 15 additions & 18 deletions lib/runfile/extensions/exec.rb → lib/runfile/exec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
# This module provides methods for easily and politely run shell commands
# through a Runfile action.
# It is mainly a convenient wrapper around `system` and `exec` and it also
# adds functions for running background tasks with ease.
require 'singleton'

module Runfile
module Exec
class << self
attr_accessor :pid_dir, :quiet

def setup
yield self
end
end
# This class provides methods for easily and politely run shell commands
# through a Runfile action.
# It is mainly a convenient wrapper around `system` and `exec` and it also
# adds functions for running background tasks with ease.
class ExecHandler
include Singleton

# Run a command, wait until it is done and continue
def run(cmd)
cmd = @before_run_block.call(cmd) if @before_run_block
return false unless cmd
say "!txtgrn!> #{cmd}" unless Exec.quiet
say "!txtgrn!> #{cmd}" unless Runfile.quiet
system cmd
@after_run_block.call(cmd) if @after_run_block
end
Expand All @@ -26,7 +22,7 @@ def run(cmd)
def run!(cmd)
cmd = @before_run_block.call(cmd) if @before_run_block
return false unless cmd
say "!txtgrn!> #{cmd}" unless Exec.quiet
say "!txtgrn!> #{cmd}" unless Runfile.quiet
exec cmd
end

Expand All @@ -36,7 +32,7 @@ def run_bg(cmd, pid: nil, log: '/dev/null')
cmd = @before_run_block.call(cmd) if @before_run_block
return false unless cmd
full_cmd = "exec #{cmd} >#{log} 2>&1"
say "!txtgrn!> #{full_cmd}" unless Exec.quiet
say "!txtgrn!> #{full_cmd}" unless Runfile.quiet
process = IO.popen "exec #{cmd} >#{log} 2>&1"
File.write pidfile(pid), process.pid if pid
@after_run_block.call(cmd) if @after_run_block
Expand All @@ -52,7 +48,7 @@ def stop_bg(pid)
File.delete file
run "kill -s TERM #{pid}"
else
say "!txtred!PID file not found." unless Exec.quiet
say "!txtred!PID file not found." unless Runfile.quiet
end
end

Expand All @@ -66,14 +62,15 @@ def after_run(&block)
@after_run_block = block
end

private

def pid_dir
defined?(Exec.pid_dir) ? Exec.pid_dir : nil
defined?(Runfile.pid_dir) ? Runfile.pid_dir : nil
end

def pidfile(pid)
pid_dir ? "#{pid_dir}/#{pid}.pid" : "#{pid}.pid"
end

end

end
end
19 changes: 19 additions & 0 deletions lib/runfile/setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Runfile
class << self
# Set the directory where PID files are stored when using `run_bg`
# Runfile.pid_dir = 'tmp'
attr_accessor :pid_dir

# Disable echoing of the command when using `run` or `run!`
# Runfile.quiet = true
attr_accessor :quiet

# You can also configure Runfile by providing a block:
# Runfile.setup do |config|
# config.quiet = true
# end
def setup
yield self
end
end
end