首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Cordova CLI源码分析(6)——添加插件

2013-09-05 
Cordova CLI源码分析(六)——添加插件添加插件源码位于src/plugin.js 不再详细分析,主要用到plugman,也是Cor

Cordova CLI源码分析(六)——添加插件

添加插件源码位于src/plugin.js 不再详细分析,主要用到plugman,也是Cordova 提供的用于安装和卸载插件的工具,见https://npmjs.org/package/plugman

主要内容摘录如下:

1环境:需要git工具支持

2 命令行使用

· Using minimum parameters, installs a plugin into a cordova project. You must specify a platform and cordova project location for that platform. You also must specify a plugin, with the different --plugin parameter forms being:

o name: The directory name where the plugin contents exist. This must be an existing directory under the --plugins_dir path (see below for more info) or a plugin in the Cordova registry.

o url: A URL starting with https:// or git://, pointing to a valid git repository that is clonable and contains a plugin.xml file. The contents of this repository would be copied into the --plugins_dir.

o path: A path to a directory containing a valid plugin which includes a plugin.xml file. This path's contents will be copied into the --plugins_dir.

· --uninstall: Uninstalls an already---install'ed plugin from a cordova project. Specify the plugin ID.

Other parameters:

· --plugins_dir defaults to <project>/cordova/plugins, but can be any directory containing a subdirectory for each fetched plugin.

· --www defaults to the project's www folder location, but can be any directory that is to be used as cordova project application web assets.

· --variable allows to specify certain variables at install time, necessary for certain plugins requiring API keys or other custom, user-defined parameters. Please see the plugin specificationfor more information.

3 源码中API接口

(1)install method

Installs a plugin into a specified cordova project of a specified platform.

· platform: one of android, ios, blackberry10, wp7 or wp8

· project_dir: path to an instance of the above specified platform's cordova project

· id: a string representing the id of the plugin, a path to a cordova plugin with a validplugin.xml file, or an https:// or git:// url to a git repository of a valid cordova plugin or a plugin published to the Cordova registry

· plugins_dir: path to directory where plugins will be stored, defaults to<project_dir>/cordova/plugins

· subdir: subdirectory within the plugin directory to consider as plugin directory root, defaults to .

· cli_variables: an object mapping cordova plugin specification variable namess (see plugin specification) to values

· www_dir: path to directory where web assets are to be copied to, defaults to the specified project directory's www dir (dependent on platform)

callback: callback to invoke once complete. If specified, will pass in an error object as a first parameter if the action failed. If not and an error occurs, plugman will throw the error

(2)uninstall method

Uninstalls a previously-installed cordova plugin from a specified cordova project of a specified platform.

· platform: one of android, ios, blackberry10, wp7 or wp8

· project_dir: path to an instance of the above specified platform's cordova project

· id: a string representing the id of the plugin

· plugins_dir: path to directory where plugins are stored, defaults to<project_dir>/cordova/plugins

· subdir: subdirectory within the plugin directory to consider as plugin directory root, defaults to .

· cli_variables: an object mapping cordova plugin specification variable namess (see plugin specification) to values

· www_dir: path to directory where web assets are to be copied to, defaults to the specified project directory's www dir (dependent on platform)

· callback: callback to invoke once complete. If specified, will pass in an error object as a first parameter if the action failed. If not and an error occurs, plugman will throw the error

(3)fetch method

Copies a cordova plugin into a single location that plugman uses to track which plugins are installed into a project.

· plugin_dir: path, URL to a plugin directory/repository or name of a plugin published to the Cordova registry.

· plugins_dir: path housing all plugins used in this project

· link: if plugin_dir points to a local path, will create a symbolic link to that folder instead of copying into plugins_dir, defaults to false

· subdir: subdirectory within the plugin directory to consider as plugin directory root, defaults to .

· gitref: if plugin_dir points to a URL, this value will be used to pass into git checkout after the repository is cloned, defaults to HEAD

· callback: callback to invoke once complete. If specified, will pass in an error object as a first parameter if the action failed. If not and an error occurs, plugman will throw the error

(4)prepare method

Finalizes plugin installation by making configuration file changes and setting up a JavaScript loader for js-module support.

· project_dir: path to an instance of the above specified platform's cordova project

· platform: one of android, ios, blackberry10, wp7 or wp8

· plugins_dir: path housing all plugins used in this project

4 插件目录结构

foo-plugin/

|- plugin.xml     # xml-based manifest

|- src/           # native source for each platform

|  |- android/

|  |  `- Foo.java

|  `- ios/

|     |- CDVFoo.h

|     `- CDVFoo.m

|- README.md

`- www/

   |- foo.js

   `- foo.png


热点排行