Skip to content

Commit

Permalink
feat: Adding minimum required version for algokit. (#273)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Daniel McGregor <[email protected]>
  • Loading branch information
negar-abbasi and daniel-makerx authored Jun 2, 2023
1 parent da8e46d commit 10aacc2
Show file tree
Hide file tree
Showing 71 changed files with 216 additions and 19 deletions.
24 changes: 16 additions & 8 deletions docs/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,43 @@
- [--color, --no-color](#--color---no-color)
- [--skip-version-check](#--skip-version-check)
- [bootstrap](#bootstrap)
- [Options](#options-1)
- [--force](#--force)
- [all](#all)
- [env](#env)
- [npm](#npm)
- [poetry](#poetry)
- [completions](#completions)
- [install](#install)
- [Options](#options-1)
- [Options](#options-2)
- [--shell ](#--shell-)
- [uninstall](#uninstall)
- [Options](#options-2)
- [Options](#options-3)
- [--shell ](#--shell--1)
- [config](#config)
- [version-prompt](#version-prompt)
- [Arguments](#arguments)
- [ENABLE](#enable)
- [doctor](#doctor)
- [Options](#options-3)
- [Options](#options-4)
- [-c, --copy-to-clipboard](#-c---copy-to-clipboard)
- [explore](#explore)
- [Arguments](#arguments-1)
- [NETWORK](#network)
- [generate](#generate)
- [client](#client)
- [Options](#options-4)
- [Options](#options-5)
- [-o, --output ](#-o---output-)
- [-l, --language ](#-l---language-)
- [Arguments](#arguments-2)
- [APP_SPEC_PATH_OR_DIR](#app_spec_path_or_dir)
- [goal](#goal)
- [Options](#options-5)
- [Options](#options-6)
- [--console](#--console)
- [Arguments](#arguments-3)
- [GOAL_ARGS](#goal_args)
- [init](#init)
- [Options](#options-6)
- [Options](#options-7)
- [-n, --name ](#-n---name-)
- [-t, --template ](#-t---template-)
- [--template-url ](#--template-url-)
Expand All @@ -57,11 +59,11 @@
- [console](#console)
- [explore](#explore-1)
- [logs](#logs)
- [Options](#options-7)
- [Options](#options-8)
- [--follow, -f](#--follow--f)
- [--tail ](#--tail-)
- [reset](#reset)
- [Options](#options-8)
- [Options](#options-9)
- [--update, --no-update](#--update---no-update)
- [start](#start)
- [status](#status)
Expand Down Expand Up @@ -104,6 +106,12 @@ key development environment setup activities.
algokit bootstrap [OPTIONS] COMMAND [ARGS]...
```

### Options


### --force
Continue even if minimum AlgoKit version is not met

### all

Runs all bootstrap sub-commands in the current directory and immediate sub directories.
Expand Down
9 changes: 4 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ questionary = "^1.10.0"
pyclip = "^0.7.0"
shellingham = "^1.5.0.post1"
algokit-client-generator = "^0.1.0b6"
tomli = { version = "^2.0.1", python = "<3.11" }

[tool.poetry.group.dev.dependencies]
pytest = "^7.2.0"
Expand Down
14 changes: 12 additions & 2 deletions src/algokit/cli/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,29 @@

import click

from algokit.core.bootstrap import bootstrap_any_including_subdirs, bootstrap_env, bootstrap_npm, bootstrap_poetry
from algokit.core.bootstrap import (
bootstrap_any_including_subdirs,
bootstrap_env,
bootstrap_npm,
bootstrap_poetry,
project_minimum_algokit_version_check,
)

logger = logging.getLogger(__name__)


@click.option(
"force", "--force", is_flag=True, default=False, help="Continue even if minimum AlgoKit version is not met"
)
@click.group(
"bootstrap", short_help="Bootstrap local dependencies in an AlgoKit project; run from project root directory."
)
def bootstrap_group() -> None:
def bootstrap_group(*, force: bool) -> None:
"""
Expedited initial setup for any developer by installing and configuring dependencies and other
key development environment setup activities.
"""
project_minimum_algokit_version_check(Path.cwd(), ignore_version_check_fail=force)


@bootstrap_group.command(
Expand Down
3 changes: 2 additions & 1 deletion src/algokit/cli/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import questionary

from algokit.core import proc, questionary_extensions
from algokit.core.bootstrap import bootstrap_any_including_subdirs
from algokit.core.bootstrap import bootstrap_any_including_subdirs, project_minimum_algokit_version_check
from algokit.core.log_handlers import EXTRA_EXCLUDE_FROM_CONSOLE
from algokit.core.sandbox import DEFAULT_ALGOD_PORT, DEFAULT_ALGOD_SERVER, DEFAULT_ALGOD_TOKEN, DEFAULT_INDEXER_PORT

Expand Down Expand Up @@ -272,6 +272,7 @@ def _maybe_bootstrap(project_path: Path, *, run_bootstrap: bool | None, use_defa
# note: we run bootstrap before git commit so that we can commit any lock files,
# but if something goes wrong, we don't want to block
try:
project_minimum_algokit_version_check(project_path)
bootstrap_any_including_subdirs(project_path)
except Exception as e:
logger.error(f"Received an error while attempting bootstrap: {e}")
Expand Down
52 changes: 52 additions & 0 deletions src/algokit/core/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@

import click

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
from packaging import version

from algokit.core import proc, questionary_extensions
from algokit.core.conf import get_current_package_version

ENV_TEMPLATE = ".env.template"
ALGOKIT_CONFIG = ".algokit.toml"
logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -218,3 +226,47 @@ def _get_base_python_path() -> str | None:
return str(candidate_path)
# give up, we tried...
return this_python


def get_min_algokit_version(project_dir: Path) -> str | None:
try:
config_path = project_dir / ALGOKIT_CONFIG
try:
config_text = config_path.read_text("utf-8")
except FileNotFoundError:
logger.debug(f"No {ALGOKIT_CONFIG} file found in the project directory.")
return None

config = tomllib.loads(config_text)

try:
min_version = config["algokit"]["min_version"]
except KeyError:
logger.debug(f"No 'min_version' specified in {ALGOKIT_CONFIG} file.")
return None
assert isinstance(min_version, str)

return min_version
except Exception as ex:
logger.debug(f"Unexpected error inspecting AlgoKit config: {ex}")
return None


def project_minimum_algokit_version_check(project_dir: Path, *, ignore_version_check_fail: bool = False) -> None:
"""
Checks the current version of AlgoKit against the minimum required version specified in the AlgoKit config file.
"""

min_version = get_min_algokit_version(project_dir)
if min_version is None:
return
algokit_version = get_current_package_version()
if version.parse(algokit_version) < version.parse(min_version):
message = (
f"This template requires AlgoKit version {min_version} or higher, "
f"but you have AlgoKit version {algokit_version}. Please update AlgoKit."
)
if ignore_version_check_fail:
logger.warning(message)
else:
raise click.ClickException(message)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Usage: algokit bootstrap [OPTIONS] COMMAND [ARGS]...
dependencies and other key development environment setup activities.

Options:
--force Continue even if minimum AlgoKit version is not met
-h, --help Show this message and exit.

Commands:
Expand Down
28 changes: 28 additions & 0 deletions tests/bootstrap/test_bootstrap_all.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest
from _pytest.tmpdir import TempPathFactory
from algokit.core.bootstrap import ALGOKIT_CONFIG
from algokit.core.conf import get_current_package_version
from approvaltests.pytest.py_test_namer import PyTestNamer

from tests.utils.approvals import verify
Expand All @@ -18,6 +20,32 @@ def test_bootstrap_all_empty(tmp_path_factory: TempPathFactory) -> None:
verify(result.output)


def test_bootstrap_all_algokit_min_version(tmp_path_factory: TempPathFactory) -> None:
cwd = tmp_path_factory.mktemp("cwd")
current_version = get_current_package_version()
(cwd / ALGOKIT_CONFIG).write_text('[algokit]\nmin_version = "999.99.99"\n')
result = invoke(
"bootstrap all",
cwd=cwd,
)

assert result.exit_code == 1
verify(result.output.replace(current_version, "{current_version}"))


def test_bootstrap_all_algokit_min_version_ignore_error(tmp_path_factory: TempPathFactory) -> None:
cwd = tmp_path_factory.mktemp("cwd")
current_version = get_current_package_version()
(cwd / ALGOKIT_CONFIG).write_text('[algokit]\nmin_version = "999.99.99"\n')
result = invoke(
"bootstrap --force all",
cwd=cwd,
)

assert result.exit_code == 0
verify(result.output.replace(current_version, "{current_version}"))


def test_bootstrap_all_env(tmp_path_factory: TempPathFactory) -> None:
cwd = tmp_path_factory.mktemp("cwd")
(cwd / ".env.template").touch()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: This template requires AlgoKit version 999.99.99 or higher, but you have AlgoKit version {current_version}. Please update AlgoKit.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
WARNING: This template requires AlgoKit version 999.99.99 or higher, but you have AlgoKit version {current_version}. Please update AlgoKit.
DEBUG: Checking {current_working_directory} for bootstrapping needs
Finished bootstrapping {current_working_directory}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
DEBUG: No .algokit.toml file found in the project directory.
DEBUG: Checking {current_working_directory} for bootstrapping needs
Finished bootstrapping {current_working_directory}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
DEBUG: Checking {current_working_directory} for bootstrapping needs
DEBUG: Running `algokit bootstrap env`
DEBUG: {current_working_directory}/.env doesn't exist yet
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
DEBUG: Checking {current_working_directory} for bootstrapping needs
DEBUG: Running `algokit bootstrap npm`
Installing npm dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
DEBUG: Checking {current_working_directory} for bootstrapping needs
DEBUG: Running `algokit bootstrap npm`
Installing npm dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
DEBUG: Checking {current_working_directory} for bootstrapping needs
DEBUG: Running `algokit bootstrap npm`
Installing npm dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
DEBUG: Checking {current_working_directory} for bootstrapping needs
DEBUG: Running `algokit bootstrap poetry`
DEBUG: Running 'poetry --version' in '{current_working_directory}'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
DEBUG: Checking {current_working_directory} for bootstrapping needs
DEBUG: Running `algokit bootstrap poetry`
DEBUG: Running 'poetry --version' in '{current_working_directory}'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
DEBUG: Checking {current_working_directory} for bootstrapping needs
DEBUG: Skipping {current_working_directory}/.venv
DEBUG: Skipping {current_working_directory}/__pycache__
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
DEBUG: Checking {current_working_directory} for bootstrapping needs
DEBUG: Checking {current_working_directory}/empty_dir for bootstrapping needs
DEBUG: Checking {current_working_directory}/live_dir for bootstrapping needs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
DEBUG: {current_working_directory}/.env doesn't exist yet
DEBUG: {current_working_directory}/.env.template exists
Copying {current_working_directory}/.env.template to {current_working_directory}/.env and prompting for empty values
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DEBUG: No .algokit.toml file found in the project directory.
.env already exists; skipping bootstrap of .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
DEBUG: {current_working_directory}/.env doesn't exist yet
DEBUG: {current_working_directory}/.env.template exists
Copying {current_working_directory}/.env.template to {current_working_directory}/.env and prompting for empty values
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
DEBUG: {current_working_directory}/.env doesn't exist yet
DEBUG: {current_working_directory}/.env.template exists
Copying {current_working_directory}/.env.template to {current_working_directory}/.env and prompting for empty values
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
DEBUG: No .algokit.toml file found in the project directory.
DEBUG: {current_working_directory}/.env doesn't exist yet
No .env or .env.template file; nothing to do here, skipping bootstrap of .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
Installing npm dependencies
DEBUG: Running 'npm install' in '{current_working_directory}'
npm: STDOUT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
Installing npm dependencies
DEBUG: Running 'npm install' in '{current_working_directory}'
npm: STDOUT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
Installing npm dependencies
DEBUG: Running 'npm.cmd install' in '{current_working_directory}'
npm.cmd: STDOUT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
Installing npm dependencies
DEBUG: Running 'npm install' in '{current_working_directory}'
Error: Failed to run `npm install` for {current_working_directory}/package.json. Is npm installed and available on PATH?
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
Installing npm dependencies
DEBUG: Running 'npm install' in '{current_working_directory}'
Error: Failed to run `npm install` for {current_working_directory}/package.json. Is npm installed and available on PATH?
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DEBUG: No .algokit.toml file found in the project directory.
Installing npm dependencies
DEBUG: Running 'npm.cmd install' in '{current_working_directory}'
Error: Failed to run `npm.cmd install` for {current_working_directory}/package.json. Is npm installed and available on PATH?
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DEBUG: No .algokit.toml file found in the project directory.
{current_working_directory}/package.json doesn't exist; nothing to do here, skipping bootstrap of npm
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DEBUG: No .algokit.toml file found in the project directory.
{current_working_directory}/package.json doesn't exist; nothing to do here, skipping bootstrap of npm
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DEBUG: No .algokit.toml file found in the project directory.
{current_working_directory}/package.json doesn't exist; nothing to do here, skipping bootstrap of npm
Loading

1 comment on commit 10aacc2

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/algokit
   __init__.py15753%6–13, 17–24, 32–34
   __main__.py220%1–3
src/algokit/cli
   completions.py105298%80, 95
   doctor.py48394%142–144
   generate.py30197%49
   goal.py30197%42
   init.py1851492%301, 304–306, 317, 361, 387, 427, 436–438, 441–446, 459
   localnet.py91397%157, 178–179
src/algokit/core
   bootstrap.py1682386%11, 117, 140, 205, 208, 214–228, 244–246, 250–252
   conf.py30487%13, 17, 25, 27
   doctor.py65789%67–69, 92–94, 134
   log_handlers.py68790%50–51, 63, 112–116, 125
   proc.py45198%98
   sandbox.py1271588%95–102, 113, 197, 213, 228–230, 246
   typed_client_generation.py80594%55–57, 70, 75
   version_prompt.py73889%27–28, 40, 59–62, 80, 109
TOTAL132110392% 

Tests Skipped Failures Errors Time
208 0 💤 0 ❌ 0 🔥 12.928s ⏱️

Please sign in to comment.