Skip to content
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: 6 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,9 @@ jobs:
echo "Run '3rd-party/flutter/bin/dart format' to fix formatting."
exit 1
fi

- name: Check gui-less snapcraft.yaml is up to date
shell: bash
run: |
pip install --quiet -r git-hooks/requirements.txt
packaging/gui-less/generate-snapcraft.py --check
13 changes: 13 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@
}
}
},
{
"name": "snap-gui-less",
"description": "Preset for snap builds without the GUI.",
"inherits": [
"snap"
],
"cacheVariables": {
"MULTIPASS_ENABLE_FLUTTER_GUI": {
"type": "BOOL",
"value": "OFF"
}
}
},
{
"name": "snap-debug",
"description": "Preset for snap builds in CI for test runs (debug).",
Expand Down
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ code injection (now or in the future).
* `git config --local commit.template .gitmessage`
- Consider adding the `commit-msg` file in the repository root as a git hook (in `.git/hooks`)
* `ln -s ../../git-hooks/commit-msg.py .git/hooks/commit-msg`
- Consider adding the `pre-commit` hook to automatically regenerate
`packaging/gui-less/snap/snapcraft.yaml` when `snap/snapcraft.yaml` changes
* `ln -s ../../git-hooks/pre-commit .git/hooks/pre-commit`

### Dependencies (DEP)

Expand Down
12 changes: 12 additions & 0 deletions git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
# Regenerates packaging/gui-less/snap/snapcraft.yaml when snap/snapcraft.yaml is staged.
# Install: ln -s ../../git-hooks/pre-commit .git/hooks/pre-commit

set -euo pipefail

if git diff --cached --name-only | grep -q "^snap/snapcraft\.yaml$"; then
REPO_ROOT="$(git rev-parse --show-toplevel)"
echo "snap/snapcraft.yaml staged — regenerating gui-less variant..."
python3 "${REPO_ROOT}/packaging/gui-less/generate-snapcraft.py"
git add "${REPO_ROOT}/packaging/gui-less/snap/snapcraft.yaml"
fi
1 change: 1 addition & 0 deletions git-hooks/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest
ruamel.yaml
92 changes: 92 additions & 0 deletions packaging/gui-less/generate-snapcraft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python3
"""Generates packaging/gui-less/snap/snapcraft.yaml from snap/snapcraft.yaml.
The transformations here are the authoritative definition of how the gui-less
variant differs from the main snap.

Usage:
./generate-snapcraft.py # regenerate packaging/gui-less/snap/snapcraft.yaml
./generate-snapcraft.py --check # verify it's up to date (for CI)
"""

import argparse
import re
import subprocess
import sys
from pathlib import Path

from ruamel.yaml import YAML
from ruamel.yaml.scalarstring import LiteralScalarString

SCRIPT_DIR = Path(__file__).parent
INPUT = SCRIPT_DIR / "../../snap/snapcraft.yaml"
OUTPUT = SCRIPT_DIR / "snap/snapcraft.yaml"

MULTIPASS_SOURCE = "https://github.com/canonical/multipass.git"
HEADER = (
"Generated by packaging/gui-less/generate-snapcraft.py — do not edit directly.\n"
"# Edit snap/snapcraft.yaml and re-run the script."
)


def generate(input_path: Path, output_path: Path) -> None:
yaml = YAML()
yaml.preserve_quotes = True
yaml.width = 4096

data = yaml.load(input_path)

# Add architectures for gui-less snap
data["platforms"]["s390x"] = None
data["platforms"]["ppc64el"] = None

# Remove the GUI app
del data["apps"]["gui"]

# Change multipass part to use remote git source
data["parts"]["multipass"]["source"] = MULTIPASS_SOURCE

# Use a CMake preset to turn off the GUI
data["parts"]["multipass"]["build-environment"].append(
{"CMAKE_PRESET": "snap-gui-less"}
)

# Change glue part to clone snap-wrappers from remote git source
glue = data["parts"]["glue"]
glue["source"] = MULTIPASS_SOURCE
Comment thread
xmkg marked this conversation as resolved.
glue["source-type"] = "git"
glue["source-subdir"] = "snap-wrappers"

data.yaml_set_start_comment(HEADER)

with output_path.open("w") as f:
yaml.dump(data, f)


def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--check", action="store_true", help="verify the output is up to date (for CI)")
args = parser.parse_args()

if not INPUT.exists():
print(f"ERROR: source file not found: {INPUT}", file=sys.stderr)
sys.exit(1)

generate(INPUT.resolve(), OUTPUT.resolve())

if args.check:
result = subprocess.run(
["git", "diff", "--exit-code", "snap/snapcraft.yaml"],
cwd=SCRIPT_DIR,
)
if result.returncode != 0:
print(
"ERROR: packaging/gui-less/snap/snapcraft.yaml is out of date.\n"
"Run packaging/gui-less/generate-snapcraft.py to update it.",
file=sys.stderr,
)
sys.exit(1)
print("packaging/gui-less/snap/snapcraft.yaml is up to date.")


if __name__ == "__main__":
main()
Loading
Loading