`
rensanning
  • 浏览: 3520899 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
Efef1dba-f7dd-3931-8a61-8e1c76c3e39f
使用Titanium Mo...
浏览量:37635
Bbab2146-6e1d-3c50-acd6-c8bae29e307d
Cordova 3.x入门...
浏览量:604849
C08766e7-8a33-3f9b-9155-654af05c3484
常用Java开源Libra...
浏览量:678751
77063fb3-0ee7-3bfa-9c72-2a0234ebf83e
搭建 CentOS 6 服...
浏览量:87712
E40e5e76-1f3b-398e-b6a6-dc9cfbb38156
Spring Boot 入...
浏览量:400201
Abe39461-b089-344f-99fa-cdfbddea0e18
基于Spring Secu...
浏览量:69168
66a41a70-fdf0-3dc9-aa31-19b7e8b24672
MQTT入门
浏览量:90727
社区版块
存档分类
最新评论

Titanium plugin开发初探

阅读更多
这里要说的是Plugin,而不是Module,这是两个不同的东西!

在Titanium Mobile 的Kitchen Sink Demo中,可以发现有一个叫做plugins的文件夹,其中在子文件夹 ti.log 中,有一个名字为plugin.py的Python脚本文件。

plugins/ti.log/plugin.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# example compiler plugin
# the config object is a set of properties
# that are passed (dependent on platform)
# that will allow you to hook into the compiler tooling
# 


def compile(config):
	print "[INFO] Compiler plugin loaded and working for %s" % config['platform']


试着去查找Appcelerator的各种公开文档,目前还没有对于plugin开发的特别说明,基本上就没有提到过这个东西。但是庆幸的是,我们可以在GitHub上找到有人做的Titanium中CoffeeScript的plugin,可以看出的是确实有人真的做出来了plugin(这个目前是一个针对让Titanium支持CoffeeScript的plugin)。

如下我们查看create命令的帮助说明,和Module的做法相同,我们也可以通过titanium.py来创建plugin的模板工程:
引用
$ titanium help create
Appcelerator Titanium
Copyright (c) 2010-2011 by Appcelerator, Inc.

Usage: titanium.py create [--platform=p] [--type=t] [--dir=d] [--name=n] [--id=i] [--ver=v]
--platform=p1,p2 platform: iphone, ipad, android, blackberry, etc.
--type=t type of project: project, module, plugin
--dir=d directory to create the new project
--name=n project name
--id=i project id (ie com.companyName.project
--ver=i platform version
--android=sdk_folder For android module - the Android SDK folder


可以很明显看出,它提供了type为plugin的这么一个接口,也就是说它本身是具备这个功能的。那么我们就试着利用这条命令来创建一个plugin模板工程:
引用
$ titanium create --type=plugin --name=myplugin --id=jp.isisredirect.myplugin
Created plugin project


命令执行后,我们就能够发现plugin模板工程被成功创建好了。在jp.isisredirect.myplugin文件夹下可以找到该工程。
引用
$ ls -l jp.isisredirect.myplugin
total 40
-rw-r--r-- 1 <username> staff 77 12 24 11:56 LICENSE
-rw-r--r-- 1 <username> staff 681 12 24 11:56 README
-rwxr-xr-x 1 <username> staff 3034 12 24 11:56 build.py
-rw-r--r-- 1 <username> staff 363 12 24 11:56 manifest
-rwxr-xr-x 1<username> staff 185 12 24 11:56 plugin.py


这里的几个文件中,build.py是用来生成plugin的脚本,我们进入jp.isisredirect.myplugin文件夹中,然后运行build.py脚本。
引用
$ ./build.py
[WARN] please update the manifest key: 'copyright' to a non-default value
[WARN] please update the manifest key: 'license' to a non-default value
[WARN] please update the LICENSE file with your license text before distributingPlugin
packaged at jp.isisredirect.myplugin-0.1.zip


这里由于是默认的模板工程,没有修改manifst和LICENSE文件。所以有警告,不过没有关系,它还是成功的创建出来了plugin(jp.isisredirect.myplugin-0.1.zip)。

我们打开README 看看里边的具体内容,可以看出的是,和Module一样,我们也可以将这个plugin放到我们工程的plugins/jp.isisredirect.myplugin/之下。但是要想正确的运行包含有这个plugin的Titanium工程,我们还需要和Module一样,在tiapp.xml中追加一下设置来导入该plugin.
<plugins>
   <plugin version="0.1">jp.isisredirect.myplugin</plugin>
</plugins>


这样一来,我们在Build这个Titanium工程的时候,plugin.py文件就能够被执行了。

这里我们没有修改plugin.py文件,至于怎么样修改?什么时候该文件被执行?等等问题还需要进一步的了解Titanium的编译构建过程(目前还没有这方面的资料)。

我们先来看看刚才运行的那个build.py文件:
for plugin in ti.properties['plugins']:
 local_plugin_file = os.path.join(local_compiler_dir,plugin['name'],'plugin.py')
 plugin_file = os.path.join(tp_compiler_dir,plugin['name'],plugin['version'],'plugin.py')
 if not os.path.exists(local_plugin_file) and not os.path.exists(plugin_file):
  o.write("+ Missing plugin at %s (checked %s also)\n" % (plugin_file,local_plugin_file))
  print "[ERROR] Build Failed (Missing plugin for %s). Please see output for more details" % plugin['name']
  sys.stdout.flush()
  sys.exit(1)
 o.write("+ Detected plugin: %s/%s\n" % (plugin['name'],plugin['version']))
 print "[INFO] Detected compiler plugin: %s/%s" % (plugin['name'],plugin['version'])
 code_path = plugin_file
 if os.path.exists(local_plugin_file): 
  code_path = local_plugin_file
 o.write("+ Loading compiler plugin at %s\n" % code_path)
 compiler_config['plugin']=plugin
 fin = open(code_path, 'rb')
 m = hashlib.md5()
 m.update(open(code_path,'rb').read())
 code_hash = m.hexdigest()
 p = imp.load_source(code_hash, code_path, fin)
 p.compile(compiler_config)
 fin.close()


依据在tiapp.xml中设置的<plugins><plugin>内容,来选择应该启动的plugin.py。
如果不能启动的话,输出“Build Failed (Missing plugin for %s)”错误信息。
如果能启动的话,调用plugin.py 中的compile 函数。

再来看看通过titanium.py 做成的 plugin.py 文件:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Titanium Compiler plugin
# jp.isisredirect.myplugin
#
def compile(config):
 print "[INFO] jp.isisredirect.myplugin plugin loaded"

这里基本上就是只是生成了一个空的compile函数,如果有需要的处理可以在这里编写代码实现。其中的参数config是在调用它的地方被设置为:
compiler_config = {
'platform':'ios',
'devicefamily':devicefamily,
'simtype':simtype,
'tiapp':ti,
'project_dir':project_dir,
'titanium_dir':titanium_dir,
'appid':appid,
'iphone_version':iphone_version,
'template_dir':template_dir,
'project_name':name,
'command':command,
'deploytype':deploytype,
'build_dir':build_dir,
'app_name':app_name,
'app_dir':app_dir,
'iphone_dir':iphone_dir
}

各个值的含义的话,仔细阅读build.py的脚本内容就能明白(这些参数的值,在Build的时候可能会有很多的处理)。

关于Titanium的plugin,可以看出它是作为Build的预处理来执行的,在Build之前,他需要验证文件的整合型,以及代码,资源文件等等的预处理。目前来说CoffeeScript的plugin就是最好的例子,应该继续的再深入研究研究。

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics