Skip to content

Commit eaa6e6f

Browse files
committed
Add --local to not fetch remote repositories
Uses the local cahe as is. Speeds up installing shards when we know the cache is up to date. It will fail when the repository wasn't cloned into the cache, and it won't find new releases until the cache is updated by running the install or update commands without --local.
1 parent d6e0a75 commit eaa6e6f

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

src/cli.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module Shards
2929
opts.on("--no-color", "Disable colored output.") { self.colors = false }
3030
opts.on("--version", "Prints the `shards` version.") { puts self.version_string; exit }
3131
opts.on("--production", "Run in release mode. No development dependencies and strict sync between shard.yml and shard.lock.") { self.production = true }
32+
opts.on("--local", "Don't update remote repositories, use the local cache only.") { self.local = true }
3233
opts.on("-v", "--verbose", "Increases the log verbosity, printing all debug statements.") { self.logger.level = Logger::Severity::DEBUG }
3334
opts.on("-q", "--quiet", "Decreases the log verbosity, printing only warnings and errors.") { self.logger.level = Logger::Severity::WARN }
3435
opts.on("-h", "--help", "Prints usage synopsis.") { self.display_help_and_exit(opts) }

src/config.cr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@ module Shards
7171
def self.bin_path=(@@bin_path : String)
7272
end
7373

74-
@@production = false
75-
76-
def self.production?
77-
@@production
78-
end
79-
80-
def self.production=(@@production)
81-
end
74+
class_property? production = false
75+
class_property? local = false
8276
end

src/resolvers/git.cr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ module Shards
172172
@updated_cache = false
173173
end
174174

175-
return if @updated_cache
175+
return if Shards.local? || @updated_cache
176176
Shards.logger.info "Fetching #{git_url}"
177177

178178
if cloned_repository?
@@ -234,6 +234,10 @@ module Shards
234234
end
235235

236236
private def run(command, path = local_path, capture = false)
237+
if Shards.local? && !Dir.exists?(path)
238+
dependency_name = File.basename(path, ".git")
239+
raise Error.new("Missing repository cache for #{dependency_name.inspect}. Please run without --local to fetch it.")
240+
end
237241
Dir.cd(path) do
238242
run_in_current_folder(command, capture)
239243
end

test/integration/update_test.cr

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,32 @@ class UpdateCommandTest < Minitest::Test
201201
assert File.exists?(foo), "Expected to have installed bin/foo executable"
202202
assert_equal "FOO\n", `#{foo}`
203203
end
204+
205+
def test_doesnt_update_local_cache
206+
metadata = {
207+
dependencies: { local_cache: "*" },
208+
}
209+
210+
with_shard(metadata) do
211+
# error: dependency isn't in local cache
212+
ex = assert_raises(FailedCommand) { run("shards install --local --no-color") }
213+
assert_match %(E: Missing repository cache for "local_cache".), ex.stdout
214+
end
215+
216+
# re-run without --local to install the dependency:
217+
create_git_repository "local_cache", "1.0", "2.0"
218+
with_shard(metadata) { run("shards install") }
219+
assert_locked "local_cache", "2.0"
220+
221+
# create a new release:
222+
create_git_release "local_cache", "3.0"
223+
224+
# re-run with --local, which won't find the new release:
225+
with_shard(metadata) { run("shards update --local") }
226+
assert_locked "local_cache", "2.0"
227+
228+
# run again without --local, which will find & install the new release:
229+
with_shard(metadata) { run("shards update") }
230+
assert_locked "local_cache", "3.0"
231+
end
204232
end

0 commit comments

Comments
 (0)