Skip to content

Commit

Permalink
BUG: Fix os#system() w/ env vars on windows
Browse files Browse the repository at this point in the history
When &shell is a Windows shell (cmd.exe), we need to use these to set
variables they can understand. Neither command.com not powershell are
properly supported at this moment

Closes #19
  • Loading branch information
LucHermitte committed Dec 27, 2023
1 parent 1f6d455 commit ec13cd3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
31 changes: 22 additions & 9 deletions autoload/lh/os.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
" <URL:http://github.com/LucHermitte/lh-vim-lib>
" License: GPLv3 with exceptions
" <URL:http://github.com/LucHermitte/lh-vim-lib/tree/master/License.md>
" Version: 4.6.4
let s:k_version = 40604
" Version: 5.4.0
let s:k_version = 50400
" Created: 10th Apr 2012
" Last Update: 29th Oct 2018
" Last Update: 27th Dec 2023
"------------------------------------------------------------------------
" Description:
" «description»
Expand Down Expand Up @@ -200,7 +200,10 @@ endfunction

" Function: lh#os#new_runner_script(command, env) {{{3
function! lh#os#new_runner_script(command, env) abort
let tmpname = tempname()
" Force .cmd extension as Windows's cmd.exe will only accept command
" files with this extension. Even with an explicit "cmd /c
" foobar.tmp.cmd", the .cmd extension is required.
let tmpname = tempname() . ".cmd"
let result = lh#on#exit()
\.register(':call delete('.string(tmpname).')')
let result._script_name = tmpname
Expand All @@ -214,9 +217,19 @@ function! lh#os#new_runner_script(command, env) abort
let result._lines += ['#!'.(env.__shebang)]
unlet env.__shebang
endif
if lh#os#OnDOSWindows() && ! lh#os#system_detected() == 'unix'
let result._lines += map(items(env), 'set v:val[0]."=".v:val[1]')
else
if &shell =~? '\vcmd(.exe)?$'
" TODO: Support powershell!
let result._cmd = result._script_name
let result._lines += ["@echo off"]
let result._lines += map(items(env), '"set ".v:val[0]."=".v:val[1]')
elseif &shell =~? 'powershell'
let result._cmd = result._script_name
let result._lines += map(items(env), '"$Env:".v:val[0]."=".s:as_string(v:val[1])')
" Warning, the exact command needs to be compatible with powershell!
else " Expecting unix like shells
" It seems we still need to say bash will interpret the script, and no
" other option (even "-c") shall be used.
let result._cmd = &shell . ' ' . result._script_name
let result._lines += map(items(env), '"export ".v:val[0]."=".s:as_string(v:val[1])')
endif
let result._lines += type(a:command) == type([]) ? a:command : [ a:command ]
Expand All @@ -232,8 +245,8 @@ function! lh#os#new_runner_script(command, env) abort
endfunction

function! s:run_script() dict abort
call s:Verbose("%1", self._lines)
let r = system(&shell . ' ' . self._script_name)
call s:Verbose("execute -> %1 which contains: %2", self._cmd, self._lines)
let r = system(self._cmd)
return r
endfunction

Expand Down
11 changes: 6 additions & 5 deletions autoload/lh/po.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
" File: autoload/lh/po.vim {{{1
" Author: Luc Hermitte <EMAIL:luc {dot} hermitte {at} gmail {dot} com>
" <URL:http://github.com/LucHermitte/lh-vim-lib>
" Version: 4.5.0.
let s:k_version = '40500'
" Version: 5.4.0.
let s:k_version = '50400'
" Created: 03rd Feb 2017
" Last Update: 27th Jun 2018
" Last Update: 27th Dec 2023
"------------------------------------------------------------------------
" Description:
" Utility functions to handle Portable Object messages
Expand Down Expand Up @@ -60,12 +60,13 @@ endfunction
" Function: s:translate(id) {{{3
let s:k_cached_translations = {}
function! s:translate(id) dict abort
" TODO: Skip translation if LANG is English/C
let cache = lh#dict#let(s:k_cached_translations, self._env.LANG .'.'. self._env.TEXTDOMAIN, {})
if !has_key(cache, a:id)
if executable('bash')
let cache[a:id] = lh#os#system('bash -c '.shellescape('echo $"'.a:id.'"'), self._env)
else
" TODO: support windows
" TODO: support pure windows w/o bash
let cache[a:id] = a:id
endif
endif
Expand All @@ -79,7 +80,7 @@ function! lh#po#context(...) abort
let res._env.TEXTDOMAIN = get(a:, 1, 'vim')
let res._env.TEXTDOMAINDIR = get(a:, 1, $VIMRUNTIME.'/lang')
let res._env.LANG = exists('$LC_MESSAGES') ? $LC_MESSAGES : v:lang
let res.translate = function(s:getSNR('translate'))
let res.translate = function(s:getSNR('translate'))
return res
endfunction

Expand Down

0 comments on commit ec13cd3

Please sign in to comment.