1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
require "pathname"
require 'net/http'
require "uri"
require "etc"
require "librarian/helpers"
require "librarian/support/abstract_method"
require "librarian/error"
require "librarian/config"
require "librarian/lockfile"
require "librarian/logger"
require "librarian/specfile"
require "librarian/resolver"
require "librarian/dsl"
require "librarian/source"
require "librarian/version"
require "librarian/environment/runtime_cache"
module Librarian
class Environment
include Support::AbstractMethod
attr_accessor :ui
attr_reader :runtime_cache
abstract_method :specfile_name, :dsl_class, :install_path
def initialize(options = { })
@pwd = options.fetch(:pwd) { Dir.pwd }
@env = options.fetch(:env) { ENV.to_hash }
@home = options.fetch(:home) { default_home }
@project_path = options[:project_path]
@runtime_cache = RuntimeCache.new
end
def logger
@logger ||= Logger.new(self)
end
def config_db
@config_db ||= begin
Config::Database.new(adapter_name,
:pwd => @pwd,
:env => @env,
:home => @home,
:project_path => @project_path,
:specfile_name => default_specfile_name
)
end
end
def default_specfile_name
@default_specfile_name ||= begin
capped = adapter_name.capitalize
"#{capped}file"
end
end
def project_path
config_db.project_path
end
def specfile_name
config_db.specfile_name
end
def specfile_path
config_db.specfile_path
end
def specfile
@specfile ||= Specfile.new(self, specfile_path)
end
def adapter_module
implementation? or return
self.class.name.split("::")[0 ... -1].inject(Object, &:const_get)
end
def adapter_name
implementation? or return
Helpers.camel_cased_to_dasherized(self.class.name.split("::")[-2])
end
def adapter_version
implementation? or return
adapter_module::VERSION
end
def lockfile_name
config_db.lockfile_name
end
def lockfile_path
config_db.lockfile_path
end
def lockfile
Lockfile.new(self, lockfile_path)
end
def ephemeral_lockfile
Lockfile.new(self, nil)
end
def resolver(options = { })
Resolver.new(self, resolver_options.merge(options))
end
def resolver_options
{
:cyclic => resolver_permit_cyclic_reslutions?,
}
end
def resolver_permit_cyclic_reslutions?
false
end
def tmp_path
part = config_db["tmp"] || "tmp"
project_path.join(part)
end
def cache_path
tmp_path.join("librarian/cache")
end
def scratch_path
tmp_path.join("librarian/scratch")
end
def project_relative_path_to(path)
Pathname.new(path).relative_path_from(project_path)
end
def spec
specfile.read
end
def lock
lockfile.read
end
def dsl(*args, &block)
dsl_class.run(self, *args, &block)
end
def dsl_class
adapter_module::Dsl
end
def version
VERSION
end
def config_keys
%[
]
end
# The HTTP proxy specified in the environment variables:
# * HTTP_PROXY
# * HTTP_PROXY_USER
# * HTTP_PROXY_PASS
# Adapted from:
# https://github.com/rubygems/rubygems/blob/v1.8.24/lib/rubygems/remote_fetcher.rb#L276-293
def http_proxy_uri
@http_proxy_uri ||= begin
keys = %w( HTTP_PROXY HTTP_PROXY_USER HTTP_PROXY_PASS )
env = Hash[ENV.
map{|k, v| [k.upcase, v]}.
select{|k, v| keys.include?(k)}.
reject{|k, v| v.nil? || v.empty?}]
uri = env["HTTP_PROXY"] or return
uri = "http://#{uri}" unless uri =~ /^(https?|ftp|file):/
uri = URI.parse(uri)
uri.user ||= env["HTTP_PROXY_USER"]
uri.password ||= env["HTTP_PROXY_PASS"]
uri
end
end
def net_http_class(host)
no_proxy?(host) ? Net::HTTP : net_http_default_class
end
def inspect
"#<#{self.class}:0x#{__id__.to_s(16)}>"
end
private
def environment
self
end
def implementation?
self.class != ::Librarian::Environment
end
def default_home
File.expand_path(ENV["HOME"] || Etc.getpwnam(Etc.getlogin).dir)
end
def no_proxy_list
@no_proxy_list ||= begin
list = ENV['NO_PROXY'] || ENV['no_proxy'] || ""
list.split(/\s*,\s*/) + %w(localhost 127.0.0.1)
end
end
def no_proxy?(host)
no_proxy_list.any? do |host_addr|
host.end_with?(host_addr)
end
end
def net_http_default_class
@net_http_default_class ||= begin
p = http_proxy_uri
p ? Net::HTTP::Proxy(p.host, p.port, p.user, p.password) : Net::HTTP
end
end
end
end
|