Module: Gemjam

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

Constant Summary collapse

VERSION =
"0.0.8"

Class Method Summary collapse

Class Method Details

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

install the bundle, using jruby command jruby

sets $? with that commands return value



104
105
106
107
# File 'lib/gemjam.rb', line 104

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_dir(jruby) ⇒ Object



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

def bundler_vendor_dir(jruby)
  return @bundler_vendor_dir if @bundler_vendor_dir
  sio = StringIO.new
  cmd("#{jruby} -e \"print ['vendor/bundle',RbConfig::CONFIG['ruby_install_name'], RbConfig::CONFIG['ruby_version'] ].join('/')\"",
      false,
      sio)
  sio.seek(0)
  @bundler_vendor_dir = sio.read()
end

.cmd(cmd_str, quiet = false, io = $stdout) ⇒ Object

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



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gemjam.rb', line 65

def cmd(cmd_str, quiet = false, io = $stdout)
  p cmd_str unless quiet
  IO.popen(cmd_str) do |f|
    loop do
      buf = f.read(1)
      break if buf.nil?
      unless quiet
        io.print buf
        io.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



83
84
85
86
87
88
89
90
# File 'lib/gemjam.rb', line 83

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



184
185
186
187
188
189
# File 'lib/gemjam.rb', line 184

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



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

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

.parse_args(args) ⇒ Object



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
58
59
60
61
# File 'lib/gemjam.rb', line 17

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("--keep", "preserve the temp working directory (for debugging)") do |o|
      options[:keep] = 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

run(parse_args(“-q”,“-o”,jarname,“-b”,“Gemfile”))



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
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/gemjam.rb', line 122

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]

      begin
        FileUtils.cd tmpdir, :verbose => !opts[:quiet]
        FileUtils.cp opts[:bundle], "Gemfile"

        # If there is a bundler lockfile, then use it too
        if FileTest.file?("#{opts[:bundle]}.lock")
          FileUtils.cp "#{opts[:bundle]}.lock", "Gemfile.lock"

          # ensure these timestamps match the original
          File.utime(File.atime(opts[:bundle]),
                     File.mtime(opts[:bundle]),
                     "Gemfile")
          File.utime(File.atime("#{opts[:bundle]}.lock"),
                     File.mtime("#{opts[:bundle]}.lock"),
                     "Gemfile.lock")

          b_opts["--deployment"] = ""
        end

        bundle_install(opts[:jruby], opts[:quiet], b_opts)
        abort("FAIL: bundler returned: #{$?}") if $? != 0
      ensure
        FileUtils.cd cwd, :verbose => !opts[:quiet]
      end
    end

    opts[:gems].each do |gem|
      gem_install(opts[:jruby], File.join(tmpdir, bundler_vendor_dir(opts[:jruby])), 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(opts[:jruby]), "cache"), true)
    end

    jarname = opts[:output] ? opts[:output] : File.basename(tmpdir) + ".jar"
    make_jar(jarname, File.join(tmpdir, bundler_vendor_dir(opts[:jruby])), 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) unless opts[:keep]
  end
end