Skip to content

Commit

Permalink
Ensure the dummy app uses the same template as real apps.
Browse files Browse the repository at this point in the history
  • Loading branch information
parndt committed Oct 29, 2017
1 parent 5fb301b commit 2ce8d43
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 58 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,6 @@ Gemfile.lock

# rspec failures
.rspec_failures

db/
config/initializers
31 changes: 5 additions & 26 deletions core/lib/generators/refinery/cms/cms_generator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'pathname'
require 'refinery/core/environment_checker'

module Refinery
class CmsGenerator < Rails::Generators::Base
Expand Down Expand Up @@ -186,36 +187,14 @@ def deploy_to_hosting_heroku!(message = nil)
run "heroku restart"
end

# Helper method to quickly convert destination_root to a Pathname for easy file path manipulation
# Helper method to quickly convert destination_root to a Pathname
# for easy file path manipulation
def destination_path
@destination_path ||= Pathname.new(self.destination_root)
end

def ensure_environments_are_sane!
# Massage environment files
%w(development test production).map{ |e| "config/environments/#{e}.rb"}.each do |env|
next unless destination_path.join(env).file?

# Refinery does not necessarily expect action_mailer to be available as
# we may not always require it (currently only the authentication extension).
# Rails, however, will optimistically place config entries for action_mailer.
current_mailer_config = File.read(destination_path.join(env)).to_s.
match(%r{^\s.+?config\.action_mailer\..+([\w\W]*\})?}).
to_a.flatten.first

if current_mailer_config.present?
new_mailer_config = [
" if config.respond_to?(:action_mailer)",
current_mailer_config.gsub(%r{\A\n+?}, ''). # remove extraneous newlines at the start
gsub(%r{^\ \ }) { |line| " #{line}" }, # add indentation on each line
" end"
].join("\n")

gsub_file env, current_mailer_config, new_mailer_config, :verbose => false
end

gsub_file env, "config.assets.compile = false", "config.assets.compile = true", :verbose => false
end
Refinery::Core::EnvironmentChecker.new(destination_path).call
end

def forced_overwriting?
Expand Down Expand Up @@ -310,7 +289,7 @@ def sanity_check_heroku_application_name!
message.join("\n")
end

options[:heroku] = '' if options[:heroku] == 'heroku'
options.delete(:heroku) if options[:heroku] == 'heroku'
end

def start_pretending?
Expand Down
41 changes: 31 additions & 10 deletions core/lib/generators/refinery/dummy/dummy_generator.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
require 'rails/generators'
require 'rails/generators/rails/app/app_generator'

module Refinery
class DummyGenerator < Rails::Generators::Base
desc "Creates blank Rails application, installs Refinery CMS, and all sample data"
desc "Creates a blank Rails application with Refinery CMS installed."

class_option :database, :default => ''

def self.source_paths
paths = self.superclass.source_paths
paths << File.expand_path('../templates', __FILE__)
paths.flatten
[
self.superclass.source_paths,
File.expand_path('../templates', __FILE__)
].flatten.compact
end

PASSTHROUGH_OPTIONS = [
:skip_active_record, :skip_javascript, :skip_action_cable, :skip_action_mailer, :database, :javascript, :quiet, :pretend, :force, :skip
:database,
:force,
:javascript,
:pretend,
:quiet,
:skip,
:skip_action_cable,
:skip_action_mailer,
:skip_active_record,
:skip_javascript
]

def generate_test_dummy
Expand All @@ -24,8 +33,13 @@ def generate_test_dummy
opts[:skip_bundle] = true
opts[:skip_action_cable] = true
opts[:skip_action_mailer] = true
opts[:skip_keeps] = true
opts[:skip_migrate] = true
opts[:template] = refinery_path.join("templates", "refinery", "edge.rb").to_s

invoke Rails::Generators::AppGenerator, [ File.expand_path(dummy_path, destination_root) ], opts
invoke Rails::Generators::AppGenerator,
[ File.expand_path(dummy_path, destination_root) ],
opts
end

def test_dummy_config
Expand Down Expand Up @@ -67,19 +81,22 @@ def test_dummy_inherited_templates

attr :database

protected
protected

def dummy_path
'spec/dummy'
end

def dummy_application_path
File.expand_path("#{dummy_path}/config/application.rb", destination_root)
end

def module_name
'Dummy'
end

def application_definition
@application_definition ||= begin
dummy_application_path = File.expand_path("#{dummy_path}/config/application.rb", destination_root)
unless options[:pretend] || !File.exists?(dummy_application_path)
contents = File.read(dummy_application_path)
contents[(contents.index("module #{module_name}"))..-1]
Expand All @@ -93,7 +110,11 @@ def camelized
end

def gemfile_path
'../../../../Gemfile'
"../../../../Gemfile"
end

