2025-01-06 00:12:03 +00:00
|
|
|
# typed: strict
|
2020-06-29 10:16:58 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-30 11:23:34 -05:00
|
|
|
require "utils/curl"
|
2020-07-03 01:51:38 +00:00
|
|
|
|
2020-08-26 09:11:39 +02:00
|
|
|
# Repology API client.
|
2020-07-06 03:32:18 +00:00
|
|
|
module Repology
|
2021-02-17 02:04:13 +05:30
|
|
|
HOMEBREW_CORE = "homebrew"
|
|
|
|
HOMEBREW_CASK = "homebrew_casks"
|
2020-08-19 09:10:57 -05:00
|
|
|
MAX_PAGINATION = 15
|
2020-08-26 09:11:39 +02:00
|
|
|
private_constant :MAX_PAGINATION
|
2020-08-19 09:10:57 -05:00
|
|
|
|
2025-01-06 00:12:03 +00:00
|
|
|
sig { params(last_package_in_response: T.nilable(String), repository: String).returns(T::Hash[String, T.untyped]) }
|
2023-04-17 10:37:59 -07:00
|
|
|
def self.query_api(last_package_in_response = "", repository:)
|
2021-09-24 22:44:27 -04:00
|
|
|
last_package_in_response += "/" if last_package_in_response.present?
|
|
|
|
url = "https://repology.org/api/v1/projects/#{last_package_in_response}?inrepo=#{repository}&outdated=1"
|
|
|
|
|
Curl: use `typed: strict`
This upgrades `utils/curl.rb` to `typed: strict`, which requires
a number of changes to pass `brew typecheck`. The most
straightforward are adding type signatures to methods, adding type
annotations (e.g., `T.let`) to variables that need them, and ensuring
that methods always use the expected return type.
I had to refactor areas where we call a `Utils::Curl` method and use
array destructuring on a `SystemCommand::Result` return value
(e.g., `output, errors, status = curl_output(...)`), as Sorbet
doesn't understand implicit array conversion. As suggested by Markus,
I've switched these areas to use `#stdout`, `#stderr`, and `#status`.
This requires the use of an intermediate variable (`result`) in some
cases but this was a fairly straightforward substitution.
I also had to refactor how `Cask::URL::BlockDSL::PageWithURL` works.
It currently uses `page.extend PageWithURL` to add a `url` attribute
but this reworks it to subclass `SimpleDelegator` and use an
`initialize` method instead. This achieves the same goal but in a way
that Sorbet can understand.
2025-01-10 21:37:20 -05:00
|
|
|
result = Utils::Curl.curl_output(
|
|
|
|
"--silent", url.to_s,
|
|
|
|
use_homebrew_curl: !Utils::Curl.curl_supports_tls13?
|
|
|
|
)
|
|
|
|
JSON.parse(result.stdout)
|
2021-12-31 17:07:53 +00:00
|
|
|
rescue
|
|
|
|
if Homebrew::EnvConfig.developer?
|
Curl: use `typed: strict`
This upgrades `utils/curl.rb` to `typed: strict`, which requires
a number of changes to pass `brew typecheck`. The most
straightforward are adding type signatures to methods, adding type
annotations (e.g., `T.let`) to variables that need them, and ensuring
that methods always use the expected return type.
I had to refactor areas where we call a `Utils::Curl` method and use
array destructuring on a `SystemCommand::Result` return value
(e.g., `output, errors, status = curl_output(...)`), as Sorbet
doesn't understand implicit array conversion. As suggested by Markus,
I've switched these areas to use `#stdout`, `#stderr`, and `#status`.
This requires the use of an intermediate variable (`result`) in some
cases but this was a fairly straightforward substitution.
I also had to refactor how `Cask::URL::BlockDSL::PageWithURL` works.
It currently uses `page.extend PageWithURL` to add a `url` attribute
but this reworks it to subclass `SimpleDelegator` and use an
`initialize` method instead. This achieves the same goal but in a way
that Sorbet can understand.
2025-01-10 21:37:20 -05:00
|
|
|
$stderr.puts result&.stderr
|
2021-12-31 17:07:53 +00:00
|
|
|
else
|
Curl: use `typed: strict`
This upgrades `utils/curl.rb` to `typed: strict`, which requires
a number of changes to pass `brew typecheck`. The most
straightforward are adding type signatures to methods, adding type
annotations (e.g., `T.let`) to variables that need them, and ensuring
that methods always use the expected return type.
I had to refactor areas where we call a `Utils::Curl` method and use
array destructuring on a `SystemCommand::Result` return value
(e.g., `output, errors, status = curl_output(...)`), as Sorbet
doesn't understand implicit array conversion. As suggested by Markus,
I've switched these areas to use `#stdout`, `#stderr`, and `#status`.
This requires the use of an intermediate variable (`result`) in some
cases but this was a fairly straightforward substitution.
I also had to refactor how `Cask::URL::BlockDSL::PageWithURL` works.
It currently uses `page.extend PageWithURL` to add a `url` attribute
but this reworks it to subclass `SimpleDelegator` and use an
`initialize` method instead. This achieves the same goal but in a way
that Sorbet can understand.
2025-01-10 21:37:20 -05:00
|
|
|
odebug result&.stderr
|
2021-12-31 17:07:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
raise
|
2020-06-29 09:57:21 -05:00
|
|
|
end
|
2020-06-29 09:51:58 -05:00
|
|
|
|
2025-01-06 00:12:03 +00:00
|
|
|
sig { params(name: String, repository: String).returns(T.nilable(T::Hash[String, T.untyped])) }
|
2023-04-17 10:37:59 -07:00
|
|
|
def self.single_package_query(name, repository:)
|
2023-08-02 09:02:44 -04:00
|
|
|
url = "https://repology.org/api/v1/project/#{name}"
|
2021-09-24 22:44:27 -04:00
|
|
|
|
Curl: use `typed: strict`
This upgrades `utils/curl.rb` to `typed: strict`, which requires
a number of changes to pass `brew typecheck`. The most
straightforward are adding type signatures to methods, adding type
annotations (e.g., `T.let`) to variables that need them, and ensuring
that methods always use the expected return type.
I had to refactor areas where we call a `Utils::Curl` method and use
array destructuring on a `SystemCommand::Result` return value
(e.g., `output, errors, status = curl_output(...)`), as Sorbet
doesn't understand implicit array conversion. As suggested by Markus,
I've switched these areas to use `#stdout`, `#stderr`, and `#status`.
This requires the use of an intermediate variable (`result`) in some
cases but this was a fairly straightforward substitution.
I also had to refactor how `Cask::URL::BlockDSL::PageWithURL` works.
It currently uses `page.extend PageWithURL` to add a `url` attribute
but this reworks it to subclass `SimpleDelegator` and use an
`initialize` method instead. This achieves the same goal but in a way
that Sorbet can understand.
2025-01-10 21:37:20 -05:00
|
|
|
result = Utils::Curl.curl_output(
|
|
|
|
"--location", "--silent", url.to_s,
|
|
|
|
use_homebrew_curl: !Utils::Curl.curl_supports_tls13?
|
|
|
|
)
|
2021-09-24 22:44:27 -04:00
|
|
|
|
Curl: use `typed: strict`
This upgrades `utils/curl.rb` to `typed: strict`, which requires
a number of changes to pass `brew typecheck`. The most
straightforward are adding type signatures to methods, adding type
annotations (e.g., `T.let`) to variables that need them, and ensuring
that methods always use the expected return type.
I had to refactor areas where we call a `Utils::Curl` method and use
array destructuring on a `SystemCommand::Result` return value
(e.g., `output, errors, status = curl_output(...)`), as Sorbet
doesn't understand implicit array conversion. As suggested by Markus,
I've switched these areas to use `#stdout`, `#stderr`, and `#status`.
This requires the use of an intermediate variable (`result`) in some
cases but this was a fairly straightforward substitution.
I also had to refactor how `Cask::URL::BlockDSL::PageWithURL` works.
It currently uses `page.extend PageWithURL` to add a `url` attribute
but this reworks it to subclass `SimpleDelegator` and use an
`initialize` method instead. This achieves the same goal but in a way
that Sorbet can understand.
2025-01-10 21:37:20 -05:00
|
|
|
data = JSON.parse(result.stdout)
|
2021-12-31 17:07:53 +00:00
|
|
|
{ name => data }
|
|
|
|
rescue => e
|
2024-07-15 13:44:01 -04:00
|
|
|
require "utils/backtrace"
|
Curl: use `typed: strict`
This upgrades `utils/curl.rb` to `typed: strict`, which requires
a number of changes to pass `brew typecheck`. The most
straightforward are adding type signatures to methods, adding type
annotations (e.g., `T.let`) to variables that need them, and ensuring
that methods always use the expected return type.
I had to refactor areas where we call a `Utils::Curl` method and use
array destructuring on a `SystemCommand::Result` return value
(e.g., `output, errors, status = curl_output(...)`), as Sorbet
doesn't understand implicit array conversion. As suggested by Markus,
I've switched these areas to use `#stdout`, `#stderr`, and `#status`.
This requires the use of an intermediate variable (`result`) in some
cases but this was a fairly straightforward substitution.
I also had to refactor how `Cask::URL::BlockDSL::PageWithURL` works.
It currently uses `page.extend PageWithURL` to add a `url` attribute
but this reworks it to subclass `SimpleDelegator` and use an
`initialize` method instead. This achieves the same goal but in a way
that Sorbet can understand.
2025-01-10 21:37:20 -05:00
|
|
|
error_output = [result&.stderr, "#{e.class}: #{e}", Utils::Backtrace.clean(e)].compact
|
2021-12-31 17:07:53 +00:00
|
|
|
if Homebrew::EnvConfig.developer?
|
|
|
|
$stderr.puts(*error_output)
|
|
|
|
else
|
|
|
|
odebug(*error_output)
|
2021-09-24 22:44:27 -04:00
|
|
|
end
|
2021-12-31 17:07:53 +00:00
|
|
|
|
|
|
|
nil
|
2020-07-28 09:30:19 -05:00
|
|
|
end
|
|
|
|
|
2025-01-06 00:12:03 +00:00
|
|
|
sig {
|
|
|
|
params(
|
|
|
|
limit: T.nilable(Integer),
|
|
|
|
last_package: T.nilable(String),
|
|
|
|
repository: String,
|
|
|
|
).returns(T::Hash[String, T.untyped])
|
|
|
|
}
|
2023-04-17 10:37:59 -07:00
|
|
|
def self.parse_api_response(limit = nil, last_package = "", repository:)
|
2021-02-17 00:09:02 +05:30
|
|
|
package_term = case repository
|
2021-02-17 02:04:13 +05:30
|
|
|
when HOMEBREW_CORE
|
2023-02-24 08:56:41 -08:00
|
|
|
"formulae"
|
2021-02-17 02:04:13 +05:30
|
|
|
when HOMEBREW_CASK
|
2023-02-24 08:56:41 -08:00
|
|
|
"casks"
|
2021-02-17 00:09:02 +05:30
|
|
|
else
|
2023-02-24 08:56:41 -08:00
|
|
|
"packages"
|
2021-02-17 00:09:02 +05:30
|
|
|
end
|
|
|
|
|
2023-02-24 08:56:41 -08:00
|
|
|
ohai "Querying outdated #{package_term} from Repology"
|
2020-06-29 09:51:58 -05:00
|
|
|
|
2020-08-18 22:25:17 +00:00
|
|
|
page_no = 1
|
2020-08-19 10:12:38 -05:00
|
|
|
outdated_packages = {}
|
2020-06-29 09:51:58 -05:00
|
|
|
|
2020-08-19 10:12:38 -05:00
|
|
|
while page_no <= MAX_PAGINATION
|
2020-07-06 09:08:41 -05:00
|
|
|
odebug "Paginating Repology API page: #{page_no}"
|
2020-06-29 09:51:58 -05:00
|
|
|
|
2024-03-07 16:20:20 +00:00
|
|
|
response = query_api(last_package, repository:)
|
2020-06-29 09:57:21 -05:00
|
|
|
outdated_packages.merge!(response)
|
2021-09-24 22:44:27 -04:00
|
|
|
last_package = response.keys.max
|
2020-08-19 10:12:38 -05:00
|
|
|
|
2020-08-19 09:10:57 -05:00
|
|
|
page_no += 1
|
2021-09-30 10:13:43 +01:00
|
|
|
break if (limit && outdated_packages.size >= limit) || response.size <= 1
|
2020-06-29 09:51:58 -05:00
|
|
|
end
|
2020-08-18 22:25:17 +00:00
|
|
|
|
2023-02-24 08:56:41 -08:00
|
|
|
package_term = package_term.chop if outdated_packages.size == 1
|
|
|
|
puts "#{outdated_packages.size} outdated #{package_term} found"
|
2020-08-16 18:45:50 -05:00
|
|
|
puts
|
2020-06-29 09:57:21 -05:00
|
|
|
|
2021-09-24 22:44:27 -04:00
|
|
|
outdated_packages.sort.to_h
|
2020-06-29 09:51:58 -05:00
|
|
|
end
|
2020-07-30 22:58:30 +00:00
|
|
|
|
2025-01-06 00:12:03 +00:00
|
|
|
sig { params(repositories: T::Array[String]).returns(T.any(String, Version)) }
|
2023-04-17 10:37:59 -07:00
|
|
|
def self.latest_version(repositories)
|
2021-01-24 19:07:17 +05:30
|
|
|
# The status is "unique" when the package is present only in Homebrew, so
|
|
|
|
# Repology has no way of knowing if the package is up-to-date.
|
2021-01-11 08:29:34 +05:30
|
|
|
is_unique = repositories.find do |repo|
|
|
|
|
repo["status"] == "unique"
|
|
|
|
end.present?
|
2020-07-30 22:58:30 +00:00
|
|
|
|
2021-01-11 08:29:34 +05:30
|
|
|
return "present only in Homebrew" if is_unique
|
2020-08-13 09:56:41 -05:00
|
|
|
|
2021-01-11 08:29:34 +05:30
|
|
|
latest_version = repositories.find do |repo|
|
|
|
|
repo["status"] == "newest"
|
2020-07-30 22:58:30 +00:00
|
|
|
end
|
|
|
|
|
2021-01-24 19:07:17 +05:30
|
|
|
# Repology cannot identify "newest" versions for packages without a version
|
|
|
|
# scheme
|
2021-01-11 08:29:34 +05:30
|
|
|
return "no latest version" if latest_version.blank?
|
2020-07-31 10:52:37 -05:00
|
|
|
|
2025-01-06 00:12:03 +00:00
|
|
|
Version.new(T.must(latest_version["version"]))
|
2020-07-31 10:52:37 -05:00
|
|
|
end
|
2020-06-29 09:51:58 -05:00
|
|
|
end
|