File: application_generator.rb

package info (click to toggle)
ruby-spring 2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 428 kB
  • sloc: ruby: 3,373; sh: 9; makefile: 7
file content (143 lines) | stat: -rw-r--r-- 4,345 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module Spring
  module Test
    class ApplicationGenerator
      attr_reader :version_constraint, :version, :application

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

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

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

      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

      def generate
        Bundler.with_clean_env { generate_files }
        install_spring
        generate_scaffold
      end

      # Sporadic SSL errors keep causing test failures so there are anti-SSL workarounds here
      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 --skip-spring --skip-listen --skip-system-test)

        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"))

        append_to_file(application.gemfile, "gem 'spring', '#{Spring::VERSION}'")

        rewrite_file(application.gemfile) do |c|
          c.sub!("https://rubygems.org", "http://rubygems.org")
          c.gsub!(/(gem '(byebug|web-console|sdoc|jbuilder)')/, "# \\1")

          if @version.to_s < '5.2'
            c.gsub!(/(gem 'sqlite3')/, "# \\1")
          end

          c
        end

        if @version.to_s < '5.2'
          append_to_file(application.gemfile, "gem 'sqlite3', '< 1.4'")
        end

        rewrite_file(application.path("config/environments/test.rb")) do |c|
          c.sub!(/config\.cache_classes\s*=\s*true/, "config.cache_classes = false")
          c
        end

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

      def rewrite_file(file)
        File.write(file, yield(file.read))
      end

      def append_to_file(file, add)
        rewrite_file(file) { |c| c << "#{add}\n" }
      end

      def generate_if_missing
        generate unless application.exists?
      end

      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

      def manually_built_gems
        %w(spring)
      end

      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 --no-doc", timeout: nil
        end
      end

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

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

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