Class: Blacksmith::Modulefile

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_blacksmith/modulefile.rb

Constant Summary collapse

FILES =
["metadata.json", "Modulefile"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Modulefile

Returns a new instance of Modulefile.

Raises:



24
25
26
27
28
# File 'lib/puppet_blacksmith/modulefile.rb', line 24

def initialize(path = nil)
  @path = path.nil? ? FILES.find {|f| File.exists? f} : path
  raise Blacksmith::Error, "Unable to find any of #{FILES}" unless @path
  @modulefile = @path =~ /Modulefile$/
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



22
23
24
# File 'lib/puppet_blacksmith/modulefile.rb', line 22

def path
  @path
end

Instance Method Details

#authorObject



47
48
49
# File 'lib/puppet_blacksmith/modulefile.rb', line 47

def author
  ['author'] || ['name'].split('-',2)[0]
end

#bump!Object



54
55
56
57
58
59
60
# File 'lib/puppet_blacksmith/modulefile.rb', line 54

def bump!
  new_version = increase_version(version)
  text = File.read(path)
  text = replace_version(text, new_version)
  File.open(path, "w") {|file| file.puts text}
  new_version
end

#increase_version(version) ⇒ Object

Raises:



72
73
74
75
76
# File 'lib/puppet_blacksmith/modulefile.rb', line 72

def increase_version(version)
  v = Gem::Version.new("#{version}.0")
  raise Blacksmith::Error, "Unable to increase prerelease version #{version}" if v.prerelease?
  v.bump.to_s
end

#metadataObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/puppet_blacksmith/modulefile.rb', line 30

def 
  unless @metadata
    if @modulefile
       = Puppet::ModuleTool::Metadata.new
      Puppet::ModuleTool::ModulefileReader.evaluate(, path)
      @metadata = { 'name' => .name, 'version' => .version, 'author' => .author }
    else
      @metadata = JSON.parse(File.read(path))
    end
  end
  @metadata
end

#nameObject

name in metadata.json is author-modulename



44
45
46
# File 'lib/puppet_blacksmith/modulefile.rb', line 44

def name
  @modulefile ? ['name'] : ['name'].split('-',2)[1]
end

#replace_version(text, version) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/puppet_blacksmith/modulefile.rb', line 62

def replace_version(text, version)
  if @modulefile
    text.gsub(/\nversion[ ]+['"].*['"]/, "\nversion '#{version}'")
  else
    json = JSON.parse(text)
    json['version'] = version
    JSON.pretty_generate(json)
  end
end

#versionObject



50
51
52
# File 'lib/puppet_blacksmith/modulefile.rb', line 50

def version
  ['version']
end