Class: Kamaze::Project::Tools::Gemspec::Writer
- Inherits:
-
BaseTool
- Object
- Observable
- BaseTool
- Kamaze::Project::Tools::Gemspec::Writer
- Defined in:
- lib/kamaze/project/tools/gemspec/writer.rb
Overview
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).
Defined Under Namespace
Classes: DepGen, Dependency
Instance Attribute Summary collapse
- #fs ⇒ FileUtils readonly protected
- #observer_peers ⇒ Hash|nil included from Concern::Observable readonly protected
- #project ⇒ Kamaze::Project
-
#templated ⇒ Pathname
Get path (almost filename) to templated gemspec.
Instance Method Summary collapse
-
#content ⇒ String
Get generated/templated content.
-
#context ⇒ Hahsh
Get template's context (variables).
-
#dependency ⇒ Dependency
Get dependency.
-
#generated ⇒ Pathname
Get path (almost filename) to generated gemspec.
- #mutable_attributes ⇒ Object
- #setup ⇒ Object protected
-
#spec_id ⇒ String
Get variable used in template for
Gem::Specification
instantiation. -
#status ⇒ Hash{Symbol => Object}
Get status for current gemspec file.
-
#template ⇒ Tenjin::Engine
protected
Get template engine.
-
#to_s ⇒ String
Get string representation.
-
#write(preserve_mtime: false) ⇒ self
Write gemspec file.
Constructor Details
This class inherits a constructor from Kamaze::Project::Tools::BaseTool
Instance Attribute Details
#fs ⇒ FileUtils (readonly, protected)
137 138 139 |
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 137 def fs @fs end |
#observer_peers ⇒ Hash|nil (readonly, protected) Originally defined in module Concern::Observable
#project ⇒ Kamaze::Project
34 35 36 |
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 34 def project @project end |
#templated ⇒ Pathname
Get path (almost filename) to templated gemspec
43 44 45 |
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 43 def templated Pathname.new(@templated) end |
Instance Method Details
#content ⇒ String
Get generated/templated content
99 100 101 |
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 99 def content template.render(templated.to_s, context) end |
#context ⇒ Hahsh
Get template's context (variables)
84 85 86 87 88 89 90 91 92 93 94 |
# 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 |
#dependency ⇒ Dependency
Get dependency
77 78 79 |
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 77 def dependency DepGen.new(spec_id).dependency end |
#generated ⇒ Pathname
Get path (almost filename) to generated gemspec
50 51 52 |
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 50 def generated Pathname.new("#{project.name}.gemspec") end |
#mutable_attributes ⇒ Object
36 37 38 |
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 36 def mutable_attributes [:templated, :project] end |
#setup ⇒ Object (protected)
139 140 141 142 143 |
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 139 def setup @templated ||= 'gemspec.tpl' @project ||= Kamaze::Project.instance @fs ||= FileUtils end |
#spec_id ⇒ String
Get variable used in template for Gem::Specification
instantiation
65 66 67 68 69 70 71 72 |
# 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 |
#status ⇒ Hash{Symbol => Object}
Get status for current gemspec file.
106 107 108 109 110 111 112 113 114 115 |
# 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 |
#template ⇒ Tenjin::Engine (protected)
Get template engine
148 149 150 |
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 148 def template Tenjin::Engine.new(cache: false) end |
#to_s ⇒ String
Get string representation
58 59 60 |
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 58 def to_s generated.to_s end |
#write(preserve_mtime: false) ⇒ self
Write gemspec file
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 120 def write(preserve_mtime: false) self.tap do (preserve_mtime ? status : {}).tap do || generated.write(content) if preserve_mtime if content == .fetch(:content, nil) fs.touch(self.to_s, mtime: .fetch(:mtime), nocreate: true) end end end end end |