Basic runtime template implementation

This commit is contained in:
2024-07-05 10:23:46 -03:00
parent 6eb9c49c22
commit 42f43db2b1
6 changed files with 82 additions and 1 deletions

View File

@ -22,15 +22,60 @@ module Faaso
return 0
end
# Create folder with a preconfigured funko for this runtime
# Get runtime template files list
template_base = ""
template_files = [] of String
if @@known.includes? "./runtimes/#{runtime}"
Log.info { "Using known runtime #{runtime}" }
template_base = "./runtimes/#{runtime}/template"
template_files = @@filelist.select { |f| f.starts_with? template_base }
elsif File.exists? runtime
Log.info { "Using directory #{runtime} as runtime" }
template_base = "#{runtime}/template"
template_files = Dir.glob("#{template_base}/**/*")
else
Log.error { "Can't find runtime #{runtime}" }
return 1
end
pp! template_files
# Create new folder
if Dir.exists? folder
Log.error { "Folder #{folder} already exists" }
return 1
end
Dir.mkdir_p folder
template_files.each do |f|
content = IO::Memory.new
# We need to use RUCKSACK_MODE=0 so it
# fallbacks to the filesystem
rucksack(f).read(content)
if content.nil?
Log.error { "Can't find file #{f}" }
return 1
end
# f is like "#{template_base}/foo"
# dst is like #{folder}/foo
dst = Path[folder] / Path[f].relative_to(template_base)
# Render templated files
if f.ends_with? ".j2"
dst = dst.sibling(dst.stem)
Log.info { "Creating file #{dst} from #{f}" }
File.open(dst, "w") do |file|
file << Crinja.render(content.to_s, {"name" => Path[folder].basename})
end
else # Just copy the file
Log.info { "Creating file #{dst} from #{f}" }
File.open(dst, "w") do |file|
file << content.to_s
end
end
end
0
end
end