Class: Gel::LockLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/gel/lock_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, gemfile = nil) ⇒ LockLoader

Returns a new instance of LockLoader.



7
8
9
10
# File 'lib/gel/lock_loader.rb', line 7

def initialize(filename, gemfile = nil)
  @filename = filename
  @gemfile = gemfile
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/gel/lock_loader.rb', line 4

def filename
  @filename
end

#gemfileObject (readonly)

Returns the value of attribute gemfile.



5
6
7
# File 'lib/gel/lock_loader.rb', line 5

def gemfile
  @gemfile
end

Instance Method Details

#activate(env, base_store, install: false, output: nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/gel/lock_loader.rb', line 63

def activate(env, base_store, install: false, output: nil)
  locked_store = Gel::LockedStore.new(base_store)

  locks = {}

  if install
    require_relative "installer"
    installer = Gel::Installer.new(base_store)
  end

  filtered_gems = Hash.new(nil)
  top_gems = []
  if gemfile && env
    gemfile.gems.each do |name, *|
      filtered_gems[name] = false
    end
    env.filtered_gems(gemfile.gems).each do |name, *|
      top_gems << name
      filtered_gems[name] = true
    end
  elsif pair = lock_content.assoc("DEPENDENCIES")
    _, list = pair
    top_gems = list.map { |name| name.split(" ", 2)[0].chomp("!") }
    top_gems.each do |name|
      filtered_gems[name] = true
    end
  end

  gems = {}
  each_gem do |section, body, name, version, platform, deps|
    next if env && !env.platform?(platform)

    gems[name] = [section, body, version, platform, deps]

    installer.known_dependencies name => deps.map(&:first) if installer
  end

  walk = lambda do |name|
    filtered_gems[name] = true
    next unless gems[name]
    gems[name].last.map(&:first).each do |dep_name|
      walk[dep_name] unless filtered_gems[dep_name]
    end
  end

  top_gems.each(&walk)

  require_relative "git_depot"
  require_relative "work_pool"

  Gel::WorkPool.new(8) do |work_pool|
    git_depot = Gel::GitDepot.new(base_store)

    gems.each do |name, (section, body, version, platform, _deps)|
      next unless filtered_gems[name]

      if section == :gem
        if installer && !base_store.gem?(name, version, platform)
          require_relative "catalog"
          catalogs = body["remote"].map { |r| Gel::Catalog.new(r, work_pool: work_pool) }
          installer.install_gem(catalogs, name, platform ? "#{version}-#{platform}" : version)
        end

        locks[name] = version
      else
        if section == :git
          remote = body["remote"].first
          revision = body["revision"].first

          dir = git_depot.git_path(remote, revision)
          if installer && !Dir.exist?(dir)
            installer.load_git_gem(remote, revision, name)

            locks[name] = -> { Gel::DirectGem.new(dir, name, version) }
            next
          end
        else
          dir = File.expand_path(body["remote"].first, File.dirname(filename))
        end

        locks[name] = Gel::DirectGem.new(dir, name, version)
      end
    end

    installer.wait(output) if installer

    locks.each do |name, locked|
      locks[name] = locked.call if locked.is_a?(Proc)
    end
  end

  locked_store.lock(locks)

  if env
    env.open(locked_store)

    env.gems_from_lock(locks)
  end

  locked_store
end

#bundler_versionObject



50
51
52
53
# File 'lib/gel/lock_loader.rb', line 50

def bundler_version
  _, (version,) = lock_content.assoc("BUNDLED WITH")
  version
end

#each_gemObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gel/lock_loader.rb', line 16

def each_gem
  lock_content.each do |(section, body)|
    case section
    when "GEM", "PATH", "GIT"
      specs = body["specs"]
      specs.each do |gem_spec, dep_specs|
        gem_spec =~ /\A(.+) \(([^-]+)(?:-(.+))?\)\z/
        name, version, platform = $1, $2, $3

        if dep_specs
          deps = dep_specs.map do |spec|
            spec =~ /\A(.+?)(?: \((.+)\))?\z/
            [$1, $2 ? $2.split(", ") : []]
          end
        else
          deps = []
        end

        sym =
          case section
          when "GEM"; :gem
          when "PATH"; :path
          when "GIT"; :git
          end
        yield sym, body, name, version, platform, deps
      end
    when "PLATFORMS", "DEPENDENCIES"
    when "BUNDLED WITH"
    else
      warn "Unknown lockfile section #{section.inspect}"
    end
  end
end

#gem_namesObject



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

def gem_names
  names = []
  each_gem do |section, body, name, version, platform, deps|
    names << name
  end
  names
end

#lock_contentObject



12
13
14
# File 'lib/gel/lock_loader.rb', line 12

def lock_content
  @lock_content ||= Gel::LockParser.new.parse(File.read(filename))
end