Class: Blacksmith::Modulefile

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

Constant Summary collapse

FILES =
["metadata.json"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Modulefile

Returns a new instance of Modulefile.

Raises:


10
11
12
13
# File 'lib/puppet_blacksmith/modulefile.rb', line 10

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
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path


8
9
10
# File 'lib/puppet_blacksmith/modulefile.rb', line 8

def path
  @path
end

Instance Method Details

#authorObject


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

def author
  ['author'] || namespace
end

#bump!(level = :patch) ⇒ Object


41
42
43
44
# File 'lib/puppet_blacksmith/modulefile.rb', line 41

def bump!(level = :patch)
  new_version = increase_version(version, level)
  bump_to_version!(new_version)
end

#bump_dep!(module_name, version) ⇒ Object


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

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

#bump_to_version!(new_version) ⇒ Object


34
35
36
37
38
39
# File 'lib/puppet_blacksmith/modulefile.rb', line 34

def bump_to_version!(new_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, level = :patch) ⇒ Object


62
63
64
65
# File 'lib/puppet_blacksmith/modulefile.rb', line 62

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

#metadataObject


15
16
17
18
# File 'lib/puppet_blacksmith/modulefile.rb', line 15

def 
  @metadata = JSON.parse(File.read(path)) unless @metadata
  @metadata
end

#nameObject

name in metadata.json is author-modulename


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

def name
  ['name'].split('-',2)[1]
end

#namespaceObject


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

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

#replace_dependency_version(text, module_name, version) ⇒ Object


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/puppet_blacksmith/modulefile.rb', line 67

def replace_dependency_version(text, module_name, version)
  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

#replace_version(text, version) ⇒ Object


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

def replace_version(text, version)
  json = JSON.parse(text)
  json['version'] = version
  JSON.pretty_generate(json)
end

#versionObject


30
31
32
# File 'lib/puppet_blacksmith/modulefile.rb', line 30

def version
  ['version']
end