Skip to content

Commit

Permalink
Don't require a schedule file when clearing. Fix #367
Browse files Browse the repository at this point in the history
  • Loading branch information
javan committed Jul 23, 2013
1 parent 1d853e7 commit fc67cd5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
40 changes: 20 additions & 20 deletions lib/whenever/command_line.rb
Expand Up @@ -6,15 +6,15 @@ class CommandLine
def self.execute(options={})
new(options).run
end

def initialize(options={})
@options = options

@options[:file] ||= 'config/schedule.rb'
@options[:cut] ||= 0
@options[:identifier] ||= default_identifier
unless File.exists?(@options[:file])

if !File.exists?(@options[:file]) && @options[:clear].nil?
warn("[fail] Can't find file: #{@options[:file]}")
exit(1)
end
Expand All @@ -30,7 +30,7 @@ def initialize(options={})
end
@options[:cut] = @options[:cut].to_i
end

def run
if @options[:update] || @options[:clear]
write_crontab(updated_crontab)
Expand All @@ -43,28 +43,28 @@ def run
exit(0)
end
end

protected

def default_identifier
File.expand_path(@options[:file])
end

def whenever_cron
return '' if @options[:clear]
@whenever_cron ||= [comment_open, Whenever.cron(@options), comment_close].compact.join("\n") + "\n"
end

def read_crontab
return @current_crontab if @current_crontab

command = ['crontab -l']
command << "-u #{@options[:user]}" if @options[:user]

command_results = %x[#{command.join(' ')} 2> /dev/null]
@current_crontab = $?.exitstatus.zero? ? prepare(command_results) : ''
end

def write_crontab(contents)
tmp_cron_file = Tempfile.open('whenever_tmp_cron')
tmp_cron_file << contents
Expand All @@ -86,8 +86,8 @@ def write_crontab(contents)
exit(1)
end
end
def updated_crontab

def updated_crontab
# Check for unopened or unclosed identifier blocks
if read_crontab =~ Regexp.new("^#{comment_open}\s*$") && (read_crontab =~ Regexp.new("^#{comment_close}\s*$")).nil?
warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'"
Expand All @@ -96,7 +96,7 @@ def updated_crontab
warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'"
exit(1)
end

# If an existing identier block is found, replace it with the new cron entries
if read_crontab =~ Regexp.new("^#{comment_open}\s*$") && read_crontab =~ Regexp.new("^#{comment_close}\s*$")
# If the existing crontab file contains backslashes they get lost going through gsub.
Expand All @@ -106,27 +106,27 @@ def updated_crontab
[read_crontab, whenever_cron].join("\n\n")
end.gsub(/\n{3,}/, "\n\n") # More than two newlines becomes just two.
end

def prepare(contents)
# Strip n lines from the top of the file as specified by the :cut option.
# Use split with a -1 limit option to ensure the join is able to rebuild
# the file with all of the original seperators in-tact.
stripped_contents = contents.split($/,-1)[@options[:cut]..-1].join($/)

# Some cron implementations require all non-comment lines to be newline-
# terminated. (issue #95) Strip all newlines and replace with the default
# terminated. (issue #95) Strip all newlines and replace with the default
# platform record seperator ($/)
stripped_contents.gsub!(/\s+$/, $/)
end

def comment_base
"Whenever generated tasks for: #{@options[:identifier]}"
end

def comment_open
"# Begin #{comment_base}"
end

def comment_close
"# End #{comment_base}"
end
Expand Down
12 changes: 12 additions & 0 deletions test/functional/command_line_test.rb
Expand Up @@ -161,6 +161,18 @@ class CommandLineTest < Test::Unit::TestCase
end
end

context "A command line clear with no schedule file" do
setup do
File.expects(:exists?).with('config/schedule.rb').returns(false)
@command = Whenever::CommandLine.new(:clear => true, :identifier => 'My identifier')
end

should "run successfully" do
@command.expects(:write_crontab).returns(true)
assert @command.run
end
end

context "A command line update with no identifier" do
setup do
File.expects(:exists?).with('config/schedule.rb').returns(true)
Expand Down

0 comments on commit fc67cd5

Please sign in to comment.