Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate templates with an option to preserve existing changes #75

Merged
merged 8 commits into from
Dec 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-1.9.2-p290
ruby-2.2.3
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ rvm:
- 1.9.3
- 2.0.0
- 2.1.1
- 2.2.3
before_install:
- gem update bundler
script: bundle exec rspec && bundle exec cucumber
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.0.0

* `config:generate_all` task allows user to choose an appropriate action when source and local templates are different.
* `config:generate_all:force` allows user to generate templates in 'force mode' (replacing local templates with the source).

# 0.9.0

* Feature: Adding a bang (!) at the end of method names, will raise when the variable
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ Run the following command to generate them:
[bundle exec] rake config:generate_all
```

This command provides options to compare changes between source and local templates, so new additions to the source templates can be safely added while preserving any project specific customization to local templates.

Alternatively, to force generate templates (replacing existing local templates with the source), run the following:

```
[bundle exec] rake config:generate_all:force
```


As with your own templates, you should commit these pre-packaged templates to
source control.

Expand Down
2 changes: 2 additions & 0 deletions dice_bag.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Gem::Specification.new do |s|
s.require_paths = ['lib']

s.add_dependency 'rake'
s.add_dependency 'thor', '~> 0.0'
s.add_dependency 'diff-lcs', '~> 1.0'
s.add_development_dependency 'aruba', '~> 0.5.1'
s.add_development_dependency 'rspec', '~> 3.0'
s.add_development_dependency 'bundler'
Expand Down
16 changes: 11 additions & 5 deletions lib/dice_bag/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
require 'dice_bag/config_file'
require 'dice_bag/template_file'
require 'dice_bag/default_template_file'
require 'thor'

module DiceBag
#This class seems a candidate to be converted to Thor, the problem is that we need to run
#in the same process than the rake task so all the gems are loaded before dice_bag
#is called and dice_bag can find what software is used in this project
class Command
include Thor::Base
include Thor::Actions

def self.source_root
Dir.pwd
end

def write_all(params = {})
Project.templates_to_generate.each do |template|
Expand All @@ -25,15 +32,14 @@ def write(template_name, params = {})
end


def generate_all_templates
def generate_all_templates(force=false)
AvailableTemplates.all.each do |template|
generate_template(template)
generate_template(template, force)
end
end

def generate_template(default_template)
default_template.assert_existence
default_template.create_file
def generate_template(default_template, force=false)
copy_file default_template.file, default_template.destination, force: force
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does copy_file puts some message when it just copies over and there was no file to overwrite?
I just want to have some notification that things indeed worked.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it prints the action performed on specific files, e.g.
if new file created:

create  config/dalli.yml.dice

if modified:

force  config/dalli.yml.dice

if no changes:

identical  config/dalli.yml.dice

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very cool

end

end
Expand Down
18 changes: 1 addition & 17 deletions lib/dice_bag/default_template_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,8 @@ def initialize(name, location=nil)
@filename = File.basename(name)
@file = name
@template_location = location
@destination = File.join(Project.root, @template_location, @filename)
end

def create_file
contents = read_template(@file)
rooted_template_location = File.join(Project.root, @template_location)
FileUtils.mkdir_p(rooted_template_location)
template_file = File.join(rooted_template_location, @filename)
File.open(template_file, 'w') do |file|
file.puts(contents)
end
puts "new template file generated in #{template_file}.
execute 'rake config:all' to get the corresponding configuration file."
end

def read_template(template)
# Some templates need the name of the project. We put a placeholder
# PROJECT_NAME there, it gets substituted by the real name of the project here
File.readlines(template).join.gsub("PROJECT_NAME", Project.name)
end
end
end
2 changes: 1 addition & 1 deletion lib/dice_bag/dice_bag_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# configuration files, templates files in the project an templates files shipped with dicebag
module DiceBag
module DiceBagFile
attr_reader :file, :filename
attr_reader :file, :filename, :destination
@@overwrite_all = false

def assert_existence
Expand Down
16 changes: 11 additions & 5 deletions lib/dice_bag/tasks/config.rake
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ namespace :config do
DiceBag::Command.new.write(filename)
end

desc "Generate all configuration file templates needed by this project"
desc 'Generate all configuration file templates with interactive commands (use config:generate_all:force to replace all without asking for input)'
task :generate_all do
DiceBag::Command.new.generate_all_templates
DiceBag::Command.new.generate_all_templates(false)
end

desc "Generate just the specified template, optionally in the specified location"
namespace :generate_all do
task :force do
DiceBag::Command.new.generate_all_templates(true)
end
end

desc 'Generate specified template, optionally in the specified location with options to control file actions'
task :generate, :filename, :location do |t, args|
filename = args[:filename]
location = args[:location]
raise "A filename needs to be provided" if filename.nil?
DiceBag::Command.new.generate_template(DiceBag::DefaultTemplateFile.new(filename,location))
raise 'A filename needs to be provided' if filename.nil?
DiceBag::Command.new.generate_template(DiceBag::DefaultTemplateFile.new(filename, location), false)
end
end
6 changes: 3 additions & 3 deletions lib/dice_bag/templates/dalli.yml.dice
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
# cache configuration code in config/environments/production.rb

development: &default
namespace: PROJECT_NAME
host: localhost
port: 11211
namespace: <%= configured.memcached_namespace || 'your_project_namespace' %>
host: <%= configured.memcached_host || 'localhost' %>
port: <%= configured.memcached_port || 11211 %>
value_max_bytes: 52428800
compress: true

Expand Down
8 changes: 4 additions & 4 deletions lib/dice_bag/templates/database.yml.dice
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
username: <%= configured[env].database_username || 'root' %>
password: <%= configured[env].database_password %>
host: <%= configured[env].database_host || 'localhost' %>
pool: 5
timeout: 5000
encoding: utf8
reconnect: false
pool: <%= configured[env].database_pool || 5 %>
timeout: <%= configured[env].database_timeout || 5000 %>
encoding: <%= configured[env].database_encoding || 'utf8' %>
reconnect: <%= configured[env].database_reconnect || false %>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 good changes

<% db_cert = configured[env].database_ssl_cert %>
<%= db_cert ? "sslca: #{db_cert}" : '' %>
<% end %>
2 changes: 1 addition & 1 deletion lib/dice_bag/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module DiceBag
VERSION = '0.9.0'
VERSION = '1.0.0'
end