Skip to content

Commit

Permalink
Merge pull request #541 from soutaro/fix-binstub
Browse files Browse the repository at this point in the history
Fix binstub
  • Loading branch information
soutaro committed Apr 25, 2022
2 parents 4c1beaa + 9102411 commit c3ef461
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
22 changes: 15 additions & 7 deletions lib/steep/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ def process_vendor

def process_binstub
path = Pathname("bin/steep")
root_path = Pathname.pwd
force = false

OptionParser.new do |opts|
Expand All @@ -227,21 +228,28 @@ def process_binstub
path = Pathname(v)
end

opts.on("--root=PATH", "The repository root path (defaults to `.`)") do |v|
root_path = (Pathname.pwd + v).cleanpath
end

opts.on("--[no-]force", "Overwrite file (defaults to false)") do
force = true
end
end.parse!(argv)

path.parent.mkpath
binstub_path = (Pathname.pwd + path).cleanpath
bindir_path = binstub_path.parent

bindir_path.mkpath

gemfile_path =
if defined?(Bundler)
Bundler.default_gemfile.relative_path_from(Pathname.pwd + path.parent)
Bundler.default_gemfile.relative_path_from(bindir_path)
else
Pathname("../Gemfile")
end

if path.file?
if binstub_path.file?
if force
stdout.puts Rainbow("#{path} already exists. Overwriting...").yellow
else
Expand All @@ -255,23 +263,23 @@ def process_binstub
BINSTUB_DIR=$(cd $(dirname $0); pwd)
GEMFILE=$(readlink -f ${BINSTUB_DIR}/#{gemfile_path})
GEMFILE_DIR=$(dirname ${GEMFILE})
ROOT_DIR=$(readlink -f ${BINSTUB_DIR}/#{root_path.relative_path_from(bindir_path)})
STEEP="bundle exec --gemfile=${GEMFILE} steep"
if type "rbenv" > /dev/null 2>&1; then
STEEP="rbenv exec ${STEEP}"
else
if type "rvm" > /dev/null 2>&1; then
STEEP="rvm ${GEMFILE_DIR} do ${STEEP}"
STEEP="rvm ${ROOT_DIR} do ${STEEP}"
fi
fi
exec $STEEP $@
TEMPLATE

path.write(template)
path.chmod(0755)
binstub_path.write(template)
binstub_path.chmod(0755)

stdout.puts Rainbow("Successfully generated executable #{path} 🎉").blue

Expand Down
8 changes: 6 additions & 2 deletions test/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -558,20 +558,24 @@ def test_binstub_generate
BINSTUB_DIR=$(cd $(dirname $0); pwd)
GEMFILE=$(readlink -f ${BINSTUB_DIR}/../Gemfile)
GEMFILE_DIR=$(dirname ${GEMFILE})
ROOT_DIR=$(readlink -f ${BINSTUB_DIR}/..)
STEEP="bundle exec --gemfile=${GEMFILE} steep"
if type "rbenv" > /dev/null 2>&1; then
STEEP="rbenv exec ${STEEP}"
else
if type "rvm" > /dev/null 2>&1; then
STEEP="rvm ${GEMFILE_DIR} do ${STEEP}"
STEEP="rvm ${ROOT_DIR} do ${STEEP}"
fi
fi
exec $STEEP $@
EOF

assert_equal <<EOM, sh!("bin/steep", "version")
#{Steep::VERSION}
EOM
end
end
end
Expand Down

0 comments on commit c3ef461

Please sign in to comment.