Expose all config options to CLIs. Helps with #1829

This commit is contained in:
Thomas Reynolds 2016-03-12 13:55:25 -08:00
parent d624dc4601
commit 1f3bf47e3c
9 changed files with 78 additions and 70 deletions

View file

@ -1,6 +1,7 @@
master
===
* Expose all top-level config options to CLI (flags now match config. latency -> watcher_latency, etc).
* Fix directory indexes with `.htm` files. #1821
# 4.1.2

View file

@ -9,6 +9,31 @@ end
require "middleman-core/load_paths"
Middleman.setup_load_paths
require 'middleman-core'
require 'middleman-core/logger'
module Middleman::Cli
class << self
attr_accessor :config
end
def self.import_config(base)
::Middleman::Cli.config.all_settings.each do |setting|
if setting.default.is_a?(String) || setting.default.is_a?(NilClass)
base.class_option setting.key,
type: :string,
default: setting.default,
desc: setting.description
elsif setting.default.is_a?(TrueClass) || setting.default.is_a?(FalseClass)
base.class_option setting.key,
type: :boolean,
default: setting.default,
desc: setting.description
end
end
end
end
require "middleman-cli"
# Change directory to the root
@ -19,5 +44,21 @@ if ARGV[0] != 'help' && (ARGV.length < 1 || ARGV.first.include?('-'))
ARGV.unshift('server')
end
::Middleman::Logger.singleton(3)
::Middleman::Cli.config = ::Middleman::Application.new do
config[:exit_before_ready] = true
end.config
# Require the Middleman version
require 'middleman-core/version'
# Include the core CLI items
require 'middleman-cli/init'
require 'middleman-cli/extension'
require 'middleman-cli/server'
require 'middleman-cli/build'
require 'middleman-cli/console'
require 'middleman-cli/config'
# Start the CLI
Middleman::Cli::Base.start(ARGV)

View file

@ -21,14 +21,3 @@ module Middleman::Cli
end
end
end
# Require the Middleman version
require 'middleman-core/version'
# Include the core CLI items
require 'middleman-cli/init'
require 'middleman-cli/extension'
require 'middleman-cli/server'
require 'middleman-cli/build'
require 'middleman-cli/console'
require 'middleman-cli/config'

View file

