summaryrefslogtreecommitdiffhomepage
path: root/lib/Justicar.rb
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2022-04-21 07:55:45 -0400
committerrealtradam <[email protected]>2022-04-21 07:55:45 -0400
commit8c0569bf34405dfdf0968b26f33dd70976866943 (patch)
tree321cc6e1b0840b0eebf84523dcd6486cabaab3c6 /lib/Justicar.rb
downloadJusticar-8c0569bf34405dfdf0968b26f33dd70976866943.tar.gz
Justicar-8c0569bf34405dfdf0968b26f33dd70976866943.zip
initv0.1.0
Diffstat (limited to 'lib/Justicar.rb')
-rw-r--r--lib/Justicar.rb86
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/Justicar.rb b/lib/Justicar.rb
new file mode 100644
index 0000000..e57845e
--- /dev/null
+++ b/lib/Justicar.rb
@@ -0,0 +1,86 @@
+# frozen_string_literal: true
+
+require_relative "Justicar/version"
+require "paggio"
+require "opal"
+
+class Justicar
+ class << self
+
+ def load_templates(dir)
+ Dir.each_child(dir) do |file|
+ if File.directory? "#{dir}/#{file}"
+ self.load_templates "#{dir}/#{file}"
+ else
+ load "#{dir}/#{file}"
+ end
+ end
+ end
+
+ def build_source(dir, target = self.output)
+
+ # only add root path
+ unless self.opl_path
+ opl_path = true
+ opl.append_paths dir
+ end
+
+ Dir.each_child(dir) do |full_file_name|
+ file_name, extension, _rb = full_file_name.split('.')
+ if File.directory?("#{dir}/#{full_file_name}")
+ target[full_file_name] = {}
+ self.build_source("#{dir}/#{full_file_name}", target[full_file_name])
+ else
+ if ['html', 'css'].include? extension
+ File.open("#{dir}/#{full_file_name}", 'r') do |file|
+ target[full_file_name] = instance_eval(file.read)
+ end
+ elsif extension == 'js'
+ opl_file, _dot, _extension = full_file_name.partition('.')
+ opl_file = "#{"#{dir}/".partition('/').last}#{opl_file}"
+ target[full_file_name] = opl.build(opl_file).to_s
+ end
+ end
+ end
+ end
+
+ def build(target_dir, public_dir, hash = self.output)
+ if Dir.exist? target_dir
+ FileUtils.rm_r target_dir
+ end
+ if Dir.exist? public_dir
+ FileUtils.copy_entry(public_dir, target_dir)
+ else
+ FileUtils.mkdir target_dir
+ end
+ hash.each do |key, val|
+ puts "key: #{key}, val: #{val}"
+ if val.is_a? String
+ file_name, type, _rb = key.to_s.split('.')
+ puts "filename: #{file_name}, type: #{type}"
+ File.open("#{target_dir}/#{file_name}.#{type}", "w") do |file|
+ file.write(val)
+ end
+ else
+ unless Dir.exist? "#{target_dir}/#{key}"
+ FileUtils.mkdir "#{target_dir}/#{key}"
+ end
+ self.build("#{target_dir}/#{key}", hash)
+ end
+ end
+ puts File.expand_path(File.dirname(__FILE__))
+ end
+
+ def output
+ @output ||= {}
+ end
+
+ def opl
+ @opl ||= Opal::Builder.new
+ end
+
+ attr_accessor :opl_path
+
+ end
+
+end