Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environments #1293

Merged
merged 1 commit into from
Jun 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
master
===

* Add support for `environments` with the `-e` CLI flag. Loads additional config from `environments/envname.rb`. Removed `development?` helper in favor of `environment?(:development)`. Added `server?` helper to differentiate between build and server mode.
* Removed `with_layout`. Use loops of `page` instead.
* Removed Queryable Sitemap API
* Removed `css_compressor` setting, use `activate :minify_css, :compressor =>` instead.
Expand Down
36 changes: 19 additions & 17 deletions middleman-cli/lib/middleman-cli/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class Build < Thor
namespace :build

desc 'build [options]', 'Builds the static site for deployment'
method_option :environment,
aliases: '-e',
default: ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development',
desc: 'The environment Middleman will run under'
method_option :clean,
type: :boolean,
default: true,
Expand Down Expand Up @@ -58,18 +62,26 @@ def build
@debugging = Middleman::Cli::Base.respond_to?(:debugging) && Middleman::Cli::Base.debugging
self.had_errors = false

self.class.shared_instance(options['verbose'], options['instrument'])
env = options['environment'].to_sym
verbose = options['verbose'] ? 0 : 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't verbose be a boolean?

instrument = options['instrument']

app = ::Middleman::Application.server.inst do
config[:mode] = :build
config[:environment] = env
::Middleman::Logger.singleton(verbose, instrument)
end

opts = {}
opts[:glob] = options['glob'] if options.key?('glob')
opts[:clean] = options['clean']

self.class.shared_instance.run_hook :before_build, self
app.run_hook :before_build, self

action BuildAction.new(self, opts)
action BuildAction.new(self, app, opts)

self.class.shared_instance.run_hook :after_build, self
self.class.shared_instance.config_context.execute_after_build_callbacks(self)
app.run_hook :after_build, self
app.config_context.execute_after_build_callbacks(self)

if had_errors && !debugging
msg = 'There were errors during this build'
Expand All @@ -87,16 +99,6 @@ class << self
def exit_on_failure?
true
end

# Middleman::Application singleton
#
# @return [Middleman::Application]
def shared_instance(verbose=false, instrument=false)
@_shared_instance ||= ::Middleman::Application.server.inst do
config[:environment] = :build
::Middleman::Logger.singleton(verbose ? 0 : 1, instrument)
end
end
end
end

Expand All @@ -109,8 +111,8 @@ class BuildAction < ::Thor::Actions::EmptyDirectory
#
# @param [Middleman::Cli::Build] base
# @param [Hash] config
def initialize(base, config={})
@app = base.class.shared_instance
def initialize(base, app, config={})
@app = app
@source_dir = Pathname(@app.source_dir)
@build_dir = Pathname(@app.config[:build_dir])
@to_clean = Set.new
Expand Down
4 changes: 1 addition & 3 deletions middleman-core/features/mount_rack.feature
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ Feature: Support Rack apps mounted using map
run MySinatra
end

configure :build do
endpoint "sinatra/index2.html", path: "/sinatra/"
end
endpoint "sinatra/index2.html", path: "/sinatra/"

endpoint "dedoo.html", path: "/sinatra/derp.html"

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set :api_key, "dev1"
2 changes: 2 additions & 0 deletions middleman-core/fixtures/env-app/environments/production.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set :api_key, "prod2"
activate :minify_css
3 changes: 3 additions & 0 deletions middleman-core/fixtures/env-app/source/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script>
// load: <%= config[:api_key] %>
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
backgroud: red;
}
4 changes: 2 additions & 2 deletions middleman-core/fixtures/generator-test/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
# Change the images directory
# set :images_dir, "alternative_image_directory"

# Build-specific configuration
configure :build do
# Production build configuration
configure :production do
# For example, change the Compass output style for deployment
# activate :minify_css

Expand Down
34 changes: 18 additions & 16 deletions middleman-core/lib/middleman-core/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
# Custom Extension API and config.rb handling
require 'middleman-core/core_extensions/extensions'

# Catch and show exceptions at the Rack level
require 'middleman-core/core_extensions/show_exceptions'

# Core Middleman Class
module Middleman
class Application
Expand Down Expand Up @@ -73,9 +70,13 @@ def self.root_path
# @return [String]
config.define_setting :source, 'source', 'Name of the source directory'

