Class: Jars::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/jars/installer.rb

Defined Under Namespace

Classes: Dependency

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec = nil) ⇒ Installer

Returns a new instance of Installer.



120
121
122
# File 'lib/jars/installer.rb', line 120

def initialize( spec = nil )
  @mvn = MavenExec.new( spec )
end

Class Method Details

.install_deps(deps, dir, require_filename, vendor) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/jars/installer.rb', line 110

def self.install_deps( deps, dir, require_filename, vendor )
  f = write_require_file( require_filename ) if require_filename
  deps.each do |dep|
    write_dep( f, dir, dep, vendor )
  end
  yield f if block_given? and f
ensure
  f.close if f
end

.install_jars(write_require_file = false) ⇒ Object



65
66
67
# File 'lib/jars/installer.rb', line 65

def self.install_jars( write_require_file = false )
  new.install_jars( write_require_file )
end

.load_from_maven(file) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/jars/installer.rb', line 73

def self.load_from_maven( file )
  result = []
  File.read( file ).each_line do |line|
    dep = Dependency.new( line )
    result << dep if dep && dep.scope == :runtime
  end
  result
end

.vendor_file(dir, dep) ⇒ Object



94
95
96
97
98
# File 'lib/jars/installer.rb', line 94

def self.vendor_file( dir, dep )
  vendored = File.join( dir, dep.path )
  FileUtils.mkdir_p( File.dirname( vendored ) )
  FileUtils.cp( dep.file, vendored ) unless dep.system?
end

.vendor_jars(write_require_file = false) ⇒ Object



69
70
71
# File 'lib/jars/installer.rb', line 69

def self.vendor_jars( write_require_file = false )
  new.vendor_jars( write_require_file )
end

.write_dep(file, dir, dep, vendor) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/jars/installer.rb', line 100

def self.write_dep( file, dir, dep, vendor )
  return if dep.type != :jar || dep.scope != :runtime
  if dep.system?
    file.puts( "require( '#{dep.file}' )" ) if file
  elsif dep.scope == :runtime
    vendor_file( dir, dep ) if vendor
    file.puts( "require_jar( '#{dep.gav.gsub( ':', "', '" )}' )" ) if file
  end
end

.write_require_file(require_filename) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/jars/installer.rb', line 82

def self.write_require_file( require_filename )
  FileUtils.mkdir_p( File.dirname( require_filename ) )
  comment = '# this is a generated file, to avoid over-writing it just delete this comment'
  if ! File.exists?( require_filename ) || File.read( require_filename ).match( comment )
    f = File.open( require_filename, 'w' )
    f.puts comment
    f.puts "require 'jar_dependencies'"
    f.puts
    f
  end
end

Instance Method Details

#has_jars?Boolean Also known as: jars?

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/jars/installer.rb', line 150

def has_jars?
  # first look if there are any requirements in the spec
  # and then if gem depends on jar-dependencies for runtime.
  # only then install the jars declared in the requirements
  result = spec && ! spec.requirements.empty? &&
           spec.dependencies.detect { |d| d.name == 'jar-dependencies' && d.type == :runtime } != nil
  if result && spec.platform.to_s != 'java'
    warn "\njar dependencies found on non-java platform gem - do not install jars\n"
    false
  else
    result
  end
end

#install_jars(write_require_file = true) ⇒ Object



141
142
143
144
# File 'lib/jars/installer.rb', line 141

def install_jars( write_require_file = true )
  return unless has_jars?
  do_install( false, write_require_file )
end

#ruby_maven_install_options=(options) ⇒ Object



146
147
148
# File 'lib/jars/installer.rb', line 146

def ruby_maven_install_options=( options )
  @mvn.ruby_maven_install_options=( options )
end

#specObject



124
# File 'lib/jars/installer.rb', line 124

def spec; @mvn.spec end

#vendor_jars(write_require_file = true) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/jars/installer.rb', line 126

def vendor_jars( write_require_file = true )
  return unless has_jars?
  case Jars.to_prop( Jars::VENDOR )
  when 'true'
    do_vendor = true
  when 'false'
    do_vendor = false
  else
    # if the spec_file does not exists this means it is a local gem
    # coming via bundle :path or :git
    do_vendor = File.exists?( spec.spec_file )
  end
  do_install( do_vendor, write_require_file )
end