dice_bag 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,19 @@
1
+ require 'dice_bag/default_template_file'
2
+
1
3
  # This class returns all the templates we can generate in this particular project
2
4
  module DiceBag
3
5
 
4
6
  class AvailableTemplates
5
7
 
8
+
9
+ # By default the final location for any template will be the config directory.
10
+ # If any template 'plugin' wants to overwrite the directory where its template will be written
11
+ # it needs to overwrite this method and return a string with the new location as relative
12
+ # path inside the project.
13
+ def templates_location
14
+ Project.config_dir
15
+ end
16
+
6
17
  class << self
7
18
 
8
19
  def template_checkers
@@ -17,18 +28,20 @@ module DiceBag
17
28
  #all the classes than inherit from us in the ruby runtime
18
29
  available_templates = []
19
30
 
20
- template_checkers.each do |checker|
21
- checker.new.templates.each do |template|
22
- available_templates.push( template)
31
+ template_checkers.each do |template_checker|
32
+ checker = template_checker.new
33
+ location = checker.templates_location
34
+ checker.templates.each do |template|
35
+ available_templates.push( DefaultTemplateFile.new(template, location) )
23
36
  end
24
37
  end
25
38
  available_templates
26
39
  end
27
40
 
28
41
  def template_filename_for(filename)
29
- self.all.each do |template_filename|
30
- if template_filename.include? filename
31
- return template_filename
42
+ self.all.each do |template|
43
+ if template.filename.include? filename
44
+ return template.file
32
45
  end
33
46
  end
34
47
  end
@@ -31,12 +31,9 @@ module DiceBag
31
31
  end
32
32
  end
33
33
 
34
- def generate_template(file)
35
- default_template = DefaultTemplateFile.new(file)
34
+ def generate_template(default_template)
36
35
  default_template.assert_existence
37
- project_template = TemplateFile.new(File.basename(file))
38
-
39
- default_template.create_file(project_template)
36
+ default_template.create_file
40
37
  end
41
38
 
42
39
  end
@@ -7,7 +7,9 @@ module DiceBag
7
7
  class ConfigFile
8
8
  include DiceBagFile
9
9
  def initialize(name)
10
- @filename = name.gsub('.local', '').gsub('.erb', '').gsub('.template','')
10
+ # The 'local', 'erb', and 'template' file extension are deprecated and
11
+ # will be removed some time prior to v1.
12
+ @filename = name.gsub('.local', '').gsub('.erb', '').gsub('.template','').gsub('.dice', '')
11
13
  @file = Project.config_files(@filename)
12
14
  end
13
15
  end
@@ -9,19 +9,23 @@ module DiceBag
9
9
  class DefaultTemplateFile
10
10
  include DiceBagFile
11
11
 
12
- def initialize(name)
12
+ def initialize(name, location=nil)
13
13
  #if called from command line with only a name we search in all our templates for the file
14
14
  if (File.dirname(name) == '.')
15
15
  name = AvailableTemplates.template_filename_for(name)
16
16
  end
17
17
  @filename = File.basename(name)
18
18
  @file = name
19
+ @template_location = location || Project.config_dir
19
20
  end
20
21
 
21
- def create_file(template_file)
22
+ def create_file
22
23
  contents = read_template(@file)
23
- template_file.write(contents)
24
- puts "new template file generated in #{template_file.file}.
24
+ template_file = File.join(Project.root, @template_location, @filename)
25
+ File.open(template_file, 'w') do |file|
26
+ file.puts(contents)
27
+ end
28
+ puts "new template file generated in #{template_file}.
25
29
  execute 'rake config:all' to get the corresponding configuration file."
26
30
  end
27
31
 
@@ -10,21 +10,36 @@ module DiceBag
10
10
  end
11
11
 
12
12
  def self.config_files(filename)
13
- File.join(self.config_dir, filename)
13
+ File.join(self.root, self.config_dir, filename)
14
14
  end
15
15
 
16
16
  # dotNet apps do not have the templates in config/ but in the root of the project
17
17
  # TODO: detect dotNet instead of detecting rails
18
18
  def self.config_dir
19
- defined?(Rails) ? File.join(Dir.pwd, 'config') : Dir.pwd
19
+ defined?(Rails) ? 'config' : "."
20
+ end
21
+
22
+ def self.root
23
+ Dir.pwd
20
24
  end
21
25
 
22
26
  #local templates always takes preference over generated templates
23
27
  def self.templates_to_generate
28
+ dice_templates = Dir["**/*.dice"]
29
+
30
+ # The following ways of identifying template files will be removed prior
31
+ # to v1.
24
32
  generated_templates = Dir[Project.config_files("**/*.erb")]
25
33
  custom_templates = Dir[Project.config_files("**/*.erb.local")]
26
34
  dotNetTemplates = Dir[Project.config_files("**/*.config.template")]
27
- all_files = generated_templates + custom_templates
35
+
36
+ legacy_files = generated_templates + custom_templates + dotNetTemplates
37
+ legacy_files.each do |file|
38
+ $stderr.puts "DEPRECATION WARNING: Use '.dice' file extension instead for '#{file}'"
39
+ end
40
+
41
+ all_files = legacy_files + dice_templates
42
+
28
43
  cleaned_templates = all_files.delete_if {|file| custom_templates.include?(file + '.local')}
