如果你有 GitHub 团队或 GitHub Enterprise Cloud 订阅,则可以使用 GitHub Codespaces 设置项目,使其在 GitHub 托管的容器内生成。 使用 Visual Studio Code 的远程功能,可以将桌面上的Visual Studio Code连接到 Codespace,并直接从 Codespace 进行编辑、生成、部署和调试。
本主题讨论使用 GitHub Codespaces 远程编辑、生成、部署和调试 Azure Sphere 应用;使用容器通过 Visual Studio Code 生成和调试介绍了如何使用Visual Studio Code在本地容器中编辑、生成、部署和调试 Azure Sphere 应用。
若要使用 Codespaces,必须将项目配置为 GitHub 存储库,并且还必须配置为在容器中使用。 在本主题中,你将使用适当的配置创建新的 Blink 项目。
为项目创建 GitHub 存储库
创建 空 的 GitHub 存储库,如下所示:
在 GitHub 主页中,选择单词“存储库”旁边的“新建”按钮。
为存储库指定一个名称(如 Blink),然后选择“ 创建存储库”。
在“ 快速设置--如果你以前做过此类操作”下,复制存储库的 HTTPS URL。
在命令提示符下,将新存储库克隆到本地桌面,如下所示:
git clone <repository-url>
应会看到一条警告,指出你克隆了一个空存储库。
在克隆中创建新的 Blink 项目
打开Visual Studio Code并创建一个新项目,如下所示:
- 选择“ 查看>命令面板>Azure Sphere:生成新项目”。
- 在 “选择模板”下,选择“ 闪烁”。
- 在 “选择文件夹 ”对话框中,指定要在其中创建新项目的文件夹。 (可以位于任何位置-你将此文件夹的内容复制到之前创建的 GitHub 存储库。)
- 指定与 GitHub 存储库名称匹配的项目名称(如 Blink),然后按 Enter。
- 将 Azure Sphere 项目文件 (包括 .vscode 和 HardwareDefinitions 等所有子文件夹 ) 复制到本地克隆、提交和推送更改。 然后,可以删除在本部分中创建的项目文件夹,因为所有内容都将位于 GitHub 存储库中。
设置 .devcontainer 文件夹
在 GitHub 存储库的顶级目录中,创建名为 .devcontainer 的文件夹。 在此文件夹中,创建包含以下内容的名为 devcontainer.json 的文件:
{
"name": "Azure Sphere Blink",
"runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined"],
"build": {
"dockerfile": "Dockerfile",
"target": "dev"
},
// Use 'settings' to set *default* container specific settings.json values on container create.
// You can edit these settings after create using File > Preferences > Settings > Remote.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
// Use 'appPort' to create a container with published ports. If the port isn't working, be sure
// your server accepts connections from all interfaces (0.0.0.0 or '*'), not just localhost.
// "appPort": [],
// Uncomment the next line to run commands after the container is created.
// "postCreateCommand": "gcc -v",
// Comment out the next line if you want to run as root instead
"remoteUser": "vscode",
// Add the IDs of extensions you want installed when the container is created in the array below.
"extensions": [
"ms-vscode.azure-sphere-tools",
"ms-vscode.azure-sphere-tools-ui"
]
}
接下来,在 .devcontainer 文件夹中创建一个名为 Dockerfile 的文件,其中包含以下内容:
FROM mcr.microsoft.com/azurespheresdk:latest AS dev
FROM dev AS build
COPY ./ /src/
WORKDIR /out
RUN cmake -G "Ninja" -DCMAKE_TOOLCHAIN_FILE="/opt/azurespheresdk/CMakeFiles/AzureSphereToolchain.cmake" \
-DAZURE_SPHERE_TARGET_API_SET="latest-lts" -DCMAKE_BUILD_TYPE="Release" "/src"
ENTRYPOINT [ "ninja" ]
初始 FROM
行将标准 Azure Sphere Docker 映像指定为基本开发容器,第二行指定使用该基本容器作为生成环境。 该 COPY
行将存储库的内容复制到容器的 /src/ 目录中。 指定 WORKDIR
生成目录。 命令 RUN
提供 CMake 命令来生成生成文件。 最后, ENTRYPOINT
指定应调用 ninja 来实际生成应用程序。
提交对 GitHub 项目的更改并推送更改。
安装 GitHub Codespaces 扩展
若要安装 GitHub Codespaces 扩展,请执行以下操作:
- 在 Visual Studio Code 中打开 GitHub 存储库文件夹(如果尚未打开)。
- 从Visual Studio Code活动栏中打开扩展。
- 搜索“GitHub Codespaces”并安装 GitHub Codespaces 扩展。
创建 codespace
选择“ 查看>命令面板>Codespaces:创建新 Codespace”。
从存储库的下拉列表中,选择“ Blink”。 如果存储库未显示在下拉列表中,则可以在列表上方的文本框中键入其名称。
从分支下拉列表中,选择相应的分支。
Visual Studio Code中的标题栏发生更改,以显示你在 Codespaces 中编辑。 如果打开左侧导航栏中的“扩展”选项卡,会看到本地安装的扩展和远程容器中安装的扩展。
生成和调试项目
按 F5 或选择 “运行>开始调试”以生成项目并开始调试。 应用程序像往常一样生成并旁加载到设备。 如果在代码中设置了断点,则应用将运行,直到到达断点。 可以使用常用的调试命令来演练代码。 有关更多详细信息,请参阅Visual Studio Code文档中的调试主题。
完成调试后,按 Shift+F5 或 停止 图标。 若要关闭 codespace,请选择“ 查看>命令面板>Codespaces:停止当前 Codespace”。