Skip to content

Commit

Permalink
Merge pull request #176902 from Homebrew/get-appcast
Browse files Browse the repository at this point in the history
find-appcast: changes
  • Loading branch information
bevanjkay authored Jun 17, 2024
2 parents 93275f9 + afb29c8 commit 8e48f62
Showing 1 changed file with 58 additions and 10 deletions.
68 changes: 58 additions & 10 deletions cmd/find-appcast.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# typed: strict
# frozen_string_literal: true

require "English"
require "abstract_command"
require "formula"
require "open3"
Expand All @@ -25,43 +27,89 @@ class FindAppcastCmd < AbstractCommand
end
end

sig { override.void }
def run
app = Pathname.new(args.named.first)
find_sparkle(app) || find_electron_builder(app)
end

sig { params(appcast_type: String, urls: T.any(String, T::Array[String])).returns(T::Boolean) }
def verify_appcast(appcast_type, *urls)
print "Looking for #{appcast_type} appcast "
urls.each do |url|
print "Looking for #{appcast_type} appcast: "
urls.flatten.each do |url|
next unless url_exist?(url)

puts "Found!"
puts " #{url}"
puts "Found appcast! \n"
livecheck_strategy = if appcast_type == "Sparkle"
":sparkle"
elsif appcast_type == "Electron Builder"
":electron_builder"
end
puts <<~EOS
livecheck do
url "#{url}"
strategy #{livecheck_strategy}
end
EOS
return true
end
puts "Not found."
false
end

sig { params(url: String).returns(T.nilable(T::Boolean)) }
def url_exist?(url)
return false unless url

system("curl", "--silent", "--location", "--fail", url, out: File::NULL)
$CHILD_STATUS.exitstatus.zero?
end

sig { params(app: Pathname).returns(T::Boolean) }
def find_sparkle(app)
plist = app.join("Contents/Info.plist")
url = Open3.capture3("defaults", "read", plist.to_path, "SUFeedURL").first.strip
return false if url.empty?

verify_appcast("Sparkle", url)
end

sig { params(app: Pathname).returns(T::Boolean) }
def find_electron_builder(app)
appcast_file = app.join("Contents/Resources/app-update.yml")
unless appcast_file.exist?
verify_appcast("electron-builder", false)
return false
return false unless appcast_file.exist?

data = YAML.load_file(appcast_file)
components = {
url: data["url"],
owner: data["owner"],
repo: data["repo"],
bucket: data["bucket"],
channel: data["channel"],
path: data["path"],
region: data["region"],
name: data["name"],
endpoint: data["endpoint"],
}.compact

possible_appcasts = [
"#{components[:url]}/latest-mac.yml",
"#{components[:url]}/updates/latest/mac/latest-mac.yml",
"https://github.com/#{components[:owner]}/#{components[:repo]}/releases.atom",
"https://#{components[:bucket]}.s3.amazon-aws.yml/#{components[:channel]}/latest-mac.yml",
"https://#{components[:bucket]}.s3.amazonaws.com/latest-mac.yml",
"https://#{components[:bucket]}.s3.amazonaws.com/#{components[:path]}/latest-mac.yml",
"https://s3-#{components[:region]}.amazonaws.com/#{components[:bucket]}/#{components[:path]}/latest-mac.yml",
"https://s3.amazonaws.com/#{components[:bucket]}/#{components[:path]}/latest-mac.yml",
"https://#{components[:name]}.#{components[:region]}.digitaloceanspaces.com/latest-mac.yml",
"https://#{components[:name]}.#{components[:region]}.digitaloceanspaces.com/#{components[:path]}/latest-mac.yml",
"#{components[:endpoint]}/#{components[:bucket]}/#{components[:path]}/latest-mac.yml",
].select do |url|
url.exclude?("///") && url.exclude?("//.")
end
if possible_appcasts.empty?
false
else
verify_appcast("Electron Builder", *possible_appcasts)
end
YAML.load_file(appcast_file)
end
end
end
Expand Down

0 comments on commit 8e48f62

Please sign in to comment.