Sha256: 39bdd5d9c3342dd4f05c9f4af8abcd424bd2d96ff29a8e599992dc8a7880c6f4

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

require 'private_gem/version'
require 'bundler'
require 'uri'

module PrivateGem
  def self.server
    urls_with_credentials = Bundler.settings.all.select do |n|
      url = URI.parse(n)
      url && ['http', 'https'].include?(url.scheme)
    end

    hosts_with_credentials = Bundler.settings.all.select do |n|
      n =~ /^([-_a-z\d]+\.)+[a-z]+$/
    end

    if private_gem_server = Bundler.settings['private_gem_server']
      if urls_with_credentials.include?(private_gem_server)
        return private_gem_server
      end

      private_gem_server_uri = URI.parse(private_gem_server)
      if private_gem_server_uri && hosts_with_credentials.include?(private_gem_server_uri.host)
        return private_gem_server
      end

      abort("You don't have any configured credentials for the private gem server at #{private_gem_server}")
    end

    if urls_with_credentials.empty?
      abort("You don't have any configured private gem credentials.")
    end

    if urls_with_credentials.size > 1
      msg = "You have multiple private gem servers defined:\n"
      urls_with_credentials.each do |s|
        msg << "  #{s}\n"
      end
      abort("#{msg}You need to select which private gem server to use using `bundle config private_gem_server URL_OF_GEM_SERVER`")
    end

    urls_with_credentials.first
  end

  def self.credentials
    Bundler.settings[server] || Bundler.settings[URI.parse(server).host]
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
private_gem-1.1.4 lib/private_gem.rb
private_gem-1.1.3 lib/private_gem.rb
private_gem-1.1.2 lib/private_gem.rb
private_gem-1.1.1 lib/private_gem.rb
private_gem-1.1.0 lib/private_gem.rb