Skip to content

Commit 56d35ef

Browse files
committed
Support nanoc environments
Introduce basic support for nanoc environment: - nanoc command is updated to include --env argument - config are overriden using the active environment if any - unless define in environment, output_dir is {{output_dir}}/{{env_name}} Setting environments is done in nanoc.yaml using the `environments` property. Example usage is: ``` output_dir: output environments: default: &default base_url: ... ... development: <<: *default base_url: ... production: <<: *default base_url: ... yet_another_env: <<: *default base_url: ... output_dir: build ``` Selecting working environment can be done: - using environment variable `NANOC_ENVIRONMENT` - using `nanoc --env=[<value>]`
1 parent f4e0f02 commit 56d35ef

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

lib/nanoc/base/repos/site_loader.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ def site_from_config(config)
3939
end
4040
end
4141

42+
if config.key? :environments
43+
env_name = ENV.fetch 'NANOC_ENVIRONMENT', Nanoc::CLI.env
44+
env_config = config[:environments][env_name.to_sym] || {}
45+
46+
# Handle specific properties
47+
env_config[:output_dir] ||= File.join(config[:output_dir].to_s, env_name.to_s)
48+
49+
# Override config with environment
50+
config.update env_config
51+
end
52+
4253
Nanoc::Int::Site.new(
4354
config: config,
4455
code_snippets: code_snippets,

lib/nanoc/cli.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ def self.debug=(boolean)
4242
@debug = boolean
4343
end
4444

45+
# @params [string] environment
46+
#
47+
# @return [void]
48+
def self.env=(environment)
49+
@environment = environment
50+
end
51+
52+
# Return environment
53+
#
54+
# @return [string]
55+
def self.env
56+
return @environment || :default
57+
end
58+
4559
# Invokes the Nanoc command-line tool with the given arguments.
4660
#
4761
# @param [Array<String>] args An array of command-line arguments

lib/nanoc/cli/commands/nanoc.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
Nanoc::CLI.debug = true
1111
end
1212

13+
opt :e, :env, 'set environment', argument: :optional do |value|
14+
Nanoc::CLI.env = value || :default
15+
end
16+
1317
opt :h, :help, 'show the help message and quit' do |_value, cmd|
1418
puts cmd.help
1519
exit 0

0 commit comments

Comments
 (0)