Class: Naether::Bootstrap

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

Overview

:title:Naether::Bootstrap

Helper for bootstrapping Naether

Authors

Michael Guymon

Constant Summary collapse

@@dependencies =
nil

Class Method Summary collapse

Class Method Details

.check_local_repo_for_deps(local_repo = nil) ⇒ Object



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

def check_local_repo_for_deps(local_repo = nil)
  
  local_repo = local_repo || ENV['M2_HOME'] || '~/.m2/repository'
  local_repo = File.expand_path(local_repo)
  
  puts "Checking #{local_repo} for jars to bootstrap Naether"
  
  deps = {:exists => [], :missing => [] }
  
  # /home/zinger/.m2/repository/com/jcraft/jsch/0.1.44-1/jsch-0.1.44-1.jar
  dependencies.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

.dependencies(dep_file = nil) ⇒ Object

List of Java dependencies for Naether



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/naether/bootstrap.rb', line 42

def dependencies( dep_file=nil )
  
  if @@dependencies
    return @@dependencies
  end
  
  if dep_file.nil?
    dep_file = "#{File.dirname( __FILE__ )}/../../jar_dependencies.yml"
  end
  
  dep = YAML.load_file( dep_file )  
  @@dependencies = dep[:dependencies]
end

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



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

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] )
  end
  
  deps[:downloaded] = []
    
  if deps[:missing].size > 0
    puts "Downloading jars for Naether"  
      
    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_mirror = "http://repo1.maven.org/maven2/#{group}/#{artifact}/#{version}/#{jar}"
        
        open(maven_path, 'wb') do |io|
          io.print open(maven_mirror).read
        end
        
        deps[:downloaded] << { dep => maven_path }
      else
        deps[:exists] << { dep => maven_path }
      end
    end
  end
  
  deps
end

.install_dependencies_to_local_repo(jars_or_dir, opts = {}) ⇒ Object



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

def install_dependencies_to_local_repo( jars_or_dir, opts = {}  )
    
  @naether = nil
  jars = []  
  unless jars_or_dir.is_a? Array
    @naether = Naether.create_from_paths( jars_or_dir, opts[:naether_jar_dir] )
    jars = Dir.glob( "#{jars_or_dir}#{File::SEPARATOR}*.jar" )
  else
    @naether = Naether.create_from_jars( jars_or_dir )
    jars = jars_or_dir
  end
  
  if opts[:local_repo]
    @naether.local_repo_path = opts[:local_repo]
  end
  
  dependencies.each do |dep|
    notation = dep.split(":")
    group = notation[0].gsub("\.", File::SEPARATOR)
    artifact = notation[1].gsub("\.", File::SEPARATOR)
    type = notation[2]
    version = notation[3]
    
    name = "#{artifact}-#{version}.#{type}"
    
    jar = jars.select { |x| x =~ /#{name}/ }
    if jar.size > 0
      jar = jar[0]
      @naether.install( dep, nil, jar )
    else
      puts "Could not find jar for #{dep}"
    end
    
  end
  
end

.naether_jarObject

Find naether jar relative to the gem install



21
22
23
# File 'lib/naether/bootstrap.rb', line 21

def naether_jar
  Dir.glob(File.expand_path("#{File.dirname(__FILE__)}/../../naether*.jar")).first
end

.write_dependencies(jar_path = nil, dest = 'jar_dependencies.yml') ⇒ Object

write bootstrap dependencies to yaml file



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/naether/bootstrap.rb', line 26

def write_dependencies( jar_path = nil, dest = 'jar_dependencies.yml' )
  Naether::Java.load_jars_dir( jar_path || Naether::JAR_LIB )
  deps = {};
  if Naether.platform == 'java'
    deps[:dependencies] = com.slackworks.naether.Bootstrap.dependencies.to_a
  else
    bootstrap = Rjb::import('com.slackworks.naether.Bootstrap')
    deps[:dependencies] = bootstrap.dependencies.toArray().map{ |dep| dep.toString() }
  end  
  
  File.open( dest, 'w' ) do |out|
    YAML.dump( deps, out )
  end
end