|
| 1 | +module Geode |
| 2 | + class Template |
| 3 | + include YAML::Serializable |
| 4 | + |
| 5 | + getter name : String |
| 6 | + getter summary : String |
| 7 | + getter author : String |
| 8 | + getter version : String |
| 9 | + getter source : String? |
| 10 | + getter files : Array(String) = [] of String |
| 11 | + @[YAML::Field(ignore: true)] |
| 12 | + property? shell_execute : Bool = false |
| 13 | + |
| 14 | + Dir.mkdir_p Config::TEMPLATES |
| 15 | + |
| 16 | + def self.list : Array({String, String}) |
| 17 | + info = [] of {String, String} |
| 18 | + |
| 19 | + Dir.each_child(Config::TEMPLATES) do |name| |
| 20 | + next unless File.exists?(Config::TEMPLATES / name / "control.yml") |
| 21 | + next unless File.exists?(Config::TEMPLATES / name / "control.lua") |
| 22 | + |
| 23 | + template = load name |
| 24 | + info << {name, template.version} |
| 25 | + end |
| 26 | + |
| 27 | + info |
| 28 | + end |
| 29 | + |
| 30 | + def self.exists?(name : String) : Bool |
| 31 | + File.exists?(Config::TEMPLATES / name / "control.yml") && |
| 32 | + File.exists?(Config::TEMPLATES / name / "control.lua") |
| 33 | + end |
| 34 | + |
| 35 | + def self.load(name : String) : self |
| 36 | + File.open(Config::TEMPLATES / name / "control.yml") do |file| |
| 37 | + Template.from_yaml file |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + def initialize(@name, @summary, @author, @source, @version, @files) |
| 42 | + end |
| 43 | + |
| 44 | + def install(source : Path) : Nil |
| 45 | + Dir.mkdir_p(dest = Config::TEMPLATES / @name) |
| 46 | + File.copy(source / "control.yml", dest / "control.yml") |
| 47 | + File.copy(source / "control.lua", dest / "control.lua") |
| 48 | + |
| 49 | + unless @files.empty? |
| 50 | + FileUtils.cp(@files.map { |f| source / f }, dest) |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + def run_script(output : IO) : Nil |
| 55 | + script = File.read Config::TEMPLATES / name / "control.lua" |
| 56 | + runner = Runner.new script, output |
| 57 | + runner.load_standard_functions |
| 58 | + end |
| 59 | + |
| 60 | + def test_script(output : IO) : Nil |
| 61 | + end |
| 62 | + end |
| 63 | +end |
0 commit comments