Class: Creng::FileGenerator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(projectname) ⇒ FileGenerator



9
10
11
12
13
# File 'lib/creng/file_generator.rb', line 9

def initialize projectname
  
  @projectname = projectname
  generate
end

Class Method Details

.generateWebrequest(projectpath) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/creng/file_generator.rb', line 226

def self.generateWebrequest projectpath

  requestInspectorFile = RequestInspectorJS.new

  unless File.file? "#{projectpath}/dev/#{requestInspectorFile.path}/#{requestInspectorFile.name}"

    File.open("#{projectpath}/dev/#{requestInspectorFile.path}/#{requestInspectorFile.name}", 'w') do |f|
      f.write requestInspectorFile.contents
  end
  puts "    create #{projectpath}/dev/#{requestInspectorFile.path}/#{requestInspectorFile.name}"

 else
  puts "    requestInspector already exists"
 end

end

.getAccessibleResources(projectname = nil) ⇒ Object



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

def self.getAccessibleResources projectname = nil

  if projectname.nil?
    allfiles = Dir["build/**/*"]
    regex = Regexp.new("^build\/")
  else
    allfiles = Dir["#{projectname}/build/**/*"]
    regex = Regexp.new("^#{projectname}\/build\/")
  end

  #allfiles = projectname.nil? ? Dir["dev/**/*"] : Dir["#{projectname}/dev/**/*"] 



  #puts "allfiles #{allfiles}, #{projectname}"
  accessible_resources = []

  allfiles.each do |file|
    unless File.directory? file
      if File.basename(file) != 'manifest.json'
        accessible_resources.push(file.gsub(regex, ""))
      end
    end
  end

  accessible_resources
end

Instance Method Details

#fetchImagesObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/creng/file_generator.rb', line 177

def fetchImages
  #fetching sample extension icons from github
  File.open("#{@projectname}/dev/images/extension-16x16.png", 'w') do |f|
  open("https://raw.github.com/traa/creng/master/lib/creng/vendor/images/extension-16x16.png", 'rb') do |read_file|
      f.write(read_file.read)
  end
  end
  puts "   fetching extension icon 16x16 from https://github.com/traa/creng"

  File.open("#{@projectname}/dev/images/extension-48x48.png", 'w') do |f|
  open("https://raw.github.com/traa/creng/master/lib/creng/vendor/images/extension-48x48.png", 'rb') do |read_file|
      f.write(read_file.read)
  end
  end
  puts "   fetching extension icon 48x48 from https://github.com/traa/creng"

  File.open("#{@projectname}/dev/images/extension-64x64.png", 'w') do |f|
  open("https://raw.github.com/traa/creng/master/lib/creng/vendor/images/extension-64x64.png", 'rb') do |read_file|
      f.write(read_file.read)
  end
  end
  puts "   fetching extension icon 64x64 from https://github.com/traa/creng"
end

#fetchLibsObject



121
122
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/creng/file_generator.rb', line 121

def fetchLibs

  require 'open-uri'

  #####################
  # Cajon
  #####################
  File.open("#{@projectname}/dev/js/vendor/content/cajon.js", 'w') do |f|

  open("https://raw.github.com/requirejs/cajon/master/cajon.js", 'rb') do |read_file|
      f.write(read_file.read)
  end

  end
  puts "   fetching cajon.js from https://github.com/requirejs/cajon"

  #####################
  # Jquery
  #####################
  File.open("#{@projectname}/dev/js/vendor/content/jquery.min.js", 'w') do |f|

  open("http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js", 'rb') do |read_file|
      f.write(read_file.read)
  end

  end
  puts "   fetching jQuery from Google CDN"

  #####################
  # Underscore
  #####################
  File.open("#{@projectname}/dev/js/vendor/underscore.min.js", 'w') do |f|

  open("https://raw.github.com/documentcloud/underscore/master/underscore-min.js", 'rb') do |read_file|
      f.write(read_file.read)
  end

  end
  puts "   fetching underscore.js from https://github.com/documentcloud/underscore"

  #####################
  # RequireJS
  #####################
  File.open("#{@projectname}/dev/js/vendor/background/require.js", 'w') do |f|

  open("https://raw.github.com/jrburke/requirejs/master/require.js", 'rb') do |read_file|
      f.write(read_file.read)
  end

  end
  puts "   fetching requirejs from https://github.com/jrburke/requirejs"


end

#generateObject

point of enter



92
93
94
95
96
97
98
99
100
101
# File 'lib/creng/file_generator.rb', line 92

def generate
  
generateJS([ProcessJS.new, ContentJS.new, BackgroundJS.new, DaemonJS.new])

fetchLibs
generateHTML
fetchImages
generateManifest
puts "    create #{@projectname}/dev/manifest.json"
end

#generateHTMLObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/creng/file_generator.rb', line 202

def generateHTML

  File.open("#{@projectname}/dev/html/background_persistent.html", 'w') do |f|

    f.write "                 <!doctype html>\n                 <html>\n                 <head>\n</head>\n\n<body>\n  <center><h1>BACKGROUND PAGE</h1></center>\n</body>\n\n\n</html>\n    ...\n\n  end\n  puts \"   create \#{@projectname}/dev/html/background_persistent.html\"\n\nend\n".gsub!(/^ {20}/, '')

#generateJS(filearray) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/creng/file_generator.rb', line 104

def generateJS filearray

  filearray.each do |file|

    File.open("#{@projectname}/dev/#{file.path}/#{file.name}", 'w') do |f|
      f.write file.contents
    end
    puts "    create #{@projectname}/dev/#{file.path}/#{file.name}"
    
  end




end

#generateManifestObject



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

def generateManifest

  accessible_resources = FileGenerator.getAccessibleResources(@projectname)

File.open("#{@projectname}/dev/manifest.json", 'w') do |f|
       f.write "                 {\n  \"name\": \"\#{@projectname}\",\n  \"version\": \"0.0.1.0\",\n  \"manifest_version\": 2,\n  \"description\": \"Sample extension generated by Creng ruby gem\",\n  \"icons\": {\n \"16\": \"images/extension-16x16.png\",\n \"48\": \"images/extension-48x48.png\",\n \"128\": \"images/extension-64x64.png\"\n  },\n  \"background\": {\n \"page\" : \"html/background.html\",\n \"persistent\": true\n  },\n  \"permissions\": [\n \"webRequest\",\n \"webRequestBlocking\",\n \"tabs\",\n \"*://*/*\"\n  ],\n  \"content_scripts\": [\n {\n   \"matches\": [\"*://*/*\"],\n   \"js\": [\n     \"js/vendor/cajon.js\",\n     \"js/vendor/jquery.min.js\",\n     \"js/vendor/underscore.min.js\",\n     \"js/process.js\"\n   ],\n   \"run_at\": \"document_start\"\n }\n  ],\n  \"web_accessible_resources\": \#{accessible_resources}\n}\n".gsub!(/^ {20}/, '')
      end

end