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

Make it so we can get the latest release automatically without having to keep updating it by hand #141

Merged
merged 6 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ dependencies = [
"rich==13.4.2",
"toml<0.11",
"typer==0.9.0",
"platformdirs<2.7"
"platformdirs<2.7",
"requests<=2.31.0"
]
description = "Command Line Interface for PyScript"
license = {text = "Apache-2.0"}
Expand All @@ -29,7 +30,8 @@ dev = [
"coverage<7.3",
"mypy<=1.4.1",
"pytest<7.5",
"types-toml<0.11"
"types-toml<0.11",
"types-requests"
]
docs = [
"Sphinx<5.2",
Expand Down
36 changes: 28 additions & 8 deletions src/pyscript/_generator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import datetime
import json
from pathlib import Path
from typing import Optional

import jinja2
import requests
import toml

from pyscript import LATEST_PYSCRIPT_VERSION, config
Expand All @@ -19,7 +19,7 @@ def create_project_html(
python_file_path: str,
config_file_path: str,
output_file_path: Path,
pyscript_version: str = LATEST_PYSCRIPT_VERSION,
pyscript_version: str,
template: str = "basic.html",
) -> None:
"""Write a Python script string to an HTML file template.
Expand Down Expand Up @@ -54,9 +54,9 @@ def save_config_file(config_file: Path, configuration: dict):

Params:

- config_file(Path): path configuration file. (i.e.: "pyscript.toml"). Supported
formats: `toml` and `json`.
- configuration(dict): app configuration to be saved
- config_file(Path): path configuration file. (i.e.: "pyscript.toml"). Supported
formats: `toml` and `json`.
- configuration(dict): app configuration to be saved

Return:
(None)
Expand All @@ -73,7 +73,7 @@ def create_project(
app_description: str,
author_name: str,
author_email: str,
pyscript_version: str = LATEST_PYSCRIPT_VERSION,
pyscript_version: Optional[str] = None,
project_type: str = "app",
wrap: bool = False,
command: Optional[str] = None,
Expand All @@ -86,7 +86,6 @@ def create_project(
main.py - a "Hello world" python starter module
index.html - start page for the project
"""
date_stamp = datetime.date.today()

if wrap:
if command:
Expand All @@ -107,13 +106,16 @@ def create_project(
# was complaining so let's add a default
app_name = app_or_file_name or "my-pyscript-app"

if not pyscript_version:
pyscript_version = _get_latest_pyscript_version()

context = {
"name": app_name,
"description": app_description,
"type": "app",
"author_name": author_name,
"author_email": author_email,
"version": f"{date_stamp.year}.{'{:02d}'.format(date_stamp.month)}.1",
"version": "v0",
}

app_dir = Path(".") / app_name
Expand Down Expand Up @@ -155,3 +157,21 @@ def create_project(
pyscript_version=pyscript_version,
template=template,
)


def _get_latest_pyscript_version() -> str:
"""Get the latest version of PyScript from GitHub."""
url = "https://api.github.com/repos/pyscript/pyscript/releases/latest"
try:
response = requests.get(url)

if not response.ok:
pyscript_version = LATEST_PYSCRIPT_VERSION
else:

data = response.json()
pyscript_version = data["tag_name"]
except Exception:
pyscript_version = LATEST_PYSCRIPT_VERSION

return pyscript_version
4 changes: 2 additions & 2 deletions src/pyscript/plugins/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import typer

from pyscript import LATEST_PYSCRIPT_VERSION, app, cli, plugins
from pyscript import app, cli, plugins
from pyscript._generator import create_project


Expand All @@ -15,7 +15,7 @@ def create(
author_name: str = typer.Option(None, help="Name of the author"),
author_email: str = typer.Option(None, help="Email of the author"),
pyscript_version: str = typer.Option(
LATEST_PYSCRIPT_VERSION,
None,
"--pyscript-version",
help="If provided, defines what version of pyscript will be used to create the app",
),
Expand Down
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
from pathlib import Path
from typing import Any
from unittest.mock import MagicMock, patch

import pytest
from _pytest.monkeypatch import MonkeyPatch

from pyscript import LATEST_PYSCRIPT_VERSION


@pytest.fixture(scope="session", autouse=True)
def requests():
mocked_result = {"tag_name": LATEST_PYSCRIPT_VERSION}

with patch("pyscript._generator.requests") as mocked_requests:
mocked_get = MagicMock()
mocked_get.ok = True
mocked_get.json = MagicMock(return_value=mocked_result)
mocked_requests.get.return_value = mocked_get
yield mocked_requests


@pytest.fixture
def auto_enter(monkeypatch):
Expand Down
Loading