aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/guff/theme/file-info.cr7
-rw-r--r--src/guff/theme/source-manifest.cr60
2 files changed, 25 insertions, 42 deletions
diff --git a/src/guff/theme/file-info.cr b/src/guff/theme/file-info.cr
index 914be75..768d625 100644
--- a/src/guff/theme/file-info.cr
+++ b/src/guff/theme/file-info.cr
@@ -1,9 +1,14 @@
class Guff::Theme::FileInfo
getter :size, :hash
- def initialize(abs_path : String, @path : String)
+ def initialize(src_dir : String, @path : String)
+ # build absolute path
+ abs_path = File.join(src_dir, "files", @path)
+
+ # get file size
@size = File.size(abs_path)
+ # hash file contents
d = OpenSSL::Digest.new("SHA1")
@hash = d.file(abs_path).hexdigest as String
end
diff --git a/src/guff/theme/source-manifest.cr b/src/guff/theme/source-manifest.cr
index 5dc1065..d0f847b 100644
--- a/src/guff/theme/source-manifest.cr
+++ b/src/guff/theme/source-manifest.cr
@@ -1,11 +1,10 @@
class Guff::Theme::SourceManifest
- getter :format, :metadata, :assets, :files
+ getter :format, :metadata, :assets
JSON.mapping(
format: Int32,
metadata: Hash(String, String),
assets: Assets,
- files: Array(String),
)
REQUIRED_METADATA_FIELDS = {
@@ -39,44 +38,18 @@ class Guff::Theme::SourceManifest
end
end
- # build path to files dir
- base_dir = File.join(src_dir, "files")
-
- # check manifest for missing files
- missing_files = r.files.select do |path|
- !File.exists?(File.join(base_dir, path))
- end
-
- # raise error if there are missing files
- if missing_files.size > 0
- raise "Missing files: #{missing_files.join(", ")}"
- end
-
- # build path lut
- lut = r.files.reduce({} of String => Bool) do |rl, path|
- rl[path] = true
- rl
- end
-
# check assets
{
scripts: r.assets.scripts,
styles: r.assets.styles,
}.each do |type, paths|
# find assets specified in manifest but not in
- missing = paths.select { |path| !lut[path]? }
-
- if missing.size > 0
- raise "Missing #{type} in manifest: #{missing.join(", ")}"
- end
- end
+ missing_files = paths.select { |path|
+ !File.exists?(File.join(src_dir, "files", path))
+ }
- # check files directory for entries missing from manifest
- Dir.glob(File.join(base_dir, "**")).each do |path|
- unless File.directory?(path)
- # build relative path
- rel_path = path.gsub(base_dir, "")
- raise "File missing from manifest: #{path}" unless lut[rel_path]?
+ if missing_files.size > 0
+ raise "Missing #{type} in manifest: #{missing_files.join(", ")}"
end
end
@@ -86,7 +59,6 @@ class Guff::Theme::SourceManifest
def to_json(io : IO, src_dir : String)
# build directory paths
- files_dir = File.join(src_dir, "files")
templates_dir = File.join(src_dir, "templates")
# return output json
@@ -98,15 +70,13 @@ class Guff::Theme::SourceManifest
metadata: @metadata,
# theme files
- files: @files.map { |path|
- FileInfo.new(File.join(files_dir, path), path)
+ files: files(File.join(src_dir, "files")).map { |path|
+ FileInfo.new(src_dir, path)
},
# theme templates
- templates: Dir.glob(File.join(templates_dir, "**")).select { |path|
- !File.directory?(path)
- }.reduce({} of String => String) do |r, path|
- r[path.gsub(templates_dir + "/", "")] = File.read(path)
+ templates: files(templates_dir).reduce({} of String => String) do |r, path|
+ r[path] = File.read(File.join(templates_dir, path))
r
end,
@@ -123,7 +93,7 @@ class Guff::Theme::SourceManifest
end)
# add files
- @files.each do |path|
+ files(File.join(src_dir, "files")).each do |path|
# build path
dst_path = File.join("files", path)
@@ -132,4 +102,12 @@ class Guff::Theme::SourceManifest
end
end
end
+
+ private def files(src_dir : String)
+ Dir.glob(File.join(src_dir, "**/*")).select { |path|
+ !File.directory?(path)
+ }.map { |path|
+ path.gsub(src_dir + "/", "")
+ }
+ end
end