Class: Bozo::Preparers::FileTemplating
- Inherits:
-
Object
- Object
- Bozo::Preparers::FileTemplating
- Defined in:
- lib/bozo/preparers/file_templating.rb
Overview
Preparer for creating files based upon configuration files and ERB-style templates before any compilation occurs.
Overview
This preparer is primarily intended for generating config files with shared values but it is capable of generating whatever files you want. Each instance of the preparer is isolated from the anothers so you could specify multiple preparers to work with different configurations and templates if you wished.
By default the preparer will load default.rb
followed by [environment].rb
followed by [machine_name].rb
from the configured config_path if the file exists. It will then load any files explicitly required through the config_file method.
Hook configuration
prepare :file_templating do |t|
t.config_path 'somewhere' # defaults to 'config' if not specified
t.config_file 'my/specific/file.rb' # must be a specific file
t.template_files 'src/**/*.config.template' # can use glob format
t.exclude_files 'src/**/obj/*' # can exclude specific files
end
Source files are expected to have an additional extension compared to the target file. For example, a source file of src/csharp/Project/Project.config.template
will generate the file src/csharp/Project/Project.config
.
Configuration files
Configuration files specify a hash of hashes in a more readable format. For example:
group :example do
set :one, 'foo'
set :two, 'bar'
end
Internally creates a hash like:
{:example => {:one => 'foo', :two => 'bar'}}
A configuration file can overwrite the values specified by a preceding one without error. Groups can be opened and closed as desired and nesting groups is possible.
Template files
To use a value within an ERB template you specify the hash hierarchy as if they were method names rather than having to use the full hash syntax.
Therefore, this is valid:
Foo is <%= example.one %>
Whilst this is not valid:
Foo is <%= self[:example][:one] %>
If a template uses a value that is not specified within the configuration then the preparer will raise an error and halt the build.
Instance Method Summary collapse
-
#config_file(path) ⇒ Object
Adds a specific file to load configuration from.
-
#config_path(path) ⇒ Object
Sets the path of the directory within which the preparer should look for the default configuration files.
-
#exclude_files(glob) ⇒ Object
Adds a set of templates files from which to exclude from templating.
-
#execute ⇒ Object
Generate all the files matching the configuration.
-
#initialize ⇒ FileTemplating
constructor
Creates a new instance.
-
#template_files(glob) ⇒ Object
Adds a set of templates files from which to generate files.
Constructor Details
#initialize ⇒ FileTemplating
Creates a new instance.
69 70 71 72 73 74 |
# File 'lib/bozo/preparers/file_templating.rb', line 69 def initialize @config_path = 'config' @template_globs = [] @config_files = [] @exclude_globs = [] end |
Instance Method Details
#config_file(path) ⇒ Object
Adds a specific file to load configuration from.
89 90 91 |
# File 'lib/bozo/preparers/file_templating.rb', line 89 def config_file(path) @config_files << path end |
#config_path(path) ⇒ Object
Sets the path of the directory within which the preparer should look for the default configuration files.
81 82 83 |
# File 'lib/bozo/preparers/file_templating.rb', line 81 def config_path(path) @config_path = path end |
#exclude_files(glob) ⇒ Object
Adds a set of templates files from which to exclude from templating.
107 108 109 |
# File 'lib/bozo/preparers/file_templating.rb', line 107 def exclude_files(glob) @exclude_globs << glob end |
#execute ⇒ Object
Generate all the files matching the configuration.
112 113 114 115 116 117 118 119 |
# File 'lib/bozo/preparers/file_templating.rb', line 112 def execute log_info '' # formatting log_info 'Generating files' get_coordinator.generate_files do |template, target| log_debug "Generating #{target} from #{template}" end end |
#template_files(glob) ⇒ Object
Adds a set of templates files from which to generate files.
98 99 100 |
# File 'lib/bozo/preparers/file_templating.rb', line 98 def template_files(glob) @template_globs << glob end |