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).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGemFromTemplate

Returns a new instance of GemFromTemplate.



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

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)


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

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)


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

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)


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

def module_name
  @module_name || Conventions.gem_name_to_module_name(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



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
127
# File 'lib/gemma/gem_from_template.rb', line 88

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

  # Copy templates in.
  FileUtils.mkdir_p destination_path
  template_paths.each do |path|
    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 0o644, files
    FileUtils.chmod 0o755, 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.
      next unless 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

#destination_pathObject

Full path of root of the gem to be created.



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

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

#good_gem_name?Boolean

Gem name is consistent with naming conventions.

Returns:

  • (Boolean)


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

def good_gem_name?
  Conventions.good_gem_name? gem_name
end