auto_cron 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
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.2"
8
+ s.version = "0.1.3"
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"]
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
30
30
  "generators/auto_cron/templates/publish.erb",
31
31
  "install.rb",
32
32
  "lib/auto_cron.rb",
33
+ "lib/auto_cron/railtie.rb",
33
34
  "lib/capistrano/auto_cron.rb",
34
35
  "lib/tasks/auto_cron_tasks.rake",
35
36
  "test/helper.rb",
data/lib/auto_cron.rb CHANGED
@@ -1,65 +1,69 @@
1
- class AutoCron
2
- def initialize( template, application )
3
- require 'erb'
1
+ require 'auto_cron/railtie' if defined?(Rails)
4
2
 
5
- @auto_cron_dir = File.join( Rails.root, "config", "auto_cron" )
6
- @template = template
7
- @application = application
8
- end
9
-
10
- def updated_crontab
11
- # Check for unopened or unclosed identifier blocks
12
- if read_crontab.index(comment_open) && !read_crontab.index(comment_close)
13
- warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'"
14
- exit(1)
15
- elsif !read_crontab.index(comment_open) && read_crontab.index(comment_close)
16
- warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'"
17
- exit(1)
3
+ module AutoCron
4
+ class AutoCron
5
+ def initialize( template, application )
6
+ require 'erb'
7
+
8
+ @auto_cron_dir = File.join( Rails.root, "config", "auto_cron" )
9
+ @template = template
10
+ @application = application
18
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
19
22
 
20
- # If an existing identier block is found, replace it with the new cron entries
21
- if read_crontab.index(comment_open) && read_crontab.index(comment_close)
22
- read_crontab.gsub(Regexp.new("#{comment_open}.+#{comment_close}", Regexp::MULTILINE), auto_cron_wrapped.chomp)
23
- else # Otherwise, append the new cron entries after any existing ones
24
- [read_crontab, auto_cron_wrapped].join("\n\n")
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
25
29
  end
26
- end
27
30
 
28
- protected
31
+ protected
29
32
 
30
- def auto_cron_wrapped
31
- @auto_cron_wrapped ||= [comment_open, auto_cron_inner, comment_close].join("\n") + "\n"
32
- end
33
+ def auto_cron_wrapped
34
+ @auto_cron_wrapped ||= [comment_open, auto_cron_inner, comment_close].join("\n") + "\n"
35
+ end
33
36
 
34
- def auto_cron_inner
35
- header_template = File.join( @auto_cron_dir, 'header.erb' )
36
- header = if File.exist?( header_template )
37
- ERB.new( File.read( header_template ) ).result
38
- else
39
- ''
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
40
46
  end
41
- full_template_path = File.join( @auto_cron_dir, "#{ @template }.erb" )
42
- header + "\n\n" + ERB.new( File.read( full_template_path ) ).result
43
- end
44
47
 
45
- def read_crontab
46
- return @current_crontab if @current_crontab
48
+ def read_crontab
49
+ return @current_crontab if @current_crontab
47
50
 
48
- command = ['crontab -l']
49
- command_results = %x[#{command.join(' ')} 2> /dev/null]
50
- @current_crontab = $?.exitstatus.zero? ? command_results : ''
51
- end
51
+ command = ['crontab -l']
52
+ command_results = %x[#{command.join(' ')} 2> /dev/null]
53
+ @current_crontab = $?.exitstatus.zero? ? command_results : ''
54
+ end
52
55
 
53
- def comment_base
54
- "auto_cron generated tasks for: #{ @application }"
55
- end
56
+ def comment_base
57
+ "auto_cron generated tasks for: #{ @application }"
58
+ end
56
59
 
57
- def comment_open
58
- "# Begin #{ comment_base }"
59
- end
60
+ def comment_open
61
+ "# Begin #{ comment_base }"
62
+ end
60
63
 
61
- def comment_close
62
- "# End #{ comment_base }"
63
- end
64
+ def comment_close
65
+ "# End #{ comment_base }"
66
+ end
64
67
 
68
+ end
65
69
  end
@@ -0,0 +1,9 @@
1
+ require 'auto_cron'
2
+ require 'rails'
3
+ module AutoCron
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ load "tasks/auto_cron_tasks.rake"
7
+ end
8
+ end
9
+ end
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: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Farrill
@@ -87,6 +87,7 @@ files:
87
87
  - generators/auto_cron/templates/publish.erb
88
88
  - install.rb
89
89
  - lib/auto_cron.rb
90
+ - lib/auto_cron/railtie.rb
90
91
  - lib/capistrano/auto_cron.rb
91
92
  - lib/tasks/auto_cron_tasks.rake
92
93
  - test/helper.rb