bundle/mac_app_store_dumper: typed: strict

This commit is contained in:
Ruoyu Zhong 2025-08-21 01:18:10 +08:00
parent 1a45660194
commit 579c31f001
No known key found for this signature in database

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil
# typed: strict
# frozen_string_literal: true
require "json"
@ -6,11 +6,14 @@ require "json"
module Homebrew
module Bundle
module MacAppStoreDumper
sig { void }
def self.reset!
@apps = nil
end
sig { returns(T::Array[[String, String]]) }
def self.apps
@apps ||= T.let(nil, T.nilable(T::Array[[String, String]]))
@apps ||= if Bundle.mas_installed?
`mas list 2>/dev/null`.split("\n").map do |app|
app_details = app.match(/\A(?<id>\d+)\s+(?<name>.*?)\s+\((?<version>[\d.]*)\)\Z/)
@ -19,7 +22,7 @@ module Homebrew
# Strip unprintable characters
if app_details
name = T.must(app_details[:name])
[app_details[:id], name.gsub(/[[:cntrl:]]|[\p{C}]/, "")]
[T.must(app_details[:id]), name.gsub(/[[:cntrl:]]|[\p{C}]/, "")]
end
end
else
@ -27,10 +30,12 @@ module Homebrew
end.compact
end
sig { returns(T::Array[Integer]) }
def self.app_ids
apps.map { |id, _| id.to_i }
end
sig { returns(String) }
def self.dump
apps.sort_by { |_, name| name.downcase }.map { |id, name| "mas \"#{name}\", id: #{id}" }.join("\n")
end