From 656a640738e767e64aac94d43038b97251a31be1 Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Thu, 29 Aug 2019 00:20:15 +0200 Subject: [PATCH] Configuration file added. --- Makefile | 4 +++- project.zsh | 1 + src/config.cr.in | 1 + src/context.cr | 18 ++++++++++++++++++ src/main.cr | 15 +++++++++++++++ 5 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 7eb00d7..4031b89 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ SHAREDIR := $(PREFIX)/share INCLUDEDIR := $(PREFIX)/include MANDIR := $(SHAREDIR)/man LIBEXECDIR := $(PREFIX)/libexec +SYSCONFDIR := $(PREFIX)/etc VERSION := 0.3.0 CC := cc @@ -54,7 +55,7 @@ assemble-apk.sh.uninstall: src/config.cr: src/config.cr.in src @echo ' SED > src/config.cr' - $(Q)sed -e 's&@PREFIX@&$(PREFIX)&;s&@BINDIR@&$(BINDIR)&;s&@LIBDIR@&$(LIBDIR)&;s&@SHAREDIR@&$(SHAREDIR)&;s&@INCLUDEDIR@&$(INCLUDEDIR)&;s&@MANDIR@&$(MANDIR)&;s&@LIBEXECDIR@&$(LIBEXECDIR)&;s&@VERSION@&$(VERSION)&;' src/config.cr.in > 'src/config.cr' + $(Q)sed -e 's&@PREFIX@&$(PREFIX)&;s&@BINDIR@&$(BINDIR)&;s&@LIBDIR@&$(LIBDIR)&;s&@SHAREDIR@&$(SHAREDIR)&;s&@INCLUDEDIR@&$(INCLUDEDIR)&;s&@MANDIR@&$(MANDIR)&;s&@LIBEXECDIR@&$(LIBEXECDIR)&;s&@SYSCONFDIR@&$(SYSCONFDIR)&;s&@VERSION@&$(VERSION)&;' src/config.cr.in > 'src/config.cr' $(Q)chmod +x 'src/config.cr' @@ -183,6 +184,7 @@ help: @echo ' - INCLUDEDIR  ${INCLUDEDIR}' @echo ' - MANDIR  ${MANDIR}' @echo ' - LIBEXECDIR  ${LIBEXECDIR}' + @echo ' - SYSCONFDIR  ${SYSCONFDIR}' @echo ' - VERSION  ${VERSION}' @echo '' @echo 'Project targets: ' diff --git a/project.zsh b/project.zsh index 271687d..69cabef 100644 --- a/project.zsh +++ b/project.zsh @@ -4,6 +4,7 @@ version=0.3.0 variables+=( LIBEXECDIR '$(PREFIX)/libexec' + SYSCONFDIR '$(PREFIX)/etc' VERSION "$version" ) diff --git a/src/config.cr.in b/src/config.cr.in index fdbda2e..16f69d2 100644 --- a/src/config.cr.in +++ b/src/config.cr.in @@ -1,6 +1,7 @@ # These values are changed by `make` at build-time. OWN_LIBEXEC_DIR = "@LIBEXECDIR@/package" +SYSCONF_DIR = "@SYSCONFDIR@" module Package Version = "@VERSION@" diff --git a/src/context.cr b/src/context.cr index abc656f..0358e05 100644 --- a/src/context.cr +++ b/src/context.cr @@ -1,3 +1,5 @@ +require "specfileparser" + require "./exception.cr" require "./config.cr" @@ -224,5 +226,21 @@ class Package::Context read_recipe recipe_file_name if repo end + + def read_configuration(filename : String) + specs = SpecFileParser.parse(filename).not_nil! + + specs.assignments.each do |key, value| + puts key, value + case key + when "packages-directory" + @packages_directory = value.as_s + when "sources-directory" + @sources_directory = value.as_s + when "prefixes" + @prefixes = value.as_a_or_s + end + end + end end diff --git a/src/main.cr b/src/main.cr index d8f201d..6ab2027 100644 --- a/src/main.cr +++ b/src/main.cr @@ -11,6 +11,9 @@ context = Context.new() context.packaging_backend = "apk" context.repositories << "." +configuration_file = "#{SYSCONF_DIR}/package.cfg" +configuration_file_requested = false + requested_recipes = [] of String download_only = false do_not_clean = false @@ -29,6 +32,11 @@ OptionParser.parse! do |parser| context.repositories << dir } + parser.on("-c FILE", "--conf FILE", "Use a configuration file other than the default one.") { |file| + configuration_file = file + configuration_file_requested = true + } + parser.on("-D", "--download-only", "Only download sources, do not build.") { download_only = true } @@ -58,6 +66,13 @@ OptionParser.parse! do |parser| end end +if File.exists? configuration_file + context.read_configuration configuration_file +elsif configuration_file_requested + STDERR.puts "ERROR: configuration file '#{configuration_file}' does not exist" + exit 1 +end + found_recipes = requested_recipes.map do |name| puts context.find_recipe(name) context.find_recipe(name) || name