From b3f9e2e7a133b5506da8995b1e41bef525d2c250 Mon Sep 17 00:00:00 2001 From: DannyB Date: Sat, 7 May 2016 05:34:42 +0000 Subject: [PATCH 1/3] merge Runfile::Exec into Runfile --- examples/r_exec/Runfile | 20 +++++++---------- lib/runfile.rb | 4 ++-- lib/runfile/dsl.rb | 32 +++++++++++++++++++++++++++ lib/runfile/{extensions => }/exec.rb | 33 +++++++++++++--------------- lib/runfile/setup.rb | 9 ++++++++ 5 files changed, 66 insertions(+), 32 deletions(-) rename lib/runfile/{extensions => }/exec.rb (71%) create mode 100644 lib/runfile/setup.rb diff --git a/examples/r_exec/Runfile b/examples/r_exec/Runfile index fe620ba..3a73c6e 100644 --- a/examples/r_exec/Runfile +++ b/examples/r_exec/Runfile @@ -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 @@ -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 @@ -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 diff --git a/lib/runfile.rb b/lib/runfile.rb index 5858120..0fcba61 100644 --- a/lib/runfile.rb +++ b/lib/runfile.rb @@ -1,9 +1,9 @@ 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' \ No newline at end of file +require 'runfile/exec' diff --git a/lib/runfile/dsl.rb b/lib/runfile/dsl.rb index e4ecb32..a5c93e3 100644 --- a/lib/runfile/dsl.rb +++ b/lib/runfile/dsl.rb @@ -55,6 +55,38 @@ def call(command_string) Runner.instance.cross_call command_string end + # Run a command, wait until it is done and continue + def run(*args) + ExecHandler.instance.run *args + end + + # Run a command, wait until it is done, then exit + 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 + 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' + def stop_bg(*args) + ExecHandler.instance.stop_bg *args + end + + # Set a block to be called before each run + def before_run(&block) + ExecHandler.instance.before_run &block + end + + # Set a block to be called after each run + 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 diff --git a/lib/runfile/extensions/exec.rb b/lib/runfile/exec.rb similarity index 71% rename from lib/runfile/extensions/exec.rb rename to lib/runfile/exec.rb index 3ea0aed..606f264 100644 --- a/lib/runfile/extensions/exec.rb +++ b/lib/runfile/exec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -66,8 +62,10 @@ 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) @@ -75,5 +73,4 @@ def pidfile(pid) end end - -end +end \ No newline at end of file diff --git a/lib/runfile/setup.rb b/lib/runfile/setup.rb new file mode 100644 index 0000000..e40a0d1 --- /dev/null +++ b/lib/runfile/setup.rb @@ -0,0 +1,9 @@ +module Runfile + class << self + attr_accessor :pid_dir, :quiet + + def setup + yield self + end + end +end \ No newline at end of file From a90f71f83c961ea6bf6c11aa4fb9de270348ba13 Mon Sep 17 00:00:00 2001 From: DannyB Date: Sat, 7 May 2016 05:55:15 +0000 Subject: [PATCH 2/3] exec: add deprecation warning on include --- lib/runfile.rb | 1 + lib/runfile/deprecations.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 lib/runfile/deprecations.rb diff --git a/lib/runfile.rb b/lib/runfile.rb index 0fcba61..9825eb6 100644 --- a/lib/runfile.rb +++ b/lib/runfile.rb @@ -7,3 +7,4 @@ require 'runfile/runner' require 'runfile/dsl' require 'runfile/exec' +require 'runfile/deprecations' diff --git a/lib/runfile/deprecations.rb b/lib/runfile/deprecations.rb new file mode 100644 index 0000000..081aad3 --- /dev/null +++ b/lib/runfile/deprecations.rb @@ -0,0 +1,12 @@ +module Runfile + + 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 \ No newline at end of file From cff6edcafe5240343626d0a627582445c7d8c00f Mon Sep 17 00:00:00 2001 From: DannyB Date: Sat, 7 May 2016 06:26:24 +0000 Subject: [PATCH 3/3] docs: improve documentation on dsl --- .gitignore | 1 + Runfile | 6 ++++-- lib/runfile/deprecations.rb | 2 ++ lib/runfile/dsl.rb | 30 ++++++++++++++++++++++++++++-- lib/runfile/setup.rb | 12 +++++++++++- 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 3c5944c..c6cecae 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ doc gems Gemfile.lock *.gem +.yardoc \ No newline at end of file diff --git a/Runfile b/Runfile index 70cee78..f7442f4 100644 --- a/Runfile +++ b/Runfile @@ -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 diff --git a/lib/runfile/deprecations.rb b/lib/runfile/deprecations.rb index 081aad3..b0363d8 100644 --- a/lib/runfile/deprecations.rb +++ b/lib/runfile/deprecations.rb @@ -1,5 +1,7 @@ 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:" diff --git a/lib/runfile/dsl.rb b/lib/runfile/dsl.rb index a5c93e3..18c8e0b 100644 --- a/lib/runfile/dsl.rb +++ b/lib/runfile/dsl.rb @@ -2,87 +2,113 @@ # 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 + # 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 diff --git a/lib/runfile/setup.rb b/lib/runfile/setup.rb index e40a0d1..39ff6d5 100644 --- a/lib/runfile/setup.rb +++ b/lib/runfile/setup.rb @@ -1,7 +1,17 @@ module Runfile class << self - attr_accessor :pid_dir, :quiet + # 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