Skip to content

Commit

Permalink
flatpak: Replace source urls with file paths
Browse files Browse the repository at this point in the history
This allows users to keep the original url in the flatpak manifest
for reference.
At build time flatpak-builder will work with the file urls.
  • Loading branch information
perlpunk committed Jan 22, 2021
1 parent d757a81 commit a9bd4e3
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 42 deletions.
76 changes: 51 additions & 25 deletions Build/Flatpak.pm
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,30 @@ sub _load_yaml_file {
die "Neither YAML::PP nor YAML::XS available\n";
}

sub _read_manifest {
my ($fn) = @_;
my $data;
if ($fn =~ m/\.ya?ml\z/) {
$data = _load_yaml_file($fn);
return { error => "Failed to parse YAML file '$fn'" } unless defined $data;
} elsif ($fn =~ m/\.json\z/) {
# We don't have JSON::PP, but YAML is a superset of JSON anyway
$data = _load_yaml_file($fn);
return { error => "Failed to parse JSON file '$fn'" } unless defined $data;
} elsif (ref($fn) eq 'SCALAR') {
$data = _load_yaml($$fn); # used in the unit test
return { error => "Failed to parse '$fn'" } unless defined $data;
} else {
$data = _load_yaml_file($fn);
return { error => "Failed to parse file '$fn'" } unless defined $data;
}
return $data;
}

