package/src/package/repository.cr

74 lines
1.4 KiB
Crystal

require "uri"
require "http"
require "specparser"
require "./exception.cr"
class Package::Package
def initialize(section : SpecParser::Section)
@file_name, @name, @version, release, @slot = section.options[0].split " "
@release = release.to_i
section.content.each do |key, value|
case key
when "dependencies"
@dependencies = value.as_a_or_s
end
end
end
end
class Package::Repository < Array(Package::Package)
getter uri : URI
def initialize(@uri)
initialize
end
def initialize(index_body : String, @uri)
initialize
import_index index_body
end
def import_index(index_body : String)
specs = SpecParser.parse index_body
specs.sections.each do |section|
case section.name
when "package"
self << ::Package::Package.new section
end
end
end
private def cache_file
Base64.strict_encode @uri.to_s
end
def download_index(context : Context)
cache = context.get_indices_cache
if @uri.scheme == "http" || @uri.scheme == "https"
context.info "Updating '#{@uri}'"
index = HTTP::Client.get("#{@uri.to_s}/repository.spec").body
File.write "#{cache}/#{cache_file}.spec", index
end
end
def import_index(context : Context)
cache = context.get_indices_cache
if @uri.scheme == "http" || @uri.scheme == "https"
cache_path = "#{cache}/#{cache_file}.spec"
if File.exists? cache_path
import_index File.read cache_path
end
end
end
end