railsthemes 0.0.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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.rvmrc ADDED
@@ -0,0 +1,4 @@
1
+ # because "Rails 3.1 requires at least Ruby 1.8.7"
2
+ # can upgrade safely when we get past this
3
+ # would also be nice to test 1.9.2, 1.9.3 to ensure that they work as well
4
+ rvm use ree-1.8.7-head@railsthemes --create
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in railsthemes.gemspec
4
+ gemspec
5
+
6
+ # development gems
7
+ group :test do
8
+ gem 'rspec'
9
+ gem 'rr'
10
+ gem 'autotest'
11
+ gem "fakefs", :require => 'fakefs/safe'
12
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 railsthemes.com
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # Railsthemes
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'railsthemes'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install railsthemes
18
+
19
+ ## Usage
20
+
21
+ Desired usage:
22
+
23
+ $ railsthemes install my_hash
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new('spec')
7
+
8
+ # If you want to make this the default task
9
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'railsthemes'
4
+
5
+ installer = Railsthemes::Installer.new
6
+ installer.execute ARGV
@@ -0,0 +1,7 @@
1
+ require 'railsthemes/version'
2
+ require 'railsthemes/safe'
3
+ require 'railsthemes/utils'
4
+ require 'railsthemes/installer'
5
+
6
+ module Railsthemes
7
+ end
@@ -0,0 +1,166 @@
1
+ require 'date'
2
+ require 'fileutils'
3
+ require 'logger'
4
+ require 'tmpdir'
5
+
6
+ module Railsthemes
7
+ class Installer
8
+ def initialize logger = nil
9
+ @logger = logger
10
+ @logger ||= Logger.new(STDOUT)
11
+ @logger.formatter = proc do |severity, datetime, progname, msg|
12
+ "#{msg}\n"
13
+ end
14
+ end
15
+
16
+ def execute args = []
17
+ if args[0] == 'install' && args.length > 1
18
+ install *args[1..-1]
19
+ else
20
+ print_usage
21
+ end
22
+ end
23
+
24
+ def install *args
25
+ ensure_in_rails_root
26
+ if args[0] == '--file'
27
+ if args[1]
28
+ install_from_file_system args[1]
29
+ else
30
+ print_usage_and_abort "The parameter --file means we need another parameter after it to specify what file to load from."
31
+ end
32
+ elsif args[0] == '--help'
33
+ print_usage
34
+ else
35
+ if args[0]
36
+ download_from_hash args[0]
37
+ else
38
+ print_usage_and_abort "railsthemes expects the hash that you got from the website as a parameter in order to download the theme you bought."
39
+ end
40
+ end
41
+ end
42
+
43
+ def ensure_in_rails_root
44
+ unless File.directory?('app') && File.directory?('public')
45
+ Safe.log_and_abort 'Must be in the Rails root directory to use this gem.'
46
+ end
47
+ end
48
+
49
+ def install_from_file_system filepath
50
+ if File.directory?(filepath)
51
+ files = files_under(filepath)
52
+ @logger.info 'Copying assets...'
53
+ files.each do |file|
54
+ copy_with_replacement filepath, file
55
+ end
56
+ @logger.info 'Done copying assets.'
57
+ post_copying_changes
58
+ print_post_installation_instructions
59
+ elsif archive?(filepath)
60
+ if File.exists?(filepath)
61
+ install_from_archive filepath
62
+ # no need for post_installation, because we haven't
63
+ else
64
+ Safe.log_and_abort 'Cannot find the file you specified.'
65
+ end
66
+ else
67
+ print_usage_and_abort 'Need to specify either a directory or an archive file when --file is used.'
68
+ end
69
+ end
70
+
71
+ # this happens after a successful copy so that we set up the environment correctly
72
+ def post_copying_changes
73
+ Utils.remove_file File.join('public', 'index.html')
74
+ @logger.info 'Analyzing existing project structure...'
75
+ create_welcome_controller unless routes_defined?
76
+ end
77
+
78
+ def create_welcome_controller
79
+ Safe.system_call('rails g controller Welcome index')
80
+ lines = []
81
+ File.open('config/routes.rb').each do |line|
82
+ if line =~ / # root :to => 'welcome#index'/
83
+ lines << " root :to => 'welcome#index'"
84
+ else
85
+ lines << line
86
+ end
87
+ end
88
+ File.open('config/routes.rb', 'w') do |f|
89
+ lines.each do |line|
90
+ f.puts line
91
+ end
92
+ end
93
+ end
94
+
95
+ def routes_defined?
96
+ Safe.system_call('rake routes').length > 0
97
+ end
98
+
99
+ def install_from_archive filepath
100
+ tempdir = generate_tmpdir
101
+ Dir.mkdir tempdir
102
+ Safe.system_call untar_string(filepath, tempdir)
103
+ install_from_file_system tempdir
104
+ #FileUtils.rm_rf tempdir
105
+ end
106
+
107
+ def files_under filepath, accum = []
108
+ files = Dir.chdir(filepath) { Dir['**/*'] }
109
+ files.select{|f| !File.directory?(File.join(filepath, f))}
110
+ end
111
+
112
+ def download_from_hash hash
113
+ @logger.info "Downloading from hash #{hash}"
114
+ end
115
+
116
+ def copy_with_replacement filepath, entry
117
+ if File.exists?(entry)
118
+ # not sure if I should put in a cp -f here, might be better to toss error
119
+ # so I'm using rename instead
120
+ File.rename entry, "#{entry}.old"
121
+ end
122
+ Utils.copy_with_path File.join(filepath, entry), entry
123
+ end
124
+
125
+ def generate_tmpdir
126
+ File.join(Dir.tmpdir, DateTime.now.strftime("railsthemes-%Y%m%d-%H%M%s"))
127
+ end
128
+
129
+ def archive? filepath
130
+ filepath =~ /\.tar$/ || filepath =~ /\.tar\.gz$/
131
+ end
132
+
133
+ def untar_string filepath, newdirpath
134
+ gzipped = (filepath =~ /\.gz$/) ? 'z' : ''
135
+ "tar -#{gzipped}xf #{filepath} --strip 1"
136
+ end
137
+
138
+ def print_usage
139
+ @logger.info <<-EOS
140
+ Usage:
141
+ ------
142
+ railsthemes install HASH
143
+ install a theme from the railsthemes website
144
+
145
+ railsthemes install --help
146
+ this message
147
+
148
+ railsthemes install --file filepath
149
+ install from the local filesystem
150
+ EOS
151
+ end
152
+
153
+ def print_post_installation_instructions
154
+ @logger.info <<-EOS
155
+ Your theme is installed!
156
+
157
+ Make sure that you restart your server if it's currently running.
158
+ EOS
159
+ end
160
+
161
+ def print_usage_and_abort s
162
+ print_usage
163
+ Safe.log_and_abort s
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,19 @@
1
+ require 'fileutils'
2
+
3
+ # a bunch of things that should never be called in testing due to side effects
4
+ module Railsthemes
5
+ class Safe
6
+ def self.system_call s
7
+ verify_not_testing
8
+ `#{s}`
9
+ end
10
+
11
+ def self.log_and_abort s
12
+ abort s
13
+ end
14
+
15
+ def self.verify_not_testing
16
+ false
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ require 'fileutils'
2
+
3
+ # a bunch of things that should never be called in testing due to side effects
4
+ module Railsthemes
5
+ class Utils
6
+ def self.remove_file filepath
7
+ if File.exists?(filepath)
8
+ File.delete filepath
9
+ end
10
+ end
11
+
12
+ def self.copy_with_path src, dest
13
+ FileUtils.mkdir_p(File.dirname(dest)) # create directory if necessary
14
+ FileUtils.cp src, dest
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module Railsthemes
2
+ unless defined?(Railsthemes::VERSION)
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/railsthemes/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["railsthemes"]
6
+ gem.email = ["panozzaj@gmail.com"]
7
+ gem.description = %q{railsthemes.com installer gem}
8
+ gem.summary = %q{Installs gems from railsthemes.com}
9
+ gem.homepage = "https://github.com/RailsThemes/railsthemes"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "railsthemes"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Railsthemes::VERSION
17
+ end
@@ -0,0 +1,165 @@
1
+ require 'spec_helper'
2
+ require 'logger'
3
+
4
+ describe Railsthemes do
5
+ before do
6
+ @logger = Logger.new(File.join Dir.tmpdir, 'railsthemes.log')
7
+ @installer = Railsthemes::Installer.new @logger
8
+ end
9
+
10
+ describe :execute do
11
+ it 'should print usage if no params given' do
12
+ mock(@installer).print_usage
13
+ @installer.execute
14
+ end
15
+
16
+ it 'should run the installer if installer is the first parameter' do
17
+ mock(@installer).install 'a', 'b', 'c'
18
+ @installer.execute(['install', 'a', 'b', 'c'])
19
+ end
20
+ end
21
+
22
+ describe :install do
23
+ before do
24
+ stub(@installer).ensure_in_rails_root
25
+ end
26
+
27
+ context 'when --file is given as a parameter' do
28
+ it 'should read from the file argument' do
29
+ mock(@installer).install_from_file_system('filepath')
30
+ @installer.install '--file', 'filepath'
31
+ end
32
+
33
+ it 'should print usage and error message and exit if filepath is nil' do
34
+ dont_allow(@installer).install_from_file_system(anything)
35
+ dont_allow(@installer).download_from_hash(anything)
36
+ mock(@installer).print_usage_and_abort(/parameter/)
37
+ @installer.install '--file'
38
+ end
39
+ end
40
+
41
+ context 'hash given' do
42
+ it 'should download that hash' do
43
+ mock(@installer).download_from_hash('hash')
44
+ @installer.install 'hash'
45
+ end
46
+ end
47
+
48
+ context '--help given' do
49
+ it 'should print the usage' do
50
+ mock(@installer).print_usage
51
+ @installer.install '--help'
52
+ end
53
+ end
54
+
55
+ context 'when no parameters given' do
56
+ it 'should print usage and error message and exit' do
57
+ dont_allow(@installer).install_from_file_system(anything)
58
+ dont_allow(@installer).download_from_hash(anything)
59
+ mock(@installer).print_usage_and_abort(/parameter/)
60
+ @installer.install '--file'
61
+ end
62
+ end
63
+ end
64
+
65
+ describe :install_from_file_system do
66
+ context 'when the filepath is a directory' do
67
+ it 'should copy the files from that directory into the Rails app' do
68
+ FileUtils.mkdir('filepath')
69
+ FileUtils.touch('filepath/a')
70
+ FileUtils.touch('filepath/b')
71
+ mock(@installer).post_copying_changes
72
+
73
+ mock(@installer).copy_with_replacement('filepath', /a$/)
74
+ mock(@installer).copy_with_replacement('filepath', /b$/)
75
+ @installer.install_from_file_system('filepath')
76
+ end
77
+ end
78
+
79
+ context 'when the filepath is an archive file' do
80
+ it 'should extract the archive file to a temp directory if the archive exists' do
81
+ archive = 'tarfile.tar'
82
+ FileUtils.touch archive
83
+ mock(@installer).install_from_archive archive
84
+ @installer.install_from_file_system archive
85
+ end
86
+
87
+ it 'should print an error message and exit if the archive cannot be found' do
88
+ mock(Railsthemes::Safe).log_and_abort(/Cannot find/)
89
+ @installer.install_from_file_system 'tarfile.tar'
90
+ end
91
+ end
92
+
93
+ context 'otherwise' do
94
+ it 'should print usage and report an error reading the file' do
95
+ mock(@installer).print_usage_and_abort(/either/)
96
+ @installer.install_from_file_system("does not exist")
97
+ end
98
+ end
99
+ end
100
+
101
+ describe :copy_with_replacement do
102
+ before do
103
+ FileUtils.mkdir 'fp'
104
+ FileUtils.touch 'fp/file'
105
+ end
106
+
107
+ context 'when the destination file does not exist' do
108
+ it 'should copy the file to the local directory' do
109
+ @installer.copy_with_replacement 'fp', 'file'
110
+ File.exists?('file').should be_true
111
+ end
112
+ end
113
+
114
+ context 'when the destination file exists' do
115
+ before do
116
+ FileUtils.touch 'file'
117
+ end
118
+
119
+ it 'should make a backup of existing file if it is present' do
120
+ @installer.copy_with_replacement 'fp', 'file'
121
+ File.exists?('file').should be_true
122
+ File.exists?('file.old').should be_true
123
+ end
124
+ end
125
+ end
126
+
127
+ describe :install_from_archive do
128
+ it 'should extract the archive correctly' do
129
+ stub(@installer).generate_tmpdir { 'tmp' }
130
+ mock(@installer).install_from_file_system 'tmp'
131
+ mock(@installer).untar_string('filepath', anything) { 'untar string' }
132
+ mock(Railsthemes::Safe).system_call('untar string')
133
+ @installer.install_from_archive 'filepath'
134
+ end
135
+ end
136
+
137
+ describe :untar_string do
138
+ it 'should return correct value for *.tar.gz file' do
139
+ result = @installer.untar_string 'file.tar.gz', 'newdirpath'
140
+ result.should == 'tar -zxf file.tar.gz'
141
+ end
142
+
143
+ it 'should return correct value for *.tar file' do
144
+ result = @installer.untar_string 'file.tar', 'newdirpath'
145
+ result.should == 'tar -xf file.tar'
146
+ end
147
+ end
148
+
149
+ describe :archive? do
150
+ it 'should be true for tar file' do
151
+ @installer.archive?('test/a/b/c/d.tar').should be_true
152
+ end
153
+
154
+ it 'should be true for tar.gz file' do
155
+ @installer.archive?('test/a/b/c/d.tar.gz').should be_true
156
+ end
157
+
158
+ it 'should be false for other extensions' do
159
+ @installer.archive?('test/a/b/c.tar/d.zip').should be_false
160
+ end
161
+ end
162
+
163
+ describe :download_from_hash
164
+
165
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'fakefs/spec_helpers'
4
+
5
+ require 'railsthemes'
6
+
7
+ RSpec.configure do |config|
8
+ config.mock_with :rr
9
+ config.include FakeFS::SpecHelpers
10
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: railsthemes
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - railsthemes
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-10 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: railsthemes.com installer gem
22
+ email:
23
+ - panozzaj@gmail.com
24
+ executables:
25
+ - railsthemes
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - .rspec
33
+ - .rvmrc
34
+ - Gemfile
35
+ - LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - bin/railsthemes
39
+ - lib/railsthemes.rb
40
+ - lib/railsthemes/installer.rb
41
+ - lib/railsthemes/safe.rb
42
+ - lib/railsthemes/utils.rb
43
+ - lib/railsthemes/version.rb
44
+ - railsthemes.gemspec
45
+ - spec/lib/railsthemes_spec.rb
46
+ - spec/spec_helper.rb
47
+ homepage: https://github.com/RailsThemes/railsthemes
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.7
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Installs gems from railsthemes.com
80
+ test_files:
81
+ - spec/lib/railsthemes_spec.rb
82
+ - spec/spec_helper.rb