2cc9dd7cf74b7022948a0d207380bb08127c32ab
[redact.git] / lib / redact / app.rb
1 #--
2 # $Id: app.rb 40 2005-05-25 20:19:45Z tilman $
3 #
4 # Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 require "redact/redact.rb"
26 require "redact/source.rb"
27 require "ftools"
28 require "tempfile"
29 require "pathname"
30 require "ostruct"
31 require "optparse"
32
33 SCRIPT_LINES__ = {}
34
35 module Redact
36         class App
37                 def initialize(args)
38                         @filename = nil
39                         @options = OpenStruct.new
40                         @option_parser = parse_args(args)
41
42                         @options.input = args.first
43                         if @options.input.nil?
44                                 puts @option_parser.help
45                                 exit
46                         end
47
48                         @options.output ||= @options.input.sub(/.[^.]+$/, ".edj")
49                 end
50
51                 def run
52                         EDJE.clear
53                         SCRIPT_LINES__.clear
54
55                         begin
56                                 load @options.input
57                         rescue LoadError
58                                 raise("Cannot load '#{@options.input}'")
59                         end
60
61                         @filename = Pathname.new(@options.input).cleanpath.to_s
62
63                         if EDJE.collections.empty?
64                                 raise("No collections found")
65                         end
66
67                         amx = compile_embryo
68
69                         begin
70                                 Eet::File.open(@options.output, "w") do |ef|
71                                         dump_amx(amx, ef)
72
73                                         dump_header(ef)
74                                         dump_collections(ef)
75                                         dump_fonts(ef)
76                                         dump_images(ef)
77                                         dump_source(ef)
78                                         dump_fontmap(ef)
79                                 end
80                         rescue Exception
81                                 File.rm_f(@options.output)
82                                 raise
83                         end
84                 end
85
86                 private
87                 def parse_args(args)
88                         OptionParser.new do |o|
89                                 o.banner = "Usage: redact [options] INPUT_FILE"
90
91                                 o.separator ""
92                                 o.separator "Specific options:"
93
94                                 o.on("-o", "--output OUTPUT_FILE",
95                                      "Write Edje to OUTPUT_FILE") do |file|
96                                         @options.output = file
97                                 end
98
99                                 o.on("--image_dir IMAGE_DIR",
100                                      "Add IMAGE_DIR to the image lookup path") do |dir|
101                                         @options.image_dir = dir
102                                 end
103
104                                 o.on("--font_dir FONT_DIR",
105                                      "Add FONT_DIR to the font lookup path") do |dir|
106                                         @options.font_dir = dir
107                                 end
108
109                                 o.separator ""
110                                 o.separator "Common options:"
111
112                                 o.on_tail("-h", "--help", "Show this message") do
113                                         puts o
114                                         exit
115                                 end
116
117                                 o.on_tail("--version", "Show version") do
118                                         puts "Redact #{Redact::VERSION}"
119                                         exit
120                                 end
121
122                                 o.parse!(args)
123                         end
124                 end
125
126                 def compile_embryo
127                         ret = {}
128
129                         EDJE.collections.each_value do |col|
130                                 next unless col.has_embryo?
131
132                                 Tempfile.open("redact_col#{col.id}.sma") do |tf_in|
133                                         tf_in.puts "#include <edje>"
134                                         dump_sma(tf_in, col)
135                                         tf_in.flush
136
137                                         Tempfile.open("redact_col#{col.id}.amx") do |tf_out|
138                                                 incl = `edje-config --datadir`.strip
139
140                                                 c = "embryo_cc " +
141                                                     "-i #{incl}/include " +
142                                                     "-o #{tf_out.path} #{tf_in.path}"
143                                                 system(c)
144                                                 unless (0..1).include?($?.exitstatus)
145                                                         raise("Cannot compile Embryo code")
146                                                 end
147
148                                                 ret[col.id] = tf_out.read
149                                         end
150                                 end
151                         end
152
153                         ret
154                 end
155
156                 def dump_sma(tf, col)
157                         if col.has_embryo?(false)
158                                 tf.puts col.script.to_embryo(col)
159                         end
160
161                         col.programs.each_value do |p|
162                                 next unless p.is_a?(ExecScriptProgram)
163
164                                 s = p.script.to_embryo(col).gsub(/^(.+)$/, "\t\\1")
165
166                                 tf.puts <<EOT
167 public _p#{p.id}(sig[], src[])
168 {
169 #{s}
170 }
171 EOT
172                         end
173                 end
174
175                 def dump_amx(amx, ef)
176                         amx.each do |id, code|
177                                 ef.write("scripts/#{id}", code)
178                         end
179                 end
180
181                 def dump_header(ef)
182                         ef.write("edje_file", EDJE.to_eet)
183                 end
184
185                 def dump_collections(ef)
186                         EDJE.collections.each_value do |col|
187                                 ef.write("collections/#{col.id}", col.to_eet)
188                         end
189                 end
190
191                 def dump_fonts(ef)
192                         EDJE.font_dir.each do |entry|
193                                 ef.write("fonts/#{entry.filename}",
194                                          File.read(entry.filename))
195                         end
196                 end
197
198                 def dump_images(ef)
199                         EDJE.image_dir.each do |entry|
200                                 ef.write_image("images/#{entry.id}",
201                                                entry.image.data_for_reading_only,
202                                                entry.image.width, entry.image.height,
203                                                entry.image.has_alpha?)
204                         end
205                 end
206
207                 def dump_source(ef)
208                         s = SourceFiles.new
209
210                         SCRIPT_LINES__.each do |file, value|
211                                 sane = Pathname.new(file).cleanpath.to_s
212                                 method = (sane == @filename) ? :unshift : :push
213                                 s.send(method, SourceFile.new(sane, value.join))
214
215                                 # include files that are read with File.read, too
216                                 value.join.grep(/File\.read\(\"(.+)\"\)/) do
217                                         sane = Pathname.new($1).cleanpath.to_s
218                                         s << SourceFile.new(sane, File.read(sane))
219                                 end
220                         end
221
222                         ef.write("edje_sources", s.to_eet)
223                 end
224
225                 def dump_fontmap(ef)
226                         fm = EDJE.font_dir.inject(FontMap.new) do |a, entry|
227                                 a << FontMapEntry.new(entry)
228                         end
229
230                         ef.write("edje_source_fontmap", fm.to_eet)
231                 end
232         end
233 end