From c8228e95f7b9f3e46825c8319fad8ee6da803d25 Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Tue, 5 Nov 2019 14:32:30 +0100 Subject: [PATCH] Installations from repositories. --- src/package.cr | 2 ++ src/package/atom.cr | 6 +++++- src/package/configuration.cr | 22 +++++++++++++++---- src/package/context.cr | 36 +++++++++++++++++++++++++------ src/package/package.cr | 2 +- src/package/repository.cr | 42 ++++++++++++++++++++++++++++++++++-- 6 files changed, 95 insertions(+), 15 deletions(-) diff --git a/src/package.cr b/src/package.cr index f67f6dd..17eb83b 100644 --- a/src/package.cr +++ b/src/package.cr @@ -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 diff --git a/src/package/atom.cr b/src/package/atom.cr index 169f10a..148f411 100644 --- a/src/package/atom.cr +++ b/src/package/atom.cr @@ -20,7 +20,11 @@ class Package::Atom # FIXME: What if other data are provided? def to_s - "#{name} :#{slot}" + if slot + "#{name} :#{slot}" + else + name + end end # FIXME: Parse slot syntax, parse operators. diff --git a/src/package/configuration.cr b/src/package/configuration.cr index 1ada1a0..e29867c 100644 --- a/src/package/configuration.cr +++ b/src/package/configuration.cr @@ -18,12 +18,26 @@ class Package::Configuration specs.sections.each do |section| case section.name when "repository" - @repositories << Repository.new( - File.read("#{section.options[0]}/repository.spec"), - section.options[0] - ) + 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(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 diff --git a/src/package/context.cr b/src/package/context.cr index 2797a30..7e5b29d 100644 --- a/src/package/context.cr +++ b/src/package/context.cr @@ -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 diff --git a/src/package/package.cr b/src/package/package.cr index a56395f..4f5ec3f 100644 --- a/src/package/package.cr +++ b/src/package/package.cr @@ -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 diff --git a/src/package/repository.cr b/src/package/repository.cr index 7335829..b920db5 100644 --- a/src/package/repository.cr +++ b/src/package/repository.cr @@ -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