译:cordova
http://cordova.apache.org/docs/en/3.5.0/guide_cli_index.md.html#The%20Command-Line%20Interface
cordova命令界面(CLI)向你展示了怎樣創(chuàng)建一個(gè)app,并且在各種各種的本地移動(dòng)平臺(tái)部署他們。這個(gè)工具允許你創(chuàng)建一個(gè)新的項(xiàng)目,編譯到不同的平臺(tái)上面,然后在真實(shí)的設(shè)備上或者模擬器里運(yùn)行。CLI是在概述里面描述的跨平臺(tái)工作流的主要工具。然而,你也可以使用CLI來(lái)初始化項(xiàng)目代碼,然后切換到各種平臺(tái)的SDK和shell工具來(lái)繼續(xù)開(kāi)發(fā)。
Prerequisites
在運(yùn)行任何命令行工具之前,你需要安裝目標(biāo)平臺(tái)的SDK(See the?Platform Guidesfor more details.)
為了給任何一個(gè)平臺(tái)添加一個(gè)支持或者重建的項(xiàng)目,你需要在支持這個(gè)平臺(tái)的SDK的機(jī)器上運(yùn)行命令行。 命令工具支持下列系統(tǒng):
- iOS (Mac)
- Amazon Fire OS (Mac, Linux, Windows)
- Android (Mac, Linux, Windows)
- BlackBerry 10 (Mac, Linux, Windows)
- Windows Phone 8 (Windows)
- Windows 8 (Windows)
- Firefox OS (Mac, Linux, Windows)
On the Mac, the command-line is available via the?Terminal?application. On the PC, it's available as?Command Prompt?underAccessories.
NOTE: For Windows-only platforms, you can still do your development on Mac hardware by running Windows in a virtual machine environment or in dual-boot mode. For available options, see the?Windows Phone Platform Guide?or the?Windows 8 Platform Guide.
The more likely it is that you run the CLI from different machines, the more it makes sense to maintain a remote source code repository, whose assets you pull down to local working directories.
安裝Cordova CLI
The Cordova command-line tool is distributed as an npm package in a ready-to-use format. It is not necessary to compile it from source.
要安裝cordova命令行工具,請(qǐng)按照下面步驟操作:
下載并安裝?Node.js.。按照步驟安裝之后,你可以在你的命令行調(diào)用?node?和?npm?。如果安裝成功,你可以選擇使用像?nvm?或?nave?這樣的工具來(lái)管理你的 Node.js 安裝配置。
下載并安裝一個(gè)?git 客戶端,如果你沒(méi)有的話。按照步驟安裝之后,你可以在你的命令行調(diào)用??git 命令。即使你不會(huì)手動(dòng)使用?git?命令,在創(chuàng)建一個(gè)新項(xiàng)目的時(shí)候CLI會(huì)在后臺(tái)使用它來(lái)下載一些資源。
使用?npm?安裝?cordova?模塊。cordova?模塊將會(huì)自動(dòng)被下載.
-
在 OS X 和Linux上:
$ sudo npm install -g cordova On OS X and Linux, prefixing the?npm?command with?sudo?may be necessary to install this development utility in otherwise restricted directories such as?/usr/local/share. If you are using the optional nvm/nave tool or have write access to the install directory, you may be able to omit the?sudo?prefix. There are?more tips?available on using?npm?without?sudo, if you desire to do that. -
在 Windows系統(tǒng)上:
C:\>npm install -g cordova ?
上面命令中的?-g?標(biāo)志告訴?npm?全局安裝?cordova?。如果沒(méi)有這個(gè)標(biāo)志,?cordova?將會(huì)安裝在當(dāng)前工作目錄下的?node_modules 子目錄。
為了全局調(diào)用?npm?安裝模塊,你可能需要把?npm?的目錄添加到你的環(huán)境變量?PATH?中。在Windows系統(tǒng)中,?npm?一般在?C:\Users\username\AppData\Roaming\npm。在?OS X 和Linux 系統(tǒng)中,一般在/usr/local/share/npm目錄下。
? ? ? The installation log may produce errors for any uninstalled platform SDKs.
Following installation, you should be able to run?cordova?on the command line with no arguments and it should print help text.
創(chuàng)建一個(gè)App
切換到你維護(hù)的源碼所在目錄,運(yùn)行如下命令:
$ cordova create hello com.example.hello HelloWorld?
It may take some time for the command to complete, so be patient. Running the command with the?-d?option displays information about its progress.
The first argument?hello?specifies a directory to be generated for your project. This directory should not already exist, Cordova will create it for you. Its?www?subdirectory houses your application's home page, along with various resources under?css,?js, and?img, which follow common web development file-naming conventions. The?config.xml?file contains important metadata needed to generate and distribute the application.
The second argument?com.example.hello?provides your project with a reverse domain-style identifier. This argument is optional, but only if you also omit the third argument, since the arguments are positional. You can edit this value later in the?config.xml?file, but do be aware that there may be code generated outside of?config.xml?using this value, such as Java package names. The default value is?io.cordova.hellocordova, but it is recommended that you select an appropriate value.
The third argument?HelloWorld?provides the application's display title. This argument is optional. You can edit this value later in the?config.xml?file, but do be aware that there may be code generated outside of?config.xml?using this value, such as Java class names. The default value is?HelloCordova, but it is recommended that you select an appropriate value.
Add Platforms添加平臺(tái)
后面所有的命令需要在項(xiàng)目目錄下運(yùn)行,或者他范圍內(nèi)任意子目錄,所以我們切換到項(xiàng)目目錄下面:
$ cd hello?
在你能編譯項(xiàng)目之前,你需要指定一系列的目標(biāo)平臺(tái)。你是否能運(yùn)行這些命令,完全取決于你的機(jī)器是否支持這些每一個(gè)SDK,或者你是否已經(jīng)安裝這些每一個(gè)SKD。在Mac下面運(yùn)行如下命令:
$ cordova platform add ios$ cordova platform add amazon-fireos$ cordova platform add android$ cordova platform add blackberry10$ cordova platform add firefoxos?
在windows系統(tǒng)下面運(yùn)行下面命令,wp?指的是Windows Phone 操作系統(tǒng)的不同版本:
$ cordova platform add wp8$ cordova platform add windows8$ cordova platform add amazon-fireos$ cordova platform add android$ cordova platform add blackberry10$ cordova platform add firefoxosRun this to check your current set of platforms:
? $ cordova platforms ls ?(Note the?platform?and?platforms?commands are synonymous.)
Run either of the following synonymous commands to remove a platform:
? ? $ cordova platform remove blackberry10$ cordova platform rm amazon-fireos$ cordova platform rm androidRunning commands to add or remove platforms affects the contents of the project's?platforms?directory, where each specified platform appears as a subdirectory. The?www?source directory is reproduced within each platform's subdirectory, appearing for example in?platforms/ios/www?or?platforms/android/assets/www. Because the CLI constantly copies over files from the source?www?folder, you should only edit these files and not the ones located under the?platforms?subdirectories. If you use version control software, you should add this source?www?folder, along with the?merges?folder, to your version control system. (More information about the?merges?folder can be found in the Customize Each Platform section below.)
WARNING: When using the CLI to build your application, you should?not?edit any files in the?/platforms/?directory unless you know what you are doing, or if documentation specifies otherwise. The files in this directory are routinely overwritten when preparing applications for building, or when plugins are reinstalled.
If you wish at this point, you can use an SDK such as Eclipse or Xcode to open the project you created. You will need to open the derivative set of assets from the?/platforms/?directory to develop with an SDK. This is because the SDK specific metadata files are stored within the appropriate?/platform/?subdirectory. (See the?Platform Guides?for information on how to develop applications within each IDE.) Use this approach if you simply want to initialize a project using the CLI and then switch to an SDK for native work.
如果你想整個(gè)開(kāi)發(fā)周期都使用跨平臺(tái)工作流方法那就繼續(xù)讀下去。
Build the App(構(gòu)建App)
?
默認(rèn)的,cordova create?命令腳本生成一個(gè)基于web應(yīng)用的骨架,這個(gè)應(yīng)用的主頁(yè)是位于www目錄下的index.html(?www/index.html?)文件。隨心所欲的編輯這個(gè)應(yīng)用程序,但是任何初始化都應(yīng)該被指定為?裝置就緒?時(shí)間句柄的一部分,被默認(rèn)的?www/js/index.js引用。
運(yùn)行下面的命令,迭代構(gòu)建項(xiàng)目:
$ cordova build上面代碼在項(xiàng)目的?platforms?子目錄下面生成指定平臺(tái)的代碼。你可以通過(guò)下面的命令有選擇的限制每次構(gòu)建的范圍來(lái)指定平臺(tái):
$ cordova build ios上面?cordova build?命令是下面的速記,在這個(gè)例子中也是有目標(biāo)的構(gòu)建單個(gè)平臺(tái):? ?
$ cordova prepare ios$ cordova compile ios在這個(gè)案例中,一旦你運(yùn)行?prepare 命令,你可以使用蘋(píng)果的Xcode SDK作為修改和編譯指定平臺(tái)代碼的一個(gè)選擇,Cordova會(huì)將它生成在?platforms/ios里。其他平臺(tái)的SDK也可以使用相同的方法。
在模擬器或真實(shí)設(shè)備上測(cè)試這個(gè)APP
? ?移動(dòng)平臺(tái)的SDK和模擬器經(jīng)常捆綁一個(gè)設(shè)備圖像,用來(lái)在主屏啟動(dòng)這個(gè)app,還可以看一下他是怎么和許多平臺(tái)特性交互的。運(yùn)行下面的命令來(lái)重建app,在一個(gè)指定的平臺(tái)模擬器里看它: $ cordova emulate android一些移動(dòng)平臺(tái)默認(rèn)模擬一個(gè)特殊的設(shè)備,例如iOS系統(tǒng)的iPhone。對(duì)于其他平臺(tái),你可能需要首先和一個(gè)模擬器連接一個(gè)設(shè)備。
注意:模擬器支持Amazon當(dāng)前不可用的Fire系統(tǒng)
(查看?平臺(tái)指南?的細(xì)節(jié)) 例如,你可能首先運(yùn)行?android?命令啟動(dòng)Android SDk,然后運(yùn)行一個(gè)特殊的設(shè)備圖像,根據(jù)它的默認(rèn)行為來(lái)啟動(dòng)自己:
接著?cordova emulate?命令刷新模擬器圖像顯示最新的應(yīng)用程序,現(xiàn)在可以在主屏啟動(dòng):
?
另外,你還可以把手機(jī)連接到你的電腦然后直接測(cè)試這個(gè)app
$ cordova run android在運(yùn)行這個(gè)命令之前,你需要建立一個(gè)設(shè)備來(lái)測(cè)試,下面的程序在每個(gè)平臺(tái)都變化多端。在Android 和Amazon Fire OS設(shè)備上,你不得不提供一個(gè)可用的?USB debugging?選項(xiàng),并且可能提供一個(gè)USB驅(qū)動(dòng)在你的開(kāi)發(fā)環(huán)境上。在平臺(tái)指南查看在每個(gè)平臺(tái)所要求的細(xì)節(jié) 。
?
Add Plugin Features
當(dāng)你構(gòu)建并查看一個(gè)新項(xiàng)目時(shí),默認(rèn)的應(yīng)用程序看上去好像不能做很多事。你可以充分利用標(biāo)準(zhǔn)的web技術(shù)許多方式來(lái)修改app,但是app想要和不同層次設(shè)備的特性緊密交流,你需要添加提供使用核心cordova API權(quán)利的插件。
插件?是一點(diǎn)附加代碼,向本地組件提供一個(gè)接口。你可以設(shè)計(jì)你自己的插件接口,例如當(dāng)設(shè)計(jì)一個(gè)混合app的時(shí)候,它和本地組件混合了一個(gè)cordova web視圖。 (看嵌入WebViews?和插件開(kāi)發(fā)指南?的詳細(xì)說(shuō)明.) 更常見(jiàn)地,你將添加一個(gè)插件來(lái)使cordova的基本設(shè)備級(jí)的特性之一能夠使用,在API Reference有更詳細(xì)的說(shuō)明 。包括社區(qū)提供的其他插件的列表,可以在?plugins.cordova.io找到。你可以使用CLI從注冊(cè)表取搜索插件。例如,搜索?bar?和?code?生成一個(gè)對(duì)大小寫(xiě)不敏感匹配這兩個(gè)子字符串的結(jié)果。:
$ cordova plugin search bar codecom.phonegap.plugins.barcodescanner - Scans Barcodes只搜索?bar?生成另外的結(jié)果:
org.apache.cordova.statusbar - Cordova StatusBar Plugin?
這個(gè)?cordova plugin add?命令需要你指定插件代碼倉(cāng)庫(kù)。請(qǐng)注意,當(dāng)你是Web項(xiàng)目開(kāi)發(fā)流并且使用CLI時(shí),CLI將會(huì)很小心的向每個(gè)平臺(tái)合適的地方添加這個(gè)插件代碼。(如果你是使用本地項(xiàng)目開(kāi)發(fā)劉,你將不得不為每個(gè)平臺(tái)多次使用Plugman添加插件(引導(dǎo)鏈接)
這是一些你可能使用CLI怎樣為app添加特性的例子:
-
基本設(shè)備信息 (Device?API):
$ cordova plugin add org.apache.cordova.device ? -
網(wǎng)絡(luò)連接(Connection)?和電池事件(Events):
? $ cordova plugin add org.apache.cordova.network-information$ cordova plugin add org.apache.cordova.battery-status ? -
Accelerometer,?Compass, and?Geolocation:
? $ cordova plugin add org.apache.cordova.device-motion$ cordova plugin add org.apache.cordova.device-orientation$ cordova plugin add org.apache.cordova.geolocation ? -
Camera,?Media?playback and?Capture:
? $ cordova plugin add org.apache.cordova.camera$ cordova plugin add org.apache.cordova.media-capture$ cordova plugin add org.apache.cordova.media ? -
設(shè)備可訪問(wèn)的文件和網(wǎng)絡(luò) (File?API):
? $ cordova plugin add org.apache.cordova.file$ cordova plugin add org.apache.cordova.file-transfer ? -
通知(Notification)?通過(guò)對(duì)話框或震動(dòng):
? $ cordova plugin add org.apache.cordova.dialogs$ cordova plugin add org.apache.cordova.vibration ? -
Contacts:
? $ cordova plugin add org.apache.cordova.contacts ? -
Globalization:
? $ cordova plugin add org.apache.cordova.globalization ? -
閃屏(Splashscreen):
$ cordova plugin add org.apache.cordova.splashscreen ? -
打開(kāi)新瀏覽器窗口(InAppBrowser):
$ cordova plugin add org.apache.cordova.inappbrowser ? -
調(diào)試控制臺(tái):
$ cordova plugin add org.apache.cordova.console?
使用?plugin ls?(或者?plugin list, 或者?plugin?本身)來(lái)查看當(dāng)前已安裝的插件。每個(gè)通過(guò)自己的標(biāo)識(shí)符顯示:
$ cordova plugin ls # or 'plugin list'[ 'org.apache.cordova.console' ] ?刪除插件指的是出現(xiàn)在列表相同的標(biāo)識(shí)符。例如,這是你將怎樣從一個(gè)發(fā)布版本移除一個(gè)調(diào)試控制臺(tái)的支持:
$ cordova plugin rm org.apache.cordova.console$ cordova plugin remove org.apache.cordova.console # same ?你可以通過(guò)每個(gè)命令指定一個(gè)以上的參數(shù)批刪除或添加插件:
$ cordova plugin add org.apache.cordova.console org.apache.cordova.device?
Advanced Plugin Options(高級(jí)插件選項(xiàng))
當(dāng)添加一個(gè)插件時(shí),幾個(gè)選項(xiàng)允許你指定從哪兒獲取這個(gè)插件。上面的例子使用了一個(gè)總所周知的registry.cordova.io?注冊(cè)表,通過(guò)?id指定這個(gè)插件:
$ cordova plugin add org.apache.cordova.console這個(gè)?id?可能包含插件的版本號(hào),在?@?符號(hào)之后添加。 The?latest?version is an alias for the most recent version. For example: ? ? $ cordova plugin add org.apache.cordova.console@latest
? ? $ cordova plugin add org.apache.cordova.console@0.2.1
If the plugin is not registered at?registry.cordova.io?but is located in another git repository, you can specify an alternate URL:
? ? $ cordova plugin add https://github.com/apache/cordova-plugin-console.gitThe git example above fetches the plugin from the end of the master branch, but an alternate git-ref such as a tag or branch can be appended after a?#?character:
? ? $ cordova plugin add https://github.com/apache/cordova-plugin-console.git#r0.2.0If the plugin (and its?plugin.xml?file) is in a subdirectory within the git repo, you can specify it with a?:?character. Note that the?#?character is still needed:
? ? $ cordova plugin add https://github.com/someone/aplugin.git#:/my/sub/dirYou can also combine both the git-ref and the subdirectory:
? ? $ cordova plugin add https://github.com/someone/aplugin.git#r0.0.1:/my/sub/dirAlternately, specify a local path to the plugin directory that contains the?plugin.xml?file:
? ? $ cordova plugin add ../my_plugin_dir轉(zhuǎn)載于:https://www.cnblogs.com/miss-radish/p/4788876.html
總結(jié)
- 上一篇: Codeforces Round #33
- 下一篇: 迅达云成参观学习---感谢信