lib/bundlegem/cli/gem.rb in bundlegem-0.0.2 vs lib/bundlegem/cli/gem.rb in bundlegem-0.0.3

- old
+ new

@@ -16,10 +16,12 @@ validate_ext_name if options[:ext] end def run # Bundler.ui.confirm "Creating gem '#{name}'..." + + raise_project_with_that_name_already_exists! if File.exists?(target) underscored_name = name.tr('-', '_') namespaced_path = name.tr('-', '/') constant_name = name.split('_').map{|p| p[0..0].upcase + p[1..-1] unless p.empty?}.join constant_name = constant_name.split('-').map{|q| q[0..0].upcase + q[1..-1] }.join('::') if constant_name =~ /-/ @@ -105,15 +107,19 @@ "ext/newgem/newgem.c.tt" => "ext/#{name}/#{underscored_name}.c" ) end template_src = match_template_src + template_directories = dynamically_generate_template_directories templates = dynamically_generate_templates || templates + create_template_directories(template_directories, target) + templates.each do |src, dst| template("#{template_src}/#{src}", target.join(dst), config) end + # Bundler.ui.info "Initializing git repo in #{target}" Dir.chdir(target) { `git init`; `git add .` } if options[:edit] @@ -121,44 +127,64 @@ thor.run("#{options["edit"]} \"#{target.join("#{name}.gemspec")}\"") end end private + + def dynamically_generate_template_directories + return nil if options["template"].nil? + template_src = get_template_src + + template_dirs = {} + Dir.glob("#{template_src}/**").each do |f| + next unless File.directory? f + base_path = f[template_src.length+1..-1] + template_dirs.merge!(base_path => base_path.gsub('#{name}', "#{name}") ) + end + template_dirs + end + def dynamically_generate_templates return nil if options["template"].nil? template_src = get_template_src - + templates = {} Dir.glob("#{template_src}/**/*.tt").each do |f| base_path = f[template_src.length+1..-1] templates.merge!(base_path => base_path.gsub(/\.tt$/, "").gsub('#{name}', "#{name}") ) end + + raise_no_files_in_template_error! if templates.empty? templates end + + def create_template_directories(template_directories, target) + template_directories.each do |k,v| + FileUtils.mkdir_p("#{target}/#{v}") + end + end def match_template_src template_src = get_template_src - return template_src if template_exists_within_repo?(template_src) or File.exists?(template_src) # 'newgem' refers to the built in template that comes with the gem - - # else message the user that the template could not be found - err_missing_template = "Could not find template folder #{options["template"]} in `~/.bundle/gem_templates/`. Please check to make sure your desired template exists." - puts err_missing_template - Bundler.ui.error err_missing_template - exit 1 + if template_exists_within_repo?(template_src) or File.exists?(template_src) + return template_src # 'newgem' refers to the built in template that comes with the gem + else + raise_templet_not_found! # else message the user that the template could not be found + end end def get_template_src template_name = options["template"].nil? ? "newgem" : options["template"] if template_exists_within_repo?(template_name) # if template_exists_within_repo?(template_name) gem_template_location = get_internal_template_location else - gem_template_location = File.expand_path("~/.bundle/gem_templates") + gem_template_location = File.expand_path("~/.bundlegem/gem_templates") end template_src = "#{gem_template_location}/#{template_name}" end def template_exists_within_repo?(template_name) @@ -286,9 +312,34 @@ # EDIT: Reworked from Thor to not rely on Thor (or do so much unneeded stuff) # def make_file(destination, config, &block) FileUtils.mkdir_p(File.dirname(destination)) File.open(destination, "wb") { |f| f.write block.call } + end + + + + def raise_no_files_in_template_error! + puts "Ooops, the template was found for '#{options['template']}' in ~/.bundlegem/gem_templates, " + puts "but no files within it ended in .tt. Did you forget to rename the extensions of your files?" + puts + puts "Exiting..." + exit + end + + def raise_project_with_that_name_already_exists! + puts "Ooops, a project with the name #{target} already exists." + puts "Can't make project. Either delete that folder or choose a new project name" + puts + puts "Exiting..." + exit + end + + def raise_templet_not_found! + err_missing_template = "Could not find template folder '#{options["template"]}' in `~/.bundle/gem_templates/`. Please check to make sure your desired template exists." + puts err_missing_template + Bundler.ui.error err_missing_template + exit 1 end end end