2023-05-22 13:04:45 +02:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'net/http'
|
|
|
|
require 'json'
|
|
|
|
require_relative '../config/config'
|
2023-05-29 22:17:35 +02:00
|
|
|
require 'term/ansicolor'
|
2023-05-22 13:04:45 +02:00
|
|
|
|
|
|
|
module SlackBuilder
|
2023-05-29 22:17:35 +02:00
|
|
|
extend Term::ANSIColor
|
|
|
|
|
2023-05-24 12:56:55 +02:00
|
|
|
# Remote repository for a single package.
|
|
|
|
class Repository
|
|
|
|
# Request the latest tag in the given repository.
|
|
|
|
def latest
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-30 11:02:20 +02:00
|
|
|
# Reads the list fo tags from the GitHub API.
|
2023-05-24 12:56:55 +02:00
|
|
|
class GitHub < Repository
|
2023-08-09 20:59:42 +02:00
|
|
|
def initialize(owner, name, version_transform = nil)
|
2023-05-24 12:56:55 +02:00
|
|
|
super()
|
|
|
|
|
|
|
|
@owner = owner
|
|
|
|
@name = name
|
2023-06-17 21:41:52 +02:00
|
|
|
@version_transform = version_transform
|
2023-05-24 12:56:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def latest
|
2023-08-10 12:47:43 +02:00
|
|
|
`./bin/slackbuilder github #{@owner} #{@name} #{@version_transform}`.strip
|
2023-05-24 12:56:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-30 11:02:20 +02:00
|
|
|
# Request the latest version from the packagist API.
|
2023-05-24 12:56:55 +02:00
|
|
|
class Packagist < Repository
|
|
|
|
def initialize(vendor, name)
|
|
|
|
super()
|
|
|
|
|
|
|
|
@vendor = vendor
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
|
|
|
|
def latest
|
2023-08-06 14:25:19 +02:00
|
|
|
`./bin/slackbuilder packagist #{@vendor} #{@name}`.strip
|
2023-05-24 12:56:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-30 11:02:20 +02:00
|
|
|
# Reads a remote LATEST file.
|
|
|
|
class LatestText < Repository
|
|
|
|
def initialize(latest_url)
|
|
|
|
super()
|
|
|
|
|
2023-08-06 14:25:19 +02:00
|
|
|
@latest_url = latest_url
|
2023-06-30 11:02:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def latest
|
2023-08-06 14:25:19 +02:00
|
|
|
`./bin/slackbuilder text #{@latest_url}`.strip
|
2023-06-30 11:02:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-29 22:17:35 +02:00
|
|
|
module_function
|
|
|
|
|
|
|
|
# Checks if there is a new version for the package and returns the latest
|
|
|
|
# version if an update is available, otherwise returns nil.
|
|
|
|
def check_for_latest(package_name, repository)
|
|
|
|
package = find_package_info package_name
|
2023-05-24 12:56:55 +02:00
|
|
|
latest_version = repository.latest
|
|
|
|
|
|
|
|
if package.version == latest_version
|
2023-05-29 22:17:35 +02:00
|
|
|
puts green "#{package_name} is up to date (Version #{package.version})."
|
|
|
|
nil
|
2023-05-24 12:56:55 +02:00
|
|
|
else
|
2023-05-29 22:17:35 +02:00
|
|
|
puts red "#{package_name}: Current version is #{package.version}, #{latest_version} is available."
|
|
|
|
latest_version
|
2023-05-24 12:56:55 +02:00
|
|
|
end
|
2023-05-22 13:04:45 +02:00
|
|
|
end
|
2023-05-29 22:17:35 +02:00
|
|
|
|
|
|
|
private_class_method def find_package_info(package_name)
|
|
|
|
package_path = Pathname.new Dir.glob("#{CONFIG[:repository]}/*/#{package_name}").first
|
|
|
|
package_category = package_path.dirname.basename.to_s
|
|
|
|
Package.parse("#{package_category}/#{package_name}", File.read("#{package_path}/#{package_name}.info"))
|
|
|
|
end
|
2023-05-22 13:04:45 +02:00
|
|
|
end
|