class Kamaze::Project::Tools::Gemspec::Writer
Class intended to generate “gemspec“ file from a template
Default template filename is “gemspec.tpl“. Consider to use “Dir.chdir“ in order read and generate contents from the right directory (especially during tests).
@see templated @see Kamaze::Project
Attributes
@return [FileUtils]
@see Kamaze.project
@type [Object|Kamaze::Project]
@return [Kamaze::Project]
Set path (almost filename) to templated gemspec
@type [String|Pathname]
Public Instance Methods
Get generated/templated content
@return [String]
# File lib/kamaze/project/tools/gemspec/writer.rb, line 99 def content template.render(templated.to_s, context) end
Get template's context (variables)
@return [Hahsh]
# File lib/kamaze/project/tools/gemspec/writer.rb, line 84 def context # @formatter:off { name: project.name, version: project.version, dependencies: dependency, }.yield_self do |variables| project.version.to_h.merge(variables) end # @formatter:on end
Get dependency
@return [Dependency]
# File lib/kamaze/project/tools/gemspec/writer.rb, line 77 def dependency DepGen.new(spec_id).dependency end
Get path (almost filename) to generated gemspec
@return [Pathname]
# File lib/kamaze/project/tools/gemspec/writer.rb, line 50 def generated Pathname.new("#{project.name}.gemspec") end
# File lib/kamaze/project/tools/gemspec/writer.rb, line 36 def mutable_attributes [:templated, :project] end
Get variable used in template for “Gem::Specification“ instantiation
@return [String]
# File lib/kamaze/project/tools/gemspec/writer.rb, line 65 def spec_id # @formatter:off templated .read .scan(/Gem::Specification\.new\s+do\s+\|([a-z]+)\|/) .flatten.fetch(0) # @formatter:on end
Get status for current gemspec file.
@return [Hash{Symbol => Object}]
# File lib/kamaze/project/tools/gemspec/writer.rb, line 106 def status Pathname.new(self.to_s).yield_self do |file| # @formatter:off { mtime: -> { return File.mtime(file) if file.file? }.call, content: -> { return file.read if file.file? }.call } # @formatter:on end end
Get path (almost filename) to templated gemspec
@return [Pathname]
# File lib/kamaze/project/tools/gemspec/writer.rb, line 43 def templated Pathname.new(@templated) end
Get string representation
@see generated @return [String]
# File lib/kamaze/project/tools/gemspec/writer.rb, line 58 def to_s generated.to_s end
Write gemspec file
@return [self]
# File lib/kamaze/project/tools/gemspec/writer.rb, line 120 def write(preserve_mtime: false) self.tap do (preserve_mtime ? status : {}).tap do |meta| generated.write(content) if preserve_mtime if content == meta.fetch(:content, nil) fs.touch(self.to_s, mtime: meta.fetch(:mtime), nocreate: true) end end end end end
Protected Instance Methods
# File lib/kamaze/project/tools/gemspec/writer.rb, line 139 def setup @templated ||= 'gemspec.tpl' @project ||= Kamaze::Project.instance @fs ||= FileUtils end
Get template engine
@return [Tenjin::Engine]
# File lib/kamaze/project/tools/gemspec/writer.rb, line 148 def template Tenjin::Engine.new(cache: false) end