在開發期間啟動元件

為了進行測試和偵錯,ffx component 指令可以快速在裝置上啟動動態元件執行個體

概念

裝置上的元件執行個體通常是使用元件資訊清單進行宣告,該資訊清單會以靜態方式定義裝置元件的拓撲和功能。不過,靜態宣告元件並不是在裝置上建立元件執行個體的唯一方法,您也可以使用 ffx component 指令,在執行階段在裝置上建立動態元件執行個體。

其中一個重要差別在於 Fuchsia 會限制所有動態元件執行個體在元件集合下執行。集合可做為動態元件執行個體的容器。因此,集合下的元件執行個體功能,會受到集合能夠公開和提供的功能限制。

雖然新的元件集合可以使用元件資訊清單 (類似於宣告靜態元件執行個體),但 Fuchsia 針對一般用途提供數個預先定義的集合。舉例來說,ffx-laboratory 是其中一個預先定義的集合。

啟動元件

如要建立新的動態元件執行個體,您必須先執行 ffx component create,將新元件新增至裝置上的元件執行個體樹狀結構。新增後,您可以執行 ffx component start 以在裝置上啟動元件。

如要在裝置上啟動新的動態元件執行個體,請按照下列步驟操作:

  1. 建立新的元件執行個體:

    ffx component create <TARGET_MONIKER> <COMPONENT_URL>
    

    更改下列內容:

    • TARGET_MONIKER:新元件執行個體的目的地路徑名稱。註解必須在路徑中加入元件集合。
    • COMPONENT_URL:元件的資源位置。

    以下範例會為 hello-world-cpp.cm 元件建立新的元件執行個體,並將該路徑名稱指派為 /core/ffx-laboratory:hello-world

    $ ffx component create /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world-cpp#meta/hello-world-cpp.cm
    URL: fuchsia-pkg://fuchsia.com/hello-world-cpp#meta/hello-world-cpp.cm
    Moniker: /core/ffx-laboratory:hello-world
    Creating component instance...
    
  2. 啟動元件執行個體:

    ffx component start <TARGET_MONIKER>
    

    TARGET_MONIKER 替換為步驟 1 中使用的路徑名稱。

    以下範例會在 /core/ffx-laboratory:hello-world 中啟動元件執行個體:

    $ ffx component start /core/ffx-laboratory:hello-world
    Moniker: /core/ffx-laboratory:hello-world
    Starting component instance...
    

在 ffx-laboratory 底下啟動元件

啟動動態元件執行個體通常需要執行一連串 ffx component createffx component start 指令 (請參閱「啟動元件」)。不過,ffx component run 指令可以在單一指令列中啟動動態元件執行個體。

如要在 ffx-laboratory 集合下方快速啟動元件,請執行下列指令:

ffx component run /core/ffx-laboratory:<NAME> <COMPONENT_URL>

COMPONENT_URL 替換為元件的資源位置,並將 NAME 替換為 ffx-laboratory 集合中元件執行個體的名稱。

以下範例會啟動裝置上的 hello-world-cpp.cm 元件:

$ ffx component run /core/ffx-laboratory:hello-world-cpp fuchsia-pkg://fuchsia.com/hello-world-cpp#meta/hello-world-cpp.cm
URL: fuchsia-pkg://fuchsia.com/hello-world-cpp#meta/hello-world-cpp.cm
Moniker: /core/ffx-laboratory:hello-world-cpp
Creating component instance...
Starting component instance...

基本上,ffx component run 指令會執行下列步驟:

  1. 執行 ffx component create,使用元件名稱做為目標路徑名稱,在 ffx-laboratory 集合下方建立新的元件執行個體。
  2. 執行 ffx component start 以啟動裝置上的元件執行個體。

舉例來說,在上述範例中執行 ffx component run /core/ffx-laboratory:hello-world-cpp fuchsia-pkg://fuchsia.com/hello-world-cpp#meta/hello-world-cpp.cm 等同於執行下列指令:

$ ffx component create /core/ffx-laboratory:hello-world-cpp fuchsia-pkg://fuchsia.com/hello-world-cpp#meta/hello-world-cpp.cm
$ ffx component start /core/ffx-laboratory:hello-world-cpp

停止元件

如要停止裝置上的執行中元件執行個體,請執行下列指令:

ffx component stop <TARGET_MONIKER>

TARGET_MONIKER 替換為元件執行個體的路徑名稱。

以下範例會在 /core/ffx-laboratory:hello-world 中停止元件執行個體:

$ ffx component stop /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Stopping component instance...

刪除元件

如要從裝置的元件執行個體樹狀結構中移除動態元件執行個體,請執行下列指令:

ffx component destroy <TARGET_MONIKER>

TARGET_MONIKER 替換為元件執行個體的路徑名稱。

以下範例會移除 /core/ffx-laboratory:hello-world 的元件執行個體:

$ ffx component destroy /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Destroying component instance...