Titaniumで久しぶりに遊んでみた.

少し前に

先取り “新” Titanium CLI

という記事を見つけて,昨日再びTitaniumと戯れたのでちょっとしたTipsのご紹介

Titaniumのcli環境の構築

cli環境の構築は「先取り “新” Titanium CLI」を参考に導入しました. ただ僕の知識が不足していてすこし躓いたので補足

インストールは出来たのに"Titanium"コマンドが認識されない

初めは,どうしても動かないんで

alias titanium='/usr/local/share/npm/lib/node_modules/titanium/bin/titanium'

みたいな変なエイリアス切って使っていたんですが,調べてみたらいわゆる「パスが通ってない」状態でした.そこで

export NODE_PATH=/usr/local/lib/node:$PATH
export PATH=/usr/local/share/npm/bin:$PATH

を.zshrcに書き込んだ所ちゃんと認識されました.ココらへんは一度勉強するべきですね

サブコマンドが補完されない

これは普通なのかもしれませんが, create とか build みたいなサブコマンド(?)が TABで補完されませんでした.これでは不便なので zshの補完関数を書いてみました.意外に簡単ですね.

なにこれ便利Σ(゚Д゚;)

compdef ticmd titanium
function ticmd {
  local -a cmds
  if *1;then
    cmds=('build' 'clean' 'config' 'create' 'help' 'info' 'login' 'logout' 'module' 'plugin' 'project' 'sdk' 'setup' 'status')
    describe -t commands "subcommand" cmds
  else
    files
  fi

CLI環境でTitaniumを動かすためのTips

ipadシミュレータを起動させる

何の事はないですが,build コマンドからプロジェクトを指定し Target platformにipadと入力してもERRORがでて起動できません.

titanium build -d project_name/
Titanium Command-Line Interface, version 3.0.9
Copyright (c) 2012, Appcelerator, Inc.  All Rights Reserved.

Please report bugs to http://jira.appcelerator.org/

Target platform [android,iphone,mobileweb]: ipad [ERROR] Invalid platform: ipad

まあ androidiphoneかmobilewebを入力しろって言われてるんですからエラーも当たり前なんですけど(笑)

結論から言うと

titanium build -d project_name -F ipad -p iphone

ipadシミュレータが起動します.

Target Platform というのは android か ios か web かを選べってことらしいです(iphoneって書くから紛らわしいよ(;´Д`) ) なので ipadのシミュレータを起動したい時でもここは iphoneで問題ありません. それとは別に --device-familyというものがあるのでここに ipadと指定します.help見ればすぐわかるんですけどね(笑)

その他のTIps

JSのライブラリをTitaniumで使うときの呼び出し方

昨日 markdownの優秀なJSパーサーである"showdown.js"をどうにか内部で呼び出せないかゴニョゴニョやってたんですが,結局commonJS風のrequire呼び出しを使って

呼び出される側(lib/showdown.js)

exports = {
    get_converter: function() {

<span class="synComment">/*</span>

ここにソースコードをコピペ */

 <span class="synIdentifier">var</span> converter = <span class="synStatement">new</span> Showdown.converter();
 <span class="synStatement">return</span> converter;
      <span class="synIdentifier">}</span>

}

呼び出し側(app.js)

var mk_module = require("lib/showdown");
var converter = mk_module.get_converter();

var html = converter.makeHtml(mk_text);

なふうにオブジェクトを返す感じで書くとソースコードも汚れないし便利かも. 他にいい方法あるのかな...

*1: CURRENT == 2