Skip to content

Commit d61ad70

Browse files
committed
Rely on well-known /usr/bin/env to find Ruby
This is pretty darn normalized on *nix systems these days, where this will be installed via Homebrew. Let's rely on that rather than hard-coding to a version of Ruby that can/will likely change over time. Also, use the Regex#match? and avoid extra object; we don't need/use the MatchData, so don't create it.
1 parent 6257144 commit d61ad70

File tree

2 files changed

+19
-47
lines changed

2 files changed

+19
-47
lines changed

lib/git_tracker/standalone.rb

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,32 @@ module Standalone
1010
# Original source files with comments are at:
1111
# https://github.com/stevenharman/git_tracker
1212
#
13-
1413
DOC
1514

16-
def save(filename, path = ".")
17-
dest = File.join(File.expand_path(path), filename)
18-
File.open(dest, "w") do |f|
19-
build(f)
20-
f.chmod(0o755)
21-
end
22-
end
23-
2415
def build(io)
25-
io.puts "#!#{ruby_executable}"
26-
io << PREAMBLE
16+
io.puts("#!/usr/bin/env ruby")
17+
io.puts(PREAMBLE)
2718

2819
each_source_file do |filename|
2920
File.open(filename, "r") do |source|
3021
inline_source(source, io)
3122
end
3223
end
3324

34-
io.puts "GitTracker::Runner.execute(*ARGV)"
25+
io.puts("GitTracker::Runner.execute(*ARGV)")
3526
io
3627
end
3728

38-
def ruby_executable
39-
if File.executable? "/usr/bin/ruby" then "/usr/bin/ruby"
40-
else
41-
require "rbconfig"
42-
File.join(RbConfig::CONFIG["bindir"], RbConfig::CONFIG["ruby_install_name"])
29+
def save(filename, path = ".")
30+
dest = File.join(File.expand_path(path), filename)
31+
File.open(dest, "w") do |f|
32+
build(f)
33+
f.chmod(0o755)
4334
end
4435
end
4536

4637
private
4738

48-
def inline_source(code, io)
49-
code.each_line do |line|
50-
io << line unless require_own_file?(line)
51-
end
52-
io.puts ""
53-
end
54-
55-
def require_own_file?(line)
56-
line =~ /^\s*require\s+["']git_tracker\//
57-
end
58-
5939
def each_source_file
6040
File.open(File.join(GIT_TRACKER_ROOT, "lib/git_tracker.rb"), "r") do |main|
6141
main.each_line do |req|
@@ -65,5 +45,16 @@ def each_source_file
6545
end
6646
end
6747
end
48+
49+
def inline_source(code, io)
50+
code.each_line do |line|
51+
io << line unless require_own_file?(line)
52+
end
53+
io.puts("")
54+
end
55+
56+
def require_own_file?(line)
57+
/^\s*require\s+["']git_tracker\//.match?(line)
58+
end
6859
end
6960
end

spec/git_tracker/standalone_spec.rb

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,4 @@
6363
expect(standalone_script).to_not match(/^require\s+["']git_tracker/)
6464
end
6565
end
66-
67-
describe "#ruby_executable" do
68-
subject(:standalone) { described_class }
69-
70-
before do
71-
allow(RbConfig::CONFIG).to receive(:[]).with("bindir") { "/some/other/bin" }
72-
allow(RbConfig::CONFIG).to receive(:[]).with("ruby_install_name") { "ruby" }
73-
end
74-
75-
it "uses user-level ruby binary when it is executable" do
76-
allow(File).to receive(:executable?).with("/usr/bin/ruby") { true }
77-
expect(standalone.ruby_executable).to eq("/usr/bin/ruby")
78-
end
79-
80-
it "uses rbconfig ruby when user-level ruby binary not executable" do
81-
allow(File).to receive(:executable?).with("/usr/bin/ruby") { false }
82-
expect(standalone.ruby_executable).to eq("/some/other/bin/ruby")
83-
end
84-
end
8566
end

0 commit comments

Comments
 (0)