如何为Visual Studio Code的Dev Container创建跨平台的initializeCommand?

问题描述 投票:0回答:1

假设我有:

  • .env
    文件,即
    .gitignore
    d
  • .env.dev.dist
    等类型的文件,为每个环境复制到
    .env
    ,并填写一些默认为空白的更多秘密变量
  • devcontainer.json
    应该尽可能自动化,所以我至少希望将
    .env.dev.dist
    文件复制到
    .env
    (如果尚不存在)并在 VSCode 中打开复制的
    .env

我看到

initializeCommand
devcontainer.json
选项可以提供帮助,因为它在容器初始化之前在主机上运行。但是,我不知道如何创建一个适用于所有系统的脚本,因为提到的任务的语法在它们之间有所不同。

我缺少为每个系统定义单独命令的任何选项吗?

另外,参考一个类似的未回答的问题,但主要问题有点不同:有没有办法在运行之前自动创建 VS Code 远程容器中的容器所需的文件?

visual-studio-code command-line cross-platform development-environment vscode-devcontainer
1个回答
0
投票

在两个平台上安装make

# for windows use chocolatey
choco install -y make

# if you want powershell-core
choco install -y powershell-core

并用它来选择要拨打的正确电话:

ifeq ($(OS),Windows_NT)
  INITIALIZERS=initialize-windows
else
  INITIALIZERS=initialize-linux
endif

ifneq (, $(shell which pwsh))
    SHELL_COMMAND=pwsh
else ifneq (, $(shell which powershell))
    SHELL_COMMAND=powershell
endif

initialize: $(INITIALIZERS)

initialize-windows:
    @echo "Initializing Windows..."
    $(SHELL_COMMAND) -File .devcontainer/setup-ssh-agent.ps1

initialize-linux:
    @echo "Initializing Linux..."

从 .devcontainer/devcontainer.json 调用:

"initializeCommand": "make initialize --ignore-errors -f .devcontainer/Makefile",

如果有人感兴趣,可以参考以下脚本: 安装-ssh-agent.ps1

if ($(Get-Service -Name ssh-agent).Status -ne 'Running')
{   
    # https://stackoverflow.com/a/57035712/1973777
    if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
        Start-Process PowerShell -Verb RunAs "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$pwd'; & '$PSCommandPath';`"";
        exit;
    }

    Set-Service ssh-agent -StartupType Automatic
    Start-Service ssh-agent
    Write-Output "SSH-Agent Started..."
}
else
{
    Write-Output "SSH-Agent Already Running..."
}
© www.soinside.com 2019 - 2024. All rights reserved.