auto_cron 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
data/auto_cron.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{auto_cron}
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alex Farrill"]
@@ -1,9 +1,8 @@
1
1
  require 'auto_cron'
2
2
  require 'rails'
3
- module AutoCron
4
- class Railtie < Rails::Railtie
5
- rake_tasks do
6
- load "tasks/auto_cron_tasks.rake"
7
- end
3
+
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ load "tasks/auto_cron_tasks.rake"
8
7
  end
9
- end
8
+ end
data/lib/auto_cron.rb CHANGED
@@ -1,69 +1,67 @@
1
1
  require 'auto_cron/railtie' if defined?(Rails)
2
2
 
3
- module AutoCron
4
- class AutoCron
5
- def initialize( template, application )
6
- require 'erb'
3
+ class AutoCron
4
+ def initialize( template, application )
5
+ require 'erb'
7
6
 
8
- @auto_cron_dir = File.join( Rails.root, "config", "auto_cron" )
9
- @template = template
10
- @application = application
11
- end
12
-
13
- def updated_crontab
14
- # Check for unopened or unclosed identifier blocks
15
- if read_crontab.index(comment_open) && !read_crontab.index(comment_close)
16
- warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'"
17
- exit(1)
18
- elsif !read_crontab.index(comment_open) && read_crontab.index(comment_close)
19
- warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'"
20
- exit(1)
21
- end
22
-
23
- # If an existing identier block is found, replace it with the new cron entries
24
- if read_crontab.index(comment_open) && read_crontab.index(comment_close)
25
- read_crontab.gsub(Regexp.new("#{comment_open}.+#{comment_close}", Regexp::MULTILINE), auto_cron_wrapped.chomp)
26
- else # Otherwise, append the new cron entries after any existing ones
27
- [read_crontab, auto_cron_wrapped].join("\n\n")
28
- end
29
- end
30
-
31
- protected
7
+ @auto_cron_dir = File.join( Rails.root, "config", "auto_cron" )
8
+ @template = template
9
+ @application = application
10
+ end
32
11
 
33
- def auto_cron_wrapped
34
- @auto_cron_wrapped ||= [comment_open, auto_cron_inner, comment_close].join("\n") + "\n"
35
- end
36
-
37
- def auto_cron_inner
38
- header_template = File.join( @auto_cron_dir, 'header.erb' )
39
- header = if File.exist?( header_template )
40
- ERB.new( File.read( header_template ) ).result
41
- else
42
- ''
43
- end
44
- full_template_path = File.join( @auto_cron_dir, "#{ @template }.erb" )
45
- header + "\n\n" + ERB.new( File.read( full_template_path ) ).result
12
+ def updated_crontab
13
+ # Check for unopened or unclosed identifier blocks
14
+ if read_crontab.index(comment_open) && !read_crontab.index(comment_close)
15
+ warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'"
16
+ exit(1)
17
+ elsif !read_crontab.index(comment_open) && read_crontab.index(comment_close)
18
+ warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'"
19
+ exit(1)
46
20
  end
47
21
 
48
- def read_crontab
49
- return @current_crontab if @current_crontab
50
-
51
- command = ['crontab -l']
52
- command_results = %x[#{command.join(' ')} 2> /dev/null]
53
- @current_crontab = $?.exitstatus.zero? ? command_results : ''
22
+ # If an existing identier block is found, replace it with the new cron entries
23
+ if read_crontab.index(comment_open) && read_crontab.index(comment_close)
24
+ read_crontab.gsub(Regexp.new("#{comment_open}.+#{comment_close}", Regexp::MULTILINE), auto_cron_wrapped.chomp)
25
+ else # Otherwise, append the new cron entries after any existing ones
26
+ [read_crontab, auto_cron_wrapped].join("\n\n")
54
27
  end
55
-
56
- def comment_base
57
- "auto_cron generated tasks for: #{ @application }"
58
- end
59
-
60
- def comment_open
61
- "# Begin #{ comment_base }"
62
- end
63
-
64
- def comment_close
65
- "# End #{ comment_base }"
28
+ end
29
+
30
+ protected
31
+
32
+ def auto_cron_wrapped
33
+ @auto_cron_wrapped ||= [comment_open, auto_cron_inner, comment_close].join("\n") + "\n"
34
+ end
35
+
36
+ def auto_cron_inner
37
+ header_template = File.join( @auto_cron_dir, 'header.erb' )
38
+ header = if File.exist?( header_template )
39
+ ERB.new( File.read( header_template ) ).result
40
+ else
41
+ ''
66
42
  end
43
+ full_template_path = File.join( @auto_cron_dir, "#{ @template }.erb" )
44
+ header + "\n\n" + ERB.new( File.read( full_template_path ) ).result
45
+ end
46
+
47
+ def read_crontab
48
+ return @current_crontab if @current_crontab
67
49
 
50
+ command = ['crontab -l']
51
+ command_results = %x[#{command.join(' ')} 2> /dev/null]
52
+ @current_crontab = $?.exitstatus.zero? ? command_results : ''
68
53
  end
69
- end
54
+
55
+ def comment_base
56
+ "auto_cron generated tasks for: #{ @application }"
57
+ end
58
+
59
+ def comment_open
60
+ "# Begin #{ comment_base }"
61
+ end
62
+
63
+ def comment_close
64
+ "# End #{ comment_base }"
65
+ end
66
+
67
+ end
@@ -3,7 +3,8 @@ require File.dirname(__FILE__) + "/../auto_cron"
3
3
  desc "Generates the auto-cron and saves it to the crontab"
4
4
  task :auto_cron do
5
5
  require 'tempfile'
6
- cron_body = AutoCron.auto_cron ENV['TEMPLATE']
6
+ auto_cron = AutoCron.new ENV['TEMPLATE'], ENV['APPLICATION']
7
+ cron_body = auto_cron.updated_crontab
7
8
  tmp_cron_path = Tempfile.new( 'auto_cron' ).path
8
9
  File.open( tmp_cron_path, File::WRONLY | File::APPEND ) do |file|
9
10
  file << cron_body
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auto_cron
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Farrill