本文档演示了如何在开发期间将组件直接添加到组件实例树中,并在运行时与这些组件进行交互。
- 会话组件:构建并运行会话
- 测试组件:运行 Fuchsia 测试
概念
在运行组件之前,您应该了解以下概念:
如需详细了解组件的执行,请参阅组件生命周期。
组件实例
运行组件的第一步是向树中添加新的组件实例。 capabilities
探索静态组件
静态组件在树中声明为另一个组件实例的子项。您可以使用 ffx component show
确定静态组件实例的名称和组件网址:
ffx component show COMPONENT_NAME
将 COMPONENT_NAME
替换为组件的名称。以下示例展示了 pkg-resolver
组件的命令输出:
$ ffx component show pkg-resolver
Moniker: /core/pkg-resolver
URL: fuchsia-pkg://fuchsia.com/pkg-resolver#meta/pkg-resolver.cm
Type: CML static component
Component State: Resolved
Execution State: Running
...
无法在运行时创建或销毁静态组件实例。
管理动态组件
动态组件是在运行时在集合内创建的。您可以使用 ffx component create
创建新的组件实例,在现有集合中提供目标名称,并提供用于解析组件的组件网址:
ffx component create TARGET_MONIKER COMPONENT_URL
将 TARGET_MONIKER
替换为现有集合内新组件的目标名称,并将 COMPONENT_URL
替换为提供组件的位置。例如,以下命令会在 ffx-laboratory
集合内创建一个名为 hello-world
的新组件实例:
$ ffx component create /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
URL: fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
Moniker: /core/ffx-laboratory:hello-world
Creating component instance...
同样,使用 ffx component destroy
提供相应名称来销毁动态组件实例:
ffx component destroy TARGET_MONIKER
将 TARGET_MONIKER
替换为要销毁的组件的名称。以下示例会销毁上面创建的 hello-world
组件:
$ ffx component destroy /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Destroying component instance...
组件执行
一旦组件实例存在于树中,您就可以使用 ffx component
启动和停止目标实例。
启动实例
使用 ffx component start
明确启动组件实例:
ffx component start TARGET_MONIKER
将 TARGET_MONIKER
替换为要启动的组件的名称。以下示例会启动之前创建的 hello-world
组件:
$ ffx component start /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Starting component instance...
停止实例
使用 ffx component stop
通过名称终止正在运行的组件实例的执行:
ffx component stop TARGET_MONIKER
将 TARGET_MONIKER
替换为要停止的组件的名称。以下示例会停止到上面启动的 hello-world
组件:
$ ffx component stop /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Stopping component instance...
运行组件
ffx component run
命令提供了有关在开发过程中运行基本组件的快速入门。这是 ffx component create
后跟 ffx component start
的快捷方式:
ffx component run TARGET_MONIKER COMPONENT_URL
将 TARGET_MONIKER
替换为现有集合内新组件的目标名称,并将 COMPONENT_URL
替换为提供组件的位置。例如,以下命令会在 ffx-laboratory
集合内创建一个名为 hello-world-rust
的新组件实例:
$ ffx component run /core/ffx-laboratory:hello-world-rust fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
URL: fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
Moniker: /core/ffx-laboratory:hello-world-rust
Creating component instance...
Starting component instance...
上面的示例相当于运行以下各个 ffx
命令:
$ ffx component create /core/ffx-laboratory:hello-world-rust fuchsia-pkg://fuchsia.com/hello-world-rust#meta/hello-world-rust.cm
$ ffx component start /core/ffx-laboratory:hello-world-rust
更新组件的方法
当您更改组件时,通常需要更新在设备上运行的该组件的其中一个实例。例如,您可以更改组件的二进制文件,然后重启组件以使用新二进制文件运行。或者,您也可以更改其清单以添加新的功能路由,并希望使这些功能路由在设备上可用。
ffx component reload
命令是最快速、最完整的组件重新加载方法。但这并不是唯一方法,了解其他方法可以为特殊用例实现更精确的操作。
下文对这些方法进行了总结并进行了详细说明。
摘要
命令 | 说明 | 更新软件包 | 更新清单 | 保留资源 |
---|---|---|---|---|
ffx 组件重新加载 | 包括停止、更新和启动 | 是 | 是 | 是 |
ffx 组件 销毁/创建/启动 | 销毁,然后启动 | 是 | 是 | 否 |
ffx 组件运行 - 重新创建 | 销毁,然后启动 | 是 | 是 | 否 |
ffx 组件 停止/开始 | 并且启动和停止 不会销毁 | 否 | 否 | 是 |
- “更新软件包”是指在重新加载软件包时更新代码。
- “更新清单”表示重新加载清单缓存,更新 FIDL 文件中包含的路由和其他信息。
- “保留资源”意味着将由销毁命令释放的资源(如存储空间)会被保留。
重新加载
使用以下命令更新组件的代码和清单,同时保留资源:
ffx component reload TARGET_MONIKER
此命令会先关闭组件,然后重新加载并重启。该命令会更新软件包和清单,而不会销毁组件或释放资源。
reload
命令可保留组件的资源,例如存储空间。如果为了进行调试而初始化、获取或重新创建处于特定状态的资源的速度很慢,则这种保留非常有用。
如果销毁成本高昂(例如,当需要关闭并重启会话或目标设备/模拟器时),重新加载速度也会更快。
销毁/创建/启动
如需完全重新加载组件并舍弃获取的资源,您可以先销毁现有的组件实例,然后再重启。使用:
$ ffx component destroy TARGET_MONIKER
$ ffx component create TARGET_MONIKER COMPONENT_URL
$ ffx component start TARGET_MONIKER
此序列将重新加载软件包和清单,因此代码和功能更改也会随之更新。不过,销毁组件也会释放其使用的所有资源。如果您的目标是从完全重新初始化的组件开始,则可能需要完全重置。
运行 --recreate
一个类似于销毁/创建/启动序列的便捷命令是包含 --recreate
的 run
命令:
ffx component run TARGET_MONIKER COMPONENT_URL --recreate
停止/开始
虽然它主要不是执行更新的方法,但其副作用是停止组件,然后再启动组件,则只会对组件进行部分更新。
$ ffx component stop TARGET_MONIKER
$ ffx component start TARGET_MONIKER
假设 ffx serve
等软件包管理器正在运行,将加载并运行组件代码的最新版本。但是,由于 Fuchsia 组件框架中缓存的工作方式,清单不会更新。该清单包含组件的路由信息以及 *.cm
文件中定义的其他信息。因此,如果您更改组件的功能路由,停止和启动组件将不会获取这些更改。
ffx-laboratory
ffx-laboratory
是一个组件集合,用于为开发提供一组受限的功能。此集合中的组件提供了以下功能:
- 协议功能
fuchsia.logger.LogSink
:录制日志消息fuchsia.process.Launcher
:创建新进程
- 存储空间容量
tmp
:临时存储(非永久性)data
:由/tmp
支持的模拟永久性存储空间cache
:由/tmp
支持的模拟缓存存储空间
- 目录功能
/dev
:驱动程序管理器提供的设备驱动程序devfs
/boot
:由组件管理器提供的只读bootfs
ffx-laboratory
是一个 transient
集合。此集合中的组件实例即使在停止后也将保留。如需销毁此集合中的组件实例,请使用 ffx component destroy
命令。
问题排查
本部分介绍您在开发期间运行组件时可能会遇到的常见问题。
无法解析该组件
使用 ffx component start
或 ffx component run
时,如果组件框架无法解析组件实例,您可能会遇到以下错误:
$ ffx component run /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
URL: fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
Moniker: /core/ffx-laboratory:hello-world
Creating component instance...
Starting component instance...
Lifecycle protocol could not bind to component instance: InstanceCannotResolve
当组件网址无法解析为有效的组件清单时,就会发生这种情况。
如需解决此问题,请验证以下内容:
- 组件网址的格式正确无误。
- 您正在运行软件包服务器。
- 您的软件包服务器已在目标中注册。
- 您的组件已发布到软件包服务器。
组件实例已存在
使用 ffx component create
或 ffx component run
时,如果组件实例已存在,您可能会遇到以下错误:
$ ffx component run /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
URL: fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
Moniker: /core/ffx-laboratory:hello-world
Creating component instance...
Component instance already exists. Use --recreate to destroy and recreate a new instance, or --name to create a new instance with a different name.
当目标名称已被其他组件实例使用时,就会发生这种情况。
如需解决此问题,请使用 ffx component
destroy
命令手动销毁该实例:
$ ffx component destroy /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Destroying component instance...
如果您使用的是 ffx component run
,请添加 --recreate
标志以销毁并重新创建实例:
$ ffx component run /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm --recreate
URL: fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
Moniker: /core/ffx-laboratory:hello-world
Creating component instance...
Component instance already exists. Destroying...
Recreating component instance...
Starting component instance...
或者,添加 --name
标志以创建一个具有不同名称的新实例:
$ ffx component run /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm --name hello-world-2
URL: fuchsia-pkg://fuchsia.com/hello-world#meta/hello-world.cm
Moniker: /core/ffx-laboratory:hello-world-2
Creating component instance...
Starting component instance...