Skip to content

Commit

Permalink
Merge pull request #51 from alkuzad/main
Browse files Browse the repository at this point in the history
Windows Support
  • Loading branch information
Morpheus636 authored Dec 31, 2022
2 parents ae7011f + 1c1d9c5 commit db12c72
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 194 deletions.
44 changes: 39 additions & 5 deletions .github/workflows/build_binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ on:

jobs:
build:
runs-on: ${{ matrix.os[0]}}
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [[ubuntu-latest, linux]]
os: [ubuntu-latest, windows-latest]

steps:
- name: Checkout Code
Expand All @@ -29,12 +29,46 @@ jobs:
- name: Install Poetry Packages
run: poetry install

- name: Get version
run: |
poetry run python ./build_system/update_version.py
- name: Build Pyinstaller
run: make build
run: |
poetry run python ./build_system/update_version.py
poetry run pyinstaller --onefile ./src/zeal_cli.py --name zeal-cli
- name: 'Upload Artifact'
uses: actions/upload-artifact@v3
with:
name: zeal-cli
path: dist/zeal-cli
retention-days: 5
if: matrix.os == 'ubuntu-latest'

- name: 'Upload Artifact - Windows'
uses: actions/upload-artifact@v3
with:
name: zeal-cli.exe
path: dist/zeal-cli.exe
retention-days: 5
if: matrix.os == 'windows-latest'

release:
needs: build

runs-on: "ubuntu-latest"

steps:
- name: download artifacts
uses: actions/download-artifact@v3
with:
path: /home/runner/artifacts

- name: Upload Executables
uses: softprops/[email protected]
with:
files: |
dist/zeal-cli
dist/zeal-cli.exe
/home/runner/artifacts/**/zeal-cli
/home/runner/artifacts/**/zeal-cli.exe
fail_on_unmatched_files: true
8 changes: 5 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
os: [ubuntu-latest]
os: [ubuntu-latest, windows-latest]

steps:
- name: Checkout Code
Expand All @@ -33,5 +33,7 @@ jobs:
- name: Run PyTest
run: poetry run pytest

- name: Build Test
run: make build
- name: Test Build
run: |
poetry run python ./build_system/update_version.py
poetry run pyinstaller --onefile ./src/zeal_cli.py --name zeal-cli
18 changes: 0 additions & 18 deletions Makefile

This file was deleted.

1 change: 0 additions & 1 deletion build_system/update_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import git


repo = git.Repo(search_parent_directories=True)
version = repo.git.describe("--tags")

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pre-commit = "^2.13.0"
pyinstaller = "^4.9"
GitPython = "^3.1.27"

[tool.poetry.scripts]
zeal-cli = "zeal_cli:main"

[tool.black]
line-length = 100
target-version = ['py39']
Expand Down
145 changes: 81 additions & 64 deletions src/zeal/config.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,91 @@
import logging
import os
import platform
from typing import Any

if platform.system() == "Windows":
import winreg

import yaml
from pathlib import Path


logger = logging.getLogger(__file__)


def get_docset_dir() -> str:
# Set zeal_docset_dir variable.
if platform.system() == "Linux":
return os.path.join(os.path.expanduser("~"), ".local", "share", "Zeal", "Zeal", "docsets")
else:
raise NotImplementedError("Zeal_CLI only supports linux at this time.")


def get_cli_data_dir() -> str:
if platform.system() == "Linux":
zeal_cli_dir = os.path.join(os.path.expanduser("~"), ".local", "share", "zeal_cli")
else:
raise NotImplementedError("Zeal_CLI only supports linux at this time.")

if os.path.isfile(zeal_cli_dir):
logger.warning(
f"The Zeal CLI data directory location ({zeal_cli_dir}) exists and is a file, not a directory. Deleting."
)
os.remove(zeal_cli_dir)
os.makedirs(zeal_cli_dir)
elif os.path.isdir(zeal_cli_dir):
logger.debug(f"The Zeal CLI data directory location ({zeal_cli_dir}) already exists.")
elif not os.path.exists(zeal_cli_dir):
logger.debug(
f"The Zeal CLI data directory location ({zeal_cli_dir}) does not exist. Creating."
)
os.makedirs(zeal_cli_dir)
return zeal_cli_dir


