2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/embulk/command/embulk_run.rb', line 2
def self.run(argv)
default_bundle_path = nil
gemfile_path = ENV['BUNDLE_GEMFILE'].to_s
gemfile_path = nil if gemfile_path.empty?
default_bundle_path = File.dirname(gemfile_path) if gemfile_path
gem_home = ENV['GEM_HOME'].to_s
if gem_home.empty?
ENV['GEM_HOME'] = File.expand_path File.join(ENV['HOME'], '.embulk', Gem.ruby_engine, RbConfig::CONFIG['ruby_version'])
Gem.clear_paths
end
i = argv.find_index {|arg| arg !~ /^\-/ }
usage nil unless i
subcmd = argv.slice!(i)
$LOAD_PATH << Embulk.home('lib')
require 'java'
require 'optparse'
op = OptionParser.new
load_paths = []
classpaths = []
classpath_separator = java.io.File.pathSeparator
options = {}
op.on('-b', '--bundle BUNDLE_DIR', 'Path to a Gemfile directory') do |path|
end
case subcmd.to_sym
when :bundle
if default_bundle_path
op.banner = "Usage: bundle [directory=#{default_bundle_path}]"
args = 0..1
else
op.banner = "Usage: bundle <directory>"
args = 1..1
end
when :run
op.banner = "Usage: run <config.yml>"
op.on('-b', '--bundle BUNDLE_DIR', 'Path to a Gemfile directory') do |path|
end
op.on('-I', '--load-path PATH', 'Add ruby script directory path ($LOAD_PATH)') do |load_path|
load_paths << load_path
end
op.on('-C', '--classpath PATH', "Add java classpath separated by #{classpath_separator} (CLASSPATH)") do |classpath|
classpaths.concat classpath.split(classpath_separator)
end
op.on('-o', '--output PATH', 'Path to a file to write the next configuration') do |path|
options[:nextConfigOutputPath] = path
end
op.on('-r', '--resume-state PATH', 'Path to a file to write or read resume state') do |path|
options[:resumeStatePath] = path
end
args = 1..1
when :cleanup
op.banner = "Usage: run <config.yml>"
op.on('-b', '--bundle BUNDLE_DIR', 'Path to a Gemfile directory') do |path|
end
op.on('-I', '--load-path PATH', 'Add ruby script directory path ($LOAD_PATH)') do |load_path|
load_paths << load_path
end
op.on('-C', '--classpath PATH', "Add java classpath separated by #{classpath_separator} (CLASSPATH)") do |classpath|
classpaths.concat classpath.split(classpath_separator)
end
op.on('-r', '--resume-state PATH', 'Path to a file to write or read resume state') do |path|
options[:resumeStatePath] = path
end
args = 1..1
when :preview
op.banner = "Usage: preview <config.yml>"
op.on('-b', '--bundle BUNDLE_DIR', 'Path to a Gemfile directory') do |path|
end
op.on('-I', '--load-path PATH', 'Add ruby script directory path ($LOAD_PATH)') do |load_path|
load_paths << load_path
end
op.on('-C', '--classpath PATH', "Add java classpath separated by #{classpath_separator} (CLASSPATH)") do |classpath|
classpaths.concat classpath.split(classpath_separator)
end
args = 1..1
when :guess
op.banner = "Usage: guess <partial-config.yml>"
op.on('-o', '--output PATH', 'Path to a file to write the guessed configuration') do |path|
options[:nextConfigOutputPath] = path
end
op.on('-I', '--load-path PATH', 'Add ruby script directory path ($LOAD_PATH)') do |load_path|
load_paths << load_path
end
op.on('-C', '--classpath PATH', "Add java classpath separated by #{classpath_separator} (CLASSPATH)") do |classpath|
classpaths.concat classpath.split(classpath_separator)
end
args = 1..1
when :gem
require 'rubygems/gem_runner'
Gem::GemRunner.new.run argv
exit 0
when :example
args = 0..1
when :exec
exec *argv
exit 127
else
usage "Unknown subcommand #{subcmd.dump}."
end
begin
op.parse!(argv)
unless args.include?(argv.length)
usage nil
end
rescue => e
usage e.to_s
end
case subcmd.to_sym
when :bundle
path = argv[0] || default_bundle_path
require 'fileutils'
require 'rubygems/gem_runner'
setup_load_paths(load_paths)
setup_classpaths(classpaths)
unless File.exists?(path)
puts "Initializing #{path}..."
FileUtils.mkdir_p File.dirname(path)
begin
success = false
if __FILE__ =~ /^classpath:/ || __FILE__.include?('!/')
resource_class = org.embulk.command.Runner.java_class
%w[.bundle/config embulk/input_example.rb embulk/filter_example.rb embulk/output_example.rb Gemfile Gemfile.lock].each do |file|
url = resource_class.resource("/embulk/data/bundle/#{file}").to_s
dst = File.join(path, file)
FileUtils.mkdir_p File.dirname(dst)
FileUtils.cp(url, dst)
end
else
tmpl = File.join(Embulk.home('lib'), 'embulk', 'data', 'bundle')
FileUtils.cp_r tmpl, path
end
setup_gem_paths(path)
Gem::GemRunner.new.run %w[install bundler]
success = true
rescue Gem::SystemExitException => e
raise e if e.exit_code != 0
success = true
ensure
FileUtils.rm_rf path unless success
end
else
setup_gem_paths(path)
end
ENV['BUNDLE_GEMFILE'] = File.expand_path File.join(path, "Gemfile")
Dir.chdir(path) do
require 'bundler'
require 'bundler/friendly_errors'
require 'bundler/cli'
Bundler.with_friendly_errors do
Bundler::CLI.start(%w[install], debug: true)
end
end
when :example
require_relative 'embulk_example'
path = ARGV[0] || "embulk-example"
puts "Creating #{path} directory..."
Embulk.create_example(path)
puts ""
puts "Run following subcommands to try embulk:"
puts ""
puts " 1. guess #{File.join(path, 'example.yml')} -o config.yml"
puts " 2. preview config.yml"
puts " 3. run config.yml"
puts ""
else
require 'json'
begin
java.lang.Class.forName('org.embulk.command.Runner')
rescue java.lang.ClassNotFoundException
classpath_dir = Embulk.home('classpath')
jars = Dir.entries(classpath_dir).select {|f| f =~ /\.jar$/ }.sort
jars.each do |jar|
require File.join(classpath_dir, jar)
end
end
setup_load_paths(load_paths)
setup_classpaths(classpaths)
org.embulk.command.Runner.new(options.to_json).main(subcmd, argv.to_java(:string))
end
end
|