Skip to content

Commit

Permalink
Fix install non-.exe executables on Windows (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Jul 19, 2023
1 parent eac9adf commit b55a827
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
15 changes: 15 additions & 0 deletions spec/integration/install_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -836,13 +836,28 @@ describe "install" do
foobar = File.join(application_path, "bin", Shards::Helpers.exe("foobar"))
baz = File.join(application_path, "bin", Shards::Helpers.exe("baz"))
foo = File.join(application_path, "bin", Shards::Helpers.exe("foo"))
crystal = File.join(application_path, "bin", "crystal.cr")

File.exists?(foobar).should be_true # "Expected to have installed bin/foobar executable"
File.exists?(baz).should be_true # "Expected to have installed bin/baz executable"
File.exists?(foo).should be_false # "Expected not to have installed bin/foo executable"
File.exists?(crystal).should be_true

`#{Process.quote(foobar)}`.should eq("OK")
`#{Process.quote(baz)}`.should eq("KO")
File.read(crystal).should eq %(puts "crystal")
end

it "errors on missing executable" do
metadata = {
dependencies: {"executable_missing": "*"},
}
with_shard(metadata) do
ex = expect_raises(FailedCommand) { run "shards install --no-color" }
ex.stdout.should contain <<-ERROR
E: Could not find executable "nonexistent"
ERROR
end
end

it "skips installing executables" do
Expand Down
6 changes: 5 additions & 1 deletion spec/integration/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,14 @@ private def setup_repositories
create_git_repository "binary"
create_executable "binary", "bin/foobar", %(print "OK")
create_executable "binary", "bin/baz", %(print "KO")
create_git_release "binary", "0.1.0", {executables: ["foobar", "baz"]}
create_file "binary", "bin/crystal.cr", %(puts "crystal")
create_git_release "binary", "0.1.0", {executables: ["foobar", "baz", "crystal.cr"]}
create_executable "binary", "bin/foo", %(print "FOO")
create_git_release "binary", "0.2.0", {executables: ["foobar", "baz", "foo"]}

create_git_repository "executable_missing"
create_git_release "executable_missing", "0.1.0", {executables: ["nonexistent"]}

create_git_repository "c"
create_git_release "c", "0.1.0", {dependencies: {d: {git: git_url(:d), version: "0.1.0"}}}
create_git_release "c", "0.2.0", {dependencies: {d: {git: git_url(:d), version: "0.2.0"}}}
Expand Down
23 changes: 19 additions & 4 deletions src/package.cr
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,13 @@ module Shards
Dir.mkdir_p(Shards.bin_path)

spec.executables.each do |name|
exe_name = Shards::Helpers.exe(name)
Log.debug { "Install bin/#{exe_name}" }
source = File.join(install_path, "bin", exe_name)
destination = File.join(Shards.bin_path, exe_name)
exe_name = find_executable_file(Path[install_path], name)
unless exe_name
raise Shards::Error.new("Could not find executable #{name.inspect}")
end
Log.debug { "Install #{exe_name}" }
source = File.join(install_path, exe_name)
destination = File.join(Shards.bin_path, File.basename(exe_name))

if File.exists?(destination)
next if File.same?(destination, source)
Expand All @@ -124,6 +127,18 @@ module Shards
end
end

def find_executable_file(install_path, name)
each_executable_path(name) do |path|
return path if File.exists?(install_path.join(path))
end
end

private def each_executable_path(name)
exe = Shards::Helpers.exe(name)
yield Path["bin", exe]
yield Path["bin", name] unless name == exe
end

def to_yaml(builder)
Dependency.new(name, resolver, version).to_yaml(builder)
end
Expand Down

0 comments on commit b55a827

Please sign in to comment.