Class: Spring::Test::ApplicationGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/spring/test/application_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_constraint) ⇒ ApplicationGenerator

Returns a new instance of ApplicationGenerator.



6
7
8
9
10
11
# File 'lib/spring/test/application_generator.rb', line 6

def initialize(version_constraint)
  @version_constraint = version_constraint
  @version            = RailsVersion.new(version_constraint.split(' ').last)
  @application        = Application.new(root)
  @bundled            = false
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



4
5
6
# File 'lib/spring/test/application_generator.rb', line 4

def application
  @application
end

#versionObject (readonly)

Returns the value of attribute version.



4
5
6
# File 'lib/spring/test/application_generator.rb', line 4

def version
  @version
end

#version_constraintObject (readonly)

Returns the value of attribute version_constraint.



4
5
6
# File 'lib/spring/test/application_generator.rb', line 4

def version_constraint
  @version_constraint
end

Instance Method Details

#build_and_install_gemsObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/spring/test/application_generator.rb', line 93

def build_and_install_gems
  manually_built_gems.each do |name|
    spec = Gem::Specification.find_by_name(name)

    FileUtils.cd(spec.gem_dir) do
      FileUtils.rm(Dir.glob("#{name}-*.gem"))
      system("gem build #{name}.gemspec 2>&1")
    end

    application.run! "gem install #{spec.gem_dir}/#{name}-*.gem", timeout: nil
  end
end

#copy_to(path) ⇒ Object



106
107
108
109
# File 'lib/spring/test/application_generator.rb', line 106

def copy_to(path)
  system("rm -rf #{path}")
  system("cp -r #{application.root} #{path}")
end

#gemspec(name) ⇒ Object



116
117
118
# File 'lib/spring/test/application_generator.rb', line 116

def gemspec(name)
  "#{Gem::Specification.find_by_name(name).gem_dir}/#{name}.gemspec"
end

#generateObject



32
33
34
35
36
# File 'lib/spring/test/application_generator.rb', line 32

def generate
  Bundler.with_clean_env { generate_files }
  install_spring
  generate_scaffold
end

#generate_filesObject

Sporadic SSL errors keep causing test failures so there are anti-SSL workarounds here



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/spring/test/application_generator.rb', line 39

def generate_files
  system("gem list '^rails$' --installed --version '#{version_constraint}' || " \
           "gem install rails --clear-sources --source http://rubygems.org --version '#{version_constraint}'")

  @version = RailsVersion.new(`ruby -e 'puts Gem::Specification.find_by_name("rails", "#{version_constraint}").version'`.chomp)

  skips = %w(--skip-bundle --skip-javascript --skip-sprockets)
  skips << "--skip-spring" if version.bundles_spring?

  system("rails _#{version}_ new #{application.root} #{skips.join(' ')}")
  raise "application generation failed" unless application.exists?

  FileUtils.mkdir_p(application.gem_home)
  FileUtils.mkdir_p(application.user_home)
  FileUtils.rm_rf(application.path("test/performance"))

  File.write(application.gemfile, "#{application.gemfile.read}gem 'spring', '#{Spring::VERSION}'\n")

  if version.needs_testunit?
    File.write(application.gemfile, "#{application.gemfile.read}gem 'spring-commands-testunit'\n")
  end

  File.write(application.gemfile, application.gemfile.read.sub("https://rubygems.org", "http://rubygems.org"))

  if application.path("bin").exist?
    FileUtils.cp_r(application.path("bin"), application.path("bin_original"))
  end
end

#generate_if_missingObject



68
69
70
# File 'lib/spring/test/application_generator.rb', line 68

def generate_if_missing
  generate unless application.exists?
end

#generate_scaffoldObject



111
112
113
114
# File 'lib/spring/test/application_generator.rb', line 111

def generate_scaffold
  application.run! "bundle exec rails g scaffold post title:string"
  application.run! "bundle exec rake db:migrate db:test:clone"
end

#install_springObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/spring/test/application_generator.rb', line 72

def install_spring
  return if @installed

  build_and_install_gems

  application.bundle

  FileUtils.rm_rf application.path("bin")

  if application.path("bin_original").exist?
    FileUtils.cp_r application.path("bin_original"), application.path("bin")
  end

  application.run! "#{application.spring} binstub --all"
  @installed = true
end

#manually_built_gemsObject



89
90
91
# File 'lib/spring/test/application_generator.rb', line 89

def manually_built_gems
  %w(spring)
end

#rootObject



17
18
19
# File 'lib/spring/test/application_generator.rb', line 17

def root
  test_root.join("apps/rails-#{version.major}-#{version.minor}-spring-#{Spring::VERSION}")
end

#system(command) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/spring/test/application_generator.rb', line 21

def system(command)
  if ENV["SPRING_DEBUG"]
    puts "$ #{command}\n"
  else
    command = "(#{command}) > /dev/null"
  end

  Kernel.system(command) or raise "command failed: #{command}"
  puts if ENV["SPRING_DEBUG"]
end

#test_rootObject



13
14
15
# File 'lib/spring/test/application_generator.rb', line 13

def test_root
  Pathname.new Spring::Test.root
end