Class: LockJar::Domain::Lockfile

Inherits:
Object
  • Object
show all
Defined in:
lib/lock_jar/domain/lockfile.rb

Overview

Class representation of the lock file

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLockfile

Returns a new instance of Lockfile.



68
69
70
71
72
73
74
75
76
77
# File 'lib/lock_jar/domain/lockfile.rb', line 68

def initialize
  @groups = { 'default' => {} }
  @maps = []
  @excludes = []
  @remote_repositories = Set.new
  @gems = []
  @merged = []

  @version = LockJar::VERSION # default version
end

Instance Attribute Details

#excludesObject

Returns the value of attribute excludes.



24
25
26
# File 'lib/lock_jar/domain/lockfile.rb', line 24

def excludes
  @excludes
end

#gemsObject

Returns the value of attribute gems.



24
25
26
# File 'lib/lock_jar/domain/lockfile.rb', line 24

def gems
  @gems
end

#groupsObject

Returns the value of attribute groups.



24
25
26
# File 'lib/lock_jar/domain/lockfile.rb', line 24

def groups
  @groups
end

#local_repositoryObject

Returns the value of attribute local_repository.



24
25
26
# File 'lib/lock_jar/domain/lockfile.rb', line 24

def local_repository
  @local_repository
end

#mapsObject

Returns the value of attribute maps.



24
25
26
# File 'lib/lock_jar/domain/lockfile.rb', line 24

def maps
  @maps
end

#mergedObject

Returns the value of attribute merged.



24
25
26
# File 'lib/lock_jar/domain/lockfile.rb', line 24

def merged
  @merged
end

#remote_repositoriesObject

Returns the value of attribute remote_repositories.



24
25
26
# File 'lib/lock_jar/domain/lockfile.rb', line 24

def remote_repositories
  @remote_repositories
end

#versionObject

Returns the value of attribute version.



24
25
26
# File 'lib/lock_jar/domain/lockfile.rb', line 24

def version
  @version
end

Class Method Details

.fs_or_classpath(path) ⇒ Object

rubocop:enable Metrics/PerceivedComplexity



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lock_jar/domain/lockfile.rb', line 48

def self.fs_or_classpath(path)
  if File.exist? path
    YAML.load_file(path)

  # Lookup of Jarfile.lock in the classpath
  elsif Naether.platform == 'java' || path.start_with?('classpath:')
    stream = java.lang.Object.java_class.resource_as_stream("/#{path.gsub('classpath:', '')}")
    if stream
      reader = java.io.BufferedReader.new(java.io.InputStreamReader.new(stream))
      lines = ''
      while (line = reader.read_line)
        lines << line << "\n"
      end
      reader.close

      YAML.load(lines)
    end
  end
end

.read(path) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lock_jar/domain/lockfile.rb', line 28

def self.read(path)
  lock_data = fs_or_classpath(path)

  fail "lockfile #{path} not found" if lock_data.nil?

  Lockfile.new.tap do |lockfile|
    lockfile.version = lock_data['version'] || LockJar::VERSION
    lockfile.merged = lock_data['merged']
    lockfile.local_repository = lock_data['local_repository']
    lockfile.merged = lock_data['merged'] || []
    lockfile.maps = lock_data['maps'] || []
    lockfile.excludes = lock_data['excludes'] || []
    lockfile.groups = lock_data['groups'] || lock_data['scopes'] || {}
    lockfile.remote_repositories =
      Set.new(Array(lock_data['remote_repositories'] || lock_data['repositories']))
    lockfile.gems = lock_data['gems'] || []
  end
end

Instance Method Details

#to_hashObject Also known as: to_h



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lock_jar/domain/lockfile.rb', line 79

def to_hash
  lock_data = { 'version' => @version }

  lock_data['local_repository'] = local_repository unless local_repository.nil?

  lock_data['merged'] = merged unless merged.empty?

  lock_data['maps'] = maps if maps.size > 0

  lock_data['excludes'] = excludes if excludes.size > 0

  lock_data['gems'] = gems unless gems.empty?

  lock_data['groups'] = groups

  if remote_repositories.size > 0
    lock_data['remote_repositories'] = remote_repositories.to_a
  end

  lock_data
end

#to_yamlObject



102
103
104
# File 'lib/lock_jar/domain/lockfile.rb', line 102

def to_yaml
  to_hash.to_yaml
end

#write(path) ⇒ Object



106
107
108
109
110
# File 'lib/lock_jar/domain/lockfile.rb', line 106

def write(path)
  File.open(path, 'w') do |f|
    f.write(to_yaml)
  end
end