Class: Engineer::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/engineer/install/install_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



5
6
7
# File 'lib/generators/engineer/install/install_generator.rb', line 5

def self.source_root
  @source_root ||= File.expand_path('../templates', __FILE__)
end

Instance Method Details

#append_gemspec_to_RakefileObject



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
# File 'lib/generators/engineer/install/install_generator.rb', line 41

def append_gemspec_to_Rakefile
  in_root do
    unless IO.read('Rakefile') =~ /Engineer::Tasks.new/
      append_file 'Rakefile' do
<<-RAKE

Engineer::Tasks.new do |gem|
  gem.name = "#{app_name}"
  gem.summary = %Q{TODO: one-line summary of your engine}
  gem.description = %Q{TODO: longer description of your engine}
  gem.email = "TODO"
  gem.homepage = "TODO"
  gem.authors = ["TODO"]
  gem.require_path = 'lib'
  gem.files =  FileList[
    "[A-Z]*",
    "{app,config,lib,public,spec,test}/**/*",
    "db/**/*.rb"
  ]

  # Include Bundler dependencies
  Bundler.definition.dependencies.each do |dependency|
    next if dependency.name == "engineer"

    if (dependency.groups & #{PRODUCTION_GROUPS.inspect}).any?
gem.add_dependency dependency.name, *dependency.requirement.as_list
    else
gem.add_development_dependency dependency.name, *dependency.requirement.as_list
    end
  end

  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
RAKE
      end
    end
  end
end

#create_engine_filesObject



9
10
11
# File 'lib/generators/engineer/install/install_generator.rb', line 9

def create_engine_files
  directory 'lib', 'lib'
end

#namespace_application_controllerObject



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
# File 'lib/generators/engineer/install/install_generator.rb', line 13

def namespace_application_controller
  unnested_path = Rails.application.paths.app.controllers.to_a.detect do |p|
    File.exists? File.join(p, 'application_controller.rb')
  end

  return unless unnested_path

  nested_path = File.join(unnested_path, app_name)
  empty_directory nested_path

  create_file File.join(nested_path, 'application_controller.rb') do
    content = File.read File.join(unnested_path, 'application_controller.rb')
    content.gsub!("class ApplicationController < ActionController::Base",
      "class #{app_module}::ApplicationController < defined?(ApplicationController) ? ApplicationController : ActionController::Base")
    content
  end

  remove_file File.join(unnested_path, 'application_controller.rb')

  controller_files = Rails.application.paths.app.controllers.to_a.collect do |p|
    Dir[File.join(p, "**", "*_controller.rb")]
  end.flatten

  controller_files.reject { |f| f =~ /application_controller\.rb$/ }.each do |file|
    gsub_file file, "< ApplicationController", "< #{app_module}::ApplicationController"
  end
end

#tweak_route_declarationObject



81
82
83
84
# File 'lib/generators/engineer/install/install_generator.rb', line 81

def tweak_route_declaration
  gsub_file(File.join("config", "routes.rb"),
    /#{Rails.application.class}.routes.draw/, "Rails.application.routes.draw")
end