Class: Creng::FileProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/creng/file_processor.rb

Class Method Summary collapse

Class Method Details

.ActionTemplate(type, title) ⇒ Object

type: browser_action or page_action



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/creng/file_processor.rb', line 117

def self.ActionTemplate type, title

        <<-"...".gsub!(/^ {3}/, '')
   "#{type}": {
"default_icon": {                    
  "19": "images/extension-48x48.png",          
  "38": "images/extension-48x48.png"            
},
"default_title": "#{title}",      
"default_popup": "#{type}.html"       
  }
  ...

end

.changeBackgroundPage(clean_path, background_pool) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/creng/file_processor.rb', line 251

def self.changeBackgroundPage clean_path, background_pool

  background_page_path = "#{clean_path}/build/html/background.html"

  background_page_text = File.read(background_page_path)

  background_page_text = background_page_text.gsub(/\<\/head\>/,"#{background_pool.join(' ')}</head>")

  File.open(background_page_path, 'w') do |f|
      f.write background_page_text
  end

end

.inProjectDirObject



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/creng/file_processor.rb', line 133

def self.inProjectDir

    curdir = Dir.pwd
  
    if (['dev', 'build'] & Dir.entries(curdir).select { |file| File.directory? File.join(curdir, file)}).length == 2
      yield curdir if block_given?
    else
      puts "you must be in project directory"
    end

end

.postDefineObject



109
110
111
112
113
114
# File 'lib/creng/file_processor.rb', line 109

def self.postDefine
  <<-"...".gsub!(/^ {12}/, '')
        }).call(this);
return exports;});
  ...
end

.preDefineObject



101
102
103
104
105
106
# File 'lib/creng/file_processor.rb', line 101

