Class: Gemirro::Server

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/gemirro/server.rb

Overview

Launch Sinatra server to easily download gems.

Constant Summary collapse

URI_REGEXP =

rubocop:disable Layout/LineLength

/^(.*)-(\d+(?:\.\d+){1,4}.*?)(?:-(x86-(?:(?:mswin|mingw)(?:32|64)).*?|java))?\.(gem(?:spec\.rz)?)$/.freeze
GEMSPEC_TYPE =

rubocop:enable Layout/LineLength

'gemspec.rz'
GEM_TYPE =
'gem'

Instance Method Summary collapse

Instance Method Details

#*nil

Try to get all request and download files if files aren’t found.

Returns:

  • (nil)


116
117
118
119
120
121
122
123
124
125
# File 'lib/gemirro/server.rb', line 116

get('*') do |path|
  resource = "#{settings.public_folder}#{path}"

  # Try to download gem
  fetch_gem(resource) unless File.exist?(resource)
  # If not found again, return a 404
  return not_found unless File.exist?(resource)

  send_file(resource)
end

#/nil

Display home page containing the list of gems already downloaded on the server

Returns:

  • (nil)


86
87
88
# File 'lib/gemirro/server.rb', line 86

get('/') do
  erb(:index, {}, gems: Utils.gems_collection)
end

#fetch_gem(resource) ⇒ Indexer

Try to fetch gem and download its if it’s possible, and build and install indicies.

Parameters:

  • resource (String)

Returns:



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
# File 'lib/gemirro/server.rb', line 134

def fetch_gem(resource)
  return unless Utils.configuration.fetch_gem

  name = File.basename(resource)
  result = name.match(URI_REGEXP)
  return unless result

  gem_name, gem_version, gem_platform, gem_type = result.captures
  return unless gem_name && gem_version

  begin
    gem = Utils.stored_gem(gem_name, gem_version, gem_platform)
    gem.gemspec = true if gem_type == GEMSPEC_TYPE

    return if Utils.gems_fetcher.gem_exists?(gem.filename(gem_version)) && gem_type == GEM_TYPE
    return if Utils.gems_fetcher.gemspec_exists?(gem.gemspec_filename(gem_version)) && gem_type == GEMSPEC_TYPE

    Utils.logger
         .info("Try to download #{gem_name} with version #{gem_version}")
    Utils.gems_fetcher.source.gems.clear
    Utils.gems_fetcher.source.gems.push(gem)
    Utils.gems_fetcher.fetch

    update_indexes if Utils.configuration.update_on_fetch
  rescue StandardError => e
    Utils.logger.error(e)
  end
end

#gem_dependencies(gem_name) ⇒ Array

List of versions and dependencies of each version from a gem name.

Returns:

  • (Array)


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/gemirro/server.rb', line 213

def gem_dependencies(gem_name)
  Utils.cache.cache(gem_name) do
    gems = Utils.gems_collection(false)
    gem_collection = gems.find_by_name(gem_name)

    return '' if gem_collection.nil?

    gem_collection = Parallel.map(gem_collection, in_threads: 4) do |gem|
      [gem, spec_for(gem.name, gem.number, gem.platform)]
    end
    gem_collection.compact!

    Parallel.map(gem_collection, in_threads: 4) do |gem, spec|
      dependencies = spec.dependencies.select do |d|
        d.type == :runtime
      end

      dependencies = Parallel.map(dependencies, in_threads: 4) do |d|
        [d.name.is_a?(Array) ? d.name.first : d.name, d.requirement.to_s]
      end

      {
        name: gem.name,
        number: gem.number,
        platform: gem.platform,
        dependencies: dependencies
      }
    end
  end
end

#query_gemsArray

Return all gems pass to query

Returns:

  • (Array)


187
188
189
# File 'lib/gemirro/server.rb', line 187

def query_gems
  params[:gems].to_s.split(',')
end

#query_gems_listArray

Return gems list from query params

Returns:

  • (Array)


196
197
198
199
200
201
202
203
204
205
# File 'lib/gemirro/server.rb', line 196

def query_gems_list
  Utils.gems_collection(false) # load collection
  gems = Parallel.map(query_gems, in_threads: 4) do |query_gem|
    gem_dependencies(query_gem)
  end

  gems.flatten!
  gems.reject!(&:empty?)
  gems
end

#update_indexesIndexer

Update indexes files

Returns:



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/gemirro/server.rb', line 168

def update_indexes
  indexer = Gemirro::Indexer.new(Utils.configuration.destination)
  indexer.only_origin = true
  indexer.ui = ::Gem::SilentUI.new

  Utils.logger.info('Generating indexes')
  indexer.update_index
  indexer.updated_gems.each do |gem|
    Utils.cache.flush_key(File.basename(gem))
  end
rescue SystemExit => e
  Utils.logger.info(e.message)
end