def refinery_path
Pathname.new File.expand_path("../../../../../../", __FILE__)
end
end
end
54 changes: 54 additions & 0 deletions core/lib/refinery/core/environment_checker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Check that Rails application configuration is consistent with what we expect.
require 'pathname'

module Refinery
module Core
class EnvironmentChecker

def initialize(root_path, environments: %w(development test production))
@root_path = Pathname.new(root_path)
@environments = environments
end

def call
environment_files.each do |env_file|
# Refinery does not necessarily expect action_mailer to be available as
# we may not always require it (currently only the authentication extension).
# Rails, however, will optimistically place config entries for action_mailer.
current_mailer_config = mailer_config(env_file)

if current_mailer_config.present?
new_mailer_config = [
" if config.respond_to?(:action_mailer)",
current_mailer_config.gsub(%r{\A\n+?}, ''). # remove extraneous newlines at the start
gsub(%r{^\ \ }) { |line| " #{line}" }, # add indentation on each line
" end"
].join("\n")

env_file.write(
env_file.read.gsub(current_mailer_config, new_mailer_config)
)
end

env_file.write(
env_file.read.gsub("assets.compile = false", "assets.compile = true")
)
end
end

private
attr_reader :environments, :root_path

def environment_files
environments.map { |env| root_path.join("config", "environments", "#{env}.rb") }
.select(&:file?)
end

def mailer_config(environment_file)
root_path.join(environment_file).read.match(
%r{^\s.+?config\.action_mailer\..+([\w\W]*\})?}
).to_a.flatten.first
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def change
t.string :image_uid
t.string :image_ext

t.timestamps
t.timestamps null: false
end
end
end
5 changes: 3 additions & 2 deletions images/lib/generators/refinery/images/images_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Refinery
class ImagesGenerator < Rails::Generators::Base
class_option :skip_migrations, :type => :boolean, :default => false, :aliases => nil, :group => :runtime,
:desc => "Skip over installing or running migrations."
class_option :skip_migrations, :type => :boolean, :default => false,
:aliases => nil, :group => :runtime,
:desc => "Skip over installing or running migrations."

source_root File.expand_path('../templates', __FILE__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def up
t.text :body
t.integer :position

t.timestamps
t.timestamps null: false
end

add_index :refinery_page_parts, :id
Expand All @@ -29,7 +29,7 @@ def up
t.string :view_template
t.string :layout_template

t.timestamps
t.timestamps null: false
end

add_index :refinery_pages, :depth
Expand Down
5 changes: 3 additions & 2 deletions pages/lib/generators/refinery/pages/pages_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Refinery
class PagesGenerator < Rails::Generators::Base
class_option :skip_migrations, :type => :boolean, :default => false, :aliases => nil, :group => :runtime,
:desc => "Skip over installing or running migrations."
class_option :skip_migrations, :type => :boolean, :default => false,
:aliases => nil, :group => :runtime,
:desc => "Skip over installing or running migrations."

source_root File.expand_path('../templates', __FILE__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def change
t.string :file_uid
t.string :file_ext

t.timestamps
t.timestamps null: false
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Refinery
class ResourcesGenerator < Rails::Generators::Base
class_option :skip_migrations, :type => :boolean, :default => false, :aliases => nil, :group => :runtime,
:desc => "Skip over installing or running migrations."
class_option :skip_migrations, :type => :boolean, :default => false,
:aliases => nil, :group => :runtime,
:desc => "Skip over installing or running migrations."

source_root File.expand_path('../templates', __FILE__)

Expand Down
20 changes: 8 additions & 12 deletions templates/refinery/edge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,16 @@
gem "coffee-rails", :group => :assets
end

if ENV['REFINERY_PATH']
append_file 'Gemfile' do
"
gem 'refinerycms', path: '#{ENV['REFINERY_PATH']}'
"
end
refinerycms_source = if ENV['REFINERY_PATH']
"path: '#{ENV['REFINERY_PATH']}'"
else
append_file 'Gemfile' do
"
gem 'refinerycms', git: 'https://github.com/refinery/refinerycms', branch: 'master'
"
end
"git: 'https://github.com/refinery/refinerycms', branch: 'master'"
end

append_file 'Gemfile' do
"
gem 'refinerycms', #{refinerycms_source}
# Add support for searching inside Refinery's admin interface.
gem 'refinerycms-acts-as-indexed', ['~> 3.0', '>= 3.0.0']
Expand All @@ -46,10 +40,12 @@
run 'bundle install'

rake 'db:create'
require 'refinery/core/environment_checker'
Refinery::Core::EnvironmentChecker.new(destination_root).call
generate "refinery:cms --fresh-installation #{ARGV.join(' ')}"

say <<-SAY
============================================================================
Your new Refinery CMS application is now running on edge and mounted to /.
Your new Refinery CMS application is now running on edge and mounted at '/'
============================================================================
SAY

0 comments on commit 2ce8d43

Please sign in to comment.