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

refactor: drop environment expansion #376

Merged
merged 1 commit into from
Oct 16, 2023
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
13 changes: 4 additions & 9 deletions rockcraft/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@

from __future__ import annotations

import functools
import pathlib
from typing import Any

import craft_cli
from craft_application import Application, AppMetadata, util
from overrides import override

Expand All @@ -39,12 +37,9 @@
class Rockcraft(Application):
"""Rockcraft application definition."""

@functools.cached_property
def project(self) -> models.Project:
"""Get this application's Project metadata."""
project_file = (pathlib.Path.cwd() / f"{self.app.name}.yaml").resolve()
craft_cli.emit.debug(f"Loading project file '{project_file!s}'")
return models.Project.from_yaml_file(project_file, work_dir=self._work_dir)
@override
def _extra_yaml_transform(self, yaml_data: dict[str, Any]) -> dict[str, Any]:
return models.transform_yaml(self._work_dir, yaml_data)

@override
def _configure_services(self, platform: str | None, build_for: str | None) -> None:
Expand Down
57 changes: 0 additions & 57 deletions rockcraft/environment.py

This file was deleted.

9 changes: 7 additions & 2 deletions rockcraft/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
"""Rockcraft models."""


from rockcraft.models.project import Project, RockcraftBuildInfo, load_project
from rockcraft.models.project import (
Project,
RockcraftBuildInfo,
load_project,
transform_yaml,
)

__all__ = ["Project", "RockcraftBuildInfo", "load_project"]
__all__ = ["Project", "RockcraftBuildInfo", "load_project", "transform_yaml"]
39 changes: 11 additions & 28 deletions rockcraft/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"""Project definition and helpers."""
import dataclasses
import operator
import pathlib
import platform as host_platform
import re
from builtins import super
Expand All @@ -40,15 +39,12 @@
import pydantic
import spdx_lookup # type: ignore
import yaml
from craft_application.errors import CraftValidationError
from craft_application.models import BuildInfo
from craft_application.models import Project as BaseProject
from craft_archives import repo
from craft_providers import bases
from overrides import override
from pydantic_yaml import YamlModelMixin

from rockcraft.environment import expand_environment
from rockcraft.errors import ProjectLoadError, ProjectValidationError
from rockcraft.extensions import apply_extensions
from rockcraft.parts import part_has_overlay, validate_part
Expand Down Expand Up @@ -487,29 +483,6 @@ def unmarshal(cls, data: Dict[str, Any]) -> "Project":

return project

@classmethod
@override
def from_yaml_file(
cls, path: pathlib.Path, *, work_dir: Optional[pathlib.Path] = None
) -> "Project":
"""Instantiate this model from a YAML file."""
# pylint: disable=import-outside-toplevel

data = load_project(path)

if work_dir is not None:
project_vars = {"version": data["version"]}
expand_environment(
data,
project_vars=project_vars,
work_dir=work_dir,
)
try:
# TODO apply extensions here
return cls.unmarshal(data)
except pydantic.ValidationError as err:
raise CraftValidationError.from_pydantic(err, file_name=path.name) from None

def generate_metadata(
self, generation_time: str, base_digest: bytes
) -> Tuple[dict, dict]:
Expand Down Expand Up @@ -679,7 +652,17 @@ def load_project(filename: Path) -> Dict[str, Any]:
msg = f"{msg}: {err.filename!r}."
raise ProjectLoadError(msg) from err

yaml_data = apply_extensions(filename.parent, yaml_data)
yaml_data = transform_yaml(filename.parent, yaml_data)
return yaml_data


def transform_yaml(project_root: Path, yaml_data: Dict[str, Any]) -> Dict[str, Any]:
"""Do Rockcraft-specific transformations on a project yaml.

:param project_root: The path that contains the "rockcraft.yaml" file.
:param yaml_data: The data dict loaded from the yaml file.
"""
yaml_data = apply_extensions(project_root, yaml_data)

_add_pebble_data(yaml_data)

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/extensions/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import pytest

from rockcraft import errors, extensions
from rockcraft.models.project import load_project
from rockcraft.models import load_project
from tests.unit.testing.extensions import (
FULL_EXTENSION_YAML,
ExperimentalExtension,
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pathlib import Path

from rockcraft.pebble import Pebble

ENVIRONMENT_YAML = """\
name: environment-test
version: 2.0
Expand Down Expand Up @@ -53,3 +55,12 @@ def test_application_expand_environment(default_application, new_dir):
"X": "ship it!",
"CRAFT_VAR": "2.0",
}


def test_application_pebble_part(default_application, new_dir):
"""Test that loading the project through the application adds the Pebble part."""
project_file = Path(new_dir) / "rockcraft.yaml"
project_file.write_text(ENVIRONMENT_YAML)

project = default_application.project
assert project.parts["pebble"] == Pebble.PEBBLE_PART_SPEC