Installations from repositories.

master
Luka Vandervelden 2019-11-05 14:32:30 +01:00
parent e458df0afd
commit c8228e95f7
6 changed files with 95 additions and 15 deletions

View File

@ -26,6 +26,8 @@ begin
context.install args.map { |s| Package::Atom.from_string s }
when "remove"
context.remove args.map { |s| Package::Atom.from_string s }
when "update"
context.update_repository_cache
end
rescue e : Package::CollisionException
context.error e

View File

@ -20,7 +20,11 @@ class Package::Atom
# FIXME: What if other data are provided?
def to_s
if slot
"#{name} :#{slot}"
else
name
end
end
# FIXME: Parse slot syntax, parse operators.

View File

@ -18,12 +18,26 @@ class Package::Configuration
specs.sections.each do |section|
case section.name
when "repository"
repository_url = section.options[0]
repository_uri = URI.parse repository_url
spec_url = "#{repository_url}/repository.spec"
spec_uri = URI.parse spec_url
if spec_uri.scheme == "" || spec_uri.scheme == "file"
@repositories << Repository.new(
File.read("#{section.options[0]}/repository.spec"),
section.options[0]
File.read(spec_uri.path),
repository_uri
)
else
@repositories << Repository.new(repository_uri)
end
end
end
end
def download_index(context : Context)
cache = context.get_indices_cache
end
end

View File

@ -25,6 +25,7 @@ class Package::Context < Weird::Context
def root=(@root)
@database.root = "#{@root}#{Database::PATH_FROM_ROOT}"
@configuration = Configuration.new "#{@root}#{Configuration::PATH_FROM_ROOT}"
@configuration.repositories.each &.import_index(self)
end
def install(package : Package, data_directory : String)
@ -97,7 +98,7 @@ class Package::Context < Weird::Context
tuple = get_installable atom
if tuple.nil?
raise Exception.new "Could not find '#{atom}'."
raise Exception.new "Could not find '#{atom.to_s}'."
end
packages_to_install << tuple
@ -117,11 +118,10 @@ class Package::Context < Weird::Context
download tuple
end
# FIXME: Install everything here.
packages_to_install.reverse.each do |tuple|
repository, package = tuple
url = "#{repository.url}/#{package.file_path}"
url = "#{repository.uri.path}/#{package.file_path}"
install url
end
@ -133,17 +133,23 @@ class Package::Context < Weird::Context
warning "(unimplemented) upgrade(Atom)"
end
# XXX: Unimplemented.
def download(tuple : Tuple(Repository, Package))
repository, package = tuple
uri = URI.parse package.file_path
uri = repository.uri
if uri.scheme == "http" || uri.scheme == "https"
FileUtils.mkdir_p @cache_directory
pp package.file_path
#File.write HTTP::Client.get
destination = "#{get_cache_directory}/#{package.file_path}"
full_url = "#{repository.uri.to_s}/#{package.file_path}"
debug "Downloading '#{package.file_path}'"
# FIXME: Check errors, probably. :|
File.write destination, HTTP::Client.get(full_url).body
package.file_path = destination
end
end
@ -228,6 +234,9 @@ class Package::Context < Weird::Context
end
def update_repository_cache
@configuration.repositories.each do |repository|
repository.download_index self
end
end
def download_to_cache(atom : Atom, url : String)
end
@ -240,5 +249,18 @@ class Package::Context < Weird::Context
FileUtils.mkdir_p directory
directory
end
def get_indices_cache
directory = "#{@root}/var/cache/package"
FileUtils.mkdir_p directory
directory
end
# Should this really be the same place? :shrug:
def get_cache_directory
directory = "#{@root}/var/cache/package"
FileUtils.mkdir_p directory
directory
end
end

View File

@ -10,7 +10,7 @@ class Package::Package
getter dependencies = Array(Atom).new
getter file_path : String
property file_path : String
def initialize(@file_path)
specs = SpecParser.parse File.read file_path

View File

@ -1,3 +1,6 @@
require "uri"
require "http"
require "specparser"
require "./exception.cr"
@ -17,11 +20,19 @@ class Package::Package
end
class Package::Repository < Array(Package::Package)
getter url : String
getter uri : URI
def initialize(index_body : String, @url)
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|
@ -31,5 +42,32 @@ class Package::Repository < Array(Package::Package)
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