Expose all config options to CLIs. Helps with #1829
This commit is contained in:
parent
d624dc4601
commit
1f3bf47e3c
9 changed files with 78 additions and 70 deletions
|
@ -1,6 +1,7 @@
|
||||||
master
|
master
|
||||||
===
|
===
|
||||||
|
|
||||||
|
* Expose all top-level config options to CLI (flags now match config. latency -> watcher_latency, etc).
|
||||||
* Fix directory indexes with `.htm` files. #1821
|
* Fix directory indexes with `.htm` files. #1821
|
||||||
|
|
||||||
# 4.1.2
|
# 4.1.2
|
||||||
|
|
|
@ -9,6 +9,31 @@ end
|
||||||
require "middleman-core/load_paths"
|
require "middleman-core/load_paths"
|
||||||
Middleman.setup_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"
|
require "middleman-cli"
|
||||||
|
|
||||||
# Change directory to the root
|
# Change directory to the root
|
||||||
|
@ -19,5 +44,21 @@ if ARGV[0] != 'help' && (ARGV.length < 1 || ARGV.first.include?('-'))
|
||||||
ARGV.unshift('server')
|
ARGV.unshift('server')
|
||||||
end
|
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
|
# Start the CLI
|
||||||
Middleman::Cli::Base.start(ARGV)
|
Middleman::Cli::Base.start(ARGV)
|
||||||
|
|
|
@ -21,14 +21,3 @@ module Middleman::Cli
|
||||||
end
|
end
|
||||||
end
|
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'
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require 'middleman-core/application'
|
||||||
|
|
||||||
# CLI Module
|
# CLI Module
|
||||||
module Middleman::Cli
|
module Middleman::Cli
|
||||||
# The CLI Build class
|
# The CLI Build class
|
||||||
|
@ -8,8 +10,7 @@ module Middleman::Cli
|
||||||
|
|
||||||
class_option :environment,
|
class_option :environment,
|
||||||
aliases: '-e',
|
aliases: '-e',
|
||||||
default: ENV['MM_ENV'] || ENV['RACK_ENV'] || 'production',
|
default: :production
|
||||||
desc: 'The environment Middleman will run under'
|
|
||||||
class_option :clean,
|
class_option :clean,
|
||||||
type: :boolean,
|
type: :boolean,
|
||||||
default: true,
|
default: true,
|
||||||
|
@ -36,6 +37,8 @@ module Middleman::Cli
|
||||||
default: false,
|
default: false,
|
||||||
desc: 'Generate profiling report for the build'
|
desc: 'Generate profiling report for the build'
|
||||||
|
|
||||||
|
Middleman::Cli.import_config(self)
|
||||||
|
|
||||||
# Core build Thor command
|
# Core build Thor command
|
||||||
# @return [void]
|
# @return [void]
|
||||||
def build
|
def build
|
||||||
|
@ -48,19 +51,27 @@ module Middleman::Cli
|
||||||
require 'middleman-core/builder'
|
require 'middleman-core/builder'
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
|
|
||||||
env = options['environment'].to_sym
|
|
||||||
verbose = options['verbose'] ? 0 : 1
|
verbose = options['verbose'] ? 0 : 1
|
||||||
instrument = options['instrument']
|
instrument = options['instrument']
|
||||||
|
|
||||||
builder = nil
|
builder = nil
|
||||||
|
cli_options = options
|
||||||
|
|
||||||
::Middleman::Logger.singleton(verbose, instrument)
|
::Middleman::Logger.singleton(verbose, instrument)
|
||||||
|
|
||||||
::Middleman::Util.instrument 'builder.setup' do
|
::Middleman::Util.instrument 'builder.setup' do
|
||||||
@app = ::Middleman::Application.new do
|
@app = ::Middleman::Application.new do
|
||||||
config[:mode] = :build
|
config[:mode] = :build
|
||||||
config[:environment] = env
|
|
||||||
config[:show_exceptions] = false
|
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
|
end
|
||||||
|
|
||||||
builder = Middleman::Builder.new(@app,
|
builder = Middleman::Builder.new(@app,
|
||||||
|
|
|
@ -5,25 +5,13 @@ module Middleman::Cli
|
||||||
check_unknown_options!
|
check_unknown_options!
|
||||||
|
|
||||||
class_option :environment,
|
class_option :environment,
|
||||||
aliases: '-e',
|
aliases: '-e'
|
||||||
default: ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development',
|
|
||||||
desc: 'The environment Middleman will run under'
|
|
||||||
class_option :port,
|
class_option :port,
|
||||||
aliases: '-p',
|
aliases: '-p'
|
||||||
desc: 'The port Middleman will listen on'
|
|
||||||
class_option :server_name,
|
class_option :server_name,
|
||||||
aliases: '-s',
|
aliases: '-s'
|
||||||
desc: 'The server name Middleman will use'
|
|
||||||
class_option :bind_address,
|
class_option :bind_address,
|
||||||
aliases: '-b',
|
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"
|
|
||||||
class_option :verbose,
|
class_option :verbose,
|
||||||
type: :boolean,
|
type: :boolean,
|
||||||
default: false,
|
default: false,
|
||||||
|
@ -32,29 +20,18 @@ module Middleman::Cli
|
||||||
type: :string,
|
type: :string,
|
||||||
default: false,
|
default: false,
|
||||||
desc: 'Print instrument messages'
|
desc: 'Print instrument messages'
|
||||||
class_option :disable_watcher,
|
|
||||||
type: :boolean,
|
|
||||||
default: false,
|
|
||||||
desc: 'Disable the file change and delete watcher process'
|
|
||||||
class_option :profile,
|
class_option :profile,
|
||||||
type: :boolean,
|
type: :boolean,
|
||||||
default: false,
|
default: false,
|
||||||
desc: 'Generate profiling report for server startup'
|
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,
|
class_option :daemon,
|
||||||
type: :boolean,
|
type: :boolean,
|
||||||
aliases: '-d',
|
aliases: '-d',
|
||||||
default: false,
|
default: false,
|
||||||
desc: 'Daemonize preview server'
|
desc: 'Daemonize preview server'
|
||||||
|
|
||||||
|
Middleman::Cli.import_config(self)
|
||||||
|
|
||||||
# Start the server
|
# Start the server
|
||||||
def server
|
def server
|
||||||
require 'middleman-core'
|
require 'middleman-core'
|
||||||
|
@ -66,24 +43,14 @@ module Middleman::Cli
|
||||||
end
|
end
|
||||||
|
|
||||||
params = {
|
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'],
|
debug: options['verbose'],
|
||||||
instrumenting: options['instrument'],
|
instrumenting: options['instrument'],
|
||||||
disable_watcher: options['disable_watcher'],
|
|
||||||
reload_paths: options['reload_paths'],
|
reload_paths: options['reload_paths'],
|
||||||
force_polling: options['force_polling'],
|
|
||||||
latency: options['latency'],
|
|
||||||
daemon: options['daemon']
|
daemon: options['daemon']
|
||||||
}
|
}
|
||||||
|
|
||||||
puts '== The Middleman is loading'
|
puts '== The Middleman is loading'
|
||||||
::Middleman::PreviewServer.start(params)
|
::Middleman::PreviewServer.start(params, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add to CLI
|
# Add to CLI
|
||||||
|
|
|
@ -106,7 +106,7 @@ module Middleman
|
||||||
|
|
||||||
# Middleman environment. Defaults to :development
|
# Middleman environment. Defaults to :development
|
||||||
# @return [String]
|
# @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
|
# Which file should be used for directory indexes
|
||||||
# @return [String]
|
# @return [String]
|
||||||
|
|
|
@ -52,7 +52,7 @@ module Middleman
|
||||||
def register(name, extension_class=nil, options={}, &block)
|
def register(name, extension_class=nil, options={}, &block)
|
||||||
raise 'Extension name must be a symbol' unless name.is_a?(Symbol)
|
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
|
# 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 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)
|
if extension_class && block_given? && options.empty? && extension_class.is_a?(Hash)
|
||||||
|
|
|
@ -18,12 +18,13 @@ module Middleman
|
||||||
|
|
||||||
# Start an instance of Middleman::Application
|
# Start an instance of Middleman::Application
|
||||||
# @return [void]
|
# @return [void]
|
||||||
def start(opts={})
|
def start(opts={}, cli_options={})
|
||||||
# Do not buffer output, otherwise testing of output does not work
|
# Do not buffer output, otherwise testing of output does not work
|
||||||
$stdout.sync = true
|
$stdout.sync = true
|
||||||
$stderr.sync = true
|
$stderr.sync = true
|
||||||
|
|
||||||
@options = opts
|
@options = opts
|
||||||
|
@cli_options = cli_options
|
||||||
@server_information = ServerInformation.new
|
@server_information = ServerInformation.new
|
||||||
@server_information.https = (@options[:https] == true)
|
@server_information.https = (@options[:https] == true)
|
||||||
|
|
||||||
|
@ -131,6 +132,7 @@ module Middleman
|
||||||
|
|
||||||
def initialize_new_app
|
def initialize_new_app
|
||||||
opts = @options.dup
|
opts = @options.dup
|
||||||
|
cli_options = @cli_options.dup
|
||||||
|
|
||||||
::Middleman::Logger.singleton(
|
::Middleman::Logger.singleton(
|
||||||
opts[:debug] ? 0 : 1,
|
opts[:debug] ? 0 : 1,
|
||||||
|
@ -138,17 +140,14 @@ module Middleman
|
||||||
)
|
)
|
||||||
|
|
||||||
app = ::Middleman::Application.new do
|
app = ::Middleman::Application.new do
|
||||||
config[:environment] = opts[:environment].to_sym if opts[:environment]
|
cli_options.each do |k, v|
|
||||||
config[:watcher_disable] = opts[:disable_watcher]
|
setting = config.setting(k.to_sym)
|
||||||
config[:watcher_force_polling] = opts[:force_polling]
|
next unless setting
|
||||||
config[:watcher_latency] = opts[:latency]
|
|
||||||
|
|
||||||
config[:port] = opts[:port] if opts[:port]
|
v = setting.options[:import].call(v) if setting.options[:import]
|
||||||
config[:bind_address] = opts[:bind_address]
|
|
||||||
config[:server_name] = opts[:server_name]
|
config[k.to_sym] = v
|
||||||
config[:https] = opts[:https] unless opts[:https].nil?
|
end
|
||||||
config[:ssl_certificate] = opts[:ssl_certificate] if opts[:ssl_certificate]
|
|
||||||
config[:ssl_private_key] = opts[:ssl_private_key] if opts[:ssl_private_key]
|
|
||||||
|
|
||||||
ready do
|
ready do
|
||||||
unless config[:watcher_disable]
|
unless config[:watcher_disable]
|
||||||
|
|
|
@ -23,8 +23,8 @@ module Middleman
|
||||||
ImportPathDescriptor = Struct.new(:from, :renameProc) do
|
ImportPathDescriptor = Struct.new(:from, :renameProc) do
|
||||||
def execute_descriptor(app, resources)
|
def execute_descriptor(app, resources)
|
||||||
resources + ::Middleman::Util.glob_directory(File.join(from, '**/*'))
|
resources + ::Middleman::Util.glob_directory(File.join(from, '**/*'))
|
||||||
.reject { |path| File.directory?(path) }
|
.reject { |path| File.directory?(path) }
|
||||||
.map do |path|
|
.map do |path|
|
||||||
target_path = Pathname(path).relative_path_from(Pathname(from).parent).to_s
|
target_path = Pathname(path).relative_path_from(Pathname(from).parent).to_s
|
||||||
|
|
||||||
::Middleman::Sitemap::Resource.new(
|
::Middleman::Sitemap::Resource.new(
|
||||||
|
|
Loading…
Reference in a new issue