Module: Gemjam

Defined in:
lib/gemjam.rb,
lib/gemjam/version.rb

Constant Summary collapse

VERSION =
"0.0.4"

Instance Method Summary collapse

Instance Method Details

#bundle_install(jruby, quiet = false) ⇒ Object

install the bundle, using jruby command jruby

sets $? with that commands return value



84
85
86
# File 'lib/gemjam.rb', line 84

def bundle_install(jruby, quiet = false)
  cmd("#{jruby} -S bundle install --path ./vendor/bundle/", quiet)
end

#bundler_vendor_dirObject



88
89
90
91
92
93
# File 'lib/gemjam.rb', line 88

def bundler_vendor_dir
  return ["vendor","bundle",
          RbConfig::CONFIG["ruby_install_name"],
          RbConfig::CONFIG["ruby_version"]].join("/")

end

#cmd(cmd_str, quiet = false) ⇒ Object

runs cmd, and sets $? with that commands return value



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gemjam.rb', line 48

def cmd(cmd_str, quiet = false)
  p cmd_str unless quiet
  IO.popen(cmd_str) do |f|
    loop do
      buf = f.read(1)
      break if buf.nil?
      unless quiet
        print buf
        $stdout.flush
      end
    end
  end
end

#gem_install(jruby, basedir, gemname, quiet = false) ⇒ Object

install rubygem gemname to directory basedir using jruby command jruby

sets $? with that commands return value



65
66
67
68
69
70
71
72
# File 'lib/gemjam.rb', line 65

def gem_install(jruby, basedir, gemname, quiet = false)
  if gemname.include?(",")
    g, v = gemname.split(",",2)
    cmd("#{jruby} -S gem install -i #{basedir} #{g} -v=#{v}", quiet)
  else
    cmd("#{jruby} -S gem install -i #{basedir} #{gemname}", quiet)
  end
end

#main(args) ⇒ Object



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
# File 'lib/gemjam.rb', line 95

def main(args)
  o = parse_args(args)
  p o unless o[:quiet]

  tmpdir = Dir.mktmpdir
  begin
    cwd = Dir.pwd
    if o[:bundle]
      FileUtils.cd tmpdir
      FileUtils.cp o[:bundle], "Gemfile"
      bundle_install(o[:jruby], o[:quiet])
      FileUtils.cd cwd
      abort("FAIL: bundler returned: #{$?}") if $? != 0
    end

    o[:gems].each do |gem|
      gem_install(o[:jruby], File.join(tmpdir, bundler_vendor_dir), gem, o[:quiet])
      abort("FAIL: gem install returned: #{$?}") if $? != 0
    end

    jarname = File.basename(tmpdir) + ".jar"
    make_jar(jarname, File.join(tmpdir, bundler_vendor_dir), o[:quiet])
    abort("FAIL: jar packaging returned: #{$?}") if $? != 0

    if o[:quiet]
      puts jarname
    else
      puts "Created #{jarname}"
    end
  ensure
    # remove the directory.
    FileUtils.remove_entry_secure(tmpdir, true)
  end
end

#make_jar(jarname, dirname, quiet = false) ⇒ Object

pack up the installed gems in dirname, to jar file jarname

sets $? with that commands return value



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

def make_jar(jarname, dirname, quiet = false)
  cmd("jar cf #{jarname} -C #{dirname} .", quiet)
end

#parse_args(args) ⇒ Object



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
# File 'lib/gemjam.rb', line 16

def parse_args(args)
  options = {
    :quiet => false,
    :jruby => "jruby",
    :gems  => [],
  }
  opts = OptionParser.new do |opts|
    opts.banner = File.basename(__FILE__) + "[-b [Gemfile]] [-g gem[,version]]..."
    opts.on("-q", "--quiet", "less output") do |o|
      options[:quiet] = o
    end
    opts.on("-j", "--jruby CMD", "CMD to use to call jruby (Default '#{options[:jruby]}')") do |o|
      options[:jruby] = o
    end
    opts.on("-g", "--gem GEMNAME", "GEMNAME to install. If ',<version>' is a append, it will specify that version of the gem") do |o|
      options[:gems] << o
    end
    opts.on("-b", "--bundle [GEMFILE]", "make the gemjar from a current directory Gemfile or specified") do |o|
      if o.nil? and ! FileTest.file?("Gemfile")
        raise "No Gemfile present or provided"
      end
      options[:bundle] = if o.nil?
                           File.join(Dir.pwd, "Gemfile")
                         else
                           File.expand_path(o)
                         end
    end
  end.parse!(args)
  return options
end