Module: Gemjam

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

Constant Summary collapse

VERSION =
"0.0.6"

Instance Method Summary collapse

Instance Method Details

#bundle_install(jruby, quiet = false, opts = {}) ⇒ Object

install the bundle, using jruby command jruby

sets $? with that commands return value



96
97
98
99
# File 'lib/gemjam.rb', line 96

def bundle_install(jruby, quiet = false, opts = {})
  ex_opts = opts.map {|k,v| [k,v] }.join(" ")
  cmd("#{jruby} -S bundle install --path ./vendor/bundle/ #{ex_opts}", quiet)
end

#bundler_vendor_dirObject



101
102
103
104
105
106
# File 'lib/gemjam.rb', line 101

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



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gemjam.rb', line 60

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



77
78
79
80
81
82
83
84
# File 'lib/gemjam.rb', line 77

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



156
157
158
159
160
161
# File 'lib/gemjam.rb', line 156

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

  run(o)
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



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

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
46
47
48
49
50
51
52
53
54
55
56
57
# 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("-o", "--output FILENAME", "output to jar FILENAME") do |o|
      options[:output] = o
    end
    opts.on("-c", "--cache", "preserve the cach/*.gem files in the jar") do |o|
      options[:cache] = o
    end
    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
    opts.on("--with WITH", "bundle install --with 'foo bar' (if needed)") do |o|
      options[:bundle_with] = o
    end
    opts.on("--without WITHOUT", "bundle install --without 'foo bar' (if needed)") do |o|
      options[:bundle_without] = o
    end
  end.parse!(args)
  return options
end

#run(opts) ⇒ Object



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
148
149
150
151
152
153
154
# File 'lib/gemjam.rb', line 108

def run(opts)
  tmpdir = Dir.mktmpdir
  begin
    cwd = Dir.pwd
    if opts[:bundle]

      b_opts = {}
      b_opts["--with"] = opts[:bundle_with] if opts[:bundle_with]
      b_opts["--without"] = opts[:bundle_without] if opts[:bundle_without]

      FileUtils.cd tmpdir
      FileUtils.cp opts[:bundle], "Gemfile"
      if FileTest.file?("#{opts[:bundle]}.lock")
        FileUtils.cp "#{opts[:bundle]}.lock", "Gemfile.lock"
        b_opts["--deployment"] = ""
        bundle_install(opts[:jruby], opts[:quiet], b_opts)
      else
        bundle_install(opts[:jruby], opts[:quiet], b_opts)
      end
      FileUtils.cd cwd
      abort("FAIL: bundler returned: #{$?}") if $? != 0
    end

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

    # the ./cache/*.gems just duplicates the size of this jar
    unless opts[:cache]
      FileUtils.remove_entry_secure(File.join(tmpdir, bundler_vendor_dir, "cache"), true)
    end

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

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