From d18c09ab36016012bb107ae7f1a861c5f6632b9b Mon Sep 17 00:00:00 2001 From: Abhi Trivedi Date: Thu, 3 Dec 2015 22:35:11 -0800 Subject: [PATCH 1/7] generating template diff with thor --- .ruby-version | 2 +- CHANGELOG.md | 4 ++++ dice_bag.gemspec | 2 ++ lib/dice_bag/command.rb | 11 ++++++++++- lib/dice_bag/default_template_file.rb | 20 +++----------------- lib/dice_bag/dice_bag_file.rb | 2 +- lib/dice_bag/project.rb | 4 ++++ lib/dice_bag/template_file.rb | 1 + lib/dice_bag/templates/dalli.yml.dice | 6 +++--- lib/dice_bag/templates/database.yml.dice | 8 ++++---- 10 files changed, 33 insertions(+), 27 deletions(-) diff --git a/.ruby-version b/.ruby-version index 4bf3096..eca07e4 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -ruby-1.9.2-p290 +2.1.2 diff --git a/CHANGELOG.md b/CHANGELOG.md index aa214e9..952d019 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 0.10.0 + +* Generates diff between template and target file, and allows user to merge changes. + # 0.9.0 * Feature: Adding a bang (!) at the end of method names, will raise when the variable diff --git a/dice_bag.gemspec b/dice_bag.gemspec index 4a385b9..e276f79 100644 --- a/dice_bag.gemspec +++ b/dice_bag.gemspec @@ -15,6 +15,8 @@ Gem::Specification.new do |s| s.require_paths = ['lib'] s.add_dependency 'rake' + s.add_dependency 'thor', '~> 0' + s.add_dependency 'diff-lcs' s.add_development_dependency 'aruba', '~> 0.5.1' s.add_development_dependency 'rspec', '~> 3.0' s.add_development_dependency 'bundler' diff --git a/lib/dice_bag/command.rb b/lib/dice_bag/command.rb index fd2fcf4..da317b8 100644 --- a/lib/dice_bag/command.rb +++ b/lib/dice_bag/command.rb @@ -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| @@ -33,7 +40,9 @@ def generate_all_templates def generate_template(default_template) default_template.assert_existence - default_template.create_file + copy_file default_template.file, default_template.destination + puts "new template file generated in #{default_template.destination}. + execute 'rake config:all' to get the corresponding configuration file." end end diff --git a/lib/dice_bag/default_template_file.rb b/lib/dice_bag/default_template_file.rb index 69265a3..57f55f5 100644 --- a/lib/dice_bag/default_template_file.rb +++ b/lib/dice_bag/default_template_file.rb @@ -1,5 +1,6 @@ require 'dice_bag/dice_bag_file' require 'dice_bag/project' +require 'dice_bag/command' require 'fileutils' require 'tempfile' @@ -10,6 +11,7 @@ module DiceBag class DefaultTemplateFile include DiceBagFile + def initialize(name, location=nil) #if called from command line with only a name we search in all our templates for the file if (File.dirname(name) == '.') @@ -18,24 +20,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 diff --git a/lib/dice_bag/dice_bag_file.rb b/lib/dice_bag/dice_bag_file.rb index 10f7126..e02472b 100644 --- a/lib/dice_bag/dice_bag_file.rb +++ b/lib/dice_bag/dice_bag_file.rb @@ -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 diff --git a/lib/dice_bag/project.rb b/lib/dice_bag/project.rb index 1c3d9a6..990e456 100644 --- a/lib/dice_bag/project.rb +++ b/lib/dice_bag/project.rb @@ -9,6 +9,10 @@ def self.name defined?(Rails) ? Rails.application.class.parent_name.downcase : DEFAULT_NAME end + def name + self.class.name + end + def self.config_files(filename) File.join(self.root, filename) end diff --git a/lib/dice_bag/template_file.rb b/lib/dice_bag/template_file.rb index 6e510dc..05fb422 100644 --- a/lib/dice_bag/template_file.rb +++ b/lib/dice_bag/template_file.rb @@ -2,6 +2,7 @@ require 'dice_bag/template_helpers' require 'dice_bag/configuration' require 'dice_bag/warning' +require 'dice_bag/project' #this class encapsulates the template files we will generate in the project #using this gem diff --git a/lib/dice_bag/templates/dalli.yml.dice b/lib/dice_bag/templates/dalli.yml.dice index adf96d6..ebf7a8d 100644 --- a/lib/dice_bag/templates/dalli.yml.dice +++ b/lib/dice_bag/templates/dalli.yml.dice @@ -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 diff --git a/lib/dice_bag/templates/database.yml.dice b/lib/dice_bag/templates/database.yml.dice index 54c95a2..54e2609 100644 --- a/lib/dice_bag/templates/database.yml.dice +++ b/lib/dice_bag/templates/database.yml.dice @@ -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 %> <% db_cert = configured[env].database_ssl_cert %> <%= db_cert ? "sslca: #{db_cert}" : '' %> <% end %> From a1ae3e13549ee2fa0dedbf0eac9e6f277fac7c03 Mon Sep 17 00:00:00 2001 From: Abhi Trivedi Date: Mon, 7 Dec 2015 12:51:40 -0800 Subject: [PATCH 2/7] unused requires --- lib/dice_bag/command.rb | 3 --- lib/dice_bag/default_template_file.rb | 2 -- lib/dice_bag/project.rb | 4 ---- lib/dice_bag/template_file.rb | 1 - 4 files changed, 10 deletions(-) diff --git a/lib/dice_bag/command.rb b/lib/dice_bag/command.rb index da317b8..89c17ce 100644 --- a/lib/dice_bag/command.rb +++ b/lib/dice_bag/command.rb @@ -39,10 +39,7 @@ def generate_all_templates end def generate_template(default_template) - default_template.assert_existence copy_file default_template.file, default_template.destination - puts "new template file generated in #{default_template.destination}. - execute 'rake config:all' to get the corresponding configuration file." end end diff --git a/lib/dice_bag/default_template_file.rb b/lib/dice_bag/default_template_file.rb index 57f55f5..cb319fb 100644 --- a/lib/dice_bag/default_template_file.rb +++ b/lib/dice_bag/default_template_file.rb @@ -1,6 +1,5 @@ require 'dice_bag/dice_bag_file' require 'dice_bag/project' -require 'dice_bag/command' require 'fileutils' require 'tempfile' @@ -11,7 +10,6 @@ module DiceBag class DefaultTemplateFile include DiceBagFile - def initialize(name, location=nil) #if called from command line with only a name we search in all our templates for the file if (File.dirname(name) == '.') diff --git a/lib/dice_bag/project.rb b/lib/dice_bag/project.rb index 990e456..1c3d9a6 100644 --- a/lib/dice_bag/project.rb +++ b/lib/dice_bag/project.rb @@ -9,10 +9,6 @@ def self.name defined?(Rails) ? Rails.application.class.parent_name.downcase : DEFAULT_NAME end - def name - self.class.name - end - def self.config_files(filename) File.join(self.root, filename) end diff --git a/lib/dice_bag/template_file.rb b/lib/dice_bag/template_file.rb index 05fb422..6e510dc 100644 --- a/lib/dice_bag/template_file.rb +++ b/lib/dice_bag/template_file.rb @@ -2,7 +2,6 @@ require 'dice_bag/template_helpers' require 'dice_bag/configuration' require 'dice_bag/warning' -require 'dice_bag/project' #this class encapsulates the template files we will generate in the project #using this gem From c4db1ca9668847bd5aafc78ee6af95c0b3e79ba8 Mon Sep 17 00:00:00 2001 From: Abhi Trivedi Date: Tue, 8 Dec 2015 11:27:19 -0800 Subject: [PATCH 3/7] - add `force` option to config:generate_all - update version and docs. --- CHANGELOG.md | 3 ++- README.md | 11 ++++++++++- lib/dice_bag/command.rb | 8 ++++---- lib/dice_bag/tasks/config.rake | 8 ++++---- lib/dice_bag/version.rb | 2 +- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 952d019..ffa3779 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 0.10.0 -* Generates diff between template and target file, and allows user to merge changes. +* Allows users to choose an appropriate action when source and local templates are different. +* `config:generate_all` rake task accepts a boolean argument indicating whether to generate templates in 'force mode'. # 0.9.0 diff --git a/README.md b/README.md index 054a704..243305b 100644 --- a/README.md +++ b/README.md @@ -109,9 +109,18 @@ directory are provided: Run the following command to generate them: ``` -[bundle exec] rake config:generate_all +[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[true] +``` + + As with your own templates, you should commit these pre-packaged templates to source control. diff --git a/lib/dice_bag/command.rb b/lib/dice_bag/command.rb index 89c17ce..0a922f1 100644 --- a/lib/dice_bag/command.rb +++ b/lib/dice_bag/command.rb @@ -32,14 +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) - copy_file default_template.file, default_template.destination + def generate_template(default_template, force=false) + copy_file default_template.file, default_template.destination, force: force end end diff --git a/lib/dice_bag/tasks/config.rake b/lib/dice_bag/tasks/config.rake index b3793e1..8f748cc 100644 --- a/lib/dice_bag/tasks/config.rake +++ b/lib/dice_bag/tasks/config.rake @@ -23,15 +23,15 @@ namespace :config do end desc "Generate all configuration file templates needed by this project" - task :generate_all do - DiceBag::Command.new.generate_all_templates + task :generate_all, :force do |t, args| + DiceBag::Command.new.generate_all_templates(args[:force]) end desc "Generate just the specified template, optionally in the specified location" - task :generate, :filename, :location do |t, args| + task :generate, :filename, :location, :force 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)) + DiceBag::Command.new.generate_template(DiceBag::DefaultTemplateFile.new(filename, location), args[:force]) end end diff --git a/lib/dice_bag/version.rb b/lib/dice_bag/version.rb index 7672ac7..df2c6db 100644 --- a/lib/dice_bag/version.rb +++ b/lib/dice_bag/version.rb @@ -1,3 +1,3 @@ module DiceBag - VERSION = '0.9.0' + VERSION = '0.10.0' end From e23708dbdc01aac821ccc801e9955184476242d9 Mon Sep 17 00:00:00 2001 From: Abhi Trivedi Date: Tue, 8 Dec 2015 11:30:50 -0800 Subject: [PATCH 4/7] Update .ruby-version --- .ruby-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ruby-version b/.ruby-version index eca07e4..4bf3096 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.1.2 +ruby-1.9.2-p290 From d88424d5cb445178521759fc22f1db5fdbbd17a4 Mon Sep 17 00:00:00 2001 From: Abhi Trivedi Date: Tue, 15 Dec 2015 11:29:14 -0800 Subject: [PATCH 5/7] - added `config:generate_all:force` task - bump version to 1.0.0 --- CHANGELOG.md | 6 +++--- README.md | 4 ++-- dice_bag.gemspec | 4 ++-- lib/dice_bag/tasks/config.rake | 20 +++++++++++++------- lib/dice_bag/version.rb | 2 +- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffa3779..abf1efb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ -# 0.10.0 +# 1.0.0 -* Allows users to choose an appropriate action when source and local templates are different. -* `config:generate_all` rake task accepts a boolean argument indicating whether to generate templates in 'force mode'. +* `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 diff --git a/README.md b/README.md index 243305b..07af943 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ directory are provided: Run the following command to generate them: ``` -[bundle exec] rake config:generate_all +[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. @@ -117,7 +117,7 @@ This command provides options to compare changes between source and local templa Alternatively, to force generate templates (replacing existing local templates with the source), run the following: ``` -[bundle exec] rake config:generate_all[true] +[bundle exec] rake config:generate_all:force ``` diff --git a/dice_bag.gemspec b/dice_bag.gemspec index e276f79..76afd98 100644 --- a/dice_bag.gemspec +++ b/dice_bag.gemspec @@ -15,8 +15,8 @@ Gem::Specification.new do |s| s.require_paths = ['lib'] s.add_dependency 'rake' - s.add_dependency 'thor', '~> 0' - s.add_dependency 'diff-lcs' + 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' diff --git a/lib/dice_bag/tasks/config.rake b/lib/dice_bag/tasks/config.rake index 8f748cc..8473165 100644 --- a/lib/dice_bag/tasks/config.rake +++ b/lib/dice_bag/tasks/config.rake @@ -22,16 +22,22 @@ namespace :config do DiceBag::Command.new.write(filename) end - desc "Generate all configuration file templates needed by this project" - task :generate_all, :force do |t, args| - DiceBag::Command.new.generate_all_templates(args[:force]) + 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(false) end - desc "Generate just the specified template, optionally in the specified location" - task :generate, :filename, :location, :force do |t, args| + 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), args[:force]) + raise 'A filename needs to be provided' if filename.nil? + DiceBag::Command.new.generate_template(DiceBag::DefaultTemplateFile.new(filename, location), false) end end diff --git a/lib/dice_bag/version.rb b/lib/dice_bag/version.rb index df2c6db..5821888 100644 --- a/lib/dice_bag/version.rb +++ b/lib/dice_bag/version.rb @@ -1,3 +1,3 @@ module DiceBag - VERSION = '0.10.0' + VERSION = '1.0.0' end From 2ae3190aefeba5b6540b6aeeb1349fc98f6aca6e Mon Sep 17 00:00:00 2001 From: Abhi Trivedi Date: Wed, 16 Dec 2015 10:12:55 -0800 Subject: [PATCH 6/7] - adding ruby 2.2.3 in travis.yml --- .ruby-version | 2 +- .travis.yml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.ruby-version b/.ruby-version index 4bf3096..8274681 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -ruby-1.9.2-p290 +ruby-2.2.3 diff --git a/.travis.yml b/.travis.yml index 6c5a2b9..5386432 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,4 +3,5 @@ rvm: - 1.9.3 - 2.0.0 - 2.1.1 + - 2.2.3 script: bundle exec rspec && bundle exec cucumber From dc0530c818d0cc33ab125970e129fdef03f47133 Mon Sep 17 00:00:00 2001 From: Abhi Trivedi Date: Wed, 16 Dec 2015 12:23:37 -0800 Subject: [PATCH 7/7] update bundler in travis ci --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 5386432..287cb71 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,4 +4,6 @@ rvm: - 2.0.0 - 2.1.1 - 2.2.3 +before_install: + - gem update bundler script: bundle exec rspec && bundle exec cucumber