railsthemes 1.1 → 1.1.1

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.
@@ -52,7 +52,7 @@ EOS
52
52
  'railsthemes/send_email' => 'railsthemes#send_email'
53
53
  })
54
54
 
55
- Utils.conditionally_install_gems 'roadie', 'premailer-rails3'
55
+ install_mail_gems_if_necessary
56
56
 
57
57
  create_file File.join('config', 'initializers', 'premailer.rb'), :verbose => false do
58
58
  "PremailerRails.config.merge(:input_encoding => 'UTF-8', :generate_text_part => true)"
@@ -61,6 +61,20 @@ EOS
61
61
  logger.warn 'Done installing email theme.'
62
62
  end
63
63
 
64
+ def install_mail_gems_if_necessary
65
+ gem_names = Utils.gemspecs.map(&:name)
66
+ logger.debug "gem_names: #{gem_names}"
67
+ unless gem_names.include?('premailer-rails3')
68
+ if (gem_names & ['hpricot', 'nokogiri']).empty?
69
+ Utils.add_gem_to_gemfile 'hpricot'
70
+ end
71
+ Utils.add_gem_to_gemfile 'premailer-rails3'
72
+ logger.warn 'Installing assistant mail gems...'
73
+ Safe.system_call 'bundle'
74
+ logger.warn 'Done installing assistant mail gems.'
75
+ end
76
+ end
77
+
64
78
  def install_from_archive filepath
65
79
  Railsthemes::Utils.with_tempdir do |tempdir|
66
80
  Utils.unarchive filepath, tempdir
@@ -1,146 +1,135 @@
1
1
  module Railsthemes
2
- class Utils
3
- extend Railsthemes::Logging
4
-
5
- # remove file only if it exists
6
- def self.remove_file filepath
7
- if File.exists?(filepath)
8
- File.delete filepath
2
+ class Utils < Thor
3
+ no_tasks do
4
+ extend Railsthemes::Logging
5
+ extend Thor::Actions
6
+
7
+ # remove file only if it exists
8
+ def self.remove_file filepath
9
+ if File.exists?(filepath)
10
+ File.delete filepath
11
+ end
9
12
  end
10
- end
11
13
 
12
- # copy a file, ensuring that the directory is present
13
- def self.copy_ensuring_directory_exists src, dest
14
- FileUtils.mkdir_p(File.dirname(dest)) # create directory if necessary
15
- FileUtils.cp src, dest
16
- end
14
+ # copy a file, ensuring that the directory is present
15
+ def self.copy_ensuring_directory_exists src, dest
16
+ FileUtils.mkdir_p(File.dirname(dest)) # create directory if necessary
17
+ FileUtils.cp src, dest
18
+ end
17
19
 
18
- def self.read_file filepath
19
- File.exists?(filepath) ? File.read(filepath) : ''
20
- end
20
+ def self.read_file filepath
21
+ File.exists?(filepath) ? File.read(filepath) : ''
22
+ end
21
23
 
