Archived
3
0
This repository has been archived on 2022-01-17. You can view files and clone it, but cannot push or open issues or pull requests.
packaging/test.cr

153 lines
3.0 KiB
Crystal
Raw Normal View History

2019-08-01 23:02:16 +02:00
require "spec"
require "./src/context.cr"
require "./src/recipe.cr"
extend Package
# FIXME: recipe.clean? context autoclean?
context = Context.new()
context.packaging_backend = "apk"
recipes = [] of Package::Recipe
class SpecFileParser
2019-08-01 23:02:16 +02:00
def get_string?(id) : String?
entry = self.assignments[id]?
unless entry.is_a? StringContainer
nil
else
entry.value
end
end
def get_string(id) : String
entry = self.assignments[id]?
unless entry.is_a? StringContainer
raise Exception.new "`#{id}` was expected to be a string, but was a #{entry.class}."
end
entry.value
end
def get_long_string?(id) : String?
entry = self.assignments[id]?
unless entry.is_a? LongStringContainer
nil
else
entry.value
end
end
def get_string_any?(id) : String?
get_string?(id) || get_long_string?(id)
end
def get_list?(id) : Array(String)?
entry = assignments[id]?
case entry
when SpecFileParser::ArrayContainer
2019-08-01 23:02:16 +02:00
entry.value
when SpecFileParser::StringContainer
2019-08-01 23:02:16 +02:00
entry.value.split(",")
when Nil
return nil
else
raise Exception.new "`#{id}` was expected to be a list (string), but was a `#{entry.class}`"
end
end
end
class Package::Recipe
def initialize(@context, specs : SpecFileParser)
2019-08-01 23:02:16 +02:00
pp! specs
@name = specs.get_string "name"
@version = specs.get_string "version"
@release = specs.get_string?("release").try(&.to_i) || 1
@url = specs.get_string? "url"
@description = specs.get_string_any? "description"
@packager = specs.get_string? "packager"
@maintainer = specs.get_string? "packager"
specs.get_string_any?("configure").try do |script|
@instructions.configure << script
end
specs.get_string_any?("build").try do |script|
@instructions.build << script
end
specs.get_string_any?("install").try do |script|
@instructions.install << script
end
specs.get_list?("dependencies").try &.each do |atom|
@dependencies << atom
end
specs.get_list?("conflicts").try &.each do |atom|
@conflicts << atom
end
specs.get_list?("provides").try &.each do |atom|
@provides << atom
end
specs.get_list?("sources").try &.each do |source|
@sources << source
end
specs.get_list?("options").try &.each do |option|
match = option.match /\(.*\) *= *\(.*\)/
unless match
puts "WARNING: misformed option: #{option}"
next
end
name, value = match
puts "OPTION: #{name} -> #{value}"
@options[name] = value
end
end
end
class Package::Context
def read_recipe(filename : String)
specs = SpecFileParser.parse filename
2019-08-01 23:02:16 +02:00
if specs.nil?
raise Exception.new "file could not be parsed"
end
Recipe.new self, specs
end
end
pp! context.read_recipe "test.spec"
if ARGV.size == 0 || ARGV[0]? == "-h"
pp recipes.map &.name
exit 0
end
recipes.each do |recipe|
pp recipe
raise "oh no, download failed" unless recipe.download
raise "oh no, extraction failed" unless recipe.extract
raise "oh no, build failed" unless recipe.build
raise "oh no, packaging failed" unless recipe.package
recipe.clean
end