Wiki

Clone wiki

tokenr / Home

Overview

This library will recursively search a specified directory for a file pattern (you can specify in the constructor). It will tokenise any files found and fail if tokens still exist after replacement. To understand more of the logic behind it, please read the Rules.

The tokens files are ruby (it gets eval'd when being read in) so you can do whatever you wish to apply "keys_for" or "shared_with", ie pull from a database.

A tokens file contains keys and values. Tokenr looks through a "master" file finding all keys, and replaces them with the token values. In a "master" file token keys begin and end with a dollar => $MyToken$

Install

gem install tokenr

It lives on ruby gems at https://rubygems.org/gems/tokenr

Example

Rakefile

If you do not pass in anything into the constructor, it defaults the parameters to:

  • Tokens Pattern - '*.master.{config,tt}' (based on Ruby's glob, a pattern to find files by)
  • Global File - './globals.rb' (a globals file is not required for it to work)

When calling "replace", you can must pass in the path and can optionally specify the environment you want to do the final generation for (ie Web.config is created for 'local'). Otherwise it will default to the first environment it finds in the tokens file.

Usage

# Using all the defaults
tokens = Tokens.new

# Specifying the pattern
tokens = Tokens.new('*.master.{txt}')

# Specifying the pattern and path to globals
tokens = Tokens.new('*.master.{txt}', './globals.rb')

# Using the default folder and environment
tokens.replace('.')

# Specifying the folder
tokens.replace('/path/to/my/folder')

# Specifying the environment
tokens.replace('.', 'myenvironment')

Usage (in a Rakefile)

require 'tokenr'

task :defaults do
    Tokens.new.replace('.')
end

desc 'Specify which environment to build for, ie App.config will use "myenvironment" tokens'
task :default_environment do
    Tokens.new.replace('/path/to/my/solution', 'dev')
end

Example Token Files

Globals.rb

keys_for :dev,
{
    :CustomErrors => true
}

keys_for :systest,
{
    :CustomErrors => false
}

Web.tokens.rb

keys_for :dev,
{
    :ConnectionString => 'Server=dev.db',
    :CustomErrors => true
}

keys_for :systest,
{
    :ConnectionString => 'Server=systest.db',
    :CustomErrors => true
}

share_with [:dev, :systest],
{ 
    :Debug => true,
}

Example Master File

Web.master.config

My connection string is $ConnectionString$
Debug is $Debug$
Custom errors are $CustomErrors$

Example Output

  • Web.dev.config
  • Web.test.config
  • Web.config <- generated for the default environment

Web.dev.config

My connection string is Server=dev.db
Debug is true
Custom errors are true

Updated