Make Vim plugin work on macOS/Linux again

This commit is contained in:
Łukasz Langa 2018-04-11 23:29:00 -07:00
parent 19d69b34e5
commit dc32757bcc

View File

@ -35,20 +35,20 @@ python3 << endpython3
import sys import sys
import vim import vim
def _find_python_binary(exec_prefix): def _get_python_binary(exec_prefix):
if sys.platform[:3] == "win": if sys.platform[:3] == "win":
return exec_prefix / 'python.exe' return exec_prefix / 'python.exe'
return exec_prefix / 'bin' / 'python3' return exec_prefix / 'bin' / 'python3'
def _find_pip(venv_path): def _get_pip(venv_path):
if sys.platform[:3] == "win": if sys.platform[:3] == "win":
return venv_path / 'Scripts' / 'pip.exe' return venv_path / 'Scripts' / 'pip.exe'
return venv_path / 'bin' / 'pip' return venv_path / 'bin' / 'pip'
def _find_virtualenv_site_packages(venv_path): def _get_virtualenv_site_packages(venv_path, pyver):
if sys.platform[:3] == "win": if sys.platform[:3] == "win":
return venv_path / 'Lib' / 'site-packages' return venv_path / 'Lib' / 'site-packages'
return venv_path / 'Lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages' return venv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages'
def _initialize_black_env(upgrade=False): def _initialize_black_env(upgrade=False):
pyver = sys.version_info[:2] pyver = sys.version_info[:2]
@ -60,13 +60,13 @@ def _initialize_black_env(upgrade=False):
import subprocess import subprocess
import venv import venv
virtualenv_path = Path(vim.eval("g:black_virtualenv")).expanduser() virtualenv_path = Path(vim.eval("g:black_virtualenv")).expanduser()
virtualenv_site_packages = str(_find_virtualenv_site_packages(virtualenv_path)) virtualenv_site_packages = str(_get_virtualenv_site_packages(virtualenv_path, pyver))
first_install = False first_install = False
if not virtualenv_path.is_dir(): if not virtualenv_path.is_dir():
print('Please wait, one time setup for Black.') print('Please wait, one time setup for Black.')
_executable = sys.executable _executable = sys.executable
try: try:
sys.executable = str(_find_python_binary(Path(sys.exec_prefix))) sys.executable = str(_get_python_binary(Path(sys.exec_prefix)))
print(f'Creating a virtualenv in {virtualenv_path}...') print(f'Creating a virtualenv in {virtualenv_path}...')
print('(this path can be customized in .vimrc by setting g:black_virtualenv)') print('(this path can be customized in .vimrc by setting g:black_virtualenv)')
venv.create(virtualenv_path, with_pip=True) venv.create(virtualenv_path, with_pip=True)
@ -78,7 +78,7 @@ def _initialize_black_env(upgrade=False):
if upgrade: if upgrade:
print('Upgrading Black with pip...') print('Upgrading Black with pip...')
if first_install or upgrade: if first_install or upgrade:
subprocess.run([str(_find_pip(virtualenv_path)), 'install', '-U', 'black']) subprocess.run([str(_get_pip(virtualenv_path)), 'install', '-U', 'black'])
print('DONE! You are all set, thanks for waiting ✨ 🍰 ✨') print('DONE! You are all set, thanks for waiting ✨ 🍰 ✨')
if first_install: if first_install:
print('Pro-tip: to upgrade Black in the future, use the :BlackUpgrade command and restart Vim.\n') print('Pro-tip: to upgrade Black in the future, use the :BlackUpgrade command and restart Vim.\n')