Module: Drakkon::Gems::Bundle

Defined in:
lib/drakkon/gem/bundle.rb

Overview

Run Command for CLI

Class Method Summary collapse

Class Method Details

.build!(args, context) ⇒ Object



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
# File 'lib/drakkon/gem/bundle.rb', line 5

def self.build!(args, context)
  # Gems are not required
  # return if Settings.gems.empty?

  @context = context

  if Settings.config[:bundle_digest] == digest && !args.include?('bundle') && File.exist?(bundle_file)
    LogBot.info('Gems Bundle', 'Nothing New')
    return unless args.include?('bundle')
  else
    LogBot.info('Gems Bundle', 'Bundling')
  end

  idx = collect

  if Settings.bundle_compile?
    LogBot.info('Gems Bundle', 'Compile')
    compiled_bundle(idx)
  else
    LogBot.info('Gems Bundle', 'Expand')
    expand_bundle(idx)
  end

  Settings.update(:bundle_digest, digest)
rescue TTY::Reader::InputInterrupt
  exit 0
end

.bundle_fileObject

General Helpers



164
165
166
# File 'lib/drakkon/gem/bundle.rb', line 164

def self.bundle_file
  "#{drakkon_dir}/bundle.rb"
end

.collectObject



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/drakkon/gem/bundle.rb', line 109

def self.collect
  idx = {}
  Settings.gems.each do |gem_name, gem|
    gem[:name] = gem_name.to_s.downcase
    case gem[:source].to_sym
    when :local then collect_local_source(idx, gem)
    else
      LogBot.fatal('Bundle', "Invalid Source: #{gem[:source].pastel(:red)}")
    end
  end

  idx
end

.collect_local_source(idx, gem) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/drakkon/gem/bundle.rb', line 123

def self.collect_local_source(idx, gem)
  path = gem[:data][:path]

  # Modules
  if gem[:data].key?(:modules)
    gem[:data][:modules].each do |mod|
      idx[mod.weight] ||= []
      mod.files.each do |file|
        file_name = "#{path}/#{file}.rb"
        unless File.exist?(file_name)
          LogBot.fatal('Bundle',
                       "Module File not found #{mod.name.pastel(:bright_blue)}, #{file_name.pastel(:red)}")
          next
        end

        # Convert full file path into single string / relative naming (no directories)
        safe_name = "#{gem.name}_#{file.gsub('/', '_')}"

        idx[mod.weight].push(path: file_name, name: file, safe_name: safe_name)
      end
    end
  end

  # Files
  if gem[:data].key?(:files)
    gem[:data][:files].each_value do |f|
      idx[f.weight] ||= []
      # idx[f.weight].push "#{path}/#{f.file}"
      safe_name = "#{gem.name}_#{f.file.gsub('/', '_')}"

      idx[f.weight].push(path: "#{path}/#{f.file}", name: f.file, safe_name: safe_name)
    end

  end

  # Return
end

.compiled_bundle(idx) ⇒ Object



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
106
107
# File 'lib/drakkon/gem/bundle.rb', line 74

def self.compiled_bundle(idx)
  # Always clear to ensure fresh directory
  FileUtils.rm_rf(unbundle_dir) if File.directory?(unbundle_dir)

  # Make Directory `app/drakkon` if it doesn't exist
  FileUtils.mkdir_p('app/drakkon') unless File.directory?('app/drakkon')

  # Touch the file if it doesn't exist
  FileUtils.touch(bundle_file) unless File.exist?(bundle_file)

  File.open(bundle_file, 'w') do |f|
    # Index Writer
    idx.keys.sort.reverse.each do |i|
      idx[i].each do |file_data|
        file = file_data[:path]

        unless File.exist?(file)
          LogBot.fatal('Bundle', "File not found #{file.pastel(:red)}")
          next
        end
        f << File.read(file)
        f << "\n"
      end
    end

    # Include other files
    f << "require 'app/drakkon/image_index.rb' \n" if Settings.image_index?
    f << "require 'app/drakkon/font_index.rb' \n" if Settings.font_index?
    f << "require 'app/drakkon/sound_index.rb' \n" if Settings.sound_index?
    f << "require 'app/drakkon/manifest.rb' \n" if Settings.manifest?

    # File Close
  end
end

.digestObject



181
182
183
# File 'lib/drakkon/gem/bundle.rb', line 181

def self.digest
  Digest::MD5.hexdigest Settings.gems.to_s
end

.drakkon_dirObject



168
169
170
# File 'lib/drakkon/gem/bundle.rb', line 168

def self.drakkon_dir
  "#{@context}/app/drakkon"
end

.expand_bundle(idx) ⇒ Object

Including files without merging them



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
# File 'lib/drakkon/gem/bundle.rb', line 34

def self.expand_bundle(idx)
  # Make Directory `app/drakkon` if it doesn't exist
  FileUtils.mkdir_p('app/drakkon') unless File.directory?('app/drakkon')

  # Create Sub Directory to make switching to compile / eaiser
  # Always clear to ensure fresh directory
  FileUtils.rm_rf(unbundle_dir) if File.directory?(unbundle_dir)
  FileUtils.mkdir_p(unbundle_dir)

  # Touch the file if it doesn't exist
  FileUtils.touch(bundle_file) unless File.exist?(bundle_file)

  File.open(bundle_file, 'w') do |f|
    idx.keys.sort.reverse.each do |i|
      idx[i].each do |file_data|
        file = file_data[:path]
        unless File.exist?(file)
          LogBot.fatal('Bundle', "File not found #{file.pastel(:red)}")
          next
        end

        file_path = "#{unbundle_dir}/#{File.basename(file_data[:safe_name])}.rb"

        FileUtils.cp(file, file_path)
        f << "require '#{file_path}'\n"
      end
    end

    # Include other files
    f << "require 'app/drakkon/image_index.rb' \n" if Settings.image_index?
    f << "require 'app/drakkon/font_index.rb' \n" if Settings.font_index?
    f << "require 'app/drakkon/sound_index.rb' \n" if Settings.sound_index?
    f << "require 'app/drakkon/manifest.rb' \n" if Settings.manifest?

    # File Close
  end

  # FileUtils.cp(bundle_file, "#{drakkon_dir}/bundle/#{Time.now.to_i}.rb")
end

.promptObject



177
178
179
# File 'lib/drakkon/gem/bundle.rb', line 177

def self.prompt
  TTY::Prompt.new(active_color: :cyan, interrupt: :exit)
end

.unbundle_dirObject

Directory for uncompiled files / Relative



173
174
175
# File 'lib/drakkon/gem/bundle.rb', line 173

def self.unbundle_dir
  'app/drakkon/unbundle'
end