[+] Ensure git bash is installed on Windows

This commit is contained in:
Hykilpikonna 2022-08-20 20:47:45 -04:00
parent 6911087eab
commit 3124ec0b3e
3 changed files with 44 additions and 3 deletions

View file

@ -46,3 +46,5 @@ class GlobalConfig:
GLOBAL_CFG = GlobalConfig(color_mode='8bit', override_distro=None, debug=False, is_light=False) GLOBAL_CFG = GlobalConfig(color_mode='8bit', override_distro=None, debug=False, is_light=False)
MINGIT_URL = 'https://github.com/git-for-windows/git/releases/download/v2.37.2.windows.2/MinGit-2.37.2.2-busybox-32-bit.zip'

View file

@ -344,6 +344,9 @@ def run():
print(f'Version is {VERSION}') print(f'Version is {VERSION}')
return return
# Ensure git bash for windows
ensure_git_bash()
# Test distro ascii art # Test distro ascii art
if args.test_distro: if args.test_distro:
print(f'Setting distro to {args.test_distro}') print(f'Setting distro to {args.test_distro}')

View file

@ -6,16 +6,19 @@ import platform
import re import re
import shlex import shlex
import subprocess import subprocess
import zipfile
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from subprocess import check_output from subprocess import check_output
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from urllib.request import urlretrieve
import pkg_resources import pkg_resources
import psutil
from typing_extensions import Literal from typing_extensions import Literal
from hyfetch.color_util import color from hyfetch.color_util import color
from .constants import GLOBAL_CFG from .constants import GLOBAL_CFG, MINGIT_URL
from .presets import ColorProfile from .presets import ColorProfile
from .serializer import from_dict from .serializer import from_dict
@ -142,6 +145,40 @@ def get_command_path() -> str:
return pkg_resources.resource_filename(__name__, 'scripts/neowofetch') return pkg_resources.resource_filename(__name__, 'scripts/neowofetch')
def ensure_git_bash() -> Path:
"""
Ensure git bash installation for windows
:returns git bash path
"""
if platform.system() == 'Windows':
# Find installation in default path
def_path = Path(r'C:\Program Files\Git\bin\bash.exe')
if def_path.is_file():
return def_path
# Find installation in PATH (C:\Program Files\Git\cmd should be in path)
pth = (os.environ.get('PATH') or '').lower().split(';')
pth = [p for p in pth if p.endswith(r'\git\cmd')]
if pth:
return Path(pth[0]).parent / r'bin\bash.exe'
# Previously downloaded portable installation
path = Path(__file__).parent / 'min_git'
pkg_path = path / 'package.zip'
if path.is_dir():
return path / r'bin\bash.exe'
# No installation found, download a portable installation
print('Git installation not found. Git is required to use HyFetch/neofetch on Windows')
print('Downloading a minimal portable package for Git...')
urlretrieve(MINGIT_URL, pkg_path)
print('Download finished! Extracting...')
with zipfile.ZipFile(pkg_path, 'r') as zip_ref:
zip_ref.extractall(path)
print('Done!')
return path / r'bin\bash.exe'
def run_command(args: str, pipe: bool = False) -> str | None: def run_command(args: str, pipe: bool = False) -> str | None:
""" """
Run neofetch command Run neofetch command
@ -153,7 +190,7 @@ def run_command(args: str, pipe: bool = False) -> str | None:
cmd = get_command_path().replace("\\", "/").replace("C:/", "/c/") cmd = get_command_path().replace("\\", "/").replace("C:/", "/c/")
args = args.replace('\\', '/').replace('C:/', '/c/') args = args.replace('\\', '/').replace('C:/', '/c/')
full_cmd = ['C:\\Program Files\\Git\\bin\\bash.exe', '-c', f'{cmd} {args}'] full_cmd = [ensure_git_bash(), '-c', f'{cmd} {args}']
# print(full_cmd) # print(full_cmd)
if pipe: if pipe:
@ -162,7 +199,6 @@ def run_command(args: str, pipe: bool = False) -> str | None:
subprocess.run(full_cmd) subprocess.run(full_cmd)
def get_distro_ascii(distro: str | None = None) -> str: def get_distro_ascii(distro: str | None = None) -> str:
""" """
Get the distro ascii of the current distro. Or if distro is specified, get the specific distro's Get the distro ascii of the current distro. Or if distro is specified, get the specific distro's