22
- # would be nice to put download status in the output (speed, progress, etc.)
23
- # needs tests
24
- def self.download params = {}
25
- url = params[:url]
26
- save_to = params[:save_to]
27
- return unless url && save_to
28
- logger.info "Downloading url: #{url}"
29
- logger.info "Saving to: #{save_to}"
30
- uri = URI(url)
31
- http = Net::HTTP.new uri.host, uri.port
32
- set_https http if uri.scheme == 'https'
33
- path = url.gsub(%r{https?://[^/]+}, '')
34
- response = http.get(path)
35
- if response.code.to_s == '200'
36
- File.open(save_to, 'wb') do |file|
37
- file.write(response.body)
24
+ # would be nice to put download status in the output (speed, progress, etc.)
25
+ # needs tests
26
+ def self.download params = {}
27
+ url = params[:url]
28
+ save_to = params[:save_to]
29
+ return unless url && save_to
30
+ logger.info "Downloading url: #{url}"
31
+ logger.info "Saving to: #{save_to}"
32
+ uri = URI(url)
33
+ http = Net::HTTP.new uri.host, uri.port
34
+ set_https http if uri.scheme == 'https'
35
+ path = url.gsub(%r{https?://[^/]+}, '')
36
+ response = http.get(path)
37
+ if response.code.to_s == '200'
38
+ File.open(save_to, 'wb') do |file|
39
+ file.write(response.body)
40
+ end
41
+ else
42
+ logger.debug "response.code: #{response.code}"
43
+ logger.debug "response.body: #{response.body}"
44
+ Safe.log_and_abort 'Had trouble downloading a file and cannot continue.'
38
45
  end
39
- else
40
- logger.debug "response.code: #{response.code}"
41
- logger.debug "response.body: #{response.body}"
42
- Safe.log_and_abort 'Had trouble downloading a file and cannot continue.'
43
46
  end
44
- end
45
47
 
46
- # needs tests I think
47
- def self.get_url url
48
- uri = URI.parse url
49
- http = Net::HTTP.new uri.host, uri.port
50
- set_https http if uri.scheme == 'https'
51
- path = url.gsub(%r{https?://[^/]+}, '')
52
- http.request_get(path)
53
- end
48
+ # needs tests I think
49
+ def self.get_url url
50
+ uri = URI.parse url
51
+ http = Net::HTTP.new uri.host, uri.port
52
+ set_https http if uri.scheme == 'https'
53
+ path = url.gsub(%r{https?://[^/]+}, '')
54
+ http.request_get(path)
55
+ end
54
56
 
55
- # needs tests
56
- def self.set_https http
57
- cacert_file = File.join(File.expand_path(File.dirname(__FILE__)), '..', '..', 'cacert.pem')
58
- http.ca_file = cacert_file
59
- http.ca_path = cacert_file
60
- ENV['SSL_CERT_FILE'] = cacert_file
57
+ # needs tests
58
+ def self.set_https http
59
+ cacert_file = File.join(File.expand_path(File.dirname(__FILE__)), '..', '..', 'cacert.pem')
60
+ http.ca_file = cacert_file
61
+ http.ca_path = cacert_file
62
+ ENV['SSL_CERT_FILE'] = cacert_file
61
63
 
62
- http.use_ssl = true
63
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
64
- end
64
+ http.use_ssl = true
65
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
66
+ end
65
67
 
66
- def self.archive? filepath
67
- File.exists?(filepath) && filepath =~ /\.tar\.gz$/
68
- end
68
+ def self.archive? filepath
69
+ File.exists?(filepath) && filepath =~ /\.tar\.gz$/
70
+ end
69
71
 
70
- # needs tests
71
- def self.with_tempdir &block
72
- tempdir = generate_tempdir_name
73
- FileUtils.mkdir_p tempdir
74
- yield tempdir
75
- FileUtils.rm_rf tempdir
76
- end
72
+ # needs tests
73
+ def self.with_tempdir &block
74
+ tempdir = generate_tempdir_name
75
+ FileUtils.mkdir_p tempdir
76
+ yield tempdir
77
+ FileUtils.rm_rf tempdir
78
+ end
77
79
 
78
- # needs tests
79
- def self.generate_tempdir_name
80
- tempdir = File.join(Dir.tmpdir, DateTime.now.strftime("railsthemes-%Y%m%d-%H%M%S-#{rand(100000000)}"))
81
- logger.debug "tempdir: #{tempdir}"
82
- tempdir
83
- end
80
+ # needs tests
81
+ def self.generate_tempdir_name
82
+ tempdir = File.join(Dir.tmpdir, DateTime.now.strftime("railsthemes-%Y%m%d-%H%M%S-#{rand(100000000)}"))
83
+ logger.debug "tempdir: #{tempdir}"
84
+ tempdir
85
+ end
84
86
 
85
- # needs tests
86
- def self.gemspecs gemfile_contents = nil
87
- gemfile_contents ||= Utils.read_file('Gemfile.lock')
88
- return [] if gemfile_contents.strip == ''
89
- lockfile = Bundler::LockfileParser.new(gemfile_contents)
90
- lockfile.specs
91
- end
87
+ # needs tests
88
+ def self.gemspecs gemfile_contents = nil
89
+ gemfile_contents ||= Utils.read_file('Gemfile.lock')
90
+ return [] if gemfile_contents.strip == ''
91
+ lockfile = Bundler::LockfileParser.new(gemfile_contents)
92
+ lockfile.specs
93
+ end
92
94
 
93
- def self.unarchive archive, extract_to
94
- Safe.log_and_abort "Archive expected at #{archive}, but none exists." unless File.exist?(archive)
95
- logger.warn "Extracting..."
96
- logger.info "Attempting to extract #{archive}"
97
- io = Tar.ungzip(File.open(archive, 'rb'))
98
- Tar.untar(io, extract_to)
99
- logger.warn "Finished extracting."
100
- end
95
+ def self.unarchive archive, extract_to
96
+ Safe.log_and_abort "Archive expected at #{archive}, but none exists." unless File.exist?(archive)
97
+ logger.warn "Extracting..."
98
+ logger.info "Attempting to extract #{archive}"
99
+ io = Tar.ungzip(File.open(archive, 'rb'))
100
+ Tar.untar(io, extract_to)
101
+ logger.warn "Finished extracting."
102
+ end
101
103
 
102
- def self.get_primary_configuration gemfile_contents = read_file('Gemfile.lock')
103
- gem_names = gemspecs(gemfile_contents).map(&:name)
104
- [(gem_names.include?('haml') ? 'haml' : 'erb'),
105
- (gem_names.include?('sass') ? 'scss' : 'css')]
106
- end
104
+ def self.get_primary_configuration gemfile_contents = read_file('Gemfile.lock')
105
+ gem_names = gemspecs(gemfile_contents).map(&:name)
106
+ [(gem_names.include?('haml') ? 'haml' : 'erb'),
107
+ (gem_names.include?('sass') ? 'scss' : 'css')]
108
+ end
107
109
 
108
- # needs tests
109
- def self.conditionally_insert_routes routes_hash
110
- if File.exists?(File.join('config', 'routes.rb'))
111
- lines = Utils.read_file('config/routes.rb').split("\n")
112
- last = lines.pop
113
- routes_hash.each do |first, second|
114
- if lines.grep(/#{second}/).empty?
115
- lines << " match '#{first}' => '#{second}'"
110
+ # needs tests
111
+ def self.conditionally_insert_routes routes_hash
112
+ if File.exists?(File.join('config', 'routes.rb'))
113
+ lines = Utils.read_file('config/routes.rb').split("\n")
114
+ last = lines.pop
115
+ routes_hash.each do |first, second|
116
+ if lines.grep(/#{second}/).empty?
117
+ lines << " match '#{first}' => '#{second}'"
118
+ end
116
119
  end
117
- end
118
- lines << last
119
- File.open(File.join('config', 'routes.rb'), 'w') do |f|
120
- lines.each do |line|
121
- f.puts line
120
+ lines << last
121
+ File.open(File.join('config', 'routes.rb'), 'w') do |f|
122
+ lines.each do |line|
123
+ f.puts line
124
+ end
122
125
  end
123
126
  end
124
127
  end
125
- end
126
128
 
127
- # needs tests
128
- def self.conditionally_install_gems *gem_names
129
- if File.exists?('Gemfile')
130
- lines = Utils.read_file('Gemfile').split("\n")
131
- gem_names.each do |gem_name|
132
- if lines.grep(/#{gem_name}/).empty?
133
- lines << "gem '#{gem_name}'"
134
- end
129
+ def self.add_gem_to_gemfile gem_name
130
+ File.open('Gemfile', 'a') do |f|
131
+ f.puts "gem '#{gem_name}'"
135
132
  end
136
- File.open('Gemfile', 'w') do |f|
137
- lines.each do |line|
138
- f.puts line
139
- end
140
- end
141
-
142
- # actually install the gems
143
- Safe.system_call 'bundle'
144
133
  end
145
134
  end
146
135
  end
@@ -1,5 +1,5 @@
1
1
  module Railsthemes
2
2
  unless defined?(Railsthemes::VERSION)
3
- VERSION = "1.1"
3
+ VERSION = "1.1.1"
4
4
  end
5
5
  end
@@ -3,6 +3,45 @@ require 'railsthemes'
3
3
 
4
4
  describe Railsthemes::EmailInstaller do
5
5
  before do
6
- setup_logging
6
+ setup_logger
7
+ @installer = Railsthemes::EmailInstaller.new
8
+ @tempdir = stub_tempdir
9
+ end
10
+
11
+ describe '#install_mail_gems_if_necessary' do
12
+ it 'should install no new gems if premailer-rails3 gem already installed' do
13
+ File.open('Gemfile.lock', 'w') do |f|
14
+ f.write using_gems 'premailer-rails3', 'hpricot'
15
+ end
16
+ dont_allow(Railsthemes::Utils).add_gem_to_gemfile(anything)
17
+ dont_allow(Railsthemes::Safe).system_call('bundle')
18
+ @installer.install_mail_gems_if_necessary
19
+ end
20
+
21
+ it 'when nokogiri already installed, should install the pr3 gem' do
22
+ File.open('Gemfile.lock', 'w') do |f|
23
+ f.write using_gems 'nokogiri'
24
+ end
25
+ mock(Railsthemes::Utils).add_gem_to_gemfile('premailer-rails3')
26
+ mock(Railsthemes::Safe).system_call('bundle')
27
+ @installer.install_mail_gems_if_necessary
28
+ end
29
+
30
+ it 'when hpricot already installed, should install the pr3 gem only' do
31
+ File.open('Gemfile.lock', 'w') do |f|
32
+ f.write using_gems 'hpricot'
33
+ end
34
+ mock(Railsthemes::Utils).add_gem_to_gemfile('premailer-rails3')
35
+ mock(Railsthemes::Safe).system_call('bundle')
36
+ @installer.install_mail_gems_if_necessary
37
+ end
38
+
39
+ it 'when no xml gem or pr3 installed, should install the pr3 gem and hpricot' do
40
+ FileUtils.touch('Gemfile.lock')
41
+ mock(Railsthemes::Utils).add_gem_to_gemfile('hpricot')
42
+ mock(Railsthemes::Utils).add_gem_to_gemfile('premailer-rails3')
43
+ mock(Railsthemes::Safe).system_call('bundle')
44
+ @installer.install_mail_gems_if_necessary
45
+ end
7
46
  end
8
47
  end
@@ -50,6 +50,17 @@ describe Railsthemes::Utils do
50
50
  end
51
51
  end
52
52
 
53
+ describe 'add_gem_to_gemfile' do
54
+ it 'should add the gem to the Gemfile' do
55
+ Railsthemes::Utils.add_gem_to_gemfile 'test'
56
+ Railsthemes::Utils.add_gem_to_gemfile 'test'
57
+ lines = File.open('Gemfile').readlines.map(&:strip)
58
+ lines.count.should == 2
59
+ lines[0].should == "gem 'test'"
60
+ lines[1].should == "gem 'test'"
61
+ end
62
+ end
63
+
53
64
  describe 'download' do
54
65
  it 'should log and abort if file not found at url' do
55
66
  FakeWeb.register_uri :get,
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railsthemes
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- version: "1.1"
9
+ - 1
10
+ version: 1.1.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - Railsthemes
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2012-07-05 00:00:00 Z
18
+ date: 2012-07-12 00:00:00 Z
18
19
  dependencies:
19
20
  - !ruby/object:Gem::Dependency
20
21
  name: thor