Skip to content

Commit

Permalink
Support nanoc environments
Browse files Browse the repository at this point in the history
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>]`
  • Loading branch information
barraq committed May 31, 2016
1 parent f4e0f02 commit 56d35ef
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/nanoc/base/repos/site_loader.rb
Expand Up @@ -39,6 +39,17 @@ def site_from_config(config)
end
end

if config.key? :environments
env_name = ENV.fetch 'NANOC_ENVIRONMENT', Nanoc::CLI.env
env_config = config[:environments][env_name.to_sym] || {}

# Handle specific properties
env_config[:output_dir] ||= File.join(config[:output_dir].to_s, env_name.to_s)

# Override config with environment
config.update env_config
end

Nanoc::Int::Site.new(
config: config,
code_snippets: code_snippets,
Expand Down
14 changes: 14 additions & 0 deletions lib/nanoc/cli.rb
Expand Up @@ -42,6 +42,20 @@ def self.debug=(boolean)
@debug = boolean
end

# @params [string] environment
#
# @return [void]
def self.env=(environment)
@environment = environment
end

# Return environment
#
# @return [string]
def self.env
return @environment || :default
end

# Invokes the Nanoc command-line tool with the given arguments.
#
# @param [Array<String>] args An array of command-line arguments
Expand Down
4 changes: 4 additions & 0 deletions lib/nanoc/cli/commands/nanoc.rb
Expand Up @@ -10,6 +10,10 @@
Nanoc::CLI.debug = true
end

opt :e, :env, 'set environment', argument: :optional do |value|
Nanoc::CLI.env = value || :default
end

opt :h, :help, 'show the help message and quit' do |_value, cmd|
puts cmd.help
exit 0
Expand Down

0 comments on commit 56d35ef

Please sign in to comment.