def self.preDefine
  <<-"...".gsub!(/^ {12}/, '')
        define(function(require) { exports = {};
(function() {
  ...
end

.process(clean_path, options, accessible_resources) ⇒ Object



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
# File 'lib/creng/file_processor.rb', line 9

def self.process(clean_path, options, accessible_resources)

   FileUtils.remove_dir "#{clean_path}/build"

   #raising build version
   FileProcessor.raiseDevBuildVersion clean_path

   FileUtils.copy_entry "#{clean_path}/dev", "#{clean_path}/build", false, true

   


   files = Dir["#{clean_path}/dev/js/*.js"]

   files.each do |file|

     #WORKING WITH ALL JS FILES (EXCEPT SUB DIRS)
     text = File.read(file)
     text_hash = Digest::MD5.hexdigest text

     filename = File.basename(file)

     #can be turned off with flag --withdevblock
     if !options[:withdevblock]

       #cutting blocks of code, which marked as "code for development version only"
       text = FileTweaker.cutDevBlock text

     end

     #build without console.* calls
     if options[:noconsole]

       text = FileTweaker.cutDebugMessages text

     end

     exclusions_from_defining = ['process.js', 'daemon.js']

     clean_path_for_regex = clean_path.gsub(/\//, '\/')

     buildpath = file.gsub(Regexp.new("#{clean_path_for_regex}\/dev"), "#{clean_path}/build")

     #if define is allowed
     if !options[:nodefine]
       unless exclusions_from_defining.include? filename
         text = "#{FileProcessor.preDefine}#{text}#{FileProcessor.postDefine}"

       end
     end
     #endof if define allowed

     #only if file text was changed
     if text_hash != (Digest::MD5.hexdigest(text))

         File.open(buildpath, 'w') do |f|
           f.write text
         end

         puts "    processed js/#{filename}"
     end




   end
   #endof files loop

   FileProcessor.processManifest clean_path, accessible_resources do |manifest_text, accessible_resources_new|
     #changing web_accessible_resources array
     manifest_text = manifest_text.gsub(/(\"web_accessible_resources\")\s?\:\s?(\[.*\])/, "\"web_accessible_resources\": [#{accessible_resources_new.join(',')}]")
     #changing background object
     manifest_text = FileProcessor.processBackgroundPage clean_path, manifest_text
     #checking js/vendor dir
     manifest_text = FileProcessor.processFrameworks clean_path, manifest_text
     #checking options page
     manifest_text = FileProcessor.processOptionsPage clean_path, manifest_text
     #checking browser_action or page_action pages
     manifest_text = FileProcessor.processExtensionType clean_path, manifest_text
     #checks for overrided history, bookmarks and new tab pages
     manifest_text = FileProcessor.processOverrideInternalPages clean_path, manifest_text




   end



end

.processBackgroundPage(path, manifest_text) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/creng/file_processor.rb', line 147

def self.processBackgroundPage path, manifest_text

  if File.file? "#{path}/build/html/background.html"
    background_page = "#{path}/build/html/background.html"
    background_persistent = false
  elsif File.file? "#{path}/build/html/background_persistent.html"
    background_page = "#{path}/build/html/background_persistent.html"
    #renaming to default
    File.rename background_page, "#{path}/build/html/background.html"
    background_persistent = true
  else
    background_page = nil
    background_persistent = nil
  end

  unless background_page.nil?
    manifest_text = manifest_text.gsub(/(\"persistent\")\s?\:\s?(true|false)/, "\"persistent\": #{background_persistent}")
  else
    manifest_text = manifest_text.gsub(/(\"background\")\s?\:\s?(\{(.|\n)*\}\,)/, "")
   
  end 
   

    puts "    processed html/background.html"
    manifest_text

end

.processExtensionType(clean_path, manifest_text) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/creng/file_processor.rb', line 282

def self.processExtensionType clean_path, manifest_text

  browser_action_page_path = "#{clean_path}/build/html/browser_action.html"
  page_action_page_path = "#{clean_path}/build/html/page_action.html"


  type = nil

  manifest_text = manifest_text.gsub(/\,?\n?\"page_action\"\s*\:\s*\{(.|\n)*\}\,?/, "")
  manifest_text = manifest_text.gsub(/\,?\n?\"browser_action\"\s*\:\s*\{(.|\n)*\}\,?/, "")

  if File.file? browser_action_page_path
    type = "browser_action"
  elsif File.file? page_action_page_path
    type = "page_action"
  end
    


  unless type.nil?
    page_text = File.read("#{clean_path}/build/html/#{type}.html")

    title = page_text.match(/\<title\>(.*)\<\/title\>/)[1]
    title = title.nil? ? "Action Title" : title
    action_text = FileProcessor.ActionTemplate type, title


    manifest_text = manifest_text.gsub(/(\n){1}(\})$/) { |m| m.gsub!($1, ",\n#{action_text}") } 
  end

  manifest_text

end

.processFrameworks(clean_path, manifest_text) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/creng/file_processor.rb', line 197

def self.processFrameworks clean_path, manifest_text

  background_files = Dir["#{clean_path}/build/js/vendor/background/*.js"]
  content_files = Dir["#{clean_path}/build/js/vendor/content/*.js"]
  both_files = Dir["#{clean_path}/build/js/vendor/*.js"]


  content_pool = ["\"js/process.js\""]
  background_pool = []


  background_files.each do |file|
    if File.file? file
      background_pool.push "<script type='text/javascript' src='js/vendor/#{File.basename file}'></script>"
    end
  end
  FileUtils.mv background_files, "#{clean_path}/build/js/vendor"


  content_files.each do |file|
   if File.file? file
      content_pool.push "\"js/vendor/#{File.basename file}\""
    end
  end
  FileUtils.mv content_files, "#{clean_path}/build/js/vendor"

  both_files.each do |file|
   if File.file? file
      content_pool.push "\"js/vendor/#{File.basename file}\""
      background_pool.push "<script type='text/javascript' src='js/vendor/#{File.basename file}'></script>"
    end
  end

  FileUtils.remove_dir "#{clean_path}/build/js/vendor/content"
  FileUtils.remove_dir "#{clean_path}/build/js/vendor/background"


 #replacing for content scripts loaded frameworks 
if content_pool.length
    manifest_text = manifest_text.gsub(/\"content_scripts\"\s*\:\s*?(?:.|\n)*\"js\"\s?\:\s*(\[(?:.|\n)*?\])/) { |m| m.gsub!($1, "[#{content_pool.join(',')}]") } 
end

#@TODO: background page logic handling (insert script tags into html)
if background_pool.length
  FileProcessor.changeBackgroundPage clean_path, background_pool
end
 


 manifest_text

end

.processManifest(clean_path, accessible_resources) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/creng/file_processor.rb', line 176

def self.processManifest clean_path, accessible_resources

  manifest_text = File.read("#{clean_path}/build/manifest.json")

  accessible_resources_new = []

  accessible_resources.each do |res|
    accessible_resources_new.push("\"#{res}\"")
  end
  
  manifest_text = yield(manifest_text, accessible_resources_new) if block_given?

  File.open("#{clean_path}/build/manifest.json", 'w') do |f|
      f.write manifest_text
  end

  puts "    processed manifest.json"

end

.processOptionsPage(clean_path, manifest_text) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/creng/file_processor.rb', line 266

def self.processOptionsPage clean_path, manifest_text

  options_page_path = "#{clean_path}/build/html/options.html"

  if File.file? options_page_path
    manifest_text = manifest_text.gsub(/\"manifest_version\"\s*\:\s*2\s*\,/, "\"manifest_text\": 2,\n \"options_page\": \"html/options.html\",")
    puts "    processed html/options.html"
  else
    manifest_text = manifest_text.gsub(/\"options_page\"\s*\:\s*\"html\/options\.html\"\,/, "")
    puts "    processed options page removing"
  end


  manifest_text
end

.processOverrideInternalPages(clean_path, manifest_text) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/creng/file_processor.rb', line 317

def self.processOverrideInternalPages clean_path, manifest_text

  override_tab_page = "override_tab.html"
  override_history_page = "override_history.html"
  override_bookmarks_page = "override_bookmarks.html"

  manifest_text = manifest_text.gsub(/\"chrome_url_overrides\"\s*\:\s*\{(.|\n)*\}\,/, "")
  overriden_pages = []


  if File.file? "#{clean_path}/dev/html/#{override_tab_page}"
    overriden_pages.push("\"newtab\": \"#{override_tab_page}\"")
  end

  if File.file? "#{clean_path}/dev/html/#{override_history_page}"
    overriden_pages.push("\"history\": \"#{override_history_page}\"")
  end

  if File.file? "#{clean_path}/dev/html/#{override_bookmarks_page}"
    overriden_pages.push("\"bookmarks\": \"#{override_bookmarks_page}\"")
  end

  if overriden_pages.length > 0
    overriden_pages_list = overriden_pages.join(" ,\n")
    overriden_pages_string = "\"chrome_url_overrides\": {\n#{overriden_pages_list}\n}" 
    manifest_text = manifest_text.gsub(/(\n){1}(\})$/) { |m| m.gsub!($1, ",\n#{overriden_pages_string}\n") }

  end

  puts "    processing overriden pages"

  manifest_text

end

.raiseDevBuildVersion(clean_path) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/creng/file_processor.rb', line 353

def self.raiseDevBuildVersion clean_path

  manifest_dev_path = "#{clean_path}/dev/manifest.json"
  manifest_text = File.read(manifest_dev_path)

  manifest_text = manifest_text.gsub(/\"version\"\s*\:\s*\"(.*)\"\,/) { 

    |text| 
      digits = $1.split '.'
      buildversion = digits[3].to_i + 1
      new_version = "#{digits[0]}.#{digits[1]}.#{digits[2]}.#{buildversion}"
      
      text.gsub($1, "#{new_version}")

  }
  
  File.open(manifest_dev_path, 'w') do |f|
      f.write manifest_text
  end

end