Code.ps1


$global:code = Join-Path $env:USERPROFILE "scoop/apps/vscode/current/bin/code.cmd"
$global:code2 = Join-Path $env:USERPROFILE "scoop/apps/vscode/current/Code"

function Install-Code {

  if (-Not(Test-Path -Path $global:code -PathType leaf -ErrorAction SilentlyContinue)) {
    Install-Scoop vscode -Bucket "extras"
  }

  # add image shortcut on desktop

  Install-CodeExtension "ms-vscode-remote.remote-ssh"
  Install-CodeExtension "ms-vscode-remote.remote-wsl"
  #Install-CodeExtension "ms-vscode.remote-server"

  ## Desktop icon
  $icon = "$([Environment]::GetFolderPath("Desktop"))\Code.lnk"
  if (-Not(Test-Path -Path $icon -PathType leaf -ErrorAction SilentlyContinue)) {
    $wshShell = New-Object -comObject WScript.Shell
    $shortcut = $wshShell.CreateShortcut($icon)
    $shortcut.TargetPath = $global:code2
    $shortcut.Save()
    Write-Host "Added Code shortcut on Desktop"
  }

}
function Install-CodeExtension {
  param (
    [string] $Name
  )

  $extensions = Invoke-Expression "$global:code --list-extensions"
  if (-Not($extensions -Contains $Name)) {
    Invoke-Expression "$global:code --install-extension $Name"
  }
}

function Update-Code {

  Install-Code
  scoop update vscode
}

function Start-Code {

  param(
    [switch] $Browser
  )

  if ($Browser) {
    Start-Process "https://vscode.dev/"
    return
    # [system.Diagnostics.Process]::Start("msedge","https://debug.to") chrome firefox
    #"Look instructions at https://xtec.dev/dev/vscode/remote/"
  }

  Install-Code
  Invoke-Expression $global:code
}