scaffoldhub 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +2 -2
- data/README.rdoc +5 -0
- data/Rakefile +9 -0
- data/lib/generators/active_record/scaffoldhub_generator.rb +20 -0
- data/lib/generators/erb/scaffoldhub_generator.rb +8 -1
- data/lib/generators/scaffold_controller/scaffoldhub_generator.rb +1 -1
- data/lib/generators/scaffoldhub/scaffoldhub_generator.rb +3 -0
- data/lib/scaffoldhub.rb +3 -3
- data/lib/scaffoldhub/helper.rb +53 -16
- data/lib/scaffoldhub/remote_file.rb +22 -10
- data/lib/scaffoldhub/scaffold_spec.rb +71 -0
- data/lib/scaffoldhub/specification.rb +48 -0
- data/lib/scaffoldhub/template_file.rb +13 -7
- data/scaffoldhub.gemspec +2 -4
- data/spec/fixtures/test_scaffoldspec.rb +21 -0
- data/spec/helper_spec.rb +139 -0
- data/spec/remote_file_spec.rb +83 -0
- data/spec/scaffold_spec_spec.rb +131 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/template_file_spec.rb +50 -0
- data/util/spec_to_yaml.rb +7 -0
- metadata +24 -10
- data/lib/scaffoldhub/scaffold.rb +0 -38
- data/lib/scaffoldhub/spec_file.rb +0 -42
data/Gemfile.lock
CHANGED
data/README.rdoc
ADDED
data/Rakefile
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
require 'rails/generators/active_record/model/model_generator'
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
class ScaffoldhubGenerator < ActiveRecord::Generators::ModelGenerator
|
6
|
+
|
7
|
+
include Scaffoldhub::Helper
|
8
|
+
|
9
|
+
source_root File.join(base_root, 'active_record', 'model', 'templates')
|
10
|
+
|
11
|
+
class_option :scaffold, :default => 'default', :banner => "SCAFFOLD_NAME", :type => :string, :desc => "Scaffold to use"
|
12
|
+
class_option :local, :default => false, :banner => "LOCAL SCAFFOLD", :type => :boolean, :desc => "Use a local scaffold, not scaffoldhub.org"
|
13
|
+
|
14
|
+
def create_model_file
|
15
|
+
model_template = find_template_file('active_record', 'templates/model.rb')
|
16
|
+
template model_template.src, File.join('app/models', class_path, "#{file_name}.rb") if model_template
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -10,8 +10,15 @@ module Erb
|
|
10
10
|
|
11
11
|
def copy_view_files
|
12
12
|
each_template_file(:erb) do |erb_template_file|
|
13
|
-
|
13
|
+
unless erb_template_file.dest == 'app/views/layouts/layout.erb'
|
14
|
+
template erb_template_file.src, File.join("app/views", controller_file_path, erb_template_file.dest)
|
15
|
+
end
|
14
16
|
end
|
15
17
|
end
|
18
|
+
|
19
|
+
def copy_layout_file
|
20
|
+
layout_template = find_template_file('erb', 'templates/layout.erb')
|
21
|
+
template layout_template.src, File.join('app/views/layouts', "#{controller_file_name}.html.erb") if layout_template
|
22
|
+
end
|
16
23
|
end
|
17
24
|
end
|
@@ -13,7 +13,7 @@ module ScaffoldController
|
|
13
13
|
|
14
14
|
def create_controller_files
|
15
15
|
each_template_file(:controller) do |controller_template_file|
|
16
|
-
template controller_template_file.src, File.join('app/controllers', class_path,
|
16
|
+
template controller_template_file.src, File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb")
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
@@ -7,6 +7,9 @@ class ScaffoldhubGenerator < Rails::Generators::ScaffoldGenerator
|
|
7
7
|
remove_hook_for :scaffold_controller
|
8
8
|
hook_for :scaffold_controller, :as => :scaffoldhub
|
9
9
|
|
10
|
+
remove_hook_for :orm
|
11
|
+
hook_for :orm, :as= => :scaffoldhub
|
12
|
+
|
10
13
|
class_option :scaffold, :default => 'default', :banner => "SCAFFOLD_NAME", :type => :string, :desc => "Scaffold to use"
|
11
14
|
class_option :local, :default => false, :banner => "LOCAL SCAFFOLD", :type => :boolean, :desc => "Use a local scaffold, not scaffoldhub.org"
|
12
15
|
|
data/lib/scaffoldhub.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'scaffoldhub/remote_file'
|
2
|
-
require 'scaffoldhub/
|
2
|
+
require 'scaffoldhub/specification'
|
3
|
+
require 'scaffoldhub/scaffold_spec'
|
3
4
|
require 'scaffoldhub/template_file'
|
4
|
-
require 'scaffoldhub/scaffold'
|
5
5
|
require 'scaffoldhub/helper'
|
6
6
|
|
7
7
|
module Scaffoldhub
|
8
|
-
VERSION = '0.0.
|
8
|
+
VERSION = '0.0.2'
|
9
9
|
end
|
data/lib/scaffoldhub/helper.rb
CHANGED
@@ -1,36 +1,73 @@
|
|
1
1
|
module Scaffoldhub
|
2
2
|
module Helper
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
class << self
|
5
|
+
def scaffold_spec
|
6
|
+
@scaffold_spec
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
def scaffold_spec=(scaffold)
|
10
|
+
@scaffold_spec = scaffold
|
11
|
+
end
|
10
12
|
end
|
11
13
|
|
12
14
|
def each_template_file(type)
|
13
15
|
begin
|
14
|
-
|
15
|
-
|
16
|
-
if options[:local]
|
17
|
-
raise Errno::ENOENT.new(template_file.src) unless File.exists?(template_file.src)
|
18
|
-
else
|
19
|
-
template_file.download
|
20
|
-
end
|
21
|
-
yield template_file
|
16
|
+
scaffold_spec.select_files(type).each do |template_file|
|
17
|
+
yield template_file.download!
|
22
18
|
end
|
23
|
-
rescue Errno::ENOENT
|
24
|
-
say_status :error,
|
19
|
+
rescue Errno::ENOENT => e
|
20
|
+
say_status :error, e.message, :red
|
21
|
+
rescue Scaffoldhub::NotFoundException => e
|
22
|
+
say_status :error, "HTTP 404 not found error for #{e.message}", :red
|
23
|
+
rescue Scaffoldhub::NetworkErrorException => e
|
24
|
+
say_status :error, "HTTP error connecting to #{e.message}", :red
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def find_template_file(type, name)
|
29
|
+
begin
|
30
|
+
template_file = scaffold_spec.find_file(type, name)
|
31
|
+
template_file.download! unless template_file.nil?
|
32
|
+
rescue Errno::ENOENT => e
|
33
|
+
say_status :error, e.message, :red
|
34
|
+
nil
|
25
35
|
rescue Scaffoldhub::NotFoundException => e
|
26
36
|
say_status :error, "HTTP 404 not found error for #{e.message}", :red
|
37
|
+
nil
|
27
38
|
rescue Scaffoldhub::NetworkErrorException => e
|
28
39
|
say_status :error, "HTTP error connecting to #{e.message}", :red
|
40
|
+
nil
|
29
41
|
end
|
30
42
|
end
|
31
43
|
|
44
|
+
def scaffold_spec
|
45
|
+
Helper.scaffold_spec ||= download_scaffold_spec!
|
46
|
+
end
|
47
|
+
|
48
|
+
def download_scaffold_spec!
|
49
|
+
scaffold_spec = ScaffoldSpec.new(scaffold_name, options[:local], status_proc)
|
50
|
+
scaffold_spec.download_and_parse!
|
51
|
+
scaffold_spec
|
52
|
+
end
|
53
|
+
|
54
|
+
def scaffold_name
|
55
|
+
parse_scaffold_option(0)
|
56
|
+
end
|
57
|
+
|
58
|
+
def scaffold_parameter
|
59
|
+
parse_scaffold_option(1)
|
60
|
+
end
|
61
|
+
|
32
62
|
def status_proc
|
33
|
-
@
|
63
|
+
@status_proc ||= lambda { |url| say_status :download, url }
|
34
64
|
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def parse_scaffold_option(index)
|
69
|
+
options[:scaffold].split(':')[index]
|
70
|
+
end
|
71
|
+
|
35
72
|
end
|
36
73
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'net/
|
1
|
+
require 'net/https'
|
2
2
|
|
3
3
|
module Scaffoldhub
|
4
4
|
|
@@ -14,18 +14,20 @@ module Scaffoldhub
|
|
14
14
|
@status_proc = status_proc
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
17
|
+
def remote_file_contents!
|
18
18
|
begin
|
19
19
|
uri = URI.parse(url)
|
20
20
|
@status_proc.call(url)
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
21
|
+
if uri.port == 443
|
22
|
+
https = Net::HTTP.new(uri.host, uri.port)
|
23
|
+
https.use_ssl = true
|
24
|
+
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
25
|
+
https.start do |https|
|
26
|
+
response_body(https.get(uri.path))
|
27
|
+
end
|
28
|
+
else
|
29
|
+
Net::HTTP.start(uri.host, uri.port) do |http|
|
30
|
+
response_body(http.get(uri.path))
|
29
31
|
end
|
30
32
|
end
|
31
33
|
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Errno::ECONNREFUSED,
|
@@ -34,5 +36,15 @@ module Scaffoldhub
|
|
34
36
|
end
|
35
37
|
end
|
36
38
|
|
39
|
+
def response_body(resp)
|
40
|
+
if resp.code.to_i == 200
|
41
|
+
resp.body
|
42
|
+
elsif resp.code.to_i == 404
|
43
|
+
raise NotFoundException.new(url)
|
44
|
+
else
|
45
|
+
raise NetworkErrorException.new(url)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
37
49
|
end
|
38
50
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module Scaffoldhub
|
2
|
+
class ScaffoldSpec < RemoteFile
|
3
|
+
|
4
|
+
SCAFFOLD_HUB_SERVER = 'scaffoldhub.org'
|
5
|
+
|
6
|
+
def initialize(url, local, status_proc)
|
7
|
+
@scaffold = url
|
8
|
+
@local = local
|
9
|
+
@status_proc = status_proc
|
10
|
+
super(@status_proc)
|
11
|
+
end
|
12
|
+
|
13
|
+
def download_and_parse!
|
14
|
+
if @local
|
15
|
+
parse_local
|
16
|
+
else
|
17
|
+
parse_remote!
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def select_files(type)
|
22
|
+
template_file_specs.select { |file_spec| file_spec[:type] == type.to_s }.collect do |file_spec|
|
23
|
+
TemplateFile.new file_spec[:src], file_spec[:dest], @local, base_url, @status_proc
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def find_file(type, name)
|
28
|
+
file_spec = template_file_specs.detect { |file_spec| file_spec[:src] == name && file_spec[:type] == type.to_s }
|
29
|
+
unless file_spec.nil?
|
30
|
+
TemplateFile.new file_spec[:src], file_spec[:dest], @local, base_url, @status_proc
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def parse_local
|
35
|
+
if File.exists?(url)
|
36
|
+
require url
|
37
|
+
else
|
38
|
+
raise Errno::ENOENT.new(url)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def parse_remote!
|
43
|
+
@spec = YAML::load(remote_file_contents!)
|
44
|
+
end
|
45
|
+
|
46
|
+
def template_file_specs
|
47
|
+
if @local
|
48
|
+
Specification.files
|
49
|
+
else
|
50
|
+
@spec[:files]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def url
|
55
|
+
if @local
|
56
|
+
@scaffold
|
57
|
+
else
|
58
|
+
"http://#{SCAFFOLD_HUB_SERVER}/scaffolds/#{@scaffold}/spec"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def base_url
|
63
|
+
if @local
|
64
|
+
File.dirname(File.expand_path(@scaffold))
|
65
|
+
else
|
66
|
+
@spec[:base_url]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Scaffoldhub
|
4
|
+
class Specification
|
5
|
+
|
6
|
+
@@files = []
|
7
|
+
@@base_url = nil
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def files
|
11
|
+
@@files
|
12
|
+
end
|
13
|
+
|
14
|
+
def files=(files)
|
15
|
+
@@files = files
|
16
|
+
end
|
17
|
+
|
18
|
+
def add_file(type, src, dest)
|
19
|
+
@@files << { :type => type, :src => src, :dest => dest }
|
20
|
+
end
|
21
|
+
|
22
|
+
def base_url
|
23
|
+
@@base_url
|
24
|
+
end
|
25
|
+
|
26
|
+
def base_url=(url)
|
27
|
+
@@base_url = url
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_yaml
|
31
|
+
{ :base_url => base_url, :files => files }.to_yaml
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def initialize
|
36
|
+
yield self
|
37
|
+
end
|
38
|
+
|
39
|
+
def method_missing(name, *args, &blk)
|
40
|
+
if name.to_s =~ /(.*)_file/ && args[0].is_a?(Hash)
|
41
|
+
self.class.add_file($1, args[0][:src], args[0][:dest])
|
42
|
+
elsif name == :base_url
|
43
|
+
self.class.base_url = args[0]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -1,15 +1,16 @@
|
|
1
1
|
module Scaffoldhub
|
2
2
|
class TemplateFile < RemoteFile
|
3
3
|
|
4
|
-
def initialize(src, dest, status_proc)
|
4
|
+
def initialize(src, dest, local, base_url, status_proc)
|
5
5
|
@src = src
|
6
6
|
@dest = dest || ''
|
7
|
-
@
|
7
|
+
@local = local
|
8
|
+
@base_url = base_url
|
8
9
|
super(status_proc)
|
9
10
|
end
|
10
11
|
|
11
12
|
def src
|
12
|
-
if
|
13
|
+
if @local
|
13
14
|
File.join(@base_url, @src)
|
14
15
|
else
|
15
16
|
@local_path
|
@@ -20,11 +21,16 @@ module Scaffoldhub
|
|
20
21
|
File.join(@dest, File.basename(@src))
|
21
22
|
end
|
22
23
|
|
23
|
-
def download
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
def download!
|
25
|
+
if @local
|
26
|
+
raise Errno::ENOENT.new(src) unless File.exists?(src)
|
27
|
+
else
|
28
|
+
@local_path = Tempfile.new(File.basename(@src)).path
|
29
|
+
open(@local_path, "wb") do |file|
|
30
|
+
file.write(remote_file_contents!)
|
31
|
+
end
|
27
32
|
end
|
33
|
+
self
|
28
34
|
end
|
29
35
|
|
30
36
|
def url
|
data/scaffoldhub.gemspec
CHANGED
@@ -10,10 +10,8 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.authors = ['Pat Shaughnessy']
|
11
11
|
s.email = ['pat@patshaughnessy.net']
|
12
12
|
s.homepage = "http://scaffoldhub.org"
|
13
|
-
s.summary = %q{Generate Rails scaffolding from scaffoldhub.org}
|
14
|
-
s.description = %q{
|
15
|
-
|
16
|
-
#s.rubyforge_project = "scaffoldhub"
|
13
|
+
s.summary = %q{Generate customized Rails scaffolding from scaffoldhub.org}
|
14
|
+
s.description = %q{Run Rails scaffold generator with customized templates downloaded from scaffoldhub.org}
|
17
15
|
|
18
16
|
s.files = `git ls-files`.split("\n")
|
19
17
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
@@ -0,0 +1,21 @@
|
|
1
|
+
Scaffoldhub::Specification.new do |s|
|
2
|
+
s.base_url 'http://github.com/patshaughnessy/scaffolds/default'
|
3
|
+
s.erb_file :src => 'templates/index3.html.erb', :dest => ''
|
4
|
+
s.controller_file :src => 'templates/index2.html.erb', :dest => ''
|
5
|
+
s.other_file :src => 'templates/index.html.erb', :dest => 'app/views/welcome'
|
6
|
+
end
|
7
|
+
|
8
|
+
# Yaml hash of scaffold options
|
9
|
+
#
|
10
|
+
#---
|
11
|
+
#:base_url: http://github.com/patshaughnessy/scaffolds/default
|
12
|
+
#:files:
|
13
|
+
#- :src: templates/index.html.erb
|
14
|
+
# :dest: app/views/welcome
|
15
|
+
# :type: other
|
16
|
+
#- :src: templates/index2.html.erb
|
17
|
+
# :dest:
|
18
|
+
# :type: controller
|
19
|
+
#- :src: templates/index3.html.erb
|
20
|
+
# :dest:
|
21
|
+
# :type: erb
|
data/spec/helper_spec.rb
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class FakeGenerator
|
4
|
+
include Scaffoldhub::Helper
|
5
|
+
|
6
|
+
attr_accessor :files
|
7
|
+
|
8
|
+
def initialize(local, options = 'some_scaffold:some_parameter')
|
9
|
+
@files = []
|
10
|
+
@local = local
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
|
14
|
+
def copy_files
|
15
|
+
each_template_file(:sometype) do |template_file|
|
16
|
+
files << template_file
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def options
|
21
|
+
{ :scaffold => @options, :local => @local }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe Scaffoldhub::Helper do
|
26
|
+
|
27
|
+
describe 'local scaffold' do
|
28
|
+
|
29
|
+
before(:all) do
|
30
|
+
Scaffoldhub::Helper.scaffold_spec = nil
|
31
|
+
status_proc = mock
|
32
|
+
status_proc.stubs(:call)
|
33
|
+
mock_spec = mock
|
34
|
+
Scaffoldhub::ScaffoldSpec.stubs(:new).with('some_scaffold', true, status_proc).returns(mock_spec)
|
35
|
+
mock_spec.stubs(:download_and_parse!)
|
36
|
+
mock_template_file_array = [
|
37
|
+
Scaffoldhub::TemplateFile.new('src1', 'dest1', true, '/some/path', status_proc),
|
38
|
+
Scaffoldhub::TemplateFile.new('src2', 'dest2', true, '/some/path', status_proc),
|
39
|
+
Scaffoldhub::TemplateFile.new('src3', 'dest3', true, '/some/path', status_proc)
|
40
|
+
]
|
41
|
+
mock_spec.stubs(:select_files).with(:sometype).returns(mock_template_file_array)
|
42
|
+
@gen = FakeGenerator.new(true)
|
43
|
+
@gen.stubs(:status_proc).returns(status_proc)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should yield the template files' do
|
47
|
+
File.expects(:exists?).with('/some/path/src1').returns(true)
|
48
|
+
File.expects(:exists?).with('/some/path/src2').returns(true)
|
49
|
+
File.expects(:exists?).with('/some/path/src3').returns(true)
|
50
|
+
@gen.copy_files
|
51
|
+
@gen.files[0].src.should == '/some/path/src1'
|
52
|
+
@gen.files[1].src.should == '/some/path/src2'
|
53
|
+
@gen.files[2].src.should == '/some/path/src3'
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should raise an exception if the template file doesn\'t exist' do
|
57
|
+
File.expects(:exists?).with('/some/path/src1').returns(false)
|
58
|
+
@gen.expects(:say_status).with(:error, 'No such file or directory - /some/path/src1', :red)
|
59
|
+
@gen.copy_files
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'remote scaffold' do
|
64
|
+
|
65
|
+
before do
|
66
|
+
Scaffoldhub::Helper.scaffold_spec = nil
|
67
|
+
status_proc = mock
|
68
|
+
status_proc.stubs(:call)
|
69
|
+
mock_spec = mock
|
70
|
+
Scaffoldhub::ScaffoldSpec.stubs(:new).with('some_scaffold', false, status_proc).returns(mock_spec)
|
71
|
+
mock_spec.stubs(:download_and_parse!)
|
72
|
+
template1 = Scaffoldhub::TemplateFile.new('src1', 'dest1', false, 'http://some.server/some/path', status_proc)
|
73
|
+
template1.expects(:download!).returns(template1)
|
74
|
+
template1.stubs(:src).returns('src1')
|
75
|
+
template2 = Scaffoldhub::TemplateFile.new('src2', 'dest2', false, 'http://some.server/some/path', status_proc)
|
76
|
+
template2.expects(:download!).returns(template2)
|
77
|
+
template2.stubs(:src).returns('src2')
|
78
|
+
template3 = Scaffoldhub::TemplateFile.new('src3', 'dest3', false, 'http://some.server/some/path', status_proc)
|
79
|
+
template3.expects(:download!).returns(template3)
|
80
|
+
template3.stubs(:src).returns('src3')
|
81
|
+
mock_template_file_array = [ template1, template2, template3 ]
|
82
|
+
mock_spec.stubs(:select_files).with(:sometype).returns(mock_template_file_array)
|
83
|
+
@gen = FakeGenerator.new(false)
|
84
|
+
@gen.stubs(:status_proc).returns(status_proc)
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'should yield the template files' do
|
88
|
+
@gen.copy_files
|
89
|
+
@gen.files[0].src.should == 'src1'
|
90
|
+
@gen.files[1].src.should == 'src2'
|
91
|
+
@gen.files[2].src.should == 'src3'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe 'sharing scaffold spec among generators' do
|
96
|
+
|
97
|
+
before do
|
98
|
+
Scaffoldhub::Helper.scaffold_spec = nil
|
99
|
+
@mock_spec = mock
|
100
|
+
@mock_spec.stubs(:download_and_parse!)
|
101
|
+
status_proc = mock
|
102
|
+
Scaffoldhub::ScaffoldSpec.expects(:new).once.with('some_scaffold', false, status_proc).returns(@mock_spec)
|
103
|
+
@gen = FakeGenerator.new(false)
|
104
|
+
@gen.stubs(:status_proc).returns(status_proc)
|
105
|
+
@gen2 = FakeGenerator.new(false)
|
106
|
+
@gen2.stubs(:status_proc).returns(status_proc)
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should save the scaffold spec in the module among different generators' do
|
110
|
+
@gen.scaffold_spec.should == @mock_spec
|
111
|
+
@gen2.scaffold_spec.should == @mock_spec
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe '#find_template_file' do
|
116
|
+
|
117
|
+
it 'should call find_file on the scaffold spec' do
|
118
|
+
gen = FakeGenerator.new(false)
|
119
|
+
gen.expects(:find_file).with('type', 'name')
|
120
|
+
gen.find_file('type', 'name')
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe '#scaffold_name' do
|
125
|
+
|
126
|
+
describe 'both values present' do
|
127
|
+
subject { FakeGenerator.new(false) }
|
128
|
+
its(:scaffold_name) { should == 'some_scaffold' }
|
129
|
+
its(:scaffold_parameter) { should == 'some_parameter' }
|
130
|
+
end
|
131
|
+
|
132
|
+
describe 'only scaffold name present' do
|
133
|
+
subject { FakeGenerator.new(false, 'scaffold_name_only') }
|
134
|
+
its(:scaffold_name) { should == 'scaffold_name_only' }
|
135
|
+
its(:scaffold_parameter) { should == nil }
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
module Net
|
4
|
+
class HTTP
|
5
|
+
|
6
|
+
def self.set_mock_http(mock_http)
|
7
|
+
@mock_http = mock_http
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.start(host, port)
|
11
|
+
yield @mock_http
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe Scaffoldhub::RemoteFile do
|
17
|
+
|
18
|
+
FAKE_SCAFFOLDHUB_URL = 'http://fake.scaffoldhub.org:1234/scaffolds/autocomplete/spec'
|
19
|
+
|
20
|
+
describe '#remote_file_contents' do
|
21
|
+
|
22
|
+
describe 'status proc' do
|
23
|
+
subject do
|
24
|
+
@status_proc = mock
|
25
|
+
@status_proc.expects(:call).with(FAKE_SCAFFOLDHUB_URL)
|
26
|
+
Scaffoldhub::RemoteFile.new(@status_proc)
|
27
|
+
end
|
28
|
+
before do
|
29
|
+
subject.stubs(:url).returns(FAKE_SCAFFOLDHUB_URL)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should call the status proc with the url' do
|
33
|
+
Net::HTTP.stubs(:start)
|
34
|
+
subject.remote_file_contents!
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe 'Net:HTTP calls' do
|
39
|
+
|
40
|
+
subject do
|
41
|
+
(status_proc = mock).stubs(:call).with(FAKE_SCAFFOLDHUB_URL)
|
42
|
+
Scaffoldhub::RemoteFile.new(status_proc)
|
43
|
+
end
|
44
|
+
before do
|
45
|
+
subject.stubs(:url).returns(FAKE_SCAFFOLDHUB_URL)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should call Net::HTTP with the proper host and port' do
|
49
|
+
Net::HTTP.expects(:start).with('fake.scaffoldhub.org', 1234)
|
50
|
+
subject.remote_file_contents!
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should call GET on the url path and return the response body' do
|
54
|
+
mock_http = mock
|
55
|
+
mock_http.expects(:get).with('/scaffolds/autocomplete/spec').returns(mock_response = mock)
|
56
|
+
mock_response.stubs(:code).returns(200)
|
57
|
+
mock_response.stubs(:body).returns('RESPONSE')
|
58
|
+
Net::HTTP.set_mock_http(mock_http)
|
59
|
+
subject.remote_file_contents!.should == 'RESPONSE'
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should throw a NotFoundException on 404' do
|
63
|
+
mock_http = mock
|
64
|
+
mock_http.expects(:get).with('/scaffolds/autocomplete/spec').returns(mock_response = mock)
|
65
|
+
mock_response.stubs(:code).returns(404)
|
66
|
+
Net::HTTP.set_mock_http(mock_http)
|
67
|
+
lambda { subject.remote_file_contents! }.should raise_error(Scaffoldhub::NotFoundException)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should throw a NetworkErrorException on some other error' do
|
71
|
+
mock_http = mock
|
72
|
+
mock_http.expects(:get).with('/scaffolds/autocomplete/spec').raises(Errno::ECONNREFUSED)
|
73
|
+
Net::HTTP.set_mock_http(mock_http)
|
74
|
+
lambda { subject.remote_file_contents! }.should raise_error(Scaffoldhub::NetworkErrorException)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe 'Net:HTTP SSL calls' do
|
79
|
+
#TBD
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Scaffoldhub::ScaffoldSpec do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@status_proc = mock
|
7
|
+
@status_proc.stubs(:call)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'parsing scaffold spec' do
|
11
|
+
|
12
|
+
describe 'parsing local scaffold spec' do
|
13
|
+
|
14
|
+
subject do
|
15
|
+
test_spec_path = File.join(File.dirname(__FILE__), 'fixtures', 'test_scaffoldspec.rb')
|
16
|
+
Scaffoldhub::ScaffoldSpec.new(test_spec_path, true, @status_proc)
|
17
|
+
end
|
18
|
+
|
19
|
+
before do
|
20
|
+
Scaffoldhub::Specification.files = []
|
21
|
+
Scaffoldhub::Specification.base_url = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should require a local scaffold spec and parse the file list' do
|
25
|
+
subject.download_and_parse!
|
26
|
+
subject.template_file_specs.should == [
|
27
|
+
{ :type => 'erb', :src => 'templates/index3.html.erb', :dest => '' },
|
28
|
+
{ :type => 'controller', :src => 'templates/index2.html.erb', :dest => '' },
|
29
|
+
{ :type => 'other', :src => 'templates/index.html.erb', :dest => 'app/views/welcome' }
|
30
|
+
]
|
31
|
+
subject.base_url.should == File.expand_path(File.join(File.dirname(__FILE__), 'fixtures'))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'parsing remote scaffold spec' do
|
36
|
+
|
37
|
+
TEST_YAML = <<YAML
|
38
|
+
---
|
39
|
+
:base_url: http://github.com/patshaughnessy/scaffolds/default
|
40
|
+
:files:
|
41
|
+
- :src: templates/index3.html.erb
|
42
|
+
:dest:
|
43
|
+
:type: erb
|
44
|
+
- :src: templates/index2.html.erb
|
45
|
+
:dest:
|
46
|
+
:type: controller
|
47
|
+
- :src: templates/index.html.erb
|
48
|
+
:dest: app/views/welcome
|
49
|
+
:type: other
|
50
|
+
YAML
|
51
|
+
|
52
|
+
subject do
|
53
|
+
Scaffoldhub::ScaffoldSpec.new('http://fake.scaffoldhub.org:1234/scaffolds/autocomplete/spec', false, @status_proc)
|
54
|
+
end
|
55
|
+
|
56
|
+
before do
|
57
|
+
Scaffoldhub::Specification.files = []
|
58
|
+
Scaffoldhub::Specification.base_url = nil
|
59
|
+
subject.expects(:remote_file_contents!).returns(TEST_YAML)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should require a local scaffold spec' do
|
63
|
+
subject.download_and_parse!
|
64
|
+
subject.template_file_specs.should == [
|
65
|
+
{ :type => 'erb', :src => 'templates/index3.html.erb', :dest => nil },
|
66
|
+
{ :type => 'controller', :src => 'templates/index2.html.erb', :dest => nil },
|
67
|
+
{ :type => 'other', :src => 'templates/index.html.erb', :dest => 'app/views/welcome' }
|
68
|
+
]
|
69
|
+
subject.base_url.should == 'http://github.com/patshaughnessy/scaffolds/default'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe '#select_files' do
|
75
|
+
|
76
|
+
subject do
|
77
|
+
Scaffoldhub::ScaffoldSpec.new('unused', true, @status_proc)
|
78
|
+
end
|
79
|
+
|
80
|
+
before do
|
81
|
+
subject.stubs(:template_file_specs).returns([
|
82
|
+
{ :type => 'type1', :src => 'some_src', :dest => 'some_dest' },
|
83
|
+
{ :type => 'type1', :src => 'some_src2', :dest => 'some_dest' },
|
84
|
+
{ :type => 'type1', :src => 'some_src3', :dest => 'some_dest' },
|
85
|
+
{ :type => 'type2', :src => 'some_src4', :dest => 'some_dest' }
|
86
|
+
])
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should select the files with the given type' do
|
90
|
+
Scaffoldhub::TemplateFile.expects(:new).returns(mock1 = mock)
|
91
|
+
Scaffoldhub::TemplateFile.expects(:new).returns(mock2 = mock)
|
92
|
+
Scaffoldhub::TemplateFile.expects(:new).returns(mock3 = mock)
|
93
|
+
files = subject.select_files('type1')
|
94
|
+
files.include?(mock1).should be_true
|
95
|
+
files.include?(mock2).should be_true
|
96
|
+
files.include?(mock3).should be_true
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe '#find_file' do
|
101
|
+
|
102
|
+
subject do
|
103
|
+
Scaffoldhub::ScaffoldSpec.new('unused', true, @status_proc)
|
104
|
+
end
|
105
|
+
|
106
|
+
before do
|
107
|
+
subject.stubs(:template_file_specs).returns([
|
108
|
+
{ :type => 'type1', :src => 'some_src', :dest => 'some_dest' },
|
109
|
+
{ :type => 'type1', :src => 'some_src2', :dest => 'some_dest' },
|
110
|
+
{ :type => 'type1', :src => 'some_src3', :dest => 'some_dest' },
|
111
|
+
{ :type => 'type2', :src => 'some_src4', :dest => 'some_dest' }
|
112
|
+
])
|
113
|
+
subject.stubs(:base_url).returns('base')
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'should find the file with the given type and src' do
|
117
|
+
Scaffoldhub::TemplateFile.expects(:new).with('some_src2', 'some_dest', true, 'base', @status_proc).returns(mock1 = mock)
|
118
|
+
subject.find_file('type1', 'some_src2').should == mock1
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'should return nil if the name is not found' do
|
122
|
+
Scaffoldhub::TemplateFile.expects(:new).never
|
123
|
+
subject.find_file('type1', 'some_src5').should be_nil
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'should return nil if the type is not found' do
|
127
|
+
Scaffoldhub::TemplateFile.expects(:new).never
|
128
|
+
subject.find_file('type3', 'some_src2').should be_nil
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'mocha'
|
5
|
+
require 'scaffoldhub'
|
6
|
+
|
7
|
+
# Requires supporting ruby files with custom matchers and macros, etc,
|
8
|
+
# in spec/support/ and its subdirectories.
|
9
|
+
Dir[File.join(File.dirname(__FILE__), "spec/support/**/*.rb")].each {|f| require f}
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.mock_with :mocha
|
13
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
+
|
4
|
+
describe Scaffoldhub::TemplateFile do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@status_proc = mock
|
8
|
+
@status_proc.stubs(:call)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'local template file' do
|
12
|
+
|
13
|
+
subject { Scaffoldhub::TemplateFile.new('templates/index.html', 'public', true, File.expand_path(File.dirname(__FILE__)), @status_proc) }
|
14
|
+
|
15
|
+
its(:src) { should == File.expand_path(File.join(File.dirname(__FILE__), 'templates', 'index.html')) }
|
16
|
+
its(:url) { should == File.expand_path(File.join(File.dirname(__FILE__), 'templates', 'index.html')) }
|
17
|
+
its(:dest) { should == File.join('public', 'index.html') }
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'remote template file' do
|
21
|
+
|
22
|
+
FAKE_GITHUB_URL = 'http://github.com/patshaughnessy/scaffolds/default'
|
23
|
+
|
24
|
+
subject { Scaffoldhub::TemplateFile.new('templates/index.html', 'public', false, FAKE_GITHUB_URL, @status_proc) }
|
25
|
+
|
26
|
+
its(:url) { should == FAKE_GITHUB_URL + '/templates/index.html' }
|
27
|
+
its(:dest) { should == File.join('public', 'index.html') }
|
28
|
+
|
29
|
+
describe '#download!' do
|
30
|
+
|
31
|
+
before do
|
32
|
+
@local_path = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'local_template_file.txt'))
|
33
|
+
File.delete(@local_path) if File.exists?(@local_path)
|
34
|
+
subject.stubs(:remote_file_contents!).returns('TEMPLATE')
|
35
|
+
tempfile = mock
|
36
|
+
Tempfile.stubs(:new).returns(tempfile)
|
37
|
+
tempfile.stubs(:path).returns(@local_path)
|
38
|
+
subject.download!
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should set the src to the local path after a download' do
|
42
|
+
subject.src.should == @local_path
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should write the template file contents into a local file' do
|
46
|
+
File.new(@local_path).read.should == 'TEMPLATE'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scaffoldhub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Pat Shaughnessy
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-03-
|
18
|
+
date: 2011-03-07 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -80,7 +80,7 @@ dependencies:
|
|
80
80
|
version: 0.9.10
|
81
81
|
type: :development
|
82
82
|
version_requirements: *id004
|
83
|
-
description:
|
83
|
+
description: Run Rails scaffold generator with customized templates downloaded from scaffoldhub.org
|
84
84
|
email:
|
85
85
|
- pat@patshaughnessy.net
|
86
86
|
executables: []
|
@@ -93,17 +93,26 @@ files:
|
|
93
93
|
- .gitignore
|
94
94
|
- Gemfile
|
95
95
|
- Gemfile.lock
|
96
|
+
- README.rdoc
|
96
97
|
- Rakefile
|
98
|
+
- lib/generators/active_record/scaffoldhub_generator.rb
|
97
99
|
- lib/generators/erb/scaffoldhub_generator.rb
|
98
100
|
- lib/generators/scaffold_controller/scaffoldhub_generator.rb
|
99
101
|
- lib/generators/scaffoldhub/scaffoldhub_generator.rb
|
100
102
|
- lib/scaffoldhub.rb
|
101
103
|
- lib/scaffoldhub/helper.rb
|
102
104
|
- lib/scaffoldhub/remote_file.rb
|
103
|
-
- lib/scaffoldhub/
|
104
|
-
- lib/scaffoldhub/
|
105
|
+
- lib/scaffoldhub/scaffold_spec.rb
|
106
|
+
- lib/scaffoldhub/specification.rb
|
105
107
|
- lib/scaffoldhub/template_file.rb
|
106
108
|
- scaffoldhub.gemspec
|
109
|
+
- spec/fixtures/test_scaffoldspec.rb
|
110
|
+
- spec/helper_spec.rb
|
111
|
+
- spec/remote_file_spec.rb
|
112
|
+
- spec/scaffold_spec_spec.rb
|
113
|
+
- spec/spec_helper.rb
|
114
|
+
- spec/template_file_spec.rb
|
115
|
+
- util/spec_to_yaml.rb
|
107
116
|
has_rdoc: true
|
108
117
|
homepage: http://scaffoldhub.org
|
109
118
|
licenses: []
|
@@ -137,6 +146,11 @@ rubyforge_project:
|
|
137
146
|
rubygems_version: 1.4.2
|
138
147
|
signing_key:
|
139
148
|
specification_version: 3
|
140
|
-
summary: Generate Rails scaffolding from scaffoldhub.org
|
141
|
-
test_files:
|
142
|
-
|
149
|
+
summary: Generate customized Rails scaffolding from scaffoldhub.org
|
150
|
+
test_files:
|
151
|
+
- spec/fixtures/test_scaffoldspec.rb
|
152
|
+
- spec/helper_spec.rb
|
153
|
+
- spec/remote_file_spec.rb
|
154
|
+
- spec/scaffold_spec_spec.rb
|
155
|
+
- spec/spec_helper.rb
|
156
|
+
- spec/template_file_spec.rb
|
data/lib/scaffoldhub/scaffold.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
module Scaffoldhub
|
2
|
-
class Scaffold < RemoteFile
|
3
|
-
|
4
|
-
SERVER_NAME = 'scaffoldhub.org'
|
5
|
-
|
6
|
-
def initialize(scaffold, local, status_proc)
|
7
|
-
@scaffold = scaffold
|
8
|
-
@local = local
|
9
|
-
super(status_proc)
|
10
|
-
end
|
11
|
-
|
12
|
-
def base_url
|
13
|
-
if @local
|
14
|
-
File.dirname(File.expand_path(@scaffold))
|
15
|
-
else
|
16
|
-
spec_url.split(/\/(?=[^\/]+(?: |$))/)[0]
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def spec_url
|
21
|
-
@spec_url ||=
|
22
|
-
if @local
|
23
|
-
@scaffold
|
24
|
-
else
|
25
|
-
download.strip
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def local
|
30
|
-
@local
|
31
|
-
end
|
32
|
-
|
33
|
-
def url
|
34
|
-
"http://#{SERVER_NAME}/scaffolds/#{@scaffold}/spec"
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
end
|
@@ -1,42 +0,0 @@
|
|
1
|
-
module Scaffoldhub
|
2
|
-
class SpecFile < RemoteFile
|
3
|
-
|
4
|
-
def initialize(status_proc)
|
5
|
-
@status_proc = status_proc
|
6
|
-
super(status_proc)
|
7
|
-
end
|
8
|
-
|
9
|
-
def url
|
10
|
-
Scaffoldhub::Helper.scaffold.spec_url
|
11
|
-
end
|
12
|
-
|
13
|
-
def select_files(type)
|
14
|
-
if Scaffoldhub::Helper.scaffold.local
|
15
|
-
load_local
|
16
|
-
else
|
17
|
-
load_remote
|
18
|
-
end
|
19
|
-
@spec[:files].select { |file_spec| file_spec[:type] == type.to_s }.collect do |file_spec|
|
20
|
-
TemplateFile.new file_spec[:src], file_spec[:dest], @status_proc
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
protected
|
25
|
-
|
26
|
-
def load_local
|
27
|
-
parse(File.new(url).read)
|
28
|
-
end
|
29
|
-
|
30
|
-
def load_remote
|
31
|
-
parse(download)
|
32
|
-
end
|
33
|
-
|
34
|
-
def parse(data)
|
35
|
-
@spec = YAML::load(data) unless data.nil?
|
36
|
-
end
|
37
|
-
|
38
|
-
#def method_missing(name, *args, &blk)
|
39
|
-
##@spec.send(name)
|
40
|
-
#end
|
41
|
-
end
|
42
|
-
end
|