Skip to content
Merged
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
38 changes: 25 additions & 13 deletions flatpaker/actions/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import annotations

import os
import pathlib
import shutil
import typing
Expand All @@ -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()

Expand Down Expand Up @@ -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)

Expand All @@ -58,33 +61,42 @@ 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)

if args.files:
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
Loading