Files
sentinel/updates.rb
T

69 lines
2.3 KiB
Ruby
Raw Normal View History

# <<*>> -^v- SNOWARE APPLICATION
# usage: Check for updates from remote.
# author: S.A.
# time: 2026-06-08
# Copyright (c) [2026] [S.A. SNOWARE-SCU]
# [MineSentinel] is licensed under Mulan PubL v2.
# You can use this software according to the terms and conditions of the Mulan PubL v2.
# You may obtain a copy of Mulan PubL v2 at:
# http://license.coscl.org.cn/MulanPubL-2.0
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PubL v2 for more details.
# frozen_string_literal: true
require 'net/http'
require 'uri'
# @type const RELEASE_SERIAL_CODE: String
RELEASE_SERIAL_CODE = 'b43527c'
# @type const CHANNEL: String
CHANNEL = 'master'
# @type const VERSION_LIST_URL: String
VERSION_LIST_URL = "https://raw.giteeusercontent.com/scu_team/minesentinel/#{CHANNEL}/APP_VERSIONS_LIST"
# @type const RECURSIVE_RETRY_DEPTH: Integer
RECURSIVE_RETRY_DEPTH = 3
class UpdateManager
# @return [String]
def self.fetch
MethodExecutor.record("debug", "updates", "Fetching version list from #{VERSION_LIST_URL}")
uri = URI(VERSION_LIST_URL)
response = Net::HTTP.get_response(uri)
case response.code.to_i
when 200
MethodExecutor.record("debug", "updates", "Version list fetched successfully.")
response.body
else
raise "Unexpected HTTP response code: #{response.code}"
end
rescue => e
MethodExecutor.record("error", "updates", "Failed to fetch version list: #{e}")
nil
end
# @return [Hash{Symbol=>Symbol, Symbol=>String}]
def self.check
raw = fetch
if raw.nil?
MethodExecutor.record("error", "updates", "Version list is empty, cannot check updates.")
raise "Unknown Error"
end
lines = raw.lines(chomp: true)
lines.each_with_index do |line, index|
line_num = index + 1
if line == RELEASE_SERIAL_CODE && line_num == lines.size
MethodExecutor.record("info", "updates", "Current version #{RELEASE_SERIAL_CODE} is up to date.")
return { type: :Newest, version: RELEASE_SERIAL_CODE }
end
end
latest = lines.last
MethodExecutor.record("info", "updates", "Newer version #{latest} available (current: #{RELEASE_SERIAL_CODE}).")
{ type: :NewerAvailable, version: latest }
end
end