Skip to content

Commit 4775b9f

Browse files
committed
Lock command: save or print lockfile
1 parent 36c38d0 commit 4775b9f

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

src/cli.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module Shards
4848
when "list"
4949
Commands::List.run(path, tree: args.includes?("--tree"))
5050
when "lock"
51-
Commands::Lock.run(path)
51+
Commands::Lock.run(path, print: args.includes?("--print"))
5252
when "outdated"
5353
Commands::Outdated.run(path, prereleases: args.includes?("--pre"))
5454
when "prune"

src/commands/lock.cr

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,18 @@ require "../solver"
44
module Shards
55
module Commands
66
class Lock < Command
7-
def run
7+
def run(print = false)
8+
Shards.logger.info { "Resolving dependencies" }
9+
810
solver = Solver.new(spec)
911
solver.prepare(development: !Shards.production?)
1012

1113
if solution = solver.solve
12-
Shards.logger.info { "Found solution:" }
13-
14-
puts "version: 1.1"
15-
puts "shards:"
16-
17-
solution.sort_by!(&.name).each do |rs|
18-
key = rs.resolver.class.key
19-
20-
puts " #{rs.name}:"
21-
puts " #{key}: #{rs.resolver.dependency[key]}"
22-
23-
if rs.commit
24-
puts " commit: #{rs.commit}"
25-
else
26-
puts " version: #{rs.version}" unless rs.commit
27-
end
28-
29-
puts
14+
if print
15+
to_lockfile(solution, STDOUT)
16+
else
17+
Shards.logger.info { "Writing #{LOCK_FILENAME}" }
18+
File.open(LOCK_FILENAME, "w") { |file| to_lockfile(solution, file) }
3019
end
3120
else
3221
solver.each_conflict do |message|
@@ -35,6 +24,26 @@ module Shards
3524
Shards.logger.error { "Failed to find a solution" }
3625
end
3726
end
27+
28+
private def to_lockfile(solution, io)
29+
io << "version: 1.1\n"
30+
io << "shards:\n"
31+
32+
solution.sort_by!(&.name).each do |rs|
33+
key = rs.resolver.class.key
34+
35+
io << " " << rs.name << ":\n"
36+
io << " " << key << ": " << rs.resolver.dependency[key] << '\n'
37+
38+
if rs.commit
39+
io << " commit: " << rs.commit << '\n'
40+
else
41+
io << " version: " << rs.version << '\n'
42+
end
43+
44+
puts
45+
end
46+
end
3847
end
3948
end
4049
end

0 commit comments

Comments
 (0)