Class: Versionist::CopyApiVersionGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
InflectorFixes
Defined in:
lib/generators/versionist/copy_api_version/copy_api_version_generator.rb

Instance Method Summary collapse

Methods included from InflectorFixes

#module_name_for_path, #module_name_for_route

Instance Method Details

#copy_controller_testsObject

due to the inflector quirks we can’t use hook_for :test_framework



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
# File 'lib/generators/versionist/copy_api_version/copy_api_version_generator.rb', line 55

def copy_controller_tests
  in_root do
    case Versionist.configuration.configured_test_framework
    when :test_unit
      if File.exists? "#{Versionist.test_path}/#{module_name_for_path(old_module_name)}"
        log "Copying all files from #{Versionist.test_path}/#{module_name_for_path(old_module_name)} to #{Versionist.test_path}/#{module_name_for_path(new_module_name)}"
        FileUtils.cp_r "#{Versionist.test_path}/#{module_name_for_path(old_module_name)}", "#{Versionist.test_path}/#{module_name_for_path(new_module_name)}"
        Dir.glob("#{Versionist.test_path}/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
          text = File.read(f)
          File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
        end
      else
        say "No tests found in #{Versionist.test_path} for #{old_version}"
      end

      if Versionist.older_than_rails_5?
        if File.exists? "test/integration/#{module_name_for_path(old_module_name)}"
          log "Copying all files from test/integration/#{module_name_for_path(old_module_name)} to test/integration/#{module_name_for_path(new_module_name)}"
          FileUtils.cp_r "test/integration/#{module_name_for_path(old_module_name)}", "test/integration/#{module_name_for_path(new_module_name)}"
          Dir.glob("test/integration/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
            text = File.read(f)
            File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
          end
        else
          say "No integration tests found in test/integration for #{old_version}"
        end
      end
    when :rspec
      if File.exists? "spec/controllers/#{module_name_for_path(old_module_name)}"
        log "Copying all files from spec/controllers/#{module_name_for_path(old_module_name)} to spec/controllers/#{module_name_for_path(new_module_name)}"
        FileUtils.cp_r "spec/controllers/#{module_name_for_path(old_module_name)}", "spec/controllers/#{module_name_for_path(new_module_name)}"
        Dir.glob("spec/controllers/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
          text = File.read(f)
          File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
        end
      else
        say "No controller specs found in spec/controllers for #{old_version}"
      end

      if File.exists? "spec/requests/#{module_name_for_path(old_module_name)}"
        log "Copying all files from spec/requests/#{module_name_for_path(old_module_name)} to spec/requests/#{module_name_for_path(new_module_name)}"
        FileUtils.cp_r "spec/requests/#{module_name_for_path(old_module_name)}", "spec/requests/#{module_name_for_path(new_module_name)}"
        Dir.glob("spec/requests/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
          text = File.read(f)
          File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
        end
      else
        say "No request specs found in spec/requests for #{old_version}"
      end
    else
      say "Unsupported test_framework: #{Versionist.configuration.configured_test_framework}"
    end
  end
end

#copy_controllersObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/generators/versionist/copy_api_version/copy_api_version_generator.rb', line 39

def copy_controllers
  in_root do
    if File.exists? "app/controllers/#{module_name_for_path(old_module_name)}"
      log "Copying all files from app/controllers/#{module_name_for_path(old_module_name)} to app/controllers/#{module_name_for_path(new_module_name)}"
      FileUtils.cp_r "app/controllers/#{module_name_for_path(old_module_name)}", "app/controllers/#{module_name_for_path(new_module_name)}"
      Dir.glob("app/controllers/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
        text = File.read(f)
        File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
      end
    else
      say "No controllers found in app/controllers for #{old_version}"
    end
  end
end

#copy_documentationObject



202
203
204
205
206
207
208
209
210
211
# File 'lib/generators/versionist/copy_api_version/copy_api_version_generator.rb', line 202

def copy_documentation
  in_root do
    if File.exists? "public/docs/#{old_version}"
      log "Copying all files from public/docs/#{old_version} to public/docs/#{new_version}"
      FileUtils.cp_r "public/docs/#{old_version}/.", "public/docs/#{new_version}"
    else
      say "No documentation found in public/docs for #{old_version}"
    end
  end
end

#copy_helper_testsObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/generators/versionist/copy_api_version/copy_api_version_generator.rb', line 171

def copy_helper_tests
  in_root do
    case Versionist.configuration.configured_test_framework
    when :test_unit
      if File.exists? "test/helpers/#{module_name_for_path(old_module_name)}"
        log "Copying all files from test/helpers/#{module_name_for_path(old_module_name)} to test/helpers/#{module_name_for_path(new_module_name)}"
        FileUtils.cp_r "test/helpers/#{module_name_for_path(old_module_name)}", "test/helpers/#{module_name_for_path(new_module_name)}"
        Dir.glob("test/helpers/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
          text = File.read(f)
          File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
        end
      else
        say "No helper tests found in test/helpers for #{old_version}"
      end
    when :rspec
      if File.exists? "spec/helpers/#{module_name_for_path(old_module_name)}"
        log "Copying all files from spec/helpers/#{module_name_for_path(old_module_name)} to spec/helpers/#{module_name_for_path(new_module_name)}"
        FileUtils.cp_r "spec/helpers/#{module_name_for_path(old_module_name)}", "spec/helpers/#{module_name_for_path(new_module_name)}"
        Dir.glob("spec/helpers/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
          text = File.read(f)
          File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
        end
      else
        say "No helper specs found in spec/helpers for #{old_version}"
      end
    else
      say "Unsupported test_framework: #{Versionist.configuration.configured_test_framework}"
    end
  end
end

#copy_helpersObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/generators/versionist/copy_api_version/copy_api_version_generator.rb', line 156

def copy_helpers
  in_root do
    if File.exists? "app/helpers/#{module_name_for_path(old_module_name)}"
      log "Copying all files from app/helpers/#{module_name_for_path(old_module_name)} to app/helpers/#{module_name_for_path(new_module_name)}"
      FileUtils.cp_r "app/helpers/#{module_name_for_path(old_module_name)}", "app/helpers/#{module_name_for_path(new_module_name)}"
      Dir.glob("app/helpers/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
        text = File.read(f)
        File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
      end
    else
      say "No helpers found in app/helpers for #{old_version}"
    end
  end
end

#copy_presenter_testsObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/generators/versionist/copy_api_version/copy_api_version_generator.rb', line 125

def copy_presenter_tests
  in_root do
    case Versionist.configuration.configured_test_framework
    when :test_unit
      if File.exists? "test/presenters/#{module_name_for_path(old_module_name)}"
        log "Copying all files from test/presenters/#{module_name_for_path(old_module_name)} to test/presenters/#{module_name_for_path(new_module_name)}"
        FileUtils.cp_r "test/presenters/#{module_name_for_path(old_module_name)}", "test/presenters/#{module_name_for_path(new_module_name)}"
        Dir.glob("test/presenters/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
          text = File.read(f)
          File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
        end
      else
        say "No presenter tests found in test/presenters for #{old_version}"
      end
    when :rspec
      if File.exists? "spec/presenters/#{module_name_for_path(old_module_name)}"
        log "Copying all files from spec/presenters/#{module_name_for_path(old_module_name)} to spec/presenters/#{module_name_for_path(new_module_name)}"
        FileUtils.cp_r "spec/presenters/#{module_name_for_path(old_module_name)}", "spec/presenters/#{module_name_for_path(new_module_name)}"
        Dir.glob("spec/presenters/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
          text = File.read(f)
          File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
        end
      else
        say "No presenter specs found in spec/presenters for #{old_version}"
      end
    else
      say "Unsupported test_framework: #{Versionist.configuration.configured_test_framework}"
    end
  end
end

#copy_presentersObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/generators/versionist/copy_api_version/copy_api_version_generator.rb', line 110

def copy_presenters
  in_root do
    if File.exists? "app/presenters/#{module_name_for_path(old_module_name)}"
      log "Copying all files from app/presenters/#{module_name_for_path(old_module_name)} to app/presenters/#{module_name_for_path(new_module_name)}"
      FileUtils.cp_r "app/presenters/#{module_name_for_path(old_module_name)}", "app/presenters/#{module_name_for_path(new_module_name)}"
      Dir.glob("app/presenters/#{module_name_for_path(new_module_name)}/*.rb").each do |f|
        text = File.read(f)
        File.open(f, 'w') {|f| f << text.gsub(/#{old_module_name}/, new_module_name)}
      end
    else
      say "No presenters found in app/presenters for #{old_version}"
    end
  end
end

#copy_routesObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/generators/versionist/copy_api_version/copy_api_version_generator.rb', line 17

def copy_routes
  in_root do
    if RUBY_VERSION =~ /1.8/ || !defined?(RUBY_ENGINE) || RUBY_ENGINE != "ruby"
      log "ERROR: Cannot copy routes as this feature relies on the Ripper library, which is only available in MRI 1.9."
      return
    end
    parser = YARD::Parser::SourceParser.parse_string(File.read("config/routes.rb"))
    existing_routes = nil
    parser.enumerator.first.traverse do |node|
      existing_routes = node.source if node.type == :fcall && node.source =~ /api_version.*:?module\s*(=>|:)\s*("|')#{module_name_for_route(old_module_name)}("|')/
    end
    if !existing_routes.nil?
      copied_routes = String.new(existing_routes)
      copied_routes.gsub!(/"#{module_name_for_route(old_module_name)}"/, "\"#{module_name_for_route(new_module_name)}\"")
      copied_routes.gsub!(/#{old_version}/, new_version)
      route copied_routes
    else
      say "No routes found in config/routes.rb for #{old_version}"
    end
  end
end