Make sure to delete the output file in case of an exception.
[redact.git] / lib / redact / app.rb
index de666280863078905bb9af53c69b31c1b9d34338..2cc9dd7cf74b7022948a0d207380bb08127c32ab 100644 (file)
@@ -1,5 +1,5 @@
 #--
-# $Id: app.rb 1 2005-03-26 01:32:38Z tilman $
+# $Id: app.rb 40 2005-05-25 20:19:45Z tilman $
 #
 # Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
 #
@@ -25,6 +25,7 @@
 require "redact/redact.rb"
 require "redact/source.rb"
 require "ftools"
+require "tempfile"
 require "pathname"
 require "ostruct"
 require "optparse"
@@ -65,15 +66,20 @@ module Redact
 
                        amx = compile_embryo
 
-                       Eet::File.open(@options.output, "w") do |ef|
-                               dump_amx(amx, ef)
-
-                               dump_header(ef)
-                               dump_collections(ef)
-                               dump_fonts(ef)
-                               dump_images(ef)
-                               dump_source(ef)
-                               dump_fontmap(ef)
+                       begin
+                               Eet::File.open(@options.output, "w") do |ef|
+                                       dump_amx(amx, ef)
+
+                                       dump_header(ef)
+                                       dump_collections(ef)
+                                       dump_fonts(ef)
+                                       dump_images(ef)
+                                       dump_source(ef)
+                                       dump_fontmap(ef)
+                               end
+                       rescue Exception
+                               File.rm_f(@options.output)
+                               raise
                        end
                end
 
@@ -109,11 +115,7 @@ module Redact
                                end
 
                                o.on_tail("--version", "Show version") do
-                                       puts <<EOT
-redact #{Redact::VERSION}
-
-Copyright (c) 2005 Tilman Sauerbeck (tilman at code-monkey de)
-EOT
+                                       puts "Redact #{Redact::VERSION}"
                                        exit
                                end
 
@@ -127,23 +129,24 @@ EOT
                        EDJE.collections.each_value do |col|
                                next unless col.has_embryo?
 
-                               File.open("/tmp/redact.sma", "w") do |tf|
-                                       tf.puts "#include <edje>"
-                                       dump_sma(tf, col)
-                                       tf.flush
+                               Tempfile.open("redact_col#{col.id}.sma") do |tf_in|
+                                       tf_in.puts "#include <edje>"
+                                       dump_sma(tf_in, col)
+                                       tf_in.flush
 
-                                       out = "/tmp/redact.amx"
-                                       File.rm_f(out)
+                                       Tempfile.open("redact_col#{col.id}.amx") do |tf_out|
+                                               incl = `edje-config --datadir`.strip
 
-                                       incl = `edje-config --datadir`.strip
-                                       c = "embryo_cc " +
-                                           "-i #{incl}/include " +
-                                           "-o #{out} #{tf.path}"
-                                       system(c)
+                                               c = "embryo_cc " +
+                                                   "-i #{incl}/include " +
+                                                   "-o #{tf_out.path} #{tf_in.path}"
+                                               system(c)
+                                               unless (0..1).include?($?.exitstatus)
+                                                       raise("Cannot compile Embryo code")
+                                               end
 
-                                       ret[col.id] = IO.read(out)
-                                       File.rm_f(out)
-                                       File.unlink(tf.path)
+                                               ret[col.id] = tf_out.read
+                                       end
                                end
                        end
 
@@ -209,7 +212,7 @@ EOT
                                method = (sane == @filename) ? :unshift : :push
                                s.send(method, SourceFile.new(sane, value.join))
 
-                               # include files that are with File.read, too
+                               # include files that are read with File.read, too
                                value.join.grep(/File\.read\(\"(.+)\"\)/) do
                                        sane = Pathname.new($1).cleanpath.to_s
                                        s << SourceFile.new(sane, File.read(sane))