Create  Edit  Diff  FrontPage  Index  Search  Changes  Login

rb2exe.hta

<?xml version="1.0" encoding="shift_JIS"?>
<html>
<head>
<title>Rb2Exe</title>
<script type="text/RubyScript">
require 'stringio'
require 'dl/win32'
require 'exerb/config'
require 'exerb/utility'
require 'exerb/version'

WINDOW_WIDTH = 680
WINDOW_HEIGHT = 460
WSH = WIN32OLE.new('WScript.Shell')

def run
  script = elem('script').value
  return if check_script(script)
  WSH.currentDirectory = File.dirname(script)
  elem('runbutton').disabled = true
  cmd = "\"#{RUBY_DIRECTORY}\\#{selected('command')}.bat\" #{make_options} #{File.basename(script)}"
  cmd << " #{elem('params').value}" if mkexy?
  execute(cmd, File.basename(script, '.*'))
end

def init
  elem('info').innerHTML = version_html
  init_core(Exerb::CORE_NAME.keys.sort.select do |core|
    Exerb::Utility.find_core_by_name(core)
  end)
  Window.resizeTo WINDOW_WIDTH, WINDOW_HEIGHT
  #Window.moveTo(100, 0)
end

def gen_exe(exy)
  execute("\"#{RUBY_DIRECTORY}\\exerb.bat\" #{exy}")
end

private

def execute(cmd, file = nil)
  exec = WSH.Exec(cmd)
  out = ''
  err = ''
  timer = Window.setInterval(
    Proc.new do
      out << exec.stdOut.readAll
      err << exec.stdErr.readAll
      unless exec.status == 0 # 0: job is still running
        Window.clearInterval(timer)
        elem('runbutton').disabled = false
        elem('result').innerHTML = create_html(out.strip, err.strip, file)
        vanish false
      end
    end, 100)
end

def write_result(io, title, data)
  io << '<h3>'
  io << title
  io << '</h3>'
  return if data.size == 0
  io << "<p><pre>\n"
  io << data
  io << "\n</pre></p>\n"
end

def add_exerb(io, fbase)
  File.open("#{fbase}.exy", 'r') do |f|
    write_result(io, "#{fbase}.exy", f.read)
  end
  io << '<div style="text-align: center;">'
  io <<  "<input type=\"button\" onclick=\"gen_exe(\'#{fbase}.exy\');\" value=\"Exerb\">"
  io << '</div>'
end

def create_html(out, err, fbase = nil)
  io = StringIO.new(ret = '')
  if out.size == 0 && err.size == 0
    write_result(io, "#{(mkexy? && fbase) ? 'mkexy' : 'exerb'} success !", '')
  else
    if out.size > 0
      write_result(io, '出力内容', out)
    end
    if err.size > 0
      write_result(io, 'エラー', err)
    end
  end
  if err.size == 0 && fbase && mkexy?
    add_exerb(io, fbase)
  end
  io << '<p>---- click here ----</p>'
  io.close
  ret
end

def version_html
  "Ruby #{RUBY_VERSION}<br/>Exerb #{Exerb::VERSION}"
end

def init_core(cores)
  cores.each do |c|
    opt = Window.document.createElement('OPTION')
    elem('core').add opt
    opt.innerText = c
    opt.value = c
  end
end

def check_script(script)
  if !script || script.size == 0
    msg = 'スクリプトファイルを選択してください'
  elsif !File.exist?(script)
    msg = "#{script}が見つかりません"
  end
  if msg
    Window.alert msg
  end
  msg
end

def make_options
  if mkexy?
    "-K#{selected('kcode')[0..0]}"
  else
    "-c#{selected('core')} -k#{selected('kcode')}"
  end
end

def toggle_param
  [['coreparam', false], ['cmdparams', true]].each do |id, mkexy|
    elem(id).style.display = (mkexy? == mkexy) ? 'inline' : 'none'
  end
end

def elem(id)
  Window.document.getElementById(id)
end

def selected(id)
  elem(id).options.item(elem(id).selectedIndex).text
end

def mkexy?
  selected('command') == 'mkexy'
end

def vanish(result)
  [['result', true ], ['form', false], ['info', false]].each do |id, rx|
    elem(id).style.display = (result == rx) ? 'none' : 'inline'
  end
end

def ruby_path
  get_module_handle = Win32API.new('Kernel32.dll', 'GetModuleHandleA', 'P', 'L')
  hmodule = get_module_handle.call('msvcrt-ruby18.dll')
  get_module_filename = Win32API.new('Kernel32.dll',
    'GetModuleFileNameA', 'LPL', 'L')
  buff = "\0" * 256
  len = get_module_filename.call(hmodule, buff, 256)
  if len == 256
    Window.alert('bad direcotry name')
    return nil
  end
  File.dirname(buff.rstrip)
end

RUBY_DIRECTORY = ruby_path

</script>
<style type="text/css">
body {
  background-color: #b0e0e6;
}
div {
  padding-top: 1em;
}
.options {
  margin-right: 1em;
}
span#cmdparams {
  margin-top: 1em;
  width: 100%;
}
div#run {
  margin-top: 1em;
  margin-bottom: 0.5em;
  margin-left: 30%;
  margin-right: 30%;
  text-align: center;
}
.button-separator {
  width: 5%;
}
div#info {
  position: absolute;
  bottom: 0px;
  width: 100%;
  border-top: groove ;
  text-align: right;
}
div#result {
  background-color: #ffe4c4;
  position: absolute;
  top: 10;
  left: 10;
  width: 90%;
  display: none;
}
</style>
</head>
<body scroll="auto" onload="init();">
 <form id="form">
  <div id="target">
   <label for="script">スクリプトファイル</label>
   <input id="script" type="file" size="64"/>
  </div>
  <div id="options">
   <span class="options">
    <label for="command">コマンド</label>
    <select id="command" value="mkexy" onchange="toggle_param();">
     <option>mkexy</option>
     <option>exerb</option>
    </select>
   </span>
   <span class="options">
    <label for="kcode">文字コード</label>
    <select id="kcode" value="sjis">
     <option>sjis</option>
     <option>utf8</option>
     <option>euc</option>
     <option>none</option>
    </select>
   </span>
   <span id="coreparam" class="options" style="display: none;">
    <label for="core">コア</label>
    <select id="core">
    </select>
   </span>
   <span id="cmdparams" style="display: inline;">
    <label for="params">スクリプト実行パラメータ</label>
    <input id="params" type="input" size="80"/>
   </span>
  </div>
  <div id="run">
   <input id="runbutton" type="button" onclick="run" value="実行"/>
   <span class="button-separator">&nbsp;</span>
   <input type="reset"/>
  </div>
 </form>
 <div id="result" onclick="vanish(true);"></div>
 <div id="info"/>
</body>
</html>
Last modified:2006/07/17 01:47:35
Keyword(s):
References:[Rb2Exe]