-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscaffold.rb
98 lines (78 loc) · 3.51 KB
/
scaffold.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require 'fileutils'
require 'liquid'
require 'yaml'
if File.exist?("config.yml")
config = YAML.load_file("config.yml")
else
raise "Error: config.yml not found."
end
settings = YAML.load_file("config.yml") || {}
#
source_dir = "./themes/#{settings["theme"] || "default"}"
destination_dir = "./_output/"
# Clean _output folder
if Dir.exist?(destination_dir)
Dir.foreach(destination_dir) do |file|
next if file == '.' || file == '..' || file == 'AUTO_GEN_FOLDER_DO_NOT_EDIT_FILE_HERE'
file_path = File.join(destination_dir, file)
if File.file?(file_path)
FileUtils.rm(file_path)
end
end
else
FileUtils.mkdir_p(destination_dir)
end
# Copy all files and directories while preserving the structure
Dir.glob("#{source_dir}/**/*").each do |entry|
relative_path = entry.sub("#{source_dir}/", '')
new_location = File.join(destination_dir, relative_path)
if File.directory?(entry)
FileUtils.mkdir_p(new_location)
else
FileUtils.cp(entry, new_location)
end
end
template_file = "#{destination_dir}/index.html"
if !File.exist?(template_file)
raise "Error: #{template_file} file not found."
end
template_content = File.read(template_file)
settings["vars"] = {}
if !settings["plugins"].nil?
settings["plugins"].each do |plugin|
pluginFileName = plugin.keys[0]
if File.exist?("./plugins/#{pluginFileName}.rb")
require_relative "./plugins/#{pluginFileName}.rb"
pluginObject = Object.const_get(pluginFileName).new(plugin.values)
settings["vars"][pluginFileName] = pluginObject.execute()
end
end
end
if !settings["links"].nil?
settings["links"].each_with_index do |link, index|
settings["links"][index]["link"]["icon"] = Liquid::Template.parse(settings["links"][index]["link"]["icon"]).render(settings)
settings["links"][index]["link"]["url"] = Liquid::Template.parse(settings["links"][index]["link"]["url"]).render(settings)
settings["links"][index]["link"]["alt"] = Liquid::Template.parse(settings["links"][index]["link"]["alt"]).render(settings)
settings["links"][index]["link"]["title"] = Liquid::Template.parse(settings["links"][index]["link"]["title"]).render(settings)
settings["links"][index]["link"]["text"] = Liquid::Template.parse(settings["links"][index]["link"]["text"]).render(settings)
end
end
if !settings["socials"].nil?
settings["socials"].each_with_index do |link, index|
settings["socials"][index]["social"]["icon"] = Liquid::Template.parse(settings["socials"][index]["social"]["icon"]).render(settings)
settings["socials"][index]["social"]["url"] = Liquid::Template.parse(settings["socials"][index]["social"]["url"]).render(settings)
settings["socials"][index]["social"]["alt"] = Liquid::Template.parse(settings["socials"][index]["social"]["alt"]).render(settings)
settings["socials"][index]["social"]["title"] = Liquid::Template.parse(settings["socials"][index]["social"]["title"]).render(settings)
end
end
settings["title"] = Liquid::Template.parse(settings["title"]).render(settings)
settings["footer"] = Liquid::Template.parse(settings["footer"]).render(settings)
settings["tagline"] = Liquid::Template.parse(settings["tagline"]).render(settings)
settings["name"] = Liquid::Template.parse(settings["name"]).render(settings)
settings["last_modified_at"] = Time.now.strftime("%Y-%m-%dT%H:%M:%S%z")
# Parse the Liquid template
liquid_template = Liquid::Template.parse(template_content)
rendered_content = liquid_template.render(settings)
File.open(template_file, 'w') do |file|
file.write(rendered_content)
end