Skip to content

Commit 391393a

Browse files
committed
Install resolved packages
1 parent 77c797a commit 391393a

File tree

5 files changed

+72
-18
lines changed

5 files changed

+72
-18
lines changed

src/manager.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ module Shards
3030
resolver.spec(version)
3131
end
3232

33+
def install
34+
resolver.install(version)
35+
end
36+
3337
private def resolver
3438
@resolver ||= Shards.find_resolver(@dependency)
3539
end

src/resolvers.cr

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ module Shards
1313

1414
abstract def read_spec(version = nil)
1515
abstract def available_versions
16+
17+
protected def install_path
18+
File.join(INSTALL_PATH, dependency.name)
19+
end
20+
21+
protected def cleanup_install_directory
22+
if File.exists?(install_path)
23+
Shards.logger.debug "rm -rf #{escape install_path}"
24+
25+
if Dir.exists?(install_path)
26+
#FileUtils.rm_rf(install_path)
27+
system("rm -rf #{escape install_path}")
28+
else
29+
File.delete(install_path)
30+
end
31+
end
32+
end
33+
34+
protected def escape(arg)
35+
"'#{arg.gsub(/'/, "\\'")}'"
36+
end
1637
end
1738

1839
@@resolver_classes = {} of String => Resolver.class

src/resolvers/git.cr

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ module Shards
33

44
class GitResolver < Resolver
55
def read_spec(version = "*")
6-
refs = case version
7-
when RELEASE_VERSION
8-
"v#{version}"
9-
when "*"
10-
"HEAD"
11-
else
12-
version
13-
end
14-
6+
refs = git_refs(version)
157
update_local_cache
168

179
if file_exists?(refs, SPEC_FILENAME)
@@ -36,6 +28,17 @@ module Shards
3628
end
3729
end
3830

31+
# OPTIMIZE: don't clean/install if the installed version is the expected one (ie. don't be dumb!)
32+
def install(version = nil)
33+
refs = git_refs(version)
34+
Shards.logger.info "Installing #{dependency.name} (#{version})"
35+
36+
cleanup_install_directory
37+
Dir.mkdir(install_path)
38+
39+
run "git archive --format=tar #{refs} | tar x -C #{escape install_path}"
40+
end
41+
3942
def local_path
4043
File.join(CACHE_DIRECTORY, dependency.name)
4144
end
@@ -44,6 +47,17 @@ module Shards
4447
dependency["git"].to_s
4548
end
4649

50+
private def git_refs(version)
51+
case version
52+
when RELEASE_VERSION
53+
"v#{version}"
54+
when "*"
55+
"HEAD"
56+
else
57+
version
58+
end
59+
end
60+
4761
private def update_local_cache
4862
return if @updated_cache
4963
Shards.logger.info "Updating #{git_url}"
@@ -58,7 +72,7 @@ module Shards
5872
end
5973

6074
private def clone_repository
61-
run "git clone --mirror --quiet -- '#{git_url.gsub(/'/, "\\'")}' #{dependency.name}",
75+
run "git clone --mirror --quiet -- #{escape git_url} #{dependency.name}",
6276
path: File.dirname(local_path)
6377
rescue Error
6478
raise Error.new("Failed to clone #{git_url}")

src/resolvers/path.cr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
lib LibC
2+
fun symlink(old_path : UInt8*, new_path : UInt8*) : Int32
3+
end
4+
5+
class File
6+
def self.symlink(old_path, new_path)
7+
ret = LibC.symlink(old_path, new_path)
8+
raise Errno.new("Error creating symlink from #{old_path} to #{new_path}") if ret == 0
9+
ret
10+
end
11+
end
12+
113
module Shards
214
class PathResolver < Resolver
315
def read_spec(version = nil)
@@ -11,6 +23,12 @@ module Shards
1123
def local_path
1224
dependency["path"].to_s
1325
end
26+
27+
def install(version = nil)
28+
Shards.logger.info "Using #{dependency.name} (#{local_path})"
29+
cleanup_install_directory
30+
File.symlink(local_path, install_path)
31+
end
1432
end
1533

1634
register_resolver :path, PathResolver

src/shards.cr

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module Shards
22
SPEC_FILENAME = "shard.yml"
3+
34
CACHE_DIRECTORY = File.join(Dir.working_directory, ".shards")
5+
Dir.mkdir(CACHE_DIRECTORY) unless Dir.exists?(CACHE_DIRECTORY)
46

5-
unless Dir.exists?(CACHE_DIRECTORY)
6-
Dir.mkdir(CACHE_DIRECTORY)
7-
end
7+
INSTALL_PATH = File.join(Dir.working_directory, "libs")
8+
Dir.mkdir(INSTALL_PATH) unless Dir.exists?(INSTALL_PATH)
89
end
910

1011
require "./logger"
@@ -16,11 +17,7 @@ begin
1617
spec = Shards::Spec.from_file(Dir.working_directory)
1718
manager = Shards::Manager.new(spec)
1819
manager.resolve
19-
20-
# TODO: install packages into the libs/ folder
21-
manager.packages.each do |package|
22-
p [package.name, package.version]
23-
end
20+
manager.packages.each(&.install)
2421
rescue ex : Shards::Error
2522
Shards.logger.error ex.message
2623
exit -1

0 commit comments

Comments
 (0)