Skip to content

Commit

Permalink
improve building the standalone script
Browse files Browse the repository at this point in the history
The source files are now concatenated in order in which they are
required in lib/hub.rb
  • Loading branch information
mislav committed Apr 17, 2012
1 parent e00ffb8 commit 0a0757f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ end
# Build
#

file "hub" => FileList.new("lib/hub/*.rb", "man/hub.1") do |task|
file "hub" => FileList.new("lib/hub.rb", "lib/hub/*.rb", "man/hub.1") do |task|
Rake::Task[:load_path].invoke
require 'hub/standalone'
Hub::Standalone.save(task.name)
Expand Down
6 changes: 4 additions & 2 deletions lib/hub/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,12 @@ def compare(args)
def hub(args)
return help(args) unless args[1] == 'standalone'
require 'hub/standalone'
$stdout.puts Hub::Standalone.build
Hub::Standalone.build $stdout
exit
rescue LoadError
abort "hub is running in standalone mode."
abort "hub is already running in standalone mode."
rescue Errno::EPIPE
exit # ignore broken pipe
end

def alias(args)
Expand Down
59 changes: 30 additions & 29 deletions lib/hub/standalone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,58 @@ module Hub
module Standalone
extend self

RUBY_BIN = if File.executable? '/usr/bin/ruby' then '/usr/bin/ruby'
else
require 'rbconfig'
File.join RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name']
end
HUB_ROOT = File.expand_path('../../..', __FILE__)

PREAMBLE = <<-preamble
#!#{RUBY_BIN}
#
# This file, hub, is generated code.
# Please DO NOT EDIT or send patches for it.
# This file is generated code. DO NOT send patches for it.
#
# Please take a look at the source from
# Original source files with comments are at:
# https://github.com/defunkt/hub
# and submit patches against the individual files
# that build hub.
#
preamble

POSTAMBLE = "Hub::Runner.execute(*ARGV)\n"
__DIR__ = File.dirname(__FILE__)
MANPAGE = "__END__\n#{File.read(__DIR__ + '/../../man/hub.1')}"

def save(filename, path = '.')
target = File.join(File.expand_path(path), filename)
File.open(target, 'w') do |f|
f.puts build
build f
f.chmod 0755
end
end

def build
root = File.dirname(__FILE__)
def build io
io.puts "#!#{ruby_executable}"
io << PREAMBLE

standalone = ''
standalone << PREAMBLE
each_source_file do |filename|
File.open(filename, 'r') do |source|
source.each_line {|line| io << line if line !~ /^\s*#/ }
end
io.puts ''
end

files = Dir["#{root}/*.rb"].sort - [__FILE__]
# ensure context.rb appears before others
ctx = files.find {|f| f['context.rb'] } and files.unshift(files.delete(ctx))
io.puts "Hub::Runner.execute(*ARGV)"
io.puts "\n__END__"
io << File.read(File.join(HUB_ROOT, 'man/hub.1'))
end

files.each do |file|
File.readlines(file).each do |line|
standalone << line if line !~ /^\s*#/
def each_source_file
File.open(File.join(HUB_ROOT, 'lib/hub.rb'), 'r') do |main|
main.each_line do |req|
if req =~ /^require\s+["'](.+)["']/
yield File.join(HUB_ROOT, 'lib', "#{$1}.rb")
end
end
end
end

standalone << POSTAMBLE
standalone << MANPAGE
standalone
def ruby_executable
if File.executable? '/usr/bin/ruby' then '/usr/bin/ruby'
else
require 'rbconfig'
File.join RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name']
end
end
end
end
1 change: 0 additions & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'test/unit'
require 'hub'
require 'hub/standalone'

# We're checking for `open` in our tests
ENV['BROWSER'] = 'open'
Expand Down
3 changes: 1 addition & 2 deletions test/hub_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1086,8 +1086,7 @@ def test_help_hub_no_groff
end

def test_hub_standalone
help_standalone = hub("hub standalone")
assert_equal Hub::Standalone.build, help_standalone
assert_includes 'This file is generated code', hub("hub standalone")
end

def test_hub_compare
Expand Down
11 changes: 8 additions & 3 deletions test/standalone_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'helper'
require 'hub/standalone'
require 'fileutils'
require 'stringio'

class StandaloneTest < Test::Unit::TestCase
include FileUtils
Expand All @@ -17,8 +19,11 @@ def teardown
end

def test_standalone
standalone = Hub::Standalone.build
assert_includes "This file, hub, is generated code", standalone
io = StringIO.new
Hub::Standalone.build io
standalone = io.string

assert_includes "This file is generated code", standalone
assert_includes "Runner", standalone
assert_includes "Args", standalone
assert_includes "Commands", standalone
Expand All @@ -31,7 +36,7 @@ def test_standalone

def test_standalone_save
Hub::Standalone.save("hub")
assert_equal Hub::Standalone.build, File.read('./hub')
assert File.size('./hub') > 100
end

def test_standalone_save_permission_denied
Expand Down

0 comments on commit 0a0757f

Please sign in to comment.