使用 GitHub Codespaces 生成和调试

如果你有GitHub团队或GitHub企业云订阅,则可以使用 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存储库

按如下所示创建 empty GitHub 存储库:

  1. 登录到 github.com

  2. 在 GitHub 主页上,选择 Repositories 旁边的 New 按钮。

  3. 为存储库命名(例如 Blink),然后选择“ 创建存储库”。

  4. “快速设置”下,如果之前已执行此操作,请复制存储库的 HTTPS URL。

  5. 在命令提示符下,将新存储库克隆到本地桌面,如下所示:

    git clone <repository-url>
    

    你应该会看到一条警告,提示你克隆的是一个空存储库。

打开Visual Studio Code并创建新项目,如下所示:

  1. 选择 View>Command Palette>Azure Sphere:生成新项目
  2. “选择模板”下,选择 “Blink”。
  3. “选择文件夹 ”对话框中,指定要在其中创建新项目的文件夹。 (这可以随处可见— 将此文件夹的内容复制到之前创建的GitHub存储库。
  4. 指定与GitHub存储库的名称匹配的项目名称,例如 Blink,然后按 Enter
  5. 将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 扩展,请执行以下操作:

  1. 在Visual Studio Code中打开GitHub存储库文件夹(如果尚未打开)。
  2. 从Visual Studio Code活动栏中打开 Extensions
  3. 搜索“GitHub Codespaces”并安装 GitHub Codespaces 扩展。

创建代码空间

  1. 选择 “查看>命令面板>代码空间:创建新的 Codespace”。

  2. 在存储库的下拉列表中,选择 “Blink”。 如果存储库未显示在下拉列表中,则可以在列表上方的文本框中键入其名称。

  3. 从分支的下拉列表中,选择相应的分支。

    Visual Studio Code 的标题栏会发生变化,表明你当前正在 Codespaces 中进行编辑。 如果在左侧导航栏中打开“扩展”选项卡,则会看到本地安装的扩展和安装在远程容器中的扩展。

生成和调试项目

F5 或选择“ 运行>开始调试”以生成项目并开始调试。 您的应用程序会像往常一样构建并侧载到您的设备上。 如果在代码中设置了断点,应用将运行,直到到达断点为止。 可以使用常用的调试命令来演练代码。 有关更多详细信息,请参阅Visual Studio Code文档中的 Debugging 主题。

完成调试后,按 Shift+F5“停止” 图标。 若要关闭代码空间,请选择 查看>命令面板>Codespaces: 停止当前代码空间