templar 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +192 -0
  3. data/Rakefile +37 -0
  4. data/bin/templarize +71 -0
  5. data/config/templar.sample.yml +19 -0
  6. data/config/templar/data.sample.yml +0 -0
  7. data/lib/rails/init.rb +1 -0
  8. data/lib/tasks/templar_tasks.rake +33 -0
  9. data/lib/templar.rb +5 -0
  10. data/lib/templar/capistrano.rb +35 -0
  11. data/lib/templar/config.rb +42 -0
  12. data/lib/templar/engine.rb +17 -0
  13. data/lib/templar/exceptions.rb +1 -0
  14. data/lib/templar/processor.rb +73 -0
  15. data/lib/templar/version.rb +11 -0
  16. data/test/dummy/Rakefile +7 -0
  17. data/test/dummy/app/assets/javascripts/application.js +9 -0
  18. data/test/dummy/app/assets/stylesheets/application.css +7 -0
  19. data/test/dummy/app/controllers/application_controller.rb +3 -0
  20. data/test/dummy/app/helpers/application_helper.rb +2 -0
  21. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  22. data/test/dummy/config.ru +4 -0
  23. data/test/dummy/config/application.rb +45 -0
  24. data/test/dummy/config/boot.rb +10 -0
  25. data/test/dummy/config/database.yml +25 -0
  26. data/test/dummy/config/environment.rb +5 -0
  27. data/test/dummy/config/environments/development.rb +30 -0
  28. data/test/dummy/config/environments/production.rb +60 -0
  29. data/test/dummy/config/environments/test.rb +39 -0
  30. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  31. data/test/dummy/config/initializers/inflections.rb +10 -0
  32. data/test/dummy/config/initializers/mime_types.rb +5 -0
  33. data/test/dummy/config/initializers/secret_token.rb +7 -0
  34. data/test/dummy/config/initializers/session_store.rb +8 -0
  35. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  36. data/test/dummy/config/locales/en.yml +5 -0
  37. data/test/dummy/config/routes.rb +58 -0
  38. data/test/dummy/public/404.html +26 -0
  39. data/test/dummy/public/422.html +26 -0
  40. data/test/dummy/public/500.html +26 -0
  41. data/test/dummy/public/favicon.ico +0 -0
  42. data/test/dummy/script/rails +6 -0
  43. data/test/templar_test.rb +7 -0
  44. data/test/test_helper.rb +10 -0
  45. metadata +178 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,192 @@
