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:



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

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.



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

def path
  @path
end

Instance Method Details

#authorObject



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

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

#bump!(level = :patch) ⇒ Object



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

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

#bump_dep!(module_name, version) ⇒ Object



67
68
69
70
71
# File 'lib/puppet_blacksmith/modulefile.rb', line 67

def bump_dep!(module_name, version)
  text = File.read(path)
  text = replace_dependency_version(text, module_name, version)
  File.open(path, "w") {|file| file.puts text}
end

#increase_version(version, level = :patch) ⇒ Object



83
84
85
86
# File 'lib/puppet_blacksmith/modulefile.rb', line 83

def increase_version(version, level = :patch)
  v = VersionHelper::Version.new(version)
  v.send("#{level}!").to_s
end

#metadataObject



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

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



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

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

#replace_dependency_version(text, module_name, version) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/puppet_blacksmith/modulefile.rb', line 88

def replace_dependency_version(text, module_name, version)
  if @modulefile
    # example: dependency 'puppetlabs/stdlib', '>= 2.3.0'
    module_name = module_name.sub(/^([^\/-]+)-/, '\1/')
    text.gsub(/\ndependency[ ]+['"].*#{module_name}['"],([ ]+['"].*['"]|)/, "\ndependency '#{module_name}', '#{version}'")
  else
    module_name = module_name.sub(/\//, '-')
    json = JSON.parse(text)
    new_dep_list = []
    json['dependencies'].each do |dep|
      dep['version_requirement'] = version if dep['name'] == module_name
      new_dep_list << dep
    end
    json['dependencies'] = new_dep_list
    JSON.pretty_generate(json)
  end
end

#replace_version(text, version) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/puppet_blacksmith/modulefile.rb', line 73

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



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

def version
  ['version']
end