templar 0.7.2 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ require 'templar'
2
2
 
3
3
  namespace :templar do
4
4
  task :update do
5
- templar = Templar::Processor.new({"always_update" => true})
5
+ templar = Templar::Processor.new(:overrides => {:always_update => true})
6
6
  end
7
7
 
8
8
  desc "use templar to manage file"
@@ -15,12 +15,21 @@ configuration.load do
15
15
 
16
16
  puts " * initializing templar..."
17
17
 
18
- @templar = Templar::Processor.new
19
- @templar.config.environment = ARGV[0] if @templar.config.use_environments
20
- @templar.config.print_format = " templar: (update: %s) %s => %s"
18
+ @templar = Templar::Processor.new(:overrides => {:print_format => " templar: (update: %s) %s => %s"})
19
+ if @templar.config.use_environments
20
+ @templar.environment = begin
21
+ if defined?(Rails)
22
+ Rails.env
23
+ elsif ENV['ENVIRONMENT']
24
+ ENV['ENVIRONMENT']
25
+ else
26
+ ARGV[0]
27
+ end
28
+ end
29
+ end
21
30
 
22
31
  begin
23
- [ARGV[0], ARGV[1]].each do |arg|
32
+ [@templar.environment].each do |arg|
24
33
  task arg do
25
34
  nil
26
35
  end
@@ -30,6 +39,7 @@ configuration.load do
30
39
  end
31
40
 
32
41
  def run_templar(opts={})
42
+ puts " ** using environment: #{@templar.environment}" if @templar.config.use_environments
33
43
  @templar.process
34
44
  end
35
45
 
@@ -7,15 +7,17 @@ module Templar
7
7
  engine_name "templar"
8
8
  end
9
9
 
10
- def self.init(overrides = {})
11
- @@processor ||= Templar::Processor.new(overrides)
12
- end
10
+ class << self
11
+ def init(overrides = {})
12
+ @processor ||= Templar::Processor.new(:overrides => overrides)
13
+ end
13
14
 
14
- def self.data
15
- @@processor.data
16
- end
15
+ def data
16
+ @processor.data
17
+ end
17
18
 
18
- def self.config
19
- @@processor.config
19
+ def config
20
+ @processor.config
21
+ end
20
22
  end
21
23
  end
@@ -8,43 +8,75 @@ module Templar
8
8
  # This is the implementation of template processing.
9
9
  class Processor
10
10
  # @return [Templar::Config] contains the templar configuration data, usually config/templar.yml
11
- attr_accessor :config
11
+ attr_reader :config
12
12
  # @return [Templar::Config] contains the data loaded from the YAML data file, usually config/templar/data.yml
13
- attr_accessor :data
13
+ attr_reader :data
14
+ # @return [String] the environment Templar is using
15
+ attr_accessor :environment
14
16
 
15
17
  ##
16
18
  # Get a new processor
17
19
  # @param [Hash] config_overrides these values will be merged on top of the defaults.
18
- def initialize(config_overrides = {})
19
- {:print_format => " templar: (update: %s) %s => %s"}.merge(config_overrides)
20
- yaml_hash = YAML.load_file('config/templar.yml') || {}
21
- config_hash = yaml_hash.merge(config_overrides)
22
- @config = Templar::Config.new(config_hash)
23
- @directory = @config.directory
24
- @templates = @config.templates || []
25
-
26
- data_hash = YAML::load_file(data_file) || {}
27
- @data = Templar::Config.new(data_hash)
20
+ def initialize(settings = {})
21
+ defaults = {
22
+ :print_format => " templar: (update: %s) %s => %s"
23
+ }
24
+
25
+ @config = load_yaml_file("config/templar.yml", :defaults => defaults, :overrides => settings[:overrides])
26
+ @use_environments = @config.use_environments
27
+ @directory = @config.directory
28
+ @templates = @config.templates || []
29
+
30
+ @data = load_yaml_file(data_file)
31
+
32
+ raise "Data is empty. Missing environment in data settings?" unless @data
28
33
  end
29
34
 
30
35
  ##
31
36
  # Process configuration templates
32
37
  # @raise [TemplarException] Raises exception when it cannot find files
33
38
  def process
34
- @use_environments = @config.use_environments
35
- @environment = @config.environment
36
- raise "Use Environments option is true, but environment is not set (#{config_hash.inspect})" if @use_environments && !@environment
39
+ if @use_environments
40
+ @environment ||= begin
41
+ return Rails.env if defined?(Rails)
42
+ return ENV['ENVIRONMENT'] if ENV['ENVIRONMENT']
43
+ nil
44
+ end
45
+ raise "Use Environments option is true, but environment is not set (#{@config.inspect})" if @use_environments && !@environment
46
+ raise "Data is empty, missing environment?" unless @data[@environment.to_sym]
47
+ @data = @data[@environment.to_sym]
48
+ end
37
49
 
50
+ raise "Data is empty, please check configuration" unless @data
38
51
  process_templates
39
52
  end
40
53
 
54
+ private
55
+
56
+ ##
57
+ # load yaml file into config object
58
+ # @param [String] file the file to load
59
+ # @param [Hash] overrides these values will be merged on top of the file data
60
+ # @return [Templar::Config] contains the file as a config object
61
+ def load_yaml_file(file, settings = {})
62
+ raise "file #{file} does not exist" unless File.exists?(file)
63
+ data = settings[:defaults] || {}
64
+ data.merge!(YAML.load_file(file)||{})
65
+ data.merge!(settings[:overrides]||{})
66
+ Templar::Config.new(data)
67
+ end
41
68
 
42
69
  ##
43
70
  # Determine the data file to use.
44
71
  # If #{directory}/data.yml exists, use that, otherwise use #{directory}/data.sample.yml
45
72
  # data.sample.yml should contain non-development template data.
46
73
  def data_file
47
- @data_file ||= File.exists?("#{@directory}/data.yml") ? "#{@directory}/data.yml" : "#{@directory}/data.sample.yml"
74
+ #@data_file ||= File.exists?("#{@directory}/data.yml") ? "#{@directory}/data.yml" : "#{@directory}/data.sample.yml"
75
+ @data_file ||= begin
76
+ return "#@directory/#@environment.yml" if @use_environments && File.exists?("#@directory/#@environment.yml")
77
+ return "#@directory/data.yml" if File.exists?("#@directory/data.yml")
78
+ "#@directory/data.sample.yml"
79
+ end
48
80
  end
49
81
 
50
82
  ##
@@ -80,8 +112,8 @@ module Templar
80
112
  if update
81
113
  template_data = File.read(source)
82
114
  erb = Erubis::Eruby.new(template_data)
83
- substitutions = @use_environments && @data[@environment.to_sym] ? @data[@environment.to_sym] : @data
84
- dest_data = erb.evaluate(:T => substitutions, :templar => @config)
115
+ substitutions = @data
116
+ dest_data = erb.evaluate(:T => substitutions, :templar => self)
85
117
  # dest_data = ERB.new(template_data).result b
86
118
  File.open(dest, 'w') { |f| f.write(dest_data) }
87
119
  end
@@ -3,7 +3,7 @@ if !defined?(Templar::VERSION)
3
3
  module Version #:nodoc:
4
4
  MAJOR = 0
5
5
  MINOR = 7
6
- TINY = 2
6
+ TINY = 3
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
9
9
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: templar
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 7
9
- - 2
10
- version: 0.7.2
9
+ - 3
10
+ version: 0.7.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Shawn Catanzarite
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-27 00:00:00 Z
18
+ date: 2012-05-04 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake
@@ -29,8 +29,8 @@ dependencies:
29
29
  - 0
30
30
  version: "0"
31
31
  type: :runtime
32
- prerelease: false
33
32
  requirement: *id001
33
+ prerelease: false
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: erubis
36
36
  version_requirements: &id002 !ruby/object:Gem::Requirement
@@ -43,8 +43,8 @@ dependencies:
43
43
  - 0
44
44
  version: "0"
45
45
  type: :runtime
46
- prerelease: false
47
46
  requirement: *id002
47
+ prerelease: false
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: yard
50
50
  version_requirements: &id003 !ruby/object:Gem::Requirement
@@ -57,8 +57,8 @@ dependencies:
57
57
  - 0
58
58
  version: "0"
59
59
  type: :development
60
- prerelease: false
61
60
  requirement: *id003
61
+ prerelease: false
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: sqlite3
64
64
  version_requirements: &id004 !ruby/object:Gem::Requirement
@@ -71,8 +71,8 @@ dependencies:
71
71
  - 0
72
72
  version: "0"
73
73
  type: :development
74
- prerelease: false
75
74
  requirement: *id004
75
+ prerelease: false
76
76
  description: "[config] templar allows you to manage development environment configuration files as templates."
77
77
  email:
78
78
  - scatanzarite@gmail.com
@@ -156,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
156
  requirements: []
157
157
 
158
158
  rubyforge_project:
159
- rubygems_version: 1.8.21
159
+ rubygems_version: 1.8.24
160
160
  signing_key:
161
161
  specification_version: 3
162
162
  summary: "[config] templar allows you to manage development environment configuration files as templates."