Skip to content

Commit 54e4197

Browse files
committed
feat(commands): update template list, implement bare template add
1 parent c2b9526 commit 54e4197

File tree

1 file changed

+67
-6
lines changed

1 file changed

+67
-6
lines changed

src/commands/template.cr

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ module Geode::Commands
44
@name = "template"
55

66
add_command List.new
7-
# add_command Add.new
8-
add_command Create.new
7+
add_command Add.new
8+
# add_command Info.new
9+
# add_command Create.new
910
# add_command Test.new
1011
# add_command Remove.new
1112
end
@@ -20,14 +21,74 @@ module Geode::Commands
2021
end
2122

2223
def run(arguments : Cling::Arguments, options : Cling::Options) : Nil
23-
Dir.each_child(Geode::Config::TEMPLATES) do |name|
24-
next unless File.exists? Geode::Config::TEMPLATES / name / "config.ini"
25-
next unless File.exists? Geode::Config::TEMPLATES / name / "build.lua"
26-
stdout << "" << name << '\n'
24+
Geode::Template.list.each do |name, version|
25+
stdout << "" << name << " (" << version << ")\n"
2726
end
2827
end
2928
end
3029

30+
class Add < Base
31+
def setup : Nil
32+
@name = "add"
33+
34+
add_argument "source", required: true
35+
end
36+
37+
def run(arguments : Cling::Arguments, options : Cling::Options) : Nil
38+
source = arguments.get("source").as_s
39+
source = Dir.current if source == "."
40+
source = Path.new source
41+
42+
unless File.exists?(source / "control.yml")
43+
error "No control.yml file in directory"
44+
exit_program
45+
end
46+
47+
unless File.exists?(source / "control.lua")
48+
error "No control.lua file in directory"
49+
exit_program
50+
end
51+
52+
template = File.open(source / "control.yml") do |file|
53+
Geode::Template.from_yaml file
54+
end
55+
56+
unless template.name.matches? /[a-zA-Z][\w:-]+/
57+
error "Template name does not match specification",
58+
"must start with a letter",
59+
"can only consist of letters, numbers, colons or dashes"
60+
exit_program
61+
end
62+
63+
# TODO: existing template with the same name & source updates
64+
# if newer is higher version
65+
if Geode::Template.exists? template.name
66+
error "A template with the name '#{template.name}' already exists"
67+
exit_program
68+
end
69+
70+
unless template.files.empty?
71+
missing = false
72+
73+
template.files.each do |path|
74+
unless File.exists?(source / path)
75+
error "Missing file: #{path}"
76+
missing = true
77+
end
78+
end
79+
80+
exit_program if missing
81+
end
82+
83+
script = File.read source / "control.lua"
84+
runner = Lua::Runner.new script, File.open File::NULL
85+
runner.load_checks_env
86+
runner.run # TODO: check if scripts are used
87+
88+
template.install source
89+
end
90+
end
91+
3192
class Create < Base
3293
private COMMAND = {% if flag?(:darwin) %}"Cmd"{% else %}"Ctrl"{% end %}
3394
private CHAR = {% if flag?(:darwin) %}"Q"{% else %}"C"{% end %}

0 commit comments

Comments
 (0)