Class: Gemma::GemFromTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/gemma/gem_from_template.rb

Overview

Configurable gem skeleton from a template.

Constant Summary collapse

TEMPLATE_ROOT =

Location of built-in templates.

File.join(File.dirname(__FILE__), '..', '..', 'template')
BUILTIN_TEMPLATES =

Built-in template names (not full paths). Order may be significant if there are files that occur in multiple templates (files in earlier templates are overwritten by those in later templates, at present).

%w(base executable minitest)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGemFromTemplate

Returns a new instance of GemFromTemplate.



22
23
24
25
26
# File 'lib/gemma/gem_from_template.rb', line 22

def initialize
  @gem_name = nil
  @module_name = nil
  @dir_name = nil
end

Instance Attribute Details

#dir_nameString

Name of the root directory of the gem to be created.

Returns:

  • (String)


65
66
67
# File 'lib/gemma/gem_from_template.rb', line 65

def dir_name
  @dir_name || @gem_name
end

#gem_nameString

The gem name to be used in the gemspec; it also gives defaults for the directory name and the gem’s module name.

Returns:

  • (String)


34
35
36
# File 'lib/gemma/gem_from_template.rb', line 34

def gem_name
  @gem_name
end

#module_nameString

Guess main module (or class) name from gem name (e.g. MyNewGem from my_new_gem). The gem contents should be contained in this module / class.

Returns:

  • (String)


51
52
53
# File 'lib/gemma/gem_from_template.rb', line 51

def module_name
  @module_name || Conventions.gem_name_to_module_name(self.gem_name)
end

Instance Method Details

#create_gem(template_paths, destination_path = self.destination_path) ⇒ Object

Copy given templates to destination_path and run erb where needed.

Parameters:

  • template_paths (Array<String>)

    absolute paths of the template directories to copy



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/gemma/gem_from_template.rb', line 87

def create_gem template_paths, destination_path=self.destination_path
  raise "destination #{destination_path} exists" if File.exists?(
    destination_path)

  # Copy templates in.
  FileUtils.mkdir_p destination_path
  for path in template_paths
    FileUtils.cp_r File.join(path,'.'), destination_path
  end

  Dir.chdir destination_path do
    dirs = Dir["**/*"].select { |f| File.directory? f }.sort
    dirs.grep(/gem_name/).each do |file|
      FileUtils.mv file, file.gsub(/gem_name/, gem_name)
    end

    files = (Dir["**/*"] + Dir["**/.*"]).select { |f| File.file? f }.sort
    FileUtils.chmod 0644, files
    FileUtils.chmod 0755, files.select{|f| File.dirname(f) == 'bin'}
    files.each do |file|
      # Rename files with names that depend on the gem name.
      if file =~ /gem_name/
        new_file = file.sub(/gem_name/, gem_name)
        FileUtils.mv file, new_file
        file = new_file
      end

      # Run erb to customize each file.
      if File.extname(file) == ".erb"
        erb_file = File.read file
        File.open file, "w" do |f|
          erb = ERB.new(erb_file)
          erb.filename = file
          f.puts erb.result(binding)
        end
        FileUtils.mv file, file.sub(/\.erb$/, '')
      end
    end
  end
end

#destination_pathObject

Full path of root of the gem to be created.



77
78
79
# File 'lib/gemma/gem_from_template.rb', line 77

def destination_path
  File.expand_path(File.join('.',self.dir_name))
end

#good_gem_name?Boolean

Gem name is consistent with naming conventions.

Returns:

  • (Boolean)


41
42
43
# File 'lib/gemma/gem_from_template.rb', line 41

def good_gem_name?
  Conventions.good_gem_name? self.gem_name
end