require 'tmpdir' module Luruju class JudeApiLoader def self.wrap_load_default_option jude_file, options={} default = {:copy => :if_locked} yield jude_file, default.merge(options) end def self.wrap_load_for_tmpdir jude_file, options={} do_jude_file = options[:copy] == :first ? copy_jude_file(jude_file, options) : jude_file yield do_jude_file, options rescue ProjectLockedException raise $! if do_jude_file != jude_file or options[:copy] != :if_locked or options[:retried] do_jude_file = copy_jude_file jude_file, options load do_jude_file, options.update(:retried => true) ensure FileUtils.remove_entry(File.dirname(do_jude_file), :force => true) if do_jude_file != jude_file end @@tmpdir_no = 0 protected def self.copy_jude_file jude_file, options @@tmpdir_no += 1 tmpdir = options[:tmpdir] || Dir.tmpdir FileUtils.makedirs(tmpdir) unless FileTest.exist? tmpdir current_tmpdir = FileUtils.makedirs File.join(tmpdir,"luruju.#{$$}.#{@@tmpdir_no}") copy_jude_file = File.join current_tmpdir, File.basename(jude_file) FileUtils.cp jude_file, copy_jude_file copy_jude_file end end end