sub parse {
my ($cf, $fn) = @_;

my $version = '';
my $data;
my @lines;
if (ref($fn) eq 'SCALAR') {
@lines = split m/(?<=\n)/, $$fn;
Expand All @@ -86,26 +105,7 @@ sub parse {
}
}

if ($fn =~ m/\.ya?ml\z/) {
$data = _load_yaml_file($fn);
return { error => "Failed to parse YAML file '$fn'" } unless defined $data;
} elsif ($fn =~ m/\.json\z/) {
# We don't have JSON::PP, but YAML is a superset of JSON
$data = _load_yaml_file($fn);
return { error => "Failed to parse JSON file '$fn'" } unless defined $data;
# open my $fh, '<:encoding(UTF-8)', $fn or die $!;
# my $json = do { local $/; <$fh> };
# close $fh;
# $data = eval { decode_json($json) };
# return { error => "Failed to parse JSON file" } unless defined $data;
} elsif (ref($fn) eq 'SCALAR') {
$data = _load_yaml($$fn); # used in the unit test
return { error => "Failed to parse '$fn'" } unless defined $data;
} else {
$data = _load_yaml_file($fn);
return { error => "Failed to parse file '$fn'" } unless defined $data;
}

my $data = _read_manifest($fn);
my $ret = {};
$ret->{version} = $version if $version;
$ret->{name} = $data->{'app-id'} or die "Flatpak file is missing 'app-id'";
Expand All @@ -124,10 +124,7 @@ sub parse {
if (my $sources = $module->{sources}) {
for my $source (@$sources) {
if ($source->{type} eq 'archive') {
my $url = $source->{url};
my $path = $url;
$path =~ s{.*/}{}; # Get filename
push @sources, $path;
push @sources, $source->{url};
}
}
}
Expand All @@ -153,4 +150,33 @@ sub show {
}
}

# This replaces http urls with local file urls because during build
# flatpak-builder has no network
sub rewrite {
my ($fn) = @ARGV;
my $data = _read_manifest($fn);
if (my $modules = $data->{modules}) {
for my $module (@$modules) {
if (my $sources = $module->{sources}) {
for my $source (@$sources) {
if ($source->{type} eq 'archive') {
my $path = $source->{url};
$path =~ s{.*/}{}; # Get filename
$source->{url} = "file:///usr/src/packages/SOURCES/$path";
}
}
}
}
}
my $yaml = '';
if ($yamlpp) {
# YAML::PP would allow us to keep key order
$yaml = $yamlpp->dump_string($data);
}
elsif ($yamlxs) {
$yaml = YAML::XS::Dump($data);
}
print $yaml;
}

1;
4 changes: 4 additions & 0 deletions build-recipe-flatpak
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ recipe_build_flatpak() {
cp $BUILD_ROOT$TOPDIR/SOURCES/* .

local FLATPAK_FILE="$TOPDIR/SOURCES/$RECIPEFILE"
local FLATPAK_FILE_LOCAL="$TOPDIR/SOURCES/flatpak-local.yaml"
local FLATPAK_APP=$(perl -I$BUILD_DIR -MBuild::Flatpak -e 'Build::Flatpak::show' -- "$BUILD_ROOT$FLATPAK_FILE" name)
local FLATPAK_APP_VERSION=$(perl -I$BUILD_DIR -MBuild::Flatpak -e 'Build::Flatpak::show' -- "$BUILD_ROOT$FLATPAK_FILE" version)
# Rewrite http urls to file urls
perl -I$BUILD_DIR -MBuild::Flatpak -e 'Build::Flatpak::rewrite' -- "$BUILD_ROOT$FLATPAK_FILE" > "$BUILD_ROOT$FLATPAK_FILE_LOCAL"
FLATPAK_FILE="$FLATPAK_FILE_LOCAL"
FLATPAK_APP_VERSION="${FLATPAK_APP_VERSION:-0}"
local FLATPAK_REPO=/tmp/flatpakrepo
local FLATPAK_REMOTE=https://dl.flathub.org/repo/flathub.flatpakrepo
Expand Down
62 changes: 62 additions & 0 deletions t/data/flatpak-rewritten.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
app-id: org.gnome.Chess
cleanup:
- /share/gnuchess
- /share/info
- /share/man
- /include
command: gnome-chess
finish-args:
- --share=ipc
- --socket=fallback-x11
- --socket=wayland
- --metadata=X-DConf=migrate-path=/org/gnome/Chess/
modules:
- build-commands:
- make
- install -D phalanx /app/bin/phalanx
- install -D pbook.phalanx /app/share/phalanx/pbook.phalanx
- install -D sbook.phalanx /app/share/phalanx/sbook.phalanx
- install -D eco.phalanx /app/share/phalanx/eco.phalanx
buildsystem: simple
name: phalanx
sources:
- sha256: b3874d5dcd22c64626b2c955b18b27bcba3aa727855361a91eab57a6324b22dd
type: archive
url: file:///usr/src/packages/SOURCES/phalanx-XXV-source.tgz
- path: phalanx-books-path.patch
type: patch
- build-commands:
- make build
- install -D stockfish /app/bin/stockfish
build-options:
arch:
aarch64:
env:
ARCH: armv7
arm:
env:
ARCH: armv7
x86_64:
env:
ARCH: x86-64
buildsystem: simple
name: stockfish
sources:
- sha256: 29bd01e7407098aa9e851b82f6ea4bf2b46d26e9075a48a269cb1e40c582a073
type: archive
url: file:///usr/src/packages/SOURCES/stockfish-10-src.zip
- name: gnuchess
sources:
- sha256: 9a99e963355706cab32099d140b698eda9de164ebce40a5420b1b9772dd04802
type: archive
url: file:///usr/src/packages/SOURCES/gnuchess-6.2.5.tar.gz
- buildsystem: meson
name: gnome-chess
sources:
- sha256: b195c9f17a59d7fcc892ff55e6a6ebdd16e7329157bf37e3c2fe593b349aab98
type: archive
url: file:///usr/src/packages/SOURCES/gnome-chess-3.36.1.tar.xz
runtime: org.gnome.Platform
runtime-version: '3.36'
sdk: org.gnome.Sdk
8 changes: 4 additions & 4 deletions t/data/flatpak.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"sources": [
{
"type": "archive",
"url": "file:///usr/src/packages/SOURCES/phalanx-XXV-source.tgz",
"url": "https://example.org/phalanx-XXV-source.tgz",
"sha256": "b3874d5dcd22c64626b2c955b18b27bcba3aa727855361a91eab57a6324b22dd"
},
{
Expand Down Expand Up @@ -63,7 +63,7 @@
"sources": [
{
"type": "archive",
"url": "file:///usr/src/packages/SOURCES/stockfish-10-src.zip",
"url": "https://example.org/stockfish-10-src.zip",
"sha256": "29bd01e7407098aa9e851b82f6ea4bf2b46d26e9075a48a269cb1e40c582a073"
}
]
Expand All @@ -73,7 +73,7 @@
"sources": [
{
"type": "archive",
"url": "file:///usr/src/packages/SOURCES/gnuchess-6.2.5.tar.gz",
"url": "https://example.org/gnuchess-6.2.5.tar.gz",
"sha256": "9a99e963355706cab32099d140b698eda9de164ebce40a5420b1b9772dd04802"
}
]
Expand All @@ -84,7 +84,7 @@
"sources": [
{
"type": "archive",
"url": "file:///usr/src/packages/SOURCES/gnome-chess-3.36.1.tar.xz",
"url": "https://example.org/gnome-chess-3.36.1.tar.xz",
"sha256": "b195c9f17a59d7fcc892ff55e6a6ebdd16e7329157bf37e3c2fe593b349aab98"
}
]
Expand Down
8 changes: 4 additions & 4 deletions t/data/flatpak.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ modules:
- install -D eco.phalanx /app/share/phalanx/eco.phalanx
sources:
- type: archive
url: "file:///usr/src/packages/SOURCES/phalanx-XXV-source.tgz"
url: "https://example.org/phalanx-XXV-source.tgz"
sha256: b3874d5dcd22c64626b2c955b18b27bcba3aa727855361a91eab57a6324b22dd
- type: patch
path: phalanx-books-path.patch
Expand All @@ -46,18 +46,18 @@ modules:
- install -D stockfish /app/bin/stockfish
sources:
- type: archive
url: "file:///usr/src/packages/SOURCES/stockfish-10-src.zip"
url: "https://example.org/stockfish-10-src.zip"
sha256: 29bd01e7407098aa9e851b82f6ea4bf2b46d26e9075a48a269cb1e40c582a073

- name: gnuchess
sources:
- type: archive
url: "file:///usr/src/packages/SOURCES/gnuchess-6.2.5.tar.gz"
url: "https://example.org/gnuchess-6.2.5.tar.gz"
sha256: 9a99e963355706cab32099d140b698eda9de164ebce40a5420b1b9772dd04802

- name: gnome-chess
buildsystem: meson
sources:
- type: archive
url: "file:///usr/src/packages/SOURCES/gnome-chess-3.36.1.tar.xz"
url: "https://example.org/gnome-chess-3.36.1.tar.xz"
sha256: b195c9f17a59d7fcc892ff55e6a6ebdd16e7329157bf37e3c2fe593b349aab98
42 changes: 33 additions & 9 deletions t/flatpak.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ my $path = "$Bin/data";
use Build;
use Build::Flatpak;

my $rewritten_manifest = do {
local $/;
open my $fh, '<', "$Bin/data/flatpak-rewritten.yaml" or die $!;
<$fh>;
};
my $conf = Build::read_config('x86_64');

sub capture_stdout {
Expand All @@ -19,15 +24,19 @@ sub capture_stdout {
return $cap;
}

my @sources = map { "https://example.org/$_" } (
"phalanx-XXV-source.tgz",
"stockfish-10-src.zip",
"gnuchess-6.2.5.tar.gz",
"gnome-chess-3.36.1.tar.xz",
);

subtest parse => sub {
my $expected = {
name => 'org.gnome.Chess',
version => '3.36.2',
sources => [
"phalanx-XXV-source.tgz",
"stockfish-10-src.zip",
"gnuchess-6.2.5.tar.gz",
"gnome-chess-3.36.1.tar.xz",
@sources,
],
deps => [
'org.gnome.Sdk-v3.36',
Expand All @@ -52,7 +61,22 @@ subtest show => sub {

@ARGV = ("$path/flatpak.yaml", 'sources');
$data = capture_stdout(sub { Build::Flatpak::show() });
is $data, "phalanx-XXV-source.tgz\nstockfish-10-src.zip\ngnuchess-6.2.5.tar.gz\ngnome-chess-3.36.1.tar.xz\n", 'Build::Flatpak::show sources';
my $exp_sources = join '', map { "$_\n" } @sources;
is $data, $exp_sources, 'Build::Flatpak::show sources';
};

subtest rewrite => sub {
local @ARGV = ("$path/flatpak.yaml");
my $yaml = capture_stdout(sub { Build::Flatpak::rewrite() });
my $ok = is $yaml, $rewritten_manifest, 'Build::Flatpak::show rewrite';
unless ($ok) {
open my $fh, '>', "/tmp/flatpak.t.compare.yaml" or die $!;
print $fh $yaml;
close $fh;
my $diff = qx{diff $Bin/data/flatpak-rewritten.yaml /tmp/flatpak.t.compare.yaml};
diag "Diff:\n$diff";
}

};

done_testing;
Expand Down Expand Up @@ -85,7 +109,7 @@ __DATA__
"sources": [
{
"type": "archive",
"url": "file:///usr/src/packages/SOURCES/phalanx-XXV-source.tgz",
"url": "https://example.org/phalanx-XXV-source.tgz",
"sha256": "b3874d5dcd22c64626b2c955b18b27bcba3aa727855361a91eab57a6324b22dd"
},
{
Expand Down Expand Up @@ -123,7 +147,7 @@ __DATA__
"sources": [
{
"type": "archive",
"url": "file:///usr/src/packages/SOURCES/stockfish-10-src.zip",
"url": "https://example.org/stockfish-10-src.zip",
"sha256": "29bd01e7407098aa9e851b82f6ea4bf2b46d26e9075a48a269cb1e40c582a073"
}
]
Expand All @@ -133,7 +157,7 @@ __DATA__
"sources": [
{
"type": "archive",
"url": "file:///usr/src/packages/SOURCES/gnuchess-6.2.5.tar.gz",
"url": "https://example.org/gnuchess-6.2.5.tar.gz",
"sha256": "9a99e963355706cab32099d140b698eda9de164ebce40a5420b1b9772dd04802"
}
]
Expand All @@ -144,7 +168,7 @@ __DATA__
"sources": [
{
"type": "archive",
"url": "file:///usr/src/packages/SOURCES/gnome-chess-3.36.1.tar.xz",
"url": "https://example.org/gnome-chess-3.36.1.tar.xz",
"sha256": "b195c9f17a59d7fcc892ff55e6a6ebdd16e7329157bf37e3c2fe593b349aab98"
}
]
Expand Down

0 comments on commit a9bd4e3

Please sign in to comment.