如果拥有GitHub团队或GitHub Enterprise云订阅,则可以使用 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.com

  • 在GitHub主页中,选择“ 存储库 ”一词旁边的 “新建 ”按钮。

  • 为存储库命名(如 Blink )并选择 “创建存储库 ”。

  • “快速设置”下,如果以前做过此类操作 ,请复制存储库的 HTTPS URL。

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

    git clone <repository-url>
    

    应会看到一条警告,指出已克隆空存储库。

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

  • 选择 ViewCommand> PaletteAzure >Sphere:生成新Project
  • “选择模板”下,选择 “闪烁”。
  • “选择文件夹 ”对话框中,指定要在其中创建新项目的文件夹。 (这可以是任意位置-你将此文件夹的内容复制到之前创建的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 扩展。
  • 创建代码空间

  • 选择 ViewCommand> PaletteCodespaces>:创建新的 Codespace

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

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

    Visual Studio Code中的标题栏进行更改,以显示你正在 Codespaces 中进行编辑。 如果在左侧导航栏中打开“扩展”选项卡,则会看到本地安装的扩展和远程容器中安装的扩展。

    生成和调试项目

    F5 或选择 RunStart>调试,生成项目并开始调试。 应用程序将像往常一样生成和旁加载到设备。 如果已在代码中设置断点,应用将一直运行,直到到达断点。 可以使用通常的调试命令来演练代码。 有关更多详细信息,请参阅Visual Studio Code文档中的调试主题。

    调试完成后,按 Shift+F5“停止 ”图标。 若要关闭代码空间,请选择 ViewCommand> PaletteCodespaces>:停止当前代码空间

  •