Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows's support for powershell and scripts entrypoints. #357

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MiguelBarro
Copy link

@MiguelBarro MiguelBarro commented Oct 10, 2024

Summary

Windows's support for powershell and scripts entrypoints. This way prettier can be executed over a container or wsl.

For example, the following script can be used as entrypoint to use a containerized version of prettier:

# See https://github.com/prettier/vim-prettier/pull/357
# Using .ps1 entrypoints under cmd.exe requires file association changes:
#  cmd> assoc .ps1=Microsoft.PowerShellScript.1
#  cmd> ftype Microsoft.PowerShellScript.1="C:\Program Files\PowerShell\7\pwsh.exe" "-File" "%1" %*
$params = @{
    Name = "prettierImageName"
    Value = "jauderho/prettier"
    Description = "The docker image to run prettier cli"
    Option = "Constant"
    Scope = "Script"
}
Set-Variable @params

$encoding = '$OutputEncoding = New-Object System.Text.UTF8Encoding;'
$encoding += '[Console]::InputEncoding = $OutputEncoding;' 
$encoding += '[Console]::OutputEncoding = $OutputEncoding;' 
$encoding += '$PSDefaultParameterValues["*:Encoding"] = "utf8";'

Invoke-Expression -Command $encoding

# check docker
if (-not (gcm docker -ErrorAction SilentlyContinue))
{
    Write-Error "Docker is not installed"
        exit 1
}

# check for the image
$image = docker images --format '{{json .}}' |
    ? { $_ -notmatch "failed to get console mode for (stdout|stdin)"} |
    ConvertFrom-Json | ? Repository -eq $prettierImageName

if (-not $image)
{
    Write-Error "Docker image jauderho/prettier is not installed"
        exit 2
}

# retrieve arguments (workaround because passing @args directly to docker fails)
$cmdline = [System.Environment]::CommandLine
$ScriptName = (gi $PSCommandPath).Name
if ($cmdline -match $ScriptName)
{
    $filtered = $cmdline -replace  ('^.*' + $ScriptName + '\s+'),'' # remove invocation
    $filtered = $filtered -replace  '\s*(}\s*)?\d?>.*$','' # remove redirection
    $use_input = $filtered | sls '--stdin-filepath=\S*'
    $filtered = $filtered -replace  '--stdin-filepath=\S*','' # remove filepath
    $filtered = $filtered.trim() -split '\s+'
}
else
{
    $filtered = $args | ? { $_ -notmatch '--stdin-filepath' }
    $use_input = $args -ne $filtered
}

# run docker or delegate to blocking script
if ($use_input -ne $null)
{
    $input | docker run -i --rm $prettierImageName @filtered 2>$null
}
else
{
    docker run --rm $prettierImageName @args 2>$null
}

The .vimrc will require the following config:

if has('win32')
    " Lure vim into identify powershell scripts as exes
    if !($PATHEXT=~'\.PS1')
        let $PATHEXT.=';.PS1'
    endif

    let g:prettier#exec_cmd_path  = "<script path>/docker-prettier.ps1"
    let g:prettier#win_async_shell_cmds = (executable('pwsh') ? 'pwsh' : 'powershell') . ' -File'
endif

" enable autocommands
let g:prettier#autoformat_config_present = 1

…ettier can be executed over a container or wsl.

Signed-off-by: Guybrush <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant