Permalink
Browse files

improve building the standalone script

The source files are now concatenated in order in which they are
required in lib/hub.rb
  • Loading branch information...
1 parent e00ffb8 commit 0a0757f2a814ae23f067418bfa40e8e065fd56cc @mislav mislav committed Apr 16, 2012
Showing with 44 additions and 38 deletions.
  1. +1 −1 Rakefile
  2. +4 −2 lib/hub/commands.rb
  3. +30 −29 lib/hub/standalone.rb
  4. +0 −1 test/helper.rb
  5. +1 −2 test/hub_test.rb
  6. +8 −3 test/standalone_test.rb
View
@@ -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)
View
@@ -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)
View
@@ -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
View
@@ -1,6 +1,5 @@
require 'test/unit'
require 'hub'
-require 'hub/standalone'
# We're checking for `open` in our tests
ENV['BROWSER'] = 'open'
View
@@ -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
@@ -1,5 +1,7 @@
require 'helper'
+require 'hub/standalone'
require 'fileutils'
+require 'stringio'
class StandaloneTest < Test::Unit::TestCase
include FileUtils
@@ -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
@@ -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

0 comments on commit 0a0757f

Please sign in to comment.