@ -1,3 +1,5 @@
require 'middleman-core/application'
# CLI Module
module Middleman::Cli
# The CLI Build class
@ -8,8 +10,7 @@ module Middleman::Cli
class_option :environment,
aliases: '-e',
default: ENV['MM_ENV'] || ENV['RACK_ENV'] || 'production',
desc: 'The environment Middleman will run under'
default: :production
class_option :clean,
type: :boolean,
default: true,
@ -36,6 +37,8 @@ module Middleman::Cli
default: false,
desc: 'Generate profiling report for the build'
Middleman::Cli.import_config(self)
# Core build Thor command
# @return [void]
def build
@ -48,19 +51,27 @@ module Middleman::Cli
require 'middleman-core/builder'
require 'fileutils'
env = options['environment'].to_sym
verbose = options['verbose'] ? 0 : 1
instrument = options['instrument']
builder = nil
cli_options = options
::Middleman::Logger.singleton(verbose, instrument)
::Middleman::Util.instrument 'builder.setup' do
@app = ::Middleman::Application.new do
config[:mode] = :build
config[:environment] = env
config[:show_exceptions] = false
cli_options.each do |k, v|
setting = config.setting(k.to_sym)
next unless setting
v = setting.options[:import].call(v) if setting.options[:import]
config[k.to_sym] = v
end
end
builder = Middleman::Builder.new(@app,

View file

@ -5,25 +5,13 @@ module Middleman::Cli
check_unknown_options!
class_option :environment,
aliases: '-e',
default: ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development',
desc: 'The environment Middleman will run under'
aliases: '-e'
class_option :port,
aliases: '-p',
desc: 'The port Middleman will listen on'
aliases: '-p'
class_option :server_name,
aliases: '-s',
desc: 'The server name Middleman will use'
aliases: '-s'
class_option :bind_address,
aliases: '-b',
desc: 'The bind address Middleman will listen on'
class_option :https,
type: :boolean,
desc: 'Serve the preview server over SSL/TLS'
class_option :ssl_certificate,
desc: 'Path to an X.509 certificate to use for the preview server'
class_option :ssl_private_key,
desc: "Path to an RSA private key for the preview server's certificate"
aliases: '-b'
class_option :verbose,
type: :boolean,
default: false,
@ -32,29 +20,18 @@ module Middleman::Cli
type: :string,
default: false,
desc: 'Print instrument messages'
class_option :disable_watcher,
type: :boolean,
default: false,
desc: 'Disable the file change and delete watcher process'
class_option :profile,
type: :boolean,
default: false,
desc: 'Generate profiling report for server startup'
class_option :force_polling,
type: :boolean,
default: false,
desc: 'Force file watcher into polling mode'
class_option :latency,
type: :numeric,
aliases: '-l',
default: 0.5,
desc: 'Set file watcher latency, in seconds'
class_option :daemon,
type: :boolean,
aliases: '-d',
default: false,
desc: 'Daemonize preview server'
Middleman::Cli.import_config(self)
# Start the server
def server
require 'middleman-core'
@ -66,24 +43,14 @@ module Middleman::Cli
end
params = {
port: options['port'],
bind_address: options['bind_address'],
https: options['https'],
server_name: options['server_name'],
ssl_certificate: options['ssl_certificate'],
ssl_private_key: options['ssl_private_key'],
environment: options['environment'],
debug: options['verbose'],
instrumenting: options['instrument'],
disable_watcher: options['disable_watcher'],
reload_paths: options['reload_paths'],
force_polling: options['force_polling'],
latency: options['latency'],
daemon: options['daemon']
}
puts '== The Middleman is loading'
::Middleman::PreviewServer.start(params)
::Middleman::PreviewServer.start(params, options)
end
# Add to CLI

View file

@ -106,7 +106,7 @@ module Middleman
# Middleman environment. Defaults to :development
# @return [String]
define_setting :environment, ((ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development), 'Middleman environment. Defaults to :development'
define_setting :environment, ((ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development), 'Middleman environment. Defaults to :development', import: proc { |s| s.to_sym }
# Which file should be used for directory indexes
# @return [String]

View file

@ -52,7 +52,7 @@ module Middleman
def register(name, extension_class=nil, options={}, &block)
raise 'Extension name must be a symbol' unless name.is_a?(Symbol)
# If we've already got an extension registered under this name, bail out
raise "There is already an extension registered with the name '#{name}'" if registered.key?(name)
# raise "There is a already an extension registered with the name '#{name}'" if registered.key?(name)
# If the extension is defined with a block, grab options out of the "extension_class" parameter.
if extension_class && block_given? && options.empty? && extension_class.is_a?(Hash)

View file

@ -18,12 +18,13 @@ module Middleman
# Start an instance of Middleman::Application
# @return [void]
def start(opts={})
def start(opts={}, cli_options={})
# Do not buffer output, otherwise testing of output does not work
$stdout.sync = true
$stderr.sync = true
@options = opts
@cli_options = cli_options
@server_information = ServerInformation.new
@server_information.https = (@options[:https] == true)
@ -131,6 +132,7 @@ module Middleman
def initialize_new_app
opts = @options.dup
cli_options = @cli_options.dup
::Middleman::Logger.singleton(
opts[:debug] ? 0 : 1,
@ -138,17 +140,14 @@ module Middleman
)
app = ::Middleman::Application.new do
config[:environment] = opts[:environment].to_sym if opts[:environment]
config[:watcher_disable] = opts[:disable_watcher]
config[:watcher_force_polling] = opts[:force_polling]
config[:watcher_latency] = opts[:latency]
cli_options.each do |k, v|
setting = config.setting(k.to_sym)
next unless setting
config[:port] = opts[:port] if opts[:port]
config[:bind_address] = opts[:bind_address]
config[:server_name] = opts[:server_name]
config[:https] = opts[:https] unless opts[:https].nil?
config[:ssl_certificate] = opts[:ssl_certificate] if opts[:ssl_certificate]
config[:ssl_private_key] = opts[:ssl_private_key] if opts[:ssl_private_key]
v = setting.options[:import].call(v) if setting.options[:import]
config[k.to_sym] = v
end
ready do
unless config[:watcher_disable]

View file

@ -23,8 +23,8 @@ module Middleman
ImportPathDescriptor = Struct.new(:from, :renameProc) do
def execute_descriptor(app, resources)
resources + ::Middleman::Util.glob_directory(File.join(from, '**/*'))
.reject { |path| File.directory?(path) }
.map do |path|
.reject { |path| File.directory?(path) }
.map do |path|
target_path = Pathname(path).relative_path_from(Pathname(from).parent).to_s
::Middleman::Sitemap::Resource.new(