Skip to content

Commit 48b4739

Browse files
authored
Merge pull request #254 from crystal-lang/feature/local-command-line-argument
--local command line argument
2 parents 4dd6e45 + eaa6e6f commit 48b4739

File tree

7 files changed

+44
-21
lines changed

7 files changed

+44
-21
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/manager.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module Shards
66
getter packages : Set
77
property locks : Array(Dependency)?
88

9-
def initialize(@spec, update_cache = true)
10-
@packages = Set.new(update_cache: update_cache)
9+
def initialize(@spec)
10+
@packages = Set.new
1111
end
1212

1313
def resolve

src/package.cr

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Shards
88
@resolver : Resolver?
99
@available_versions : Array(String)?
1010

11-
def initialize(@dependency : Dependency, @update_cache = false)
11+
def initialize(@dependency : Dependency)
1212
@requirements = [] of String
1313
end
1414

@@ -120,7 +120,7 @@ module Shards
120120
end
121121

122122
def resolver
123-
@resolver ||= Shards.find_resolver(@dependency, update_cache: @update_cache)
123+
@resolver ||= Shards.find_resolver(@dependency)
124124
end
125125

126126
private def available_versions
@@ -129,15 +129,11 @@ module Shards
129129
end
130130

131131
class Set < Array(Package)
132-
def initialize(@update_cache = true)
133-
super()
134-
end
135-
136132
def add(dependency)
137133
package = find { |package| package.name == dependency.name }
138134

139135
unless package
140-
package = Package.new(dependency, update_cache: @update_cache)
136+
package = Package.new(dependency)
141137
self << package
142138
end
143139

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 !@update_cache || @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

src/resolvers/resolver.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Shards
88
abstract class Resolver
99
getter dependency : Dependency
1010

11-
def initialize(@dependency, @update_cache = true)
11+
def initialize(@dependency)
1212
end
1313

1414
def spec(version = nil)
@@ -57,11 +57,11 @@ module Shards
5757
@@resolver_classes[resolver.key] = resolver
5858
end
5959

60-
def self.find_resolver(dependency, update_cache = true)
60+
def self.find_resolver(dependency)
6161
@@resolvers[dependency.name] ||= begin
6262
klass = get_resolver_class(dependency.keys)
6363
raise Error.new("Failed can't resolve dependency #{dependency.name} (unsupported resolver)") unless klass
64-
klass.new(dependency, update_cache: update_cache)
64+
klass.new(dependency)
6565
end
6666
end
6767

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)