Class: Naether::Bootstrap

Inherits:
Object
  • Object
show all
Defined in:
lib/naether/bootstrap.rb

Overview

Helper for bootstrapping Naether

Author:

  • Michael Guymon

Constant Summary collapse

@@dependencies =
nil

Class Method Summary collapse

Class Method Details

.bootstrap_local_repo(local_repo = nil, opts = {}) ⇒ Object

Bootstrap the local repo by downloading Naether’s dependencies

Parameters:

  • local_repo (String) (defaults to: nil)

    defaults to #default_local_repo

  • opts (hash) (defaults to: {})


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/naether/bootstrap.rb', line 73

def bootstrap_local_repo(local_repo = nil, opts = {})
  local_repo = local_repo || default_local_repo

  opts[:local_repo] = local_repo

  temp_naether_dir = File.join(local_repo, ".naether")

  deps = download_dependencies(temp_naether_dir, opts)

  jars = (deps[:exists] + deps[:downloaded]).map { |jar| jar.values.first }

  jars = Naether::Java.internal_load_paths(jars)

  if (deps[:downloaded].size > 0)
    install_dependencies_to_local_repo(deps[:downloaded], jars, opts)
  end
end

.check_local_repo_for_deps(local_repo = nil, opts = {}) ⇒ Hash

Check local_repo for Naether dependencies

Parameters:

  • local_repo (String) (defaults to: nil)
  • opts (Hash) (defaults to: {})

Returns:

  • (Hash)

    with status of missing, downloaded, exists dependencies



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
# File 'lib/naether/bootstrap.rb', line 155

def check_local_repo_for_deps(local_repo = nil, opts = {})
  local_repo = local_repo || default_local_repo
  local_repo = File.expand_path(local_repo)

  #puts "Checking #{local_repo} for jars to bootstrap Naether"

  deps = { :exists => [], :missing => [], :downloaded => [] }

  dependencies(opts[:dep_yaml]).each do |dep|
    notation = dep.split(":")
    group = notation[0].gsub("\.", File::SEPARATOR)
    artifact = notation[1].gsub("\.", File::SEPARATOR)
    type = notation[2]
    version = notation[3]

    jar = "#{artifact}-#{version}.#{type}"

    maven_path = "#{local_repo}#{File::SEPARATOR}#{group}#{File::SEPARATOR}#{artifact}#{File::SEPARATOR}#{version}#{File::SEPARATOR}#{jar}"

    if File.exists? maven_path
      deps[:exists] << { dep => maven_path }
    else
      deps[:missing] << dep
    end
  end
  deps
end

.default_local_repoString

Default local repo of ENV or ~/.m2/repository

Returns:

  • (String)


24
25
26
# File 'lib/naether/bootstrap.rb', line 24

def default_local_repo
  ENV["M2_REPO"] || File.expand_path("~/.m2/repository")
end

.dependencies(dep_file = nil) ⇒ List

List of Java dependencies for Naether from yaml dependency file. Caches result after first run.

Parameters:

  • dep_file (String) (defaults to: nil)

    path, defaults to Naether::Configuration.dependencies_yml

Returns:

  • (List)


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/naether/bootstrap.rb', line 55

def dependencies(dep_file = nil)
  if @@dependencies
    return @@dependencies
  end

  if dep_file.nil?
    dep_file = Naether::Configuration.dependencies_yml
  end

  dep = YAML.load_file(dep_file)
  @@dependencies = dep[:dependencies]
end

.download_dependencies(dest, opts = {}) ⇒ Hash

Download Naether dependencies

Parameters:

  • dest (String)

    to download dependencies t

  • opts (Hash) (defaults to: {})

Returns:

  • (Hash)

    with status of missing, downloaded, exists dependencies



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
# File 'lib/naether/bootstrap.rb', line 97

def download_dependencies(dest, opts = {})
  if !File.exists? dest
    FileUtils.mkdir_p(dest)
  end

  deps = {}

  if opts[:deps]
    deps[:missing] = opts[:deps]
  else
    deps = check_local_repo_for_deps(opts[:local_repo], opts)
  end

  deps[:downloaded] = []

  if deps[:missing].size > 0
    http_client = HTTPClient.new

    deps[:missing].each do |dep|
      notation = dep.split(":")
      group = notation[0].gsub("\.", File::SEPARATOR)
      artifact = notation[1]
      type = notation[2]
      version = notation[3]

      jar = "#{artifact}-#{version}.#{type}"

      maven_path = "#{dest}#{File::SEPARATOR}#{jar}"

      if !File.exists? maven_path
        maven_jar_url = "#{maven_url}/#{group}/#{artifact}/#{version}/#{jar}"

        data = http_client.get(maven_jar_url)

        if data.ok?
          open(maven_path, "wb") do |io|
            io.print data.content
          end
        else
          raise "Failed to download #{maven_jar_url}"
        end

        deps[:downloaded] << { dep => maven_path }
      else
        deps[:downloaded] << { dep => maven_path }
      end
    end
  end

  deps
end

.install_dependencies_to_local_repo(install_jars, naether_jars, opts = {}) ⇒ Naether

Install Naether Dependencies to local_repo

Parameters:

  • install_jars (Array<String>)
  • naether_jars (Array<String>)

    to bootstrap Naether. These may overlap with install_jars.

  • opts (Hash) (defaults to: {})

Returns:



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/naether/bootstrap.rb', line 191

def install_dependencies_to_local_repo(install_jars, naether_jars, opts = {})
  require "#{File.dirname(__FILE__)}/../naether"

  @naether = Naether.create_from_jars(naether_jars)

  if opts[:local_repo]
    @naether.local_repo_path = opts[:local_repo]
  end

  install_jars.each do |dep|
    notation, path = dep.to_a.first
    @naether.install(notation, nil, path)
  end

  @naether
end

.maven_urlString

Maven URL to fetch jars from, ENV or

Returns:

  • (String)


31
32
33
# File 'lib/naether/bootstrap.rb', line 31

def maven_url
  ENV["NAETHER_MIRROR"] || "https://repo1.maven.org/maven2"
end

.write_dependencies(dest = "jar_dependencies.yml") ⇒ Object

Write bootstrap dependencies to yaml file



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/naether/bootstrap.rb', line 36

def write_dependencies(dest = "jar_dependencies.yml")
  deps = {}
  if Naether::Configuration.platform == "java"
    deps[:dependencies] = com.tobedevoured.naether.Bootstrap.dependencies.to_a
  else
    bootstrap = Rjb::import("com.tobedevoured.naether.Bootstrap")
    deps[:dependencies] = bootstrap.dependencies.toArray().map { |dep| dep.toString() }
  end

  File.open(dest, "w") do |out|
    YAML.dump(deps, out)
  end
end