Class: RefinerycmsGenerator

Inherits:
Refinery::Generators::EngineInstaller
  • Object
show all
Defined in:
lib/generators/refinerycms_generator.rb

Instance Method Summary collapse

Instance Method Details

#generateObject



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
# File 'lib/generators/refinerycms_generator.rb', line 11

def generate
  # Ensure the generator doesn't output anything using 'puts'
  self.silence_puts = true

  # Only pretend to do the next actions if this is Refinery to stay DRY
  if Rails.root == Refinery.root
    say_status :'-- pretending to make changes that happen in an actual installation --', nil, :yellow
    old_pretend = self.options[:pretend]
    new_options = self.options.dup
    new_options[:pretend] = true
    self.options = new_options
  end

  unless self.options[:update]
    # First, effectively move / rename files that get in the way of Refinery CMS
    %w(public/index.html app/views/layouts/application.html.erb public/javascripts/rails.js).each do |roadblock|
      if (roadblock_path = Rails.root.join(roadblock)).file?
        create_file "#{roadblock}.backup",
                    :verbose => true do roadblock_path.read end
        remove_file roadblock_path, :verbose => true
      end
    end

    # Copy asset files (JS, CSS) so they're ready to use.
    %w(application.css formatting.css home.css theme.css).map{ |ss|
      Refinery.roots('core').join('public', 'stylesheets', ss)
    }.reject{|ss| !ss.file?}.each do |stylesheet|
      copy_file stylesheet,
                Rails.root.join('public', 'stylesheets', stylesheet.basename),
                :verbose => true
    end
    copy_file Refinery.roots('core').join('public', 'javascripts', 'admin.js'),
              Rails.root.join('public', 'javascripts', 'admin.js'),
              :verbose => true
  end

  # Ensure the config.serve_static_assets setting is present and enabled
  %w(development test production).map{|e| "config/environments/#{e}.rb"}.each do |env|
    gsub_file env, %r{#.*config.serve_static_assets}, 'config.serve_static_assets', :verbose => false

    gsub_file env, "serve_static_assets = false", "serve_static_assets = true # Refinery CMS requires this to be true", :verbose => false

    unless Rails.root.join(env).read =~ %r{Refinery.rescue_not_found}
      append_file env, "Refinery.rescue_not_found = #{env.split('/').last.split('.rb').first == 'production'}"
    end
  end

  # Stop pretending
  if Rails.root == Refinery.root
    say_status :'-- finished pretending --', nil, :yellow
    new_options = self.options.dup
    new_options[:pretend] = old_pretend
    self.options = new_options
  end

  # Ensure .gitignore exists and append our rules to it.
  create_file ".gitignore" unless Rails.root.join('.gitignore').file?
  our_ignore_rules = self.class.source_root.join('.gitignore').read
  our_ignore_rules = our_ignore_rules.split('# REFINERY CMS DEVELOPMENT').first if Rails.root != Refinery.root
  append_file ".gitignore", our_ignore_rules

  # If the admin/base_controller.rb file exists, ensure it does not do the old inheritance
  if (admin_base = Rails.root.join('app', 'controllers', 'admin', 'base_controller.rb')).file?
    gsub_file admin_base,
              "# You can extend refinery backend with your own functions here and they will likely not get overriden in an update.",
              "",
              :verbose => self.options[:update]

    gsub_file admin_base, "< Refinery::AdminBaseController", "< ActionController::Base",
              :verbose => self.options[:update]
  end


  # Append seeds.
  create_file "db/seeds.rb" unless Rails.root.join('db', 'seeds.rb').file?
  append_file 'db/seeds.rb', :verbose => true do
    self.class.source_root.join('db', 'seeds.rb').read
  end

  force_options = self.options.dup
  force_options[:force] = self.options[:force] || self.options[:update]
  self.options = force_options
  # Seeds and migrations now need to be copied from their various engines.
  existing_source_root = self.class.source_root
  ::Refinery::Plugins.registered.pathnames.reject{|p| !p.join('db').directory?}.each do |pathname|
    self.class.source_root pathname
    super
  end
  self.class.source_root existing_source_root

  super

  # The engine installer only installs database templates.
  Pathname.glob(self.class.source_root.join('**', '*')).reject{|f|
    f.directory? or f.to_s =~ /\/db\//
  }.sort.each do |path|
    copy_file path, path.to_s.gsub(self.class.source_root.to_s, Rails.root.to_s)
  end

  # Ensure i18n exists and is up to date.
  if defined?(::Refinery::I18n)
    require 'generators/refinerycms_i18n_generator'
    ::RefinerycmsI18n.new.generate
  end
end