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:



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

def initialize(path = nil)
  @path = path.nil? ? FILES.find { |f| File.exist? 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.



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

def path
  @path
end

Instance Method Details

#authorObject



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

def author
  ['author'] || namespace
end

#bump!(level = :patch) ⇒ Object



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

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

#bump_dep!(module_name, version) ⇒ Object



52
53
54
55
56
# File 'lib/puppet_blacksmith/modulefile.rb', line 52

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



36
37
38
39
40
41
# File 'lib/puppet_blacksmith/modulefile.rb', line 36

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



64
65
66
67
# File 'lib/puppet_blacksmith/modulefile.rb', line 64

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

#metadataObject



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

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

#nameObject

name in metadata.json is author-modulename



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

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



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

def replace_dependency_version(text, module_name, version)
  module_name = module_name.sub(%r{/}, '-')
  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



58
59
60
61
62
# File 'lib/puppet_blacksmith/modulefile.rb', line 58

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

#versionObject



32
33
34
# File 'lib/puppet_blacksmith/modulefile.rb', line 32

def version
  ['version']
end