flex-compiler 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,326 @@
1
+ require "flex-compiler/version"
2
+ require "flex-compiler/compiler_options"
3
+ require "yaml"
4
+
5
+ module FlexCompiler
6
+
7
+ # generates a compc command and execute it.
8
+ #
9
+ # The options argument can be a hash or a yaml file
10
+ # containing the following values:
11
+ # required
12
+ # - flex_home: the folder that contains the flex sdk
13
+ # optional
14
+ # - output_folder: (default: "bin")
15
+ # - output_name: (default: the name of the folder that contains the project)
16
+ # - src_dir: (default: "src")
17
+ # - locale_dir: (default: "locale")
18
+ # - libs: (no default TODO: add default)
19
+ #
20
+ # * *Args* :
21
+ # - +options+ -> either a hash or a string pointing to a yml file.
22
+ # * *Returns* :
23
+ # - the output from compc
24
+ # * *Raises* :
25
+ # - +ArgumentError+ -> if any value is nil or negative
26
+ #
27
+ def self.compile(options = nil)
28
+
29
+
30
+ if( options.nil? )
31
+ options = "flex-compiler-config.yml"
32
+ end
33
+
34
+ if( options.class == "Hash")
35
+ opts = options
36
+ elsif( options.class.to_s == "String" )
37
+ yml = YAML.load_file( options )
38
+ opts = Hash.new
39
+ opts[:flex_home] = yml["flex_home"] unless yml["flex_home"].nil?
40
+ opts[:src_dir] = yml["src_dir"] unless yml["src_dir"].nil?
41
+ opts[:locale_dir] = yml["locale_dir"] unless yml["locale_dir"].nil?
42
+ opts[:output_name] = yml["output_name"] unless yml["output_name"].nil?
43
+ opts[:output_folder] = yml["output_folder"] unless yml["output_folder"].nil?
44
+ opts[:libs] = yml["libs"] unless yml["libs"].nil?
45
+ opts[:test_mode] = yml["test_mode"] unless yml["test_mode"].nil?
46
+ opts[:application] = yml["application"] unless yml["application"].nil?
47
+ opts[:ignore_files] = yml["ignore_files"] unless yml["ignore_files"].nil?
48
+ end
49
+
50
+ generator = CommandGenerator.new( CompilerOptions.new opts )
51
+ command = generator.command
52
+
53
+ puts "-- command --"
54
+ puts command
55
+ puts "-- end command --"
56
+
57
+ if( opts[:test_mode] )
58
+ return
59
+ end
60
+
61
+ result = `#{command}`
62
+ puts result
63
+
64
+ raise "errror executing process" unless $?.to_i == 0
65
+
66
+ end
67
+
68
+
69
+
70
+ class CommandGenerator
71
+
72
+ PLAYER_GLOBAL = "playerglobal"
73
+
74
+ def initialize(options)
75
+ @options = options
76
+ end
77
+
78
+ def command
79
+
80
+ bin = "#{@options.flex_home}\\bin\\"
81
+
82
+ if( application.nil? )
83
+ exe = "#{bin}compc.exe"
84
+ else
85
+ exe = "#{bin}mxmlc.exe"
86
+ end
87
+
88
+ sources = "-source-path #{src_dir}"
89
+ if( File.exists? locale_dir )
90
+ locale_string = "-locale #{discover_locale}"
91
+ include_bundles = "-include-resource-bundles #{discover_rb}"
92
+ sources += " #{locale_dir}/{locale}"
93
+ end
94
+
95
+ args = [
96
+ source_path_overlap,
97
+ locale_string,
98
+ include_bundles,
99
+ framework_library_paths,
100
+ lib_paths,
101
+ sources,
102
+ dump_config,
103
+ output
104
+ ]
105
+
106
+ if( compc? )
107
+ #the ordering is important here:
108
+ args = [include_stylesheets, include_assets, classes ] + args
109
+ else
110
+ args << "#{src_dir}/#{application}.mxml"
111
+ end
112
+
113
+ add_output_folder
114
+
115
+ command = "\"#{exe}\" #{args.join(' ')}"
116
+ command
117
+ end
118
+
119
+ private
120
+
121
+ def add_output_folder
122
+ FileUtils.mkdir_p output_folder unless File.exists? output_folder
123
+ end
124
+
125
+
126
+ def source_path_overlap
127
+ "-allow-source-path-overlap=true"
128
+ end
129
+
130
+ def compc?; application.nil?; end
131
+
132
+ def application; @options.application; end
133
+
134
+ def output
135
+ type = compc? ? "swc" : "swf"
136
+ "-output=\"#{output_folder}/#{output_name}.#{type}\""
137
+ end
138
+
139
+ def output_folder
140
+ @options.output_folder
141
+ end
142
+
143
+ def output_name
144
+ @options.output_name
145
+ end
146
+
147
+ def src_dir
148
+ @options.src_dir
149
+ end
150
+
151
+ def locale_dir
152
+ @options.locale_dir
153
+ end
154
+
155
+ def libs; @options.libs; end
156
+
157
+ def dump_config
158
+ "-dump-config \"#{output_folder}/#{output_name}-config.xml\""
159
+ end
160
+
161
+ def classes
162
+ files = Dir["#{src_dir}/**/*.{as,mxml}"]
163
+
164
+ if files.nil? || files.empty?
165
+ return ""
166
+ end
167
+
168
+ out = "-include-classes "
169
+
170
+ class_array = []
171
+
172
+ files.each{ |f|
173
+ if( !is_ignore_file(f) )
174
+ f.gsub!("src/", "")
175
+ f.gsub!("/", ".")
176
+ f.gsub!(".as", "")
177
+ f.gsub!(".mxml", "")
178
+ class_array << f
179
+ end
180
+ }
181
+
182
+ "#{out} #{class_array.join ' '}"
183
+ end
184
+
185
+ def is_ignore_file( file )
186
+
187
+ ignore_files.each{ |ignore|
188
+ if( file.include? ignore )
189
+ return true
190
+ end
191
+ }
192
+ false
193
+ end
194
+
195
+ def ignore_files; @options.ignore_files; end
196
+
197
+ # create the locale argument by inspecting the contents of the locale folder.
198
+ # @return a locale string - eg: "en_US,de_DE"
199
+ def discover_locale
200
+ locales = Dir["#{locale_dir}/*"]
201
+ locales.map! { |e| File.basename(e) }
202
+ locales.join(" ")
203
+ end
204
+
205
+ def discover_rb
206
+ locales = Dir["#{locale_dir}/*"]
207
+ raise "can't find locales folders" if locales.nil? || locales.length == 0
208
+ props = Dir["#{locales[0]}/*.properties"]
209
+ props.map! { |e| File.basename(e, ".*")}
210
+ props.join(' ')
211
+ end
212
+
213
+ def add_locale_to_source_path
214
+ locales = Dir["#{locale_dir}/*"]
215
+ locales.join(" ")
216
+ end
217
+
218
+ def include_stylesheets
219
+ name_paths = get_name_paths "css", "-include-file"
220
+ if( name_paths.empty? )
221
+ return ""
222
+ end
223
+ name_paths
224
+ end
225
+
226
+ def include_assets
227
+ name_paths = get_name_paths "jpg,jpeg,png,gif", "-include-file"
228
+ if( name_paths.empty? )
229
+ return ""
230
+ end
231
+ name_paths
232
+ end
233
+
234
+ def get_name_paths( types, directive )
235
+
236
+ assets = Dir["#{src_dir}/**/*.{#{types}}"]
237
+
238
+ if assets.nil? || assets.empty?
239
+ return ""
240
+ end
241
+
242
+ name_paths = assets.map{ |e| NamePath.new(src_dir, e)}
243
+
244
+ output = ""
245
+ name_paths.each{ |np| output << "#{directive} #{np.name_path} " }
246
+ output
247
+ end
248
+
249
+ def framework_library_paths
250
+ home = @options.flex_home.gsub("\\", "/")
251
+ frameworks_swcs = Dir["#{home}/frameworks/libs/**/*.swc"]
252
+
253
+ if frameworks_swcs.nil? || frameworks_swcs.empty?
254
+ raise "Error: can't find framework swcs"
255
+ end
256
+
257
+ directive = compc? ? "-external-library-path+=" : "-library-path+="
258
+ output = ""
259
+ frameworks_swcs.each{ |swc|
260
+ if( swc.include? "#{PLAYER_GLOBAL}.swc")
261
+ output << "-external-library-path+=\"#{swc}\" "
262
+ else
263
+ output << "#{directive}\"#{swc}\" "
264
+ end
265
+ }
266
+ output
267
+ end
268
+
269
+ def lib_paths
270
+
271
+ if( libs.nil? || libs.empty? )
272
+ return ""
273
+ end
274
+
275
+ output = ""
276
+ libs.each{ |l|
277
+ if( l.end_with? "swc")
278
+ f = File.open(l)
279
+
280
+ #its a swc add it directly
281
+ output << "-library-path+=\"#{l}\" "
282
+ else
283
+ #its a folder add all the swcs in the folder
284
+ swcs = Dir["#{l}/**/*.swc"]
285
+ swcs.each{ |swc|
286
+ f = File.open(swc)
287
+ output << "-library-path+=\"#{File.expand_path(f)}\" "
288
+ }
289
+ end
290
+ }
291
+ output
292
+ end
293
+ end
294
+
295
+ class NamePath
296
+
297
+ attr_reader :path
298
+
299
+ def initialize(src_dir, path)
300
+ @path = path
301
+ @src_dir = src_dir
302
+ end
303
+
304
+ def name
305
+
306
+ src = starts_with?("/", @src_dir) ? @src_dir[1, @src_dir.length] : @src_dir
307
+ out = path.gsub( "#{src}", "")
308
+ puts "NamePath:name: #{out}, #{path}"
309
+ out
310
+ end
311
+
312
+ def name_path
313
+ "#{name} #{path}"
314
+ end
315
+
316
+ private
317
+ def starts_with?(prefix, str)
318
+ prefix = prefix.to_s
319
+ str[0, prefix.length] == prefix
320
+ end
321
+ end
322
+
323
+
324
+
325
+
326
+ end
@@ -0,0 +1,47 @@
1
+ class CompilerOptions
2
+ # provides a set of defaults
3
+ # a hash of options
4
+ def initialize( options )
5
+ @options = options
6
+ end
7
+
8
+
9
+ # required
10
+ def flex_home
11
+ @options[:flex_home]
12
+ end
13
+
14
+ def output_folder
15
+ @options[:output_folder] || "bin"
16
+ end
17
+
18
+ def output_name
19
+ @options[:output_name] || File.basename(Dir.pwd)
20
+ end
21
+
22
+ def src_dir
23
+ @options[:src_dir] || "src"
24
+ end
25
+
26
+ def locale_dir
27
+ @options[:locale_dir] || "locale"
28
+ end
29
+
30
+ def test_mode
31
+ @options[:test_mode] || false
32
+ end
33
+
34
+ def libs
35
+ @options[:libs] || ["libs"]
36
+ end
37
+
38
+ def application
39
+ @options[:application] || nil
40
+ end
41
+
42
+ def ignore_files
43
+ @options[:ignore_files] || []
44
+ end
45
+
46
+
47
+ end
@@ -0,0 +1,4 @@
1
+ module FlexCompiler
2
+
3
+ VERSION = "0.0.1"
4
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flex-compiler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ed eustace
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-08 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: &23823288 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *23823288
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &23822952 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.6'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *23822952
36
+ description: ..
37
+ email:
38
+ - ed.eustace@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - lib/flex-compiler/compiler_options.rb
44
+ - lib/flex-compiler/version.rb
45
+ - lib/flex-compiler.rb
46
+ homepage: ''
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project: flex-compiler
66
+ rubygems_version: 1.7.2
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: A simple api for using flex sdk command line tools
70
+ test_files: []