-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathcommand.cr
More file actions
135 lines (113 loc) · 4.08 KB
/
Copy pathcommand.cr
File metadata and controls
135 lines (113 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
require "../lock"
require "../spec"
require "../override"
require "levenshtein"
module Shards
abstract class Command
getter path : String
getter spec_path : String
getter lockfile_path : String
getter override_path : String?
@spec : Spec?
@locks : Lock?
@override : Override?
def initialize(path)
if File.directory?(path)
@path = path
@spec_path = File.join(path, SPEC_FILENAME)
else
@path = File.dirname(path)
@spec_path = path
end
@lockfile_path = File.join(@path, LOCK_FILENAME)
# If global override is defined via SHARDS_OVERRIDE env var we use that.
# Otherwise we check if the is a shard.override.yml file next to the shard.yml
@override_path = Shards.global_override_filename
unless @override_path
local_override = File.join(@path, OVERRIDE_FILENAME)
@override_path = File.exists?(local_override) ? local_override : nil
end
end
def self.run(path, *args, **kwargs)
new(path).run(*args, **kwargs)
end
def spec
@spec ||= if File.exists?(spec_path)
Spec.from_file(spec_path)
else
raise Error.new("Missing #{spec_filename}. Please run 'shards init'")
end
end
def spec_filename
File.basename(spec_path)
end
def locks
@locks ||= if lockfile?
Shards::Lock.from_file(lockfile_path)
else
raise Error.new("Missing #{LOCK_FILENAME}. Please run 'shards install'")
end
end
def lockfile?
File.exists?(lockfile_path)
end
def override
@override ||= override_path.try { |p| Shards::Override.from_file(p) }
end
def write_lockfile(packages)
Log.info { "Writing #{LOCK_FILENAME}" }
override_path = @override_path
override_path = File.basename(override_path) if override_path && File.dirname(override_path) == @path
Shards::Lock.write(packages, override_path, LOCK_FILENAME)
end
private def log_available_tags(conflicts)
conflicts.join(separator: "\n") do |k, v|
req = v.requirement
tags = req.resolver.available_tags
req = req.requirement
if req.is_a?(Version) || (req.is_a?(VersionReq) && req.patterns.size == 1 && req.patterns[0] !~ /^(<|>|=)/)
req = "v" + req.to_s
found = Levenshtein.find(req, tags, 6)
"For #{k} the closest available tag to #{req} is: #{found}"
elsif tags.empty?
"#{k} doesn't have any tag"
else
"For #{k} the last available tags are #{tags.reverse.first(5).join(", ")}"
end
end
end
def handle_resolver_errors(solver, &)
yield
rescue e : Molinillo::VersionConflict(Shards::Dependency, Shards::Spec)
Log.error { e.message }
Log.error { log_available_tags(e.conflicts) }
raise Shards::Error.new("Failed to resolve dependencies")
rescue e : Molinillo::ResolverError
Log.error { e.message }
raise Shards::Error.new("Failed to resolve dependencies")
end
def check_crystal_version(packages)
crystal_version = Shards::Version.new Shards.crystal_version
packages.each do |package|
crystal_req = MolinilloSolver.crystal_version_req(package.spec)
if !Shards::Versions.matches?(crystal_version, crystal_req)
Log.warn { "Shard \"#{package.name}\" may be incompatible with Crystal #{Shards.crystal_version}" }
end
end
end
def check_symlink_privilege
{% if flag?(:win32) %}
return if Shards::Helpers.developer_mode?
return if Shards::Helpers.privilege_enabled?("SeCreateSymbolicLinkPrivilege")
raise Shards::Error.new(<<-EOS)
Shards needs symlinks to work. Please enable Developer Mode, or run Shards with elevated rights:
https://learn.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development
EOS
{% end %}
end
def touch_install_path
Dir.mkdir_p(Shards.install_path)
File.touch(Shards.install_path)
end
end
end