# Middleman environment. Defaults to :development, set to :build by the build process
# Middleman mode. Defaults to :server, set to :build by the build process
# @return [String]
config.define_setting :mode, ((ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :server), 'Middleman mode. Defaults to :server'

# Middleman environment. Defaults to :development
# @return [String]
config.define_setting :environment, ((ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development), 'Middleman environment. Defaults to :development, set to :build by the build process'
config.define_setting :environment, :development, 'Middleman environment. Defaults to :development'

# Which file should be used for directory indexes
# @return [String]
Expand Down Expand Up @@ -139,9 +140,6 @@ def self.root_path
# Basic Rack Request Handling
include Middleman::CoreExtensions::Request

# Handle exceptions
include Middleman::CoreExtensions::ShowExceptions

# Setup custom rendering
include Middleman::CoreExtensions::Rendering

Expand Down Expand Up @@ -179,9 +177,7 @@ def initialize
# Setup the default values from calls to set before initialization
self.class.config.load_settings(self.class.superclass.config.all_settings)

::Middleman::Extensions.auto_activate[:before_sitemap].each do |ext_name|
activate ext_name
end
::Middleman::Extensions.auto_activate(:before_sitemap, self)

# Initialize the Sitemap
@sitemap = ::Middleman::Sitemap::Store.new(self)
Expand All @@ -204,16 +200,22 @@ def add_to_config_context(name, &func)
@config_context.define_singleton_method(name, &func)
end

# Whether we're in development mode
# Whether we're in server mode
# @return [Boolean] If we're in dev mode
def development?
config[:environment] == :development
def server?
config[:mode] == :server
end

# Whether we're in build mode
# @return [Boolean] If we're in build mode
# @return [Boolean] If we're in dev mode
def build?
config[:environment] == :build
config[:mode] == :build
end

# Whether we're in a specific environment
# @return [Boolean]
def environment?(key)
config[:environment] == key
end

# The full path to the source directory
Expand Down
10 changes: 9 additions & 1 deletion middleman-core/lib/middleman-core/config_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ConfigContext
attr_reader :app

# Whitelist methods that can reach out.
delegate :config, :logger, :activate, :use, :map, :mime_type, :data, :root, to: :app
delegate :config, :logger, :activate, :use, :map, :mime_type, :data, :files, :root, to: :app

def initialize(app, template_context_class)
@app = app
Expand All @@ -34,6 +34,14 @@ def helpers(*helper_modules, &block)
end
end

def include_environment(name)
path = File.dirname(__FILE__)
other_config = File.join(path, name.to_s)
if File.exist? other_config
instance_eval File.read(other_config), other_config, 1
end
end

def ready(&block)
@ready_callbacks << block
end
Expand Down
6 changes: 6 additions & 0 deletions middleman-core/lib/middleman-core/core_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
Middleman::CoreExtensions::Data
end

# Catch and show exceptions at the Rack level
Middleman::Extensions.register :show_exceptions, auto_activate: :before_configuration, modes: [:server] do
require 'middleman-core/core_extensions/show_exceptions'
Middleman::CoreExtensions::ShowExceptions
end

# File Change Notifier
Middleman::Extensions.register :file_watcher, auto_activate: :before_sitemap do
require 'middleman-core/core_extensions/file_watcher'
Expand Down
34 changes: 14 additions & 20 deletions middleman-core/lib/middleman-core/core_extensions/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ def self.included(app)
app.define_hook :instance_available
app.define_hook :after_configuration
app.define_hook :before_configuration
app.define_hook :build_config
app.define_hook :development_config

app.config.define_setting :autoload_sprockets, true, 'Automatically load sprockets at startup?'
app.config[:autoload_sprockets] = (ENV['AUTOLOAD_SPROCKETS'] == 'true') if ENV['AUTOLOAD_SPROCKETS']
Expand All @@ -26,11 +24,11 @@ module ClassMethods
#
# @example
# # Only minify when building
# configure :build do
# configure :production do
# activate :minify_javascript
# end
#
# @param [String, Symbol] env The environment to run in (:build, :development)
# @param [String, Symbol] env The environment to run in
# @return [void]
def configure(env, &block)
send("#{env}_config", &block)
Expand Down Expand Up @@ -80,18 +78,19 @@ def extensions

# Override application initialization to load `config.rb` and to call lifecycle hooks.
def initialize(&block)
super

self.class.inst = self

# Search the root of the project for required files
$LOAD_PATH.unshift(root) unless $LOAD_PATH.include?(root)

# Evaluate a passed block if given
config_context.instance_exec(&block) if block_given?

super

::Middleman::Extension.clear_after_extension_callbacks

::Middleman::Extensions.auto_activate[:before_configuration].each do |ext_name|
activate ext_name
end
::Middleman::Extensions.auto_activate(:before_configuration, self)

if config[:autoload_sprockets]
begin
Expand All @@ -102,29 +101,24 @@ def initialize(&block)
end
end

# Evaluate a passed block if given
config_context.instance_exec(&block) if block_given?

run_hook :initialized

run_hook :before_configuration

# Check for and evaluate local configuration in `config.rb`
local_config = File.join(root, 'config.rb')
if File.exist? local_config
logger.debug '== Reading: Local config'
logger.debug '== Reading: Local config'
config_context.instance_eval File.read(local_config), local_config, 1
end

if build?
run_hook :build_config
config_context.execute_configure_callbacks(:build)
env_config = File.join(root, 'environments', "#{config[:environment]}.rb")
if File.exist? env_config
logger.debug "== Reading: #{config[:environment]} config"
config_context.instance_eval File.read(env_config), env_config, 1
end

if development?
run_hook :development_config
config_context.execute_configure_callbacks(:development)
end
config_context.execute_configure_callbacks(config[:environment])

run_hook :instance_available

Expand Down
4 changes: 1 addition & 3 deletions middleman-core/lib/middleman-core/core_extensions/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ def after_configuration

configure_i18n

unless app.build?
logger.info "== Locales: #{langs.join(', ')} (Default #{@mount_at_root})"
end
logger.info "== Locales: #{langs.join(', ')} (Default #{@mount_at_root})"

# Don't output localizable files
app.ignore File.join(options[:templates_dir], '**')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
# Support rack/showexceptions during development
module Middleman
module CoreExtensions
module ShowExceptions
def self.included(app)
# Require lib
require 'rack/showexceptions'
module Middleman::CoreExtensions
class ShowExceptions < ::Middleman::Extension
def initialize(app, options_hash={}, &block)
super

# Whether to catch and display exceptions
# @return [Boolean]
app.config.define_setting :show_exceptions, true, 'Whether to catch and display exceptions'
require 'rack/showexceptions'
end

# When in dev
app.configure :development do
# Include middlemare
use ::Rack::ShowExceptions if config[:show_exceptions]
end
end
def after_configuration
app.use ::Rack::ShowExceptions
end
end
end
Loading