summaryrefslogtreecommitdiffhomepage
path: root/lib/Justicar.rb
blob: 3dd73c1804421adf45bb771ce4ee9aa459616648 (plain)
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
99
100
101
102
103
104
105
# frozen_string_literal: true

require_relative "Justicar/version"
require "paggio"
require "opal"
require "ostruct"

class Justicar
  class << self

    def load_preprocessors(dir)
      Dir.each_child(dir) do |full_file_name|
        file_name, extension, _rb = full_file_name.split('.')
        if File.directory?("#{dir}/#{full_file_name}")
          self.load_preprocessors("#{dir}/#{full_file_name}")
        else
          if ['pre'].include? extension
            load "#{dir}/#{full_file_name}"
          end
        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
          # single html and css files
          if ['html', 'css'].include? extension
            File.open("#{dir}/#{full_file_name}", 'r') do |file|
              target[full_file_name] = instance_eval(file.read)
            end
            # does "post processing" and lets you execute custom code depending on preprocessing
          elsif ['post'].include? extension
            File.open("#{dir}/#{full_file_name}", 'r') do |file|
              # use dir and target
              instance_eval(file.read)
            end
          elsif ['js'].include? extension
            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_initialize(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
      build(target_dir, public_dir, hash)
    end

    def build(target_dir, public_dir, hash = self.output)
      hash.each do |key, val|
        if val.is_a? String
          file_name, type, _rb = key.to_s.split('.')
          File.open("#{target_dir}/#{file_name}.#{type}", "w") do |file|
            file.write(val)
          end
        else
          ensure_dir "#{target_dir}/#{key}"
          self.build("#{target_dir}/#{key}", public_dir, hash[key])
        end
      end
    end

    def ensure_dir(dir)
      unless Dir.exist? dir
        FileUtils.mkdir dir
      end
      File.directory? dir
    end

    def output
      @output ||= {}
    end

    def opl
      @opl ||= Opal::Builder.new
    end

    attr_accessor :opl_path

  end

  PreProcessor = OpenStruct.new

end