diff --git a/flatpaker/actions/generate.py b/flatpaker/actions/generate.py index 94af8b1..8a28bf9 100644 --- a/flatpaker/actions/generate.py +++ b/flatpaker/actions/generate.py @@ -3,6 +3,7 @@ from __future__ import annotations +import os import pathlib import shutil import typing @@ -19,7 +20,9 @@ def generate(args: GenerateArguments) -> bool: name = f'{args.url}.{util.sanitize_name(args.appname)}' - sourcedir = pathlib.Path('sources') / name + projectdir = pathlib.Path(name) + sourcedir = projectdir / 'sources' + patchdir = projectdir / 'patches' doc = tomlkit.document() @@ -47,7 +50,7 @@ def add(table: tomlkit.items.Table, key: str, entry: object, archives: list[tomlkit.items.Table] = [] for src in [args.archive] + args.archives: archive = tomlkit.table() - add(archive, 'path', sourcedir.joinpath(pathlib.Path(src).name).as_posix()) + add(archive, 'path', os.path.join(sourcedir.name, os.path.basename(src))) add(archive, 'sha256', util.sha256(pathlib.Path(src))) archives.append(archive) @@ -58,7 +61,7 @@ def add(table: tomlkit.items.Table, key: str, entry: object, patches: list[tomlkit.items.Table] = [] for src in args.patches: patch = tomlkit.table() - add(patch, 'path', sourcedir.joinpath(pathlib.Path(src).name).as_posix()) + add(patch, 'path', os.path.join(patchdir.name, os.path.basename(src))) patches.append(patch) sources.add('patches', patches) @@ -66,25 +69,34 @@ def add(table: tomlkit.items.Table, key: str, entry: object, files: list[tomlkit.items.Table] = [] for src in args.patches: file = tomlkit.table() - add(file, 'path', sourcedir.joinpath(pathlib.Path(src).name).as_posix()) + add(file, 'path', os.path.join(sourcedir.name, os.path.basename(src))) files.append(file) sources.add('files', files) doc.add('sources', sources) - sourcedir.mkdir(parents=True, exist_ok=True) - # this ensures that even if the sources are not checked into git that the + # this ensures that even if there are not patches checked into git that the # folder will be - sourcedir.joinpath('.gitkeep').touch() + patchdir.mkdir(parents=True, exist_ok=True) + patchdir.joinpath('.gitkeep').touch() + + # It's assumed that sources will not be checked into git but by writing a + # file in the directory, we ensure that the directory will be saved/restored + # by `git`. + sourcedir.mkdir(parents=True, exist_ok=True) + with sourcedir.joinpath('.gitignore').open('w', encoding='utf-8') as f: + f.write('*') - with open(f'{name}.toml', 'w') as f: + with projectdir.joinpath('build.toml').open('w', encoding='utf-8') as f: tomlkit.dump(doc, f) # move files after writing the toml, so we don't move things then fail - for src in [args.archive] + args.archives + args.patches + args.files: - srcp = pathlib.Path(src) - dest = sourcedir / srcp.name - if dest != srcp: - shutil.move(src, sourcedir) + for srcs, subdir in [([args.archive] + args.archives + args.files, sourcedir), + (args.patches, patchdir)]: + for src in srcs: + srcp = pathlib.Path(src) + dest = subdir / srcp.name + if dest != srcp: + shutil.move(src, subdir) return True