From 2eb20aa09b9927874b6df0a6940f46551bf401bb Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Fri, 5 Jul 2024 10:33:40 -0300 Subject: [PATCH] More templating, lint --- .../template/{funko.yml => funko.yml.j2} | 2 +- .../{shard.yml => template/shard.yml.j2} | 2 +- src/commands/new.cr | 24 +++++++++---------- 3 files changed, 13 insertions(+), 15 deletions(-) rename runtimes/crystal/template/{funko.yml => funko.yml.j2} (50%) rename runtimes/crystal/{shard.yml => template/shard.yml.j2} (91%) diff --git a/runtimes/crystal/template/funko.yml b/runtimes/crystal/template/funko.yml.j2 similarity index 50% rename from runtimes/crystal/template/funko.yml rename to runtimes/crystal/template/funko.yml.j2 index 44354bb..367f4f6 100644 --- a/runtimes/crystal/template/funko.yml +++ b/runtimes/crystal/template/funko.yml.j2 @@ -1,2 +1,2 @@ -name: hello +name: {{ name }} runtime: crystal diff --git a/runtimes/crystal/shard.yml b/runtimes/crystal/template/shard.yml.j2 similarity index 91% rename from runtimes/crystal/shard.yml rename to runtimes/crystal/template/shard.yml.j2 index f4442ad..086a4e8 100644 --- a/runtimes/crystal/shard.yml +++ b/runtimes/crystal/template/shard.yml.j2 @@ -1,4 +1,4 @@ -name: function +name: {{ name }} version: 0.1.0 targets: diff --git a/src/commands/new.cr b/src/commands/new.cr index 628bd0b..e098994 100644 --- a/src/commands/new.cr +++ b/src/commands/new.cr @@ -23,12 +23,12 @@ module Faaso end # Get runtime template files list - template_base = "" + 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 } + template_files = @@filelist.select(&.starts_with?(template_base)) elsif File.exists? runtime Log.info { "Using directory #{runtime} as runtime" } template_base = "#{runtime}/template" @@ -38,8 +38,6 @@ module Faaso return 1 end - pp! template_files - # Create new folder if Dir.exists? folder Log.error { "Folder #{folder} already exists" } @@ -48,28 +46,28 @@ module Faaso Dir.mkdir_p folder - template_files.each do |f| + template_files.each do |t_file| content = IO::Memory.new # We need to use RUCKSACK_MODE=0 so it # fallbacks to the filesystem - rucksack(f).read(content) + rucksack(t_file).read(content) if content.nil? - Log.error { "Can't find file #{f}" } + Log.error { "Can't find file #{t_file}" } return 1 end - # f is like "#{template_base}/foo" + # t_file is like "#{template_base}/foo" # dst is like #{folder}/foo - dst = Path[folder] / Path[f].relative_to(template_base) + dst = Path[folder] / Path[t_file].relative_to(template_base) # Render templated files - if f.ends_with? ".j2" + if t_file.ends_with? ".j2" dst = dst.sibling(dst.stem) - Log.info { "Creating file #{dst} from #{f}" } + Log.info { " Creating file #{dst} from #{t_file}" } 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}" } + else # Just copy the file + Log.info { " Creating file #{dst} from #{t_file}" } File.open(dst, "w") do |file| file << content.to_s end