Class: Bozo::Preparers::FileTemplating

Inherits:
Object
  • Object
show all
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
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

Constructor Details

#initializeFileTemplating

Creates a new instance.



68
69
70
71
72
# File 'lib/bozo/preparers/file_templating.rb', line 68

def initialize
  @config_path = 'config'
  @template_globs = []
  @config_files = []
end

Instance Method Details

#config_file(path) ⇒ Object

Adds a specific file to load configuration from.

Parameters:

  • path (String)

    The path to a configuration file.



87
88
89
# File 'lib/bozo/preparers/file_templating.rb', line 87

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.

Parameters:

  • path (String)

    The path to the directory containing the default configuration files.



79
80
81
# File 'lib/bozo/preparers/file_templating.rb', line 79

def config_path(path)
  @config_path = path
end

#executeObject

Generate all the files matching the configuration.



101
102
103
104
105
106
107
108
# File 'lib/bozo/preparers/file_templating.rb', line 101

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.

Parameters:

  • glob (String)

    A glob that points to a set of files that should pass through the templating engine.



96
97
98
# File 'lib/bozo/preparers/file_templating.rb', line 96

def template_files(glob)
  @template_globs << glob
end