Skip to content

Commit

Permalink
chore: overide InitService, render name, fix spread tests
Browse files Browse the repository at this point in the history
Signed-off-by: Callahan Kovacs <[email protected]>
  • Loading branch information
mr-cal committed Nov 7, 2024
1 parent 00db805 commit e49653c
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 154 deletions.
1 change: 0 additions & 1 deletion snapcraft/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@
"Other",
[
commands.LintCommand,
commands.InitCommand,
],
),
]
Expand Down
2 changes: 0 additions & 2 deletions snapcraft/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
ExtensionsCommand,
ListExtensionsCommand,
)
from .init import InitCommand
from .legacy import (
StoreLegacyCreateKeyCommand,
StoreLegacyGatedCommand,
Expand Down Expand Up @@ -67,7 +66,6 @@
__all__ = [
"ExpandExtensionsCommand",
"ExtensionsCommand",
"InitCommand",
"LintCommand",
"ListExtensionsCommand",
"ListPluginsCommand",
Expand Down
59 changes: 0 additions & 59 deletions snapcraft/commands/init.py

This file was deleted.

6 changes: 3 additions & 3 deletions snapcraft/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def _validate_version_name(version: str, model_name: str) -> None:
)


def _validate_name(*, name: str, field_name: str) -> str:
def validate_name(*, name: str, field_name: str) -> str:
"""Validate a name.
:param name: The name to validate.
Expand Down Expand Up @@ -261,7 +261,7 @@ def _validate_component(name: str) -> str:
raise ValueError(
"component names cannot start with the reserved prefix 'snap-'"
)
return _validate_name(name=name, field_name="component")
return validate_name(name=name, field_name="component")


def _get_partitions_from_components(
Expand Down Expand Up @@ -729,7 +729,7 @@ def _validate_mandatory_base(self):
@pydantic.field_validator("name")
@classmethod
def _validate_snap_name(cls, name):
return _validate_name(name=name, field_name="snap")
return validate_name(name=name, field_name="snap")

@pydantic.field_validator("components")
@classmethod
Expand Down
2 changes: 2 additions & 0 deletions snapcraft/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""Snapcraft services."""

from snapcraft.services.assertions import Assertion
from snapcraft.services.init import Init
from snapcraft.services.lifecycle import Lifecycle
from snapcraft.services.package import Package
from snapcraft.services.provider import Provider
Expand All @@ -26,6 +27,7 @@

__all__ = [
"Assertion",
"Init",
"Lifecycle",
"Package",
"Provider",
Expand Down
82 changes: 82 additions & 0 deletions snapcraft/services/init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2024 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Service for initializing a project."""

import pathlib

import craft_cli
from craft_application import services
from typing_extensions import override

from snapcraft import errors
from snapcraft.models.project import validate_name
from snapcraft.parts.yaml_utils import get_snap_project


class Init(services.InitService):
"""Service class for initializing a project."""

@override
def initialise_project(
self,
*,
project_dir: pathlib.Path,
project_name: str,
template_dir: pathlib.Path,
) -> None:
try:
validate_name(name=project_name, field_name="snap")
if len(project_name) > 40:
raise ValueError("snap names must be 40 characters or less")
except ValueError as err:
raise errors.SnapcraftError(
message=f"Invalid snap name {project_name!r}: {str(err)}.",
resolution="Provide a valid name with '--name' or rename the project directory.",
) from err

super().initialise_project(
project_dir=project_dir,
project_name=project_name,
template_dir=template_dir,
)
craft_cli.emit.message(
"Go to https://docs.snapcraft.io/the-snapcraft-format/8337 for more "
"information about the snapcraft.yaml format."
)

@override
def check_for_existing_files(
self,
*,
project_dir: pathlib.Path,
template_dir: pathlib.Path,
) -> None:
try:
craft_cli.emit.progress("Checking for an existing 'snapcraft.yaml'.")
project = get_snap_project(project_dir)
raise errors.SnapcraftError(
"Could not initialise a new snapcraft project because "
f"{str(project.project_file)!r} already exists"
)
# the `ProjectMissing` error means a new project can be initialised
except errors.ProjectMissing:
craft_cli.emit.progress("Could not find an existing 'snapcraft.yaml'.")

super().check_for_existing_files(
project_dir=project_dir,
template_dir=template_dir,
)
3 changes: 3 additions & 0 deletions snapcraft/services/service_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class SnapcraftServiceFactory(ServiceFactory):
project: models.Project | None = None # type: ignore[reportIncompatibleVariableOverride]

# These are overrides of default ServiceFactory services
InitClass: type[services.Init] = ( # type: ignore[reportIncompatibleVariableOverride]
services.Init
)
LifecycleClass: type[services.Lifecycle] = ( # type: ignore[reportIncompatibleVariableOverride]
services.Lifecycle
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: my-snap-name # you probably want to 'snapcraft register <name>'
name: {{ name }} # you probably want to 'snapcraft register <name>'
base: core24 # the base snap is the execution environment for this snap
version: '0.1' # just for humans, typically '1.2+git' or '1.3.2'
summary: Single-line elevator pitch for your amazing snap # 79 char long summary
Expand Down
30 changes: 24 additions & 6 deletions tests/spread/general/init/task.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
summary: Run snapcraft init

systems: [ubuntu-22*]

environment:
PROFILE/default_profile: null
PROFILE/simple_profile: simple
PROFILE: null
PROJECT_DIR/default_dir: null
PROJECT_DIR/project_dir: test-project-dir
PROJECT_DIR: null
NAME: null
PROFILE/default_profile: null
PROFILE/simple_profile: simple
NAME/with_name: test-snap-name
PROJECT_DIR/with_project_dir: test-project-dir
NAME/with_project_dir_and_name: test-snap-name
PROJECT_DIR/with_project_dir_and_name: test-project-dir

restore: |
unset SNAPCRAFT_BUILD_ENVIRONMENT
if [[ -n "$PROJECT_DIR" ]]; then
cd "$PROJECT_DIR"
fi
snapcraft clean
rm -f ./*.snap
rm -rf ./snap
execute: |
# unset SNAPCRAFT_BUILD_ENVIRONMENT=host
unset SNAPCRAFT_BUILD_ENVIRONMENT
args=("init")
Expand All @@ -26,6 +34,10 @@ execute: |
args+=("--profile" "$PROFILE")
fi
if [[ -n "$NAME" ]]; then
args+=("--name" "$NAME")
fi
if [[ -n "$PROJECT_DIR" ]]; then
args+=("$PROJECT_DIR")
fi
Expand All @@ -36,7 +48,13 @@ execute: |
cd "$PROJECT_DIR"
fi
# the base should be core24
if [[ -n "$NAME" ]]; then
expected_name="$NAME"
else
expected_name="$(basename "$PWD")"
fi
grep "^name: ${expected_name}" snap/snapcraft.yaml
grep "^base: core24" snap/snapcraft.yaml
# 'snapcraft init' should create a usable snapcraft.yaml file
Expand Down
Loading

0 comments on commit e49653c

Please sign in to comment.