Use Tempfile's for the embryo source- and amx code.
[redact.git] / lib / redact / app.rb
1 #--
2 # $Id: app.rb 2 2005-03-26 19:16:37Z 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                         Eet::File.open(@options.output, "w") do |ef|
70                                 dump_amx(amx, ef)
71
72                                 dump_header(ef)
73                                 dump_collections(ef)
74                                 dump_fonts(ef)
75                                 dump_images(ef)
76                                 dump_source(ef)
77                                 dump_fontmap(ef)
78                         end
79                 end
80
81                 private
82                 def parse_args(args)
83                         OptionParser.new do |o|
84                                 o.banner = "Usage: redact [options] INPUT_FILE"
85
86                                 o.separator ""
87                                 o.separator "Specific options:"
88
89                                 o.on("-o", "--output OUTPUT_FILE",
90                                      "Write Edje to OUTPUT_FILE") do |file|
91                                         @options.output = file
92                                 end
93
94                                 o.on("--image_dir IMAGE_DIR",
95                                      "Add IMAGE_DIR to the image lookup path") do |dir|
96                                         @options.image_dir = dir
97                                 end
98
99                                 o.on("--font_dir FONT_DIR",
100                                      "Add FONT_DIR to the font lookup path") do |dir|
101                                         @options.font_dir = dir
102                                 end
103
104                                 o.separator ""
105                                 o.separator "Common options:"
106
107                                 o.on_tail("-h", "--help", "Show this message") do
108                                         puts o
109                                         exit
110                                 end
111
112                                 o.on_tail("--version", "Show version") do
113                                         puts <<EOT
114 redact #{Redact::VERSION}
115
116 Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
117 EOT
118                                         exit
119                                 end
120
121                                 o.parse!(args)
122                         end
123                 end
124
125                 def compile_embryo
126                         ret = {}
127
128                         EDJE.collections.each_value do |col|
129                                 next unless col.has_embryo?
130
131                                 Tempfile.open("redact_col#{col.id}.sma") do |tf_in|
132                                         tf_in.puts "#include <edje>"
133                                         dump_sma(tf_in, col)
134                                         tf_in.flush
135
136                                         Tempfile.open("redact_col#{col.id}.amx") do |tf_out|
137                                                 incl = `edje-config --datadir`.strip
138
139                                                 c = "embryo_cc " +
140                                                     "-i #{incl}/include " +
141                                                     "-o #{tf_out.path} #{tf_in.path}"
142                                                 system(c)
143
144                                                 ret[col.id] = tf_out.read
145                                         end
146                                 end
147                         end
148
149                         ret
150                 end
151
152                 def dump_sma(tf, col)
153                         if col.has_embryo?(false)
154                                 tf.puts col.script.to_embryo(col)
155                         end
156
157                         col.programs.each_value do |p|
158                                 next unless p.is_a?(ExecScriptProgram)
159
160                                 s = p.script.to_embryo(col).gsub(/^(.+)$/, "\t\\1")
161
162                                 tf.puts <<EOT
163 public _p#{p.id}(sig[], src[])
164 {
165 #{s}
166 }
167 EOT
168                         end
169                 end
170
171                 def dump_amx(amx, ef)
172                         amx.each do |id, code|
173                                 ef.write("scripts/#{id}", code)
174                         end
175                 end
176
177                 def dump_header(ef)
178                         ef.write("edje_file", EDJE.to_eet)
179                 end
180
181                 def dump_collections(ef)
182                         EDJE.collections.each_value do |col|
183                                 ef.write("collections/#{col.id}", col.to_eet)
184                         end
185                 end
186
187                 def dump_fonts(ef)
188                         EDJE.font_dir.each do |entry|
189                                 ef.write("fonts/#{entry.filename}",
190                                          File.read(entry.filename))
191                         end
192                 end
193
194                 def dump_images(ef)
195                         EDJE.image_dir.each do |entry|
196                                 ef.write_image("images/#{entry.id}",
197                                                entry.image.data_for_reading_only,
198                                                entry.image.width, entry.image.height,
199                                                entry.image.has_alpha?)
200                         end
201                 end
202
203                 def dump_source(ef)
204                         s = SourceFiles.new
205
206                         SCRIPT_LINES__.each do |file, value|
207                                 sane = Pathname.new(file).cleanpath.to_s
208                                 method = (sane == @filename) ? :unshift : :push
209                                 s.send(method, SourceFile.new(sane, value.join))
210
211                                 # include files that are with File.read, too
212                                 value.join.grep(/File\.read\(\"(.+)\"\)/) do
213                                         sane = Pathname.new($1).cleanpath.to_s
214                                         s << SourceFile.new(sane, File.read(sane))
215                                 end
216                         end
217
218                         ef.write("edje_sources", s.to_eet)
219                 end
220
221                 def dump_fontmap(ef)
222                         fm = EDJE.font_dir.inject(FontMap.new) do |a, entry|
223                                 a << FontMapEntry.new(entry)
224                         end
225
226                         ef.write("edje_source_fontmap", fm.to_eet)
227                 end
228         end
229 end