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



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

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

#bump!(level = :patch) ⇒ Object



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

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

#bump_dep!(module_name, version) ⇒ Object



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

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



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

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



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

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

#replace_dependency_version(text, module_name, version) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/puppet_blacksmith/modulefile.rb', line 64

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



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

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

#versionObject



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

def version
  ['version']
end