feat: 🔥 oh god

This commit is contained in:
David Bailey 2023-08-12 20:36:26 +02:00
parent cdb5492bfe
commit 3c7f48016d
30 changed files with 708 additions and 280 deletions

View file

@ -1,4 +1,6 @@
require 'digest'
require_relative 'artefact_engine.rb'
require_relative 'context.rb'
@ -7,7 +9,9 @@ module Comfpile
class Core
attr_reader :processing_stack
def initialize()
def initialize(build_dir_path: File.join('/tmp/comfpile/', Digest::MD5.hexdigest(Dir.pwd)))
@build_dir_path = File.expand_path(build_dir_path)
@artefact_engines = []
@artefact_prio_counter = 0
@ -15,44 +19,18 @@ module Comfpile
# First query is stage, second filename.
@artefacts = {}
@valid_contexts = {}
# Stack-style processing queue for item processing
@processing_stack = []
@context_key_provider = ContextKeyProvider.new()
end
# Sanitize and unify context tags.
# This function will take either an array of tags, or a symbol,
# and sanitize and unify it to match a predictable context tag
# format.
#
# Context tags are symbols containing a list of unique configs
# types to use, separated by underscore ('_')
#
# @param [Array<Symbol,String>, Symbol] context The context to
# sanitize
#
# @return [Symbol] Sanitized context. Unique, sorted list
# of tags, joined into a Symbol.
#
def sanitize_context(context)
if context.nil?
return :default
elsif context.is_a? Array
new_context = context.uniq.sort.join('_').to_sym
@valid_contexts[new_context] = true
return new_context
elsif context.is_a? Symbol
return context if @valid_contexts[context]
return sanitize_context(context.to_s.split('_'))
else
raise ArgumentError, "Unknown context key type supplied!"
end
def get_context_key(key)
return @context_key_provider.provide_context(key)
end
def find_artefact(stage, file = :none, context = :default)
context = sanitize_context(context)
context = get_context_key(context)
@artefacts.dig(context, stage, file)
end
@ -73,7 +51,7 @@ module Comfpile
# @return [Comfpile::Artefact, nil] Found or created artefact, or nil if nothing could be done
#
def craft_artefact(stage, file = :none, context = :default)
context = sanitize_context(context)
context = get_context_key(context)
artefact = find_artefact(stage, file, context)
@ -97,7 +75,7 @@ module Comfpile
end
def add_artefact(stage, file = :none, context = :default, engine: nil, artefact_class: Comfpile::Artefact)
context = sanitize_context context
context = get_context_key context
return unless find_artefact(stage, file, context).nil?
@ -165,6 +143,12 @@ module Comfpile
nil
end
def get_context_build_dir_path(context)
context = context.context if context.is_a? Artefact
File.expand_path(File.join(@build_dir_path, context.to_s))
end
def execute_step
return if @processing_stack.empty?
# puts "Got #{@processing_stack.length} items..."
@ -177,9 +161,14 @@ module Comfpile
# puts "Processing artefact #{artefact.stage} #{artefact.target}"
begin
artefact.execute_step
rescue ArtefactExecSkipError
build_path = get_context_build_dir_path(artefact)
FileUtils.mkpath build_path
Dir.chdir build_path do
begin
artefact.execute_step
rescue ArtefactExecSkipError
end
end
nil