1
+ Templar
2
+ =======
3
+
4
+ [Config] **Templar** provides the ability to manage development configurations for any file in a project, using ERB templates.
5
+
6
+ Usage
7
+ -----
8
+
9
+ Once **Templar** is installed and configured, it will automatically keep your local copies of configurations up-to-date. It
10
+ does this by running prior to initialization of the Rails environment.
11
+
12
+ The *Example* section below, will give you the best idea of the usage of **Templar**.
13
+
14
+ Example
15
+ -------
16
+
17
+ Most of this example is now handled by `rake templar:file[filename]`.
18
+
19
+ This will take you, step by step, through the process of configuring and using **Templar** to manage the database.yml file
20
+ in a Rails application.
21
+
22
+ Before we begin, please follow the instructions in the *Installation* section below.
23
+
24
+ ### the `database.yml` file
25
+
26
+ ```ruby
27
+ development:
28
+ adapter: mysql
29
+ database: name_development
30
+ user:
31
+ username: root
32
+ password:
33
+ production:
34
+ ...
35
+ ```
36
+
37
+ ### `git mv` the `database.yml` file
38
+
39
+ Move the file from it's normal location to the templar directory and add the `erb` extension.
40
+
41
+ ```
42
+ git mv config/database.yml config/templar/database.yml.erb
43
+ ```
44
+
45
+ ### add `config/database.yml` to `.gitignore`
46
+
47
+ This means you will track the template, but not the output file.
48
+
49
+ ```
50
+ echo "config/database.yml" > .gitignore
51
+ ```
52
+
53
+ ### add file to `templar.yml` config
54
+
55
+ edit the `config/templar.yml` file and add an entry to the `templates` section for `database.yml`
56
+
57
+ ```yaml
58
+ templates:
59
+ ...
60
+ - { file: database.yml.erb, dest: config/database.yml }
61
+ ```
62
+
63
+ ### copy the configuration info from the `database.yml`
64
+
65
+ Copy and paste the configuration information from `config/templar/database.yml.erb` to `templar/data.yml`
66
+
67
+ ```yaml
68
+ # database.yml
69
+ database:
70
+ adapter: mysql
71
+ database: name_development
72
+ username: root
73
+ password:
74
+ host: 127.0.0.1
75
+ ```
76
+
77
+ ### change the `database.yml.erb` file
78
+
79
+ Change the values in the `config/templar/database.yml.erb` file to use the template variables.
80
+
81
+ The configuration values from the `config/templar/data.yml` file are available in the @T template variable.
82
+
83
+ ```yaml
84
+ development:
85
+ adapter: <%= @T.database.adapter %>
86
+ database: <%= @T.database.database %>
87
+ user: <%= @T.database.user %>
88
+ username: <%= @T.database.username %>
89
+ password: <%= @T.database.password %>
90
+ production:
91
+ ...
92
+ ```
93
+
94
+ ### done
95
+
96
+ The `database.yml` is now managed by **Templar**. I'm hoping that this entire process will be simplified with a rake task.
97
+
98
+ Installation
99
+ ------------
100
+
101
+ Add the gem into the Gemfile.
102
+
103
+ ### Gemfile
104
+
105
+ ```ruby
106
+ gem 'templar'
107
+ ```
108
+
109
+ Then run `bundle install`
110
+
111
+ ### config/environment.rb
112
+
113
+ Add the following line to this file:
114
+
115
+ ```ruby
116
+ Templar.init()
117
+ ```
118
+
119
+ ### The `templarize` command
120
+
121
+ Similar to `capify` or `wheneverize` this command will add the basic configuration files for **Templar** to your
122
+ rails application directory.
123
+
124
+ From within the rails base directory, run the command `templarize .` You must specify the `.` as part of the command.
125
+
126
+ Once the command is complete, it will have created three new files `config/templar/data.yml`, `config/templar/data.sample.yml` and `config/templar.yml`.
127
+ It will also have added some lines to the `.gitignore` file.
128
+
129
+ Confguration
130
+ ------------
131
+
132
+ There are two configuration files for **Templar**.
133
+
134
+ ### config/templar.yml
135
+
136
+ The `templar.yml` file is the primary configuration for **Templar**. Most often, you will only need to change the
137
+ `templates` section of this file.
138
+
139
+ ```yaml
140
+ # templates: the list of files and destinations to be handled by **Templar**
141
+ templates:
142
+ # example
143
+ #- {file: randomconfig.yml.erb, dest: config/randomconfig.yml}
144
+
145
+ # default options
146
+ # you generally will not need to change these options
147
+
148
+ # directory: the directory containing the templated configuration files (erb) and the data file
149
+ directory: config/templar
150
+
151
+ # always_update: force the template files to be regenerated everytime
152
+ # default: false. The files will only be regenerated when the erb template is newer than the destination file
153
+ always_update: false
154
+
155
+ # the file containing the data used in the templates
156
+ # default: data.yml (relative to the "directory" configuration value)
157
+ data_file: data.yml
158
+ ```
159
+
160
+ ### config/templar/data.sample.yml
161
+
162
+ This file should contain the default local development environment configuration data. This is used to create the
163
+ `config/templar/data.yml` file for users when they first setup their local development environment for the app.
164
+
165
+ ### config/templar/data.yml
166
+
167
+ This file is ignored by git automatically (as part of the installation process). This should contain the configuration
168
+ for the local development environment.
169
+
170
+ An example `data.yml`.
171
+
172
+ ```yaml
173
+ # database.yml
174
+ database:
175
+ adapter: mysql
176
+ database: name_development
177
+ username: root
178
+ password:
179
+ host: 127.0.0.1
180
+ ```
181
+
182
+ This file contains any of the data that will be inserted into the template configuration files. Such that if you have a
183
+ `database.yml.erb` file managed by **Templar**, you can populate the data from the database block in the `data.yml` file.
184
+
185
+ See *Example* for more info.
186
+
187
+ TODO
188
+ ----
189
+
190
+ ### Clean up Capistrano integration
191
+
192
+ Still needs a bit of help.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Templar'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task :default => :test
data/bin/templarize ADDED
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+ # shamelessly stolen from "capify" script
3
+
4
+ require 'optparse'
5
+ require 'fileutils'
6
+
7
+ OptionParser.new do |opts|
8
+ opts.banner = "Usage: #{File.basename($0)} [path]"
9
+
10
+ opts.on("-h", "--help", "Displays this help info") do
11
+ puts opts
12
+ exit 0
13
+ end
14
+
15
+ begin
16
+ opts.parse!(ARGV)
17
+ rescue OptionParser::ParseError => e
18
+ warn e.message
19
+ puts opts
20
+ exit 1
21
+ end
22
+ end
23
+
24
+ if ARGV.empty?
25
+ abort "Please specify the directory to templarize, e.g. `#{File.basename($0)} .'"
26
+ elsif !File.exists?(ARGV.first)
27
+ abort "`#{ARGV.first}' does not exist."
28
+ elsif !File.directory?(ARGV.first)
29
+ abort "`#{ARGV.first}' is not a directory."
30
+ elsif ARGV.length > 1
31
+ abort "Too many arguments; please specify only the directory to capify."
32
+ end
33
+
34
+ def unindent(string)
35
+ indentation = string[/\A\s*/]
36
+ string.strip.gsub(/^#{indentation}/, "")
37
+ end
38
+
39
+ files = {
40
+ "config/templar.yml" =>
41
+ File.open(File.expand_path("../config/templar.sample.yml", File.dirname(__FILE__))).read,
42
+ "config/templar/data.sample.yml" =>
43
+ File.open(File.expand_path("../config/templar/data.sample.yml", File.dirname(__FILE__))).read,
44
+ "config/templar/data.yml" =>
45
+ File.open(File.expand_path("../config/templar/data.sample.yml", File.dirname(__FILE__))).read
46
+ }
47
+
48
+ base = ARGV.shift
49
+ files.each do |file, content|
50
+ file = File.join(base, file)
51
+ if File.exists?(file)
52
+ warn "[skip] '#{file}' already exists"
53
+ elsif File.exists?(file.downcase)
54
+ warn "[skip] '#{file.downcase}' exists, which could conflict with `#{file}'"
55
+ else
56
+ unless File.exists?(File.dirname(file))
57
+ puts "[add] making directory '#{File.dirname(file)}'"
58
+ FileUtils.mkdir(File.dirname(file))
59
+ end
60
+ puts "[add] writing '#{file}'"
61
+ File.open(file, "w") { |f| f.write(content) }
62
+ end
63
+ end
64
+
65
+ gitignore = File.join(base, ".gitignore")
66
+ if File.exists?(gitignore)
67
+ puts "[append] #{gitignore}"
68
+ File.open(gitignore, "a") {|f| f.puts "\n#### Added by templarize script (Templar)\nconfig/templar/data.yml\n"}
69
+ end
70
+
71
+ puts "[done] templarized!"
@@ -0,0 +1,19 @@
1
+
2
+ # templates: the list of files and destinations to be handled by Templar
3
+ templates:
4
+ # example
5
+ #- {file: randomconfig.yml.erb, dest: config/randomconfig.yml}
6
+
7
+ # default options
8
+ # you generally will not need to change these options
9
+
10
+ # directory: the directory containing the templated configuration files (erb) and the data file
11
+ directory: config/templar
12
+
13
+ # always_update: force the template files to be regenerated everytime
14
+ # default: false. The files will only be regenerated when the erb template is newer than the destination file
15
+ always_update: false
16
+
17
+ # the file containing the data used in the templates
18
+ # default: data.yml (relative to the "directory" configuration value)
19
+ data_file: data.yml
File without changes
data/lib/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'templar'
@@ -0,0 +1,33 @@
1
+ require 'templar'
2
+
3
+ namespace :templar do
4
+ task :update do
5
+ templar = Templar::Processor.new({"always_update" => true})
6
+ end
7
+
8
+ desc "use templar to manage file"
9
+ task :file, [:filename] => :environment do |t, args|
10
+ dir = Templar.config.directory
11
+ source = args[:filename]
12
+ file = File.basename(source)
13
+ dest = "#{dir}/#{file}.erb"
14
+
15
+ puts "[move] #{source} -> #{dest}"
16
+ FileUtils.move(source, dest)
17
+
18
+ puts "[ignore] #{source}"
19
+ gitignore = File.join(".gitignore")
20
+ if File.exists?(gitignore)
21
+ puts "[append] #{gitignore}"
22
+ File.open(gitignore, "a") {|f| f.puts "\n#### Added by 'rake templar:file'\n#{source}\n"}
23
+ end
24
+
25
+ puts "[template] #{dest}"
26
+ config = YAML.load_file('config/templar.yml')
27
+ config["templates"] ||= []
28
+ config["templates"] << {'file' => "#{file}.erb", 'dest' => source}
29
+ File.open('config/templar.yml', 'w') do |out|
30
+ YAML.dump(config, out)
31
+ end
32
+ end
33
+ end
data/lib/templar.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'templar/version'
2
+ require 'templar/config'
3
+ require 'templar/processor'
4
+ require 'templar/exceptions'
5
+ require 'templar/engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
@@ -0,0 +1,35 @@
1
+ require 'templar'
2
+ require 'awesome_print'
3
+
4
+ configuration = Capistrano::Configuration.respond_to?(:instance) ?
5
+ Capistrano::Configuration.instance(:must_exist) :
6
+ Capistrano.configuration(:must_exist)
7
+
8
+ configuration.load do
9
+
10
+ puts " * initializing templar..."
11
+
12
+ @templar = Templar::Processor.new
13
+ @templar.config.environment = ARGV[0] if @templar.config.use_environments
14
+ @templar.config.print_format = " templar: (update: %s) %s => %s"
15
+
16
+ begin
17
+ [ARGV[0], ARGV[1]].each do |arg|
18
+ task arg do
19
+ nil
20
+ end
21
+ end
22
+ rescue
23
+ nil
24
+ end
25
+
26
+ def run_templar(opts={})
27
+ @templar.process
28
+ end
29
+
30
+ task :templar, :once => true do
31
+ # force updates if we're called directly
32
+ @templar.config.always_update = true
33
+ run_templar
34
+ end
35
+ end
@@ -0,0 +1,42 @@
1
+ # stolen from mjijackson
2
+ # http://mjijackson.com/2010/02/flexible-ruby-config-objects
3
+
4
+ module Templar
5
+ class Config
6
+ def initialize(data={ })
7
+ @data = { }
8
+ update!(data)
9
+ end
10
+
11
+ def update!(data)
12
+ data.each do |key, value|
13
+ self[key] = value
14
+ end
15
+ end
16
+
17
+ def [](key)
18
+ @data[key.to_sym]
19
+ end
20
+
21
+ def []=(key, value)
22
+ if value.class == Hash
23
+ @data[key.to_sym] = Config.new(value)
24
+ elsif value.class == Array
25
+ @data[key.to_sym] = []
26
+ value.each do |v|
27
+ @data[key.to_sym] << Config.new(v)
28
+ end
29
+ else
30
+ @data[key.to_sym] = value
31
+ end
32
+ end
33
+
34
+ def method_missing(sym, *args)
35
+ if sym.to_s =~ /(.+)=$/
36
+ self[$1] = args.first
37
+ else
38
+ self[sym]
39
+ end
40
+ end
41
+ end
42
+ end