Class: Kamaze::Project::Tools::Gemspec::Writer

Inherits:
BaseTool show all
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

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Kamaze::Project::Tools::BaseTool

Instance Attribute Details

#fsFileUtils (readonly, protected)

Returns:

  • (FileUtils)


137
138
139
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 137

def fs
  @fs
end

#observer_peersHash|nil (readonly, protected) Originally defined in module Concern::Observable

Returns:

  • (Hash|nil)

#projectKamaze::Project

Returns:

See Also:



34
35
36
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 34

def project
  @project
end

#templatedPathname

Get path (almost filename) to templated gemspec

Returns:

  • (Pathname)


43
44
45
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 43

def templated
  Pathname.new(@templated)
end

Instance Method Details

#contentString

Get generated/templated content

Returns:

  • (String)


99
100
101
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 99

def content
  template.render(templated.to_s, context)
end

#contextHahsh

Get template's context (variables)

Returns:

  • (Hahsh)


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

#dependencyDependency

Get dependency

Returns:



77
78
79
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 77

def dependency
  DepGen.new(spec_id).dependency
end

#generatedPathname

Get path (almost filename) to generated gemspec

Returns:

  • (Pathname)


50
51
52
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 50

def generated
  Pathname.new("#{project.name}.gemspec")
end

#mutable_attributesObject



36
37
38
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 36

def mutable_attributes
  [:templated, :project]
end

#setupObject (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_idString

Get variable used in template for Gem::Specification instantiation

Returns:

  • (String)


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

#statusHash{Symbol => Object}

Get status for current gemspec file.

Returns:



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

#templateTenjin::Engine (protected)

Get template engine

Returns:

  • (Tenjin::Engine)


148
149
150
# File 'lib/kamaze/project/tools/gemspec/writer.rb', line 148

def template
  Tenjin::Engine.new(cache: false)
end

#to_sString

Get string representation

Returns:

  • (String)

See Also:



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

Returns:

  • (self)


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 |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