Corresponding repository:
The idea is basically that it creates (from the resources folder) a file archive.cr which contains a Hash literal, which in turns holds the paths as keys and the file content as values.
The aim is embedding resource files in a desktop Webview-based app (in a OS independent manner).
require "path"
require "dir"
require "./archive.cr"
# TODO 1 make it recursive (tree structure)
# TODO 2 replace \n by a portable (OS dependent) newline symbol
# TODO 3 Test in combination with a server.
# TODO 4 Test on Windows.
Root = Dir.new("public")
Root_path = Path.new("public")
Dico = Hash(String, String).new
Arc = "archive.cr"
def to_path(file)
return Root_path.join(file).to_s
end
Root.each_child do |child|
path = Root_path.join(child)
content = File.read(path)
Dico[path.to_s] = content.to_s
end
File.open(Arc, "w") do |file|
file.print("def get_archive()
\n\treturn Hash {\n\n")
end
File.open(Arc, "a") do |file|
Dico.each_key do |key|
token = %{"#{key}" => %{#{Dico[key]}}, \n}
file.print(token)
file.print("\n")
end
end
File.open(Arc, "a") do |file|
file.print("}")
end
File.open(Arc, "a") do |file|
file.print("\n\nend")
end
arc = get_archive()
# arc.each do |item|
# p item
# puts "\n- - -\n"
# end
puts "file = #{arc[to_path("index.html")]}"
the (generated) archive.cr file looks like :
def get_archive()
return Hash {
"public/index.html" => %{<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
Test
</body>
</html>},
"public/two.html" => %{<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
Two
</body>
</html>},
}
end