~singpolyma/rld

9ec604441f6f5980eb906df22f13898b6f53b8b1 — Stephen Paul Weber 7 months ago 1d1cbfb
Support linking in-place
2 files changed, 14 insertions(+), 3 deletions(-)

M README
M bin/rld
M README => README +1 -0
@@ 3,4 3,5 @@ Usage: rld [options] file.rb ...
    -L, -I PATH                      Search directory
    -o FILE                          Output file
    -l, -r FILE                      Require this file in output
    -i                               Write output in-place
    -w                               Only warn on LoadError

M bin/rld => bin/rld +13 -3
@@ 59,13 59,17 @@ op = OptionParser.new do |opts|
	end

	opts.on("-o FILE", Pathname, "Output file") do |v|
		$options[:o] = v
		$options[:o] = v.open("w+")
	end

	opts.on("-l FILE", "-r FILE", Pathname, "Require this file in output") do |v|
		$options[:l] << "require #{require(v).inspect}"
	end

	opts.on("-i", "Write output in-place") do
		$options[:i] = true
	end

	opts.on("-w", "Only warn on LoadError") do
		$options[:w] = true
	end


@@ 114,10 118,16 @@ end
$options[:p] ||= Pathname.new(ARGV[0]).dirname.realdirpath
$LOAD_PATH.unshift($options[:p])

$options[:l].each(&$options[:o].method(:puts))
$options[:l].each(&$options[:o].method(:puts)) unless $options[:i]

ARGV.each do |file|
	buffer = Parser::Source::Buffer.new(file, source: Pathname.new(file).read)
	file = Pathname.new(file)
	source = file.read
	if $options[:i]
		$options[:o] = file.open("w+")
		$options[:l].each(&$options[:o].method(:puts))
	end
	buffer = Parser::Source::Buffer.new(file, source: source)
	ast = Parser::CurrentRuby.new.parse(buffer)
	$options[:o].write Linker.new.rewrite(buffer, ast)
end