29
44
  dotNetTemplates = dotNetTemplates.delete_if {|file| file.include?("/bin/")}
30
45
  (cleaned_templates + dotNetTemplates).map { |template| File.basename(template) }
@@ -2,7 +2,7 @@ require 'erb'
2
2
  require 'dice_bag/command'
3
3
 
4
4
  desc "Configure the application from the environment. It creates template files if you need and their config files"
5
- task :config => ['config:generate_all', 'config:all']
5
+ task :config => ['config:all']
6
6
 
7
7
  namespace :config do
8
8
  desc "Create all the files from their templates in config/"
@@ -22,10 +22,11 @@ namespace :config do
22
22
  DiceBag::Command.new.generate_all_templates
23
23
  end
24
24
 
25
- desc "Regenerate a given template"
26
- task :generate, :filename do |t, args|
25
+ desc "Regenerate a given template, params: filename of the template, location for the generated file[optional]"
26
+ task :generate, :filename, :location do |t, args|
27
27
  filename = args[:filename]
28
+ location = args[:location]
28
29
  raise "A filename needs to be provided" if filename.nil?
29
- DiceBag::Command.new.generate_template(filename)
30
+ DiceBag::Command.new.generate_template(DiceBag::DefaultTemplateFile.new(filename,location))
30
31
  end
31
32
  end
@@ -6,6 +6,6 @@ development: &default
6
6
 
7
7
  test:
8
8
  <<: *default
9
-
9
+
10
10
  production:
11
11
  <<: *default
@@ -12,7 +12,7 @@ development: &default
12
12
  port: 11211
13
13
  value_max_bytes: 52428800
14
14
  compress: true
15
-
15
+
16
16
  test:
17
17
  <<: *default
18
18
 
@@ -12,23 +12,23 @@ module DiceBag
12
12
  configured = Configuration.new
13
13
 
14
14
  if defined?(Dalli)
15
- add_template('dalli.yml.erb')
15
+ add_template('dalli.yml.dice')
16
16
  end
17
17
 
18
18
  if defined?(Mysql2)
19
- add_template('database.yml.erb')
19
+ add_template('database.yml.dice')
20
20
  end
21
21
 
22
22
  if defined?(AWS)
23
- add_template('aws.yml.erb')
23
+ add_template('aws.yml.dice')
24
24
  end
25
25
 
26
26
  if configured.google_analytics_id
27
- add_template('google_analytics.yml.erb')
27
+ add_template('google_analytics.yml.dice')
28
28
  end
29
29
 
30
30
  if defined?(NewRelic)
31
- add_template('newrelic.yml.erb')
31
+ add_template('newrelic.yml.dice')
32
32
  end
33
33
 
34
34
  @needed_templates
@@ -1,3 +1,3 @@
1
1
  module DiceBag
2
- VERSION = '0.4.1'
2
+ VERSION = '0.5.0'
3
3
  end
metadata CHANGED
@@ -1,30 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dice_bag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Andrew Smith
9
+ - Jordi Carres
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2013-02-21 00:00:00.000000000Z
13
+ date: 2013-02-27 00:00:00.000000000Z
13
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: aruba
17
+ requirement: &72996330 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 0.5.1
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *72996330
14
26
  - !ruby/object:Gem::Dependency
15
27
  name: rspec
16
- requirement: &74949160 !ruby/object:Gem::Requirement
28
+ requirement: &72996080 !ruby/object:Gem::Requirement
17
29
  none: false
18
30
  requirements:
19
- - - ! '>='
31
+ - - ~>
20
32
  - !ruby/object:Gem::Version
21
- version: '0'
33
+ version: '2.12'
22
34
  type: :development
23
35
  prerelease: false
24
- version_requirements: *74949160
36
+ version_requirements: *72996080
25
37
  description:
26
38
  email:
27
39
  - asmith@mdsol.com
40
+ - jcarres@mdsol.com
28
41
  executables: []
29
42
  extensions: []
30
43
  extra_rdoc_files: []
@@ -39,12 +52,12 @@ files:
39
52
  - lib/dice_bag/dice_bag_file.rb
40
53
  - lib/dice_bag/config_file.rb
41
54
  - lib/dice_bag/version.rb
42
- - lib/dice_bag/templates/newrelic.yml.erb
43
- - lib/dice_bag/templates/database.yml.erb
55
+ - lib/dice_bag/templates/dalli.yml.dice
56
+ - lib/dice_bag/templates/database.yml.dice
44
57
  - lib/dice_bag/templates/gems_checker.rb
45
- - lib/dice_bag/templates/google_analytics.yml.erb
46
- - lib/dice_bag/templates/dalli.yml.erb
47
- - lib/dice_bag/templates/aws.yml.erb
58
+ - lib/dice_bag/templates/newrelic.yml.dice
59
+ - lib/dice_bag/templates/google_analytics.yml.dice
60
+ - lib/dice_bag/templates/aws.yml.dice
48
61
  - lib/dice_bag/railtie.rb
49
62
  - lib/dice_bag/configuration.rb
50
63
  - lib/dice_bag/tasks/config.rake