spm_version_updates 1.1.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 279945ab3b62528b1f9b66eba830cbe7f685563c8541e3f0df626ab7f821ee0e
4
- data.tar.gz: 1ee8b6492ed6ffbadc9a21ed8b332435b28bdbfe78a0b1619dbe743b4a2cc93a
3
+ metadata.gz: f7d008830011f3c063859526406f0ff3cb686dc80990d87bc3c684289f9eb833
4
+ data.tar.gz: 5257644ec20b9e1798a2f5072609173cb5892ace8bf1ea8bcfe3240dcc2a8b3b
5
5
  SHA512:
6
- metadata.gz: a850bcab42c57ab8d4a0cd0f432039d54a2ae5641f1b77bbbfdb3b6617d0557810a87db23b4d7f09ae72ae5acf00cb6f3705669ef4f517d2f33e2a3288137958
7
- data.tar.gz: d4c853aeb34ba83b545ea43a64adec34273e77f9e7f8fd7a5941e892a88e501e900aa3addd976d622dc8e965b5bd52829a7f7a4fc3eef058e13aa131ffd92faf
6
+ metadata.gz: 38899d7c7a51654d7574625ac1bc2581f1817771f656c94bf4d24103dc861e1b162539295402c62be7c9f1138867a7161dc57f0f9a7ce1e8e586fb275bb794b3
7
+ data.tar.gz: 308cf9c70aaf5ea5ba120188d2dcb0df36a2d28d3cb9f671a01a71f92e935c9f82a7c29facc06013b6f6388bc9c69e7d137a37c3779f156ea6edb3d04fcd886f
data/README.md CHANGED
@@ -23,18 +23,17 @@ require "spm_version_updates"
23
23
 
24
24
  checker = SpmChecker.new
25
25
 
26
- # Manifest mode: check one or more Package.swift files (a Package.resolved
27
- # next to each manifest is used automatically when present).
28
- warnings = checker.check_manifests(["path/to/Package.swift"])
26
+ # Manifest mode: check one or more Package.swift files.
27
+ result = checker.check_manifests(["path/to/Package.swift"])
29
28
 
30
29
  # Xcode mode: check the packages referenced by an Xcode project.
31
- # warnings = checker.check_for_updates("path/to/App.xcodeproj")
30
+ # result = checker.check_for_updates("path/to/App.xcodeproj")
32
31
 
33
- warnings.each { |warning| puts warning }
32
+ result.updates.each { |update| puts update["message"] }
34
33
 
35
- # Structured details (repository URL, current/available version, severity,
36
- # suggested update command, ...) for each warning:
37
- checker.warning_details.each { |detail| p detail }
34
+ # String-keyed details include repository URL, current/available version,
35
+ # suggested update command, source, and related report fields.
36
+ result.updates.each { |update| p update }
38
37
  ```
39
38
 
40
39
  Behavior is configurable through accessors on `SpmChecker` — for example
@@ -42,6 +41,92 @@ Behavior is configurable through accessors on `SpmChecker` — for example
42
41
  `report_pre_releases`, `ignore_repos`, and allow-host restrictions. See the
43
42
  class documentation for the full list.
44
43
 
44
+ ## Errors
45
+
46
+ Everything the gem raises descends from one of two roots (defined in
47
+ `lib/spm_version_updates/errors.rb`), so callers can rescue by failure
48
+ category instead of enumerating concrete classes:
49
+
50
+ - **`SpmVersionUpdates::Error < StandardError`**
51
+ - **`FileNotFoundError`** — a required file is missing:
52
+ - `ManifestParser::CouldNotFindManifest` — a `Package.swift` path does
53
+ not exist.
54
+ - `ManifestParser::CouldNotFindResolvedFile` — an expected
55
+ `Package.resolved` is missing in manifest mode; the message names the
56
+ missing file(s). Raised rather than silently reporting incomplete
57
+ results.
58
+ - `XcodeParser::CouldNotFindResolvedFile` — no `Package.resolved` was
59
+ found in the Xcode workspace locations.
60
+ - **`ParseError`** — a file exists but could not be read:
61
+ - `PackageResolved::MalformedFileError` — a corrupt or unrecognized
62
+ `Package.resolved`.
63
+ - **`NetworkError`** — git lookup failures:
64
+ - `GitOperations::LsRemoteError` — `git ls-remote` failed after bounded
65
+ retries (unreachable host, authentication failure). Messages are
66
+ credential-redacted.
67
+ - **`PolicyError`** — security-gate violations:
68
+ - `SpmChecker::DisallowedRepositoryHost` — `allow_hosts` is configured
69
+ and a dependency's host is not on the list. Raised before git is
70
+ contacted.
71
+ - **`SpmVersionUpdates::ConfigurationError < ArgumentError`** — invalid
72
+ caller-supplied configuration: `ManifestParser::ManifestPathMustBeSet`,
73
+ `XcodeParser::XcodeprojPathMustBeSet`, `allow_hosts` entries that don't
74
+ parse as hostnames, and every invalid repo-rules YAML shape. It inherits
75
+ `ArgumentError` (not `Error`) so existing callers that rescue
76
+ `ArgumentError` keep working — rescue it alongside
77
+ `SpmVersionUpdates::Error` when catching everything the gem raises:
78
+
79
+ ```ruby
80
+ begin
81
+ checker.check_manifests(["Modules/Package.swift"])
82
+ rescue SpmVersionUpdates::ConfigurationError, SpmVersionUpdates::Error => error
83
+ abort(error.message)
84
+ end
85
+ ```
86
+
87
+ ## Continuing past per-dependency failures
88
+
89
+ By default, the first failed git lookup or malformed `Package.resolved`
90
+ raises and aborts the run. Two optional handlers turn those into callbacks so
91
+ the remaining dependencies keep being checked:
92
+
93
+ ```ruby
94
+ # Called as (package, error) instead of raising GitOperations::LsRemoteError.
95
+ # A dependency shared by several manifests is reported only once per run.
96
+ checker.lookup_failure_handler = ->(package, error) {
97
+ puts("Skipping #{package.name}: #{error.message}")
98
+ }
99
+
100
+ # Called as (resolved_path, error) instead of raising
101
+ # PackageResolved::MalformedFileError; the file's pins are skipped.
102
+ checker.malformed_resolved_handler = ->(path, error) {
103
+ puts("Ignoring #{path}: #{error.message}")
104
+ }
105
+ ```
106
+
107
+ ## Parse warnings
108
+
109
+ A `.package(...)` declaration whose version requirement isn't recognized (or
110
+ that has unbalanced parentheses) is skipped rather than guessed at. Each skip
111
+ is recorded on the checker — separate from update warnings, so update counts
112
+ and fail-on thresholds are unaffected:
113
+
114
+ ```ruby
115
+ result = checker.check_manifests(["Modules/Package.swift"])
116
+
117
+ result.parse_warnings.each do |record|
118
+ # String-keyed hash: "type" ("parse_warning"), "reason", "source"
119
+ # (the manifest path), "snippet" (credential-redacted, truncated),
120
+ # and a human-readable "message".
121
+ puts(record["message"])
122
+ puts(ParseWarning.describe_reason(record)) # the reason as a readable phrase
123
+ puts(ParseWarning.issue_link(record)) # pre-filled GitHub new-issue URL
124
+ end
125
+ ```
126
+
127
+ The snippet is redacted and shown in the report only — it is never embedded
128
+ in the issue URL, where it could leak private repository URLs.
129
+
45
130
  ## License
46
131
 
47
132
  MIT — see [LICENSE.txt](LICENSE.txt).
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Category base classes for every error raised by spm_version_updates, so
4
+ # callers can rescue by failure kind instead of enumerating each concrete
5
+ # class. The concrete classes (e.g. ManifestParser::CouldNotFindManifest)
6
+ # keep their existing names and namespaces; only their superclasses point
7
+ # here.
8
+ module SpmVersionUpdates
9
+ # Base class for all errors raised by spm_version_updates.
10
+ class Error < StandardError; end
11
+
12
+ # Invalid user-supplied configuration or inputs. Inherits ArgumentError (not
13
+ # Error) so existing callers that rescue ArgumentError keep working; rescue
14
+ # it alongside Error when catching everything this gem raises.
15
+ class ConfigurationError < ArgumentError; end
16
+
17
+ # A required file (manifest, Package.resolved) could not be found.
18
+ class FileNotFoundError < Error; end
19
+
20
+ # A file exists but could not be parsed.
21
+ class ParseError < Error; end
22
+
23
+ # git or network lookup failures.
24
+ class NetworkError < Error; end
25
+
26
+ # Policy violations, e.g. a repository host blocked by allow-hosts.
27
+ class PolicyError < Error; end
28
+ end
@@ -1,18 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "errors"
3
4
  require_relative "update_severity"
4
5
 
5
6
  # Parses fail-on inputs and evaluates whether reported updates should fail.
6
7
  module FailOnThreshold
7
8
  ANY = "any"
8
9
 
9
- def self.from_inputs(explicit_fail_on, legacy_fail_on)
10
- input_name, value = [
11
- ["fail-on", explicit_fail_on],
12
- ["fail-on-updates", legacy_fail_on],
13
- ["fail-on-updates", "false"],
14
- ].find { |_name, candidate| candidate }
15
- normalize(value, input_name)
10
+ def self.from_input(value)
11
+ normalize(value)
16
12
  end
17
13
 
18
14
  def self.failure_message(threshold, reporter)
@@ -28,13 +24,13 @@ module FailOnThreshold
28
24
  UpdateSeverity.count_at_or_above(reporter.severity_counts, threshold)
29
25
  end
30
26
 
31
- def self.normalize(value, input_name)
32
- normalized = value.downcase
33
- return nil if ["false", "none"].include?(normalized)
34
- return ANY if normalized == "true"
27
+ def self.normalize(value)
28
+ normalized = value.to_s.strip.downcase
29
+ return nil if ["", "false", "none"].include?(normalized)
30
+ return ANY if ["true", ANY].include?(normalized)
35
31
  return normalized if UpdateSeverity.threshold?(normalized)
36
32
 
37
- raise(ArgumentError, "#{input_name} must be false, true, major, minor, or patch")
33
+ raise(SpmVersionUpdates::ConfigurationError, "fail-on must be false, true, any, major, minor, or patch")
38
34
  end
39
35
 
40
36
  def self.build_message(threshold, count)
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "open3"
4
4
  require_relative "credential_redactor"
5
+ require_relative "errors"
5
6
  require_relative "git_host_normalizer"
6
7
  require_relative "semver"
7
8
 
@@ -16,7 +17,7 @@ module GitOperations
16
17
  TAG_REF_PATTERNS = ["[0-9]*.[0-9]*", "v[0-9]*.[0-9]*"].freeze
17
18
 
18
19
  # Raised when git cannot complete a remote reference lookup.
19
- class LsRemoteError < StandardError; end
20
+ class LsRemoteError < SpmVersionUpdates::NetworkError; end
20
21
 
21
22
  # Removes protocol and trailing .git from a repo URL
22
23
  # @param [String] repo_url The URL of the repository
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "errors"
3
4
  require_relative "git_operations"
4
5
  require_relative "package_resolved"
5
6
 
@@ -20,6 +21,123 @@ require_relative "package_resolved"
20
21
  # `"revision"`) so the same comparison logic can be reused for both modes.
21
22
  module ManifestParser
22
23
  PACKAGE_CALL = ".package("
24
+ # Raw body and byte offsets for a direct `.package(...)` declaration.
25
+ PackageCallSpan = Struct.new(:body, :body_start, :body_end, keyword_init: true)
26
+
27
+ # Navigates a normal Swift double-quoted string literal.
28
+ class StringLiteral
29
+ def initialize(content, index)
30
+ @content = content
31
+ @index = index
32
+ @cursor = index
33
+ end
34
+
35
+ def copy_to(output)
36
+ output << @content[@index...skip_index]
37
+ skip_index
38
+ end
39
+
40
+ def skip_index
41
+ @cursor = @index + 1
42
+ @cursor = next_cursor until finished?
43
+ finish_index
44
+ end
45
+
46
+ private
47
+
48
+ def finished?
49
+ @cursor >= @content.length || @content[@cursor] == '"'
50
+ end
51
+
52
+ def next_cursor
53
+ @content[@cursor] == "\\" ? @cursor + 2 : @cursor + 1
54
+ end
55
+
56
+ def finish_index
57
+ @cursor >= @content.length ? @cursor : @cursor + 1
58
+ end
59
+ end
60
+ private_constant :StringLiteral
61
+
62
+ # Navigates a Swift raw string literal such as #"..."# or #"""..."""#.
63
+ class RawStringLiteral
64
+ def initialize(content, index)
65
+ @content = content
66
+ @index = index
67
+ @hash_count = leading_hash_count
68
+ @quote_count = quote_count
69
+ end
70
+
71
+ def literal?
72
+ @hash_count.positive? && @quote_count.positive?
73
+ end
74
+
75
+ def copy_to(output)
76
+ output << @content[@index...skip_index]
77
+ skip_index
78
+ end
79
+
80
+ def skip_index
81
+ length = @content.length
82
+ index = @index + @hash_count + @quote_count
83
+ index += 1 until index >= length || ends_at?(index)
84
+ [index + @quote_count + @hash_count, length].min
85
+ end
86
+
87
+ private
88
+
89
+ def leading_hash_count
90
+ index = @index
91
+ index += 1 while @content[index] == "#"
92
+ index - @index
93
+ end
94
+
95
+ def quote_count
96
+ quote_index = @index + @hash_count
97
+ return 3 if @content[quote_index, 3] == '"""'
98
+ return 1 if @content[quote_index] == '"'
99
+
100
+ 0
101
+ end
102
+
103
+ def ends_at?(index)
104
+ @content[index, @quote_count] == '"' * @quote_count &&
105
+ @content[(index + @quote_count), @hash_count] == "#" * @hash_count
106
+ end
107
+ end
108
+ private_constant :RawStringLiteral
109
+
110
+ # Finds `.package(` markers while skipping Swift string literals.
111
+ class PackageCallFinder
112
+ def initialize(content)
113
+ @content = content
114
+ end
115
+
116
+ def next_from(search_start)
117
+ index = search_start
118
+ while index < @content.length
119
+ return index if package_call_at?(index)
120
+
121
+ index = next_index(index)
122
+ end
123
+ nil
124
+ end
125
+
126
+ private
127
+
128
+ def package_call_at?(index)
129
+ @content[index, PACKAGE_CALL.length] == PACKAGE_CALL
130
+ end
131
+
132
+ def next_index(index)
133
+ raw_string = RawStringLiteral.new(@content, index)
134
+ return raw_string.skip_index if raw_string.literal?
135
+ return StringLiteral.new(@content, index).skip_index if @content[index] == '"'
136
+
137
+ index + 1
138
+ end
139
+ end
140
+ private_constant :PackageCallFinder
23
141
 
24
142
  # Find the direct SPM dependencies declared in a `Package.swift` manifest.
25
143
  #
@@ -31,20 +149,35 @@ module ManifestParser
31
149
  # scheme-bearing `repository_url` is retained for git operations.
32
150
  #
33
151
  # @param [String] manifest_path The path to a `Package.swift` file
152
+ # @yield [Hash] optionally receives `{ reason:, snippet: }` for each
153
+ # `.package(...)` declaration that had to be skipped, so callers can
154
+ # surface parse warnings instead of dropping dependencies silently
34
155
  # @raise [ManifestPathMustBeSet] if the manifest_path is blank
35
156
  # @raise [CouldNotFindManifest] if the file does not exist
36
157
  # @return [Hash<String, Hash>] normalized URL => { "repository_url", "requirement" }
37
- def self.get_packages(manifest_path)
158
+ def self.get_packages(manifest_path, &on_skip)
38
159
  raise(ManifestPathMustBeSet) if manifest_path.nil? || manifest_path.empty?
39
160
  raise(CouldNotFindManifest, manifest_path) unless File.exist?(manifest_path)
40
161
 
41
162
  content = strip_comments(File.read(manifest_path))
42
- package_calls(content).each_with_object({}) { |call, packages|
163
+ package_calls(content, &on_skip).each_with_object({}) { |call, packages|
164
+ if call.include?("\\(")
165
+ on_skip&.call({ reason: "unsupported_string_interpolation", snippet: call })
166
+ next
167
+ end
168
+ if call.match?(/#+"/)
169
+ on_skip&.call({ reason: "unsupported_raw_string", snippet: call })
170
+ next
171
+ end
172
+
43
173
  url = call[/\burl\s*:\s*"([^"]+)"/, 1]
44
174
  next if url.nil? # local package (path:) or otherwise unrecognized
45
175
 
46
176
  requirement = requirement_for(call)
47
- next if requirement.nil?
177
+ if requirement.nil?
178
+ on_skip&.call({ reason: "unrecognized_requirement", snippet: call })
179
+ next
180
+ end
48
181
 
49
182
  packages[GitOperations.trim_repo_url(url)] = { "repository_url" => url, "requirement" => requirement }
50
183
  }
@@ -66,23 +199,60 @@ module ManifestParser
66
199
  File.join(File.dirname(manifest_path), "Package.resolved")
67
200
  end
68
201
 
202
+ # Extract raw source spans for `.package(...)` calls. Offsets are byte indexes
203
+ # into the original content and point to the call body, excluding outer parens.
204
+ #
205
+ # @param [String] content raw manifest source
206
+ # @return [Array<PackageCallSpan>]
207
+ def self.package_call_spans(content)
208
+ package_spans(content).map { |span| PackageCallSpan.new(**span) }
209
+ end
210
+
69
211
  # Extract the argument body of each `.package( ... )` call, honoring nested
70
212
  # parentheses (e.g. `.upToNextMajor(from: "1.0.0")`) and string literals.
71
213
  #
214
+ # An unclosed call cannot be skipped safely (there is no closing paren to
215
+ # resume after), so scanning stops there; the skip callback says so.
216
+ #
72
217
  # @param [String] content The (comment-stripped) manifest source
73
218
  # @return [Array<String>]
74
- def self.package_calls(content)
219
+ def self.package_calls(content, &on_skip)
220
+ package_spans(content, &on_skip).map { |span| span[:body] }
221
+ end
222
+
223
+ def self.package_spans(content, &on_skip)
75
224
  calls = []
225
+ scan_package_spans(content, on_skip) { |span| calls << span }
226
+ calls
227
+ end
228
+
229
+ def self.scan_package_spans(content, on_skip)
76
230
  search_start = 0
77
- while (marker_index = content.index(PACKAGE_CALL, search_start))
78
- open_index = marker_index + PACKAGE_CALL.length - 1
79
- close_index = matching_paren(content, open_index)
80
- break if close_index.nil?
231
+ while (marker_index = next_package_call(content, search_start))
232
+ span = package_span(content, marker_index, on_skip)
233
+ break unless span
81
234
 
82
- calls << content[(open_index + 1)...close_index]
83
- search_start = close_index + 1
235
+ yield(span)
236
+ search_start = span[:body_end] + 1
84
237
  end
85
- calls
238
+ end
239
+
240
+ def self.package_span(content, marker_index, on_skip)
241
+ open_index = marker_index + PACKAGE_CALL.length - 1
242
+ close_index = matching_paren(content, open_index)
243
+ return unbalanced_package_span(content, marker_index, on_skip) unless close_index
244
+
245
+ body_start = open_index + 1
246
+ { body: content[body_start...close_index], body_start:, body_end: close_index }
247
+ end
248
+
249
+ def self.unbalanced_package_span(content, marker_index, on_skip)
250
+ on_skip&.call({ reason: "unbalanced_parentheses", snippet: content[marker_index, 300] })
251
+ nil
252
+ end
253
+
254
+ def self.next_package_call(content, search_start)
255
+ PackageCallFinder.new(content).next_from(search_start)
86
256
  end
87
257
 
88
258
  # Map the body of a `.package(...)` call to an Xcodeproj-style requirement.
@@ -143,17 +313,15 @@ module ManifestParser
143
313
  depth = 0
144
314
  index = open_index
145
315
  length = content.length
146
- in_string = false
147
316
  while index < length
148
317
  char = content[index]
149
- if in_string
150
- if char == "\\"
151
- index += 2
152
- next
153
- end
154
- in_string = false if char == '"'
318
+ raw_string = RawStringLiteral.new(content, index)
319
+ if raw_string.literal?
320
+ index = raw_string.skip_index
321
+ next
155
322
  elsif char == '"'
156
- in_string = true
323
+ index = StringLiteral.new(content, index).skip_index
324
+ next
157
325
  elsif char == "("
158
326
  depth += 1
159
327
  elsif char == ")"
@@ -177,7 +345,10 @@ module ManifestParser
177
345
  while index < length
178
346
  char = content[index]
179
347
  nxt = content[index + 1]
180
- if char == '"'
348
+ raw_string = RawStringLiteral.new(content, index)
349
+ if raw_string.literal?
350
+ index = raw_string.copy_to(output)
351
+ elsif char == '"'
181
352
  index = copy_string_literal(content, index, output)
182
353
  elsif char == "/" && nxt == "/"
183
354
  index += 1 while index < length && content[index] != "\n"
@@ -196,21 +367,7 @@ module ManifestParser
196
367
  #
197
368
  # @return [Integer]
198
369
  def self.copy_string_literal(content, index, output)
199
- length = content.length
200
- output << content[index] # opening quote
201
- index += 1
202
- while index < length
203
- char = content[index]
204
- output << char
205
- if char == "\\"
206
- output << content[index + 1] if index + 1 < length
207
- index += 2
208
- next
209
- end
210
- index += 1
211
- break if char == '"'
212
- end
213
- index
370
+ StringLiteral.new(content, index).copy_to(output)
214
371
  end
215
372
 
216
373
  # Return the index just past the closing `*/` of a block comment. Swift block
@@ -236,7 +393,11 @@ module ManifestParser
236
393
  end
237
394
 
238
395
  private_class_method :package_calls,
239
- :requirement_for,
396
+ :package_spans,
397
+ :scan_package_spans,
398
+ :package_span,
399
+ :unbalanced_package_span,
400
+ :next_package_call,
240
401
  :version_range_requirement,
241
402
  :increment_patch_version,
242
403
  :matching_paren,
@@ -245,14 +406,26 @@ module ManifestParser
245
406
  :skip_block_comment
246
407
 
247
408
  # Raised when manifest mode is invoked without a manifest path.
248
- class ManifestPathMustBeSet < StandardError
409
+ class ManifestPathMustBeSet < SpmVersionUpdates::ConfigurationError
410
+ def initialize(message = "package-manifest-paths must be set")
411
+ super
412
+ end
249
413
  end
250
414
 
251
415
  # Raised when a configured Package.swift manifest is missing.
252
- class CouldNotFindManifest < StandardError
416
+ class CouldNotFindManifest < SpmVersionUpdates::FileNotFoundError
417
+ def initialize(path)
418
+ super("Could not find Package.swift manifest: #{path}")
419
+ end
253
420
  end
254
421
 
255
422
  # Raised when manifest mode cannot find an expected Package.resolved file.
256
- class CouldNotFindResolvedFile < StandardError
423
+ class CouldNotFindResolvedFile < SpmVersionUpdates::FileNotFoundError
424
+ def initialize(paths)
425
+ super(
426
+ "Could not find any Package.resolved file (looked in: #{paths}). " \
427
+ "Commit a Package.resolved next to each manifest or set package-resolved-paths."
428
+ )
429
+ end
257
430
  end
258
431
  end