From 3e331d7b1af8c38aae8dedb960e1d6e495a93e20 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Wed, 30 Mar 2022 01:54:03 +0200 Subject: [PATCH] Refactoring the Peace::Model::Release generate_build method. --- lib/Peace/Model/Release.pm | 111 ++++++++++++++++++++++++++++--------- 1 file changed, 86 insertions(+), 25 deletions(-) diff --git a/lib/Peace/Model/Release.pm b/lib/Peace/Model/Release.pm index 11935b5..c111237 100644 --- a/lib/Peace/Model/Release.pm +++ b/lib/Peace/Model/Release.pm @@ -6,7 +6,7 @@ use strict; use warnings; use Params::ValidationCompiler qw/validation_for/; -use Types::Standard qw/InstanceOf Str HasMethods/; +use Types::Standard qw/InstanceOf Str HasMethods HashRef/; use Path::Tiny; use File::pushd; use YAML; @@ -74,29 +74,21 @@ use List::AllUtils qw/any/; say "CLONE_DIR: $clone_dir"; my $current_dir = pushd $clone_dir; my $flatpak_data = $self->_parse_flatpak_builder_file; - my $app_id = $flatpak_data->{"app-id"}; - say "$app_id is not the provided app_id ($application->app_id)", - return 1 - if $app_id ne $application->app_id; - say "APP_ID: $app_id"; - my $sdk = $flatpak_data->{sdk}; - say "SDK: $sdk"; - my $runtime = $flatpak_data->{runtime}; - say "RUNTIME: $runtime"; - my %installed_apps = map { (join(',',[split "\t"]->@[1,4]) => 1) } split "\n", `flatpak list`; - system 'flatpak', '--user', 'list' and return 1; - system 'flatpak', '--user', 'remote-add', '--if-not-exists', - 'remote', $application->flatpak_repo - and return 1; - say $sdk; - exists $installed_apps{"$sdk,$arch"} or system 'flatpak', '--user', 'install', '--arch', $arch, '-y', - "$sdk" - and return 1; - say $runtime; - exists $installed_apps{"$runtime,$arch"} or system 'flatpak', '--user', 'install', '--arch', $arch, '-y', - "$runtime" - and return 1; + if ( !defined $flatpak_data ) { + return 1; + } + my $app_id = $application->app_id; + my ( $sdk, $runtime ) = $flatpak_data->@{qw/sdk runtime/}; + say "APP_ID: $app_id"; + say "SDK: $sdk"; + say "RUNTIME: $runtime"; + return 1 + if $self->_install_dependencies( + arch => $arch, + runtime => $runtime, + sdk => $sdk + ); system 'flatpak-builder', '--arch', $arch, '--install', '--user', 'build', $application->flatpak_builder_file, $app_id and return 1; @@ -104,7 +96,7 @@ use List::AllUtils qw/any/; $output_dir->mkpath; return system 'flatpak', 'build-bundle', '--arch', $arch, $dir->child('.local/share/flatpak/repo/'), - $output_dir->child($arch), $app_id; + $output_dir->child("$arch.flatpak"), $app_id; }; my $success = 1; if ($exit) { @@ -118,6 +110,69 @@ use List::AllUtils qw/any/; } } +{ + my $validator = validation_for( + params => { + arch => { type => Str }, + runtime => { type => Str }, + sdk => { type => Str }, + } + ); + + sub _install_dependencies { + my $self = shift; + my %params = $validator->(@_); + my $application = $self->application; + my ( $arch, $runtime, $sdk ) = @params{qw/arch runtime sdk/}; + return 1 + if system 'flatpak', '--user', 'remote-add', '--if-not-exists', + 'remote', $application->flatpak_repo; + my $installed_apps = $self->_get_installed_flatpak_packages; + return 1 + unless $self->_install_flatpak_package_if_not_exists( + arch => $arch, + package => $sdk, + installed_apps => $installed_apps, + ); + return 1 + unless $self->_install_flatpak_package_if_not_exists( + arch => $arch, + package => $runtime, + installed_apps => $installed_apps, + ); + return; + } +} + +sub _get_installed_flatpak_packages { + return { + map { ( join( ',', [ split "\t" ]->@[ 1, 4 ] ) => 1 ) } + split "\n", + `flatpak list` + }; + +} + +{ + my $validator = validation_for( + params => { + arch => { type => Str }, + package => { type => Str }, + installed_apps => { type => HashRef }, + } + ); + + sub _install_flatpak_package_if_not_exists { + my $self = shift; + my %params = $validator->(@_); + my ( $arch, $package, $installed_apps ) = + @params{ 'arch', 'package', 'installed_apps' }; + return 1 if exists $installed_apps->{"$package,$arch"}; + return !system qw/flatpak --user install --arch/, $arch, '-y', + "$package"; + } +} + sub _parse_flatpak_builder_file { my $self = shift; my $application = $self->application; @@ -130,7 +185,13 @@ sub _parse_flatpak_builder_file { eval { ($structure) = Load($payload); }; } if ( !defined $structure ) { - die "Unable to parse flatpak builder file."; + say "Unable to parse flatpak builder file."; + return undef; + } + my $app_id = $structure->{"app-id"}; + if ( $app_id ne $application->app_id ) { + say "$app_id is not the provided app_id ($application->app_id)"; + return undef; } return $structure; }