Class: Librarian::Environment

Inherits:
Object
  • Object
show all
Includes:
Support::AbstractMethod
Defined in:
lib/librarian/environment.rb

Direct Known Subclasses

Mock::Environment

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::AbstractMethod

included

Constructor Details

#initialize(options = { }) ⇒ Environment

Returns a new instance of Environment.



26
27
28
29
30
31
32
# File 'lib/librarian/environment.rb', line 26

def initialize(options = { })
  @pwd = options.fetch(:pwd) { Dir.pwd }
  @env = options.fetch(:env) { ENV.to_hash }
  @home = options.fetch(:home) { File.expand_path("~") }
  @project_path = options[:project_path]
  @specfile_name = options[:specfile_name]
end

Instance Attribute Details

#uiObject

Returns the value of attribute ui.



22
23
24
# File 'lib/librarian/environment.rb', line 22

def ui
  @ui
end

Instance Method Details

#adapter_nameObject



73
74
75
# File 'lib/librarian/environment.rb', line 73

def adapter_name
  nil
end

#cache_pathObject



102
103
104
# File 'lib/librarian/environment.rb', line 102

def cache_path
  tmp_path.join("librarian/cache")
end

#config_dbObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/librarian/environment.rb', line 38

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

#config_keysObject



134
135
136
137
# File 'lib/librarian/environment.rb', line 134

def config_keys
  %[
  ]
end

#default_specfile_nameObject



50
51
52
53
54
55
# File 'lib/librarian/environment.rb', line 50

def default_specfile_name
  @default_specfile_name ||= begin
    capped = adapter_name.capitalize
    "#{capped}file"
  end
end

#dsl(*args, &block) ⇒ Object



122
123
124
# File 'lib/librarian/environment.rb', line 122

def dsl(*args, &block)
  dsl_class.run(self, *args, &block)
end

#dsl_classObject



126
127
128
# File 'lib/librarian/environment.rb', line 126

def dsl_class
  self.class.name.split("::")[0 ... -1].inject(Object, &:const_get)::Dsl
end

#ephemeral_lockfileObject



89
90
91
# File 'lib/librarian/environment.rb', line 89

def ephemeral_lockfile
  Lockfile.new(self, nil)
end

#http_proxy_uriObject

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


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/librarian/environment.rb', line 145

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

#lockObject



118
119
120
# File 'lib/librarian/environment.rb', line 118

def lock
  lockfile.read
end

#lockfileObject



85
86
87
# File 'lib/librarian/environment.rb', line 85

def lockfile
  Lockfile.new(self, lockfile_path)
end

#lockfile_nameObject



77
78
79
# File 'lib/librarian/environment.rb', line 77

def lockfile_name
  config_db.lockfile_name
end

#lockfile_pathObject



81
82
83
# File 'lib/librarian/environment.rb', line 81

def lockfile_path
  config_db.lockfile_path
end

#loggerObject



34
35
36
# File 'lib/librarian/environment.rb', line 34

def logger
  @logger ||= Logger.new(self)
end

#net_http_class(host) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/librarian/environment.rb', line 162

def net_http_class(host)
  return Net::HTTP if no_proxy?(host)

  @net_http_class ||= begin
    p = http_proxy_uri
    p ? Net::HTTP::Proxy(p.host, p.port, p.user, p.password) : Net::HTTP
  end
end

#project_pathObject



57
58
59
# File 'lib/librarian/environment.rb', line 57

def project_path
  config_db.project_path
end

#project_relative_path_to(path) ⇒ Object



110
111
112
# File 'lib/librarian/environment.rb', line 110

def project_relative_path_to(path)
  Pathname.new(path).relative_path_from(project_path)
end

#resolverObject



93
94
95
# File 'lib/librarian/environment.rb', line 93

def resolver
  Resolver.new(self)
end

#scratch_pathObject



106
107
108
# File 'lib/librarian/environment.rb', line 106

def scratch_path
  tmp_path.join("librarian/scratch")
end

#specObject



114
115
116
# File 'lib/librarian/environment.rb', line 114

def spec
  specfile.read
end

#specfileObject



69
70
71
# File 'lib/librarian/environment.rb', line 69

def specfile
  Specfile.new(self, specfile_path)
end

#specfile_nameObject



61
62
63
# File 'lib/librarian/environment.rb', line 61

def specfile_name
  config_db.specfile_name
end

#specfile_pathObject



65
66
67
# File 'lib/librarian/environment.rb', line 65

def specfile_path
  config_db.specfile_path
end

#tmp_pathObject



97
98
99
100
# File 'lib/librarian/environment.rb', line 97

def tmp_path
  part = config_db["tmp"] || "tmp"
  project_path.join(part)
end

#versionObject



130
131
132
# File 'lib/librarian/environment.rb', line 130

def version
  VERSION
end