def set_default_config(config_path: str) -> dict:
config_dict = {"docset_dir": get_docset_dir()}
if os.path.isfile(config_path):
os.remove(config_path)
with open(config_path, "x") as file:
yaml.safe_dump(config_dict, stream=file)
return config_dict


def get_config(config_path: str) -> dict:
if os.path.isfile(config_path):
logger.debug(f"Using config file found at {config_path}.")
with open(config_path, "r") as file:
config_dict = yaml.safe_load(file)
else:
logger.warning(f"Did not find a config file at {config_path}. Creating default.")
config_dict = set_default_config(config_path)
if not config_dict:
logger.warning(f"Config file found at {config_path} is empty. Resetting to defaults.")
config_dict = set_default_config(config_path)
return config_dict


def set_config_value(key, value, config_path):
with open(config_path, "r+") as config_file:
config_dict = yaml.safe_load(config_file)
config_dict[key] = value
yaml.safe_dump(config_dict, stream=config_file)


cli_data_dir = get_cli_data_dir()
cli_config_path = os.path.join(cli_data_dir, "config.yml")
cli_config = get_config(cli_config_path)
docset_dir = cli_config["docset_dir"]
class Config:
def _get_docset_dir(self) -> Path:
# Set zeal_docset_dir variable.
if platform.system() == "Linux":
return Path("~", ".local", "share", "Zeal", "Zeal", "docsets").expanduser()
elif platform.system() == "Windows":
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Zeal\\Zeal\\docsets") as key:
path, _type = winreg.QueryValueEx(key, "path")
return Path(path).expanduser()
else:
raise RuntimeError("Systems other than Linux and Windows are not supported")

def _get_cli_data_dir(self) -> Path:
zeal_cli_dir = Path("~", ".local", "share", "zeal_cli").expanduser()

if zeal_cli_dir.is_file():
logger.warning(
f"The Zeal CLI data directory location ({zeal_cli_dir}) exists and is a file, not a directory. Deleting."
)
os.remove(zeal_cli_dir)
zeal_cli_dir.mkdir(parents=True)
elif zeal_cli_dir.is_dir():
logger.debug(f"The Zeal CLI data directory location ({zeal_cli_dir}) already exists.")
elif not zeal_cli_dir.exists():
logger.debug(
f"The Zeal CLI data directory location ({zeal_cli_dir}) does not exist. Creating."
)
zeal_cli_dir.mkdir(parents=True)
return zeal_cli_dir

def _set_default_config(self, config_path: Path) -> dict:
config_dict = {"docset_dir": str(self._get_docset_dir().resolve())}
if config_path.is_file():
os.remove(config_path)
with config_path.open(mode="x") as file:
yaml.safe_dump(config_dict, stream=file)
return config_dict

def _get_config(self, config_path: Path) -> dict:
if config_path.is_file():
logger.debug(f"Using config file found at {config_path}.")
with config_path.open() as file:
config_dict = yaml.safe_load(file)
else:
logger.warning(f"Did not find a config file at {config_path}. Creating default.")
config_dict = self._set_default_config(config_path)
if not config_dict:
logger.warning(f"Config file found at {config_path} is empty. Resetting to defaults.")
config_dict = self._set_default_config(config_path)
return config_dict

def set_config_value(self, key: Any, value: Any, config_path: Path):
with config_path.open(mode="r+") as config_file:
config_dict = yaml.safe_load(config_file)
config_dict[key] = value
yaml.safe_dump(config_dict, stream=config_file)

@property
def cli_data_dir(self):
return self._get_cli_data_dir()

@property
def cli_config_path(self):
return Path(self.cli_data_dir, "config.yml")

@property
def cli_config(self):
return self._get_config(self.cli_config_path)

@property
def docset_dir(self):
if self.cli_config:
return Path(self.cli_config["docset_dir"])


config = Config()
Loading

0 comments on commit db12c72

Please sign in to comment.