Peace/lib/Peace/Model/Release.pm

279 lines
6.9 KiB
Perl

package Peace::Model::Release;
use v5.30.0;
use strict;
use warnings;
use Params::ValidationCompiler qw/validation_for/;
use Types::Standard qw/InstanceOf Str HasMethods/;
use Path::Tiny;
use File::pushd;
use YAML;
use JSON;
{
my $validator = validation_for(
params => {
uuid => { type => Str, optional => 1 },
date_creation => { type => InstanceOf ['DateTime'], optional => 1 },
application => {
type => InstanceOf ['Peace::Model::Application'],
optional => 1
},
application_uuid => { type => Str, optional => 1 },
dbh => { type => HasMethods ['selectrow_hashref'], optional => 1 },
tag => { type => Str },
name => { type => Str },
}
);
sub new {
my $class = shift;
my %params = $validator->(@_);
die
'application or application_uuid with dbh should be passed on construct'
unless exists $params{application}
|| ( exists $params{application_uuid} && exists $params{dbh} );
return bless {%params}, $class;
}
}
{
my $validator = validation_for(
params => {
arch => { type => Str },
}
);
sub generate_build {
my $self = shift;
my %params = $validator->(@_);
my $application = $self->application;
my $flatpak_builder_file = $application->flatpak_builder_file;
my $arch = $params{arch};
my $dir = Path::Tiny->tempdir;
my $clone_dir = Path::Tiny->tempdir;
my $output_dir = Path::Tiny->new($ENV{HOME})->child('.peace-apps');
local $ENV{HOME} = $dir;
say $dir;
system 'git', 'clone', $application->git_repo, $clone_dir;
my $current_dir = pushd $clone_dir;
my $flatpak_data = $self->_parse_flatpak_builder_file;
my $app_id = $flatpak_data->{"app-id"};
my $sdk = $flatpak_data->{sdk};
my $runtime = $flatpak_data->{runtime};
system 'flatpak', '--user', 'list';
system 'flatpak', '--user', 'remote-add', '--if-not-exists', 'remote', $application->flatpak_repo;
say $sdk;
system 'flatpak', '--user', 'install', '--arch', $arch, '-y', "$sdk";
say $runtime;
system 'flatpak', '--user', 'install', '--arch', $arch, '-y', "$runtime";
system 'flatpak-builder', '--arch', $arch, '--install', '--user', 'build', $application->flatpak_builder_file, $app_id;
$output_dir = $output_dir->child($self->uuid);
$output_dir->mkpath;
system 'flatpak', 'build-bundle', '--arch', $arch, $dir->child('.local/share/flatpak/repo/'), $output_dir->child($arch), $app_id;
}
}
sub _parse_flatpak_builder_file {
my $self = shift;
my $application = $self->application;
my $flatpak_builder_file = $application->flatpak_builder_file;
my $path = Path::Tiny->new($flatpak_builder_file);
my $payload = $path->slurp_utf8;
my $structure;
eval {
$structure = decode_json($payload);
};
if (!defined $structure) {
eval {
($structure) = Load($payload);
};
}
if (!defined $structure) {
die "Unable to parse flatpak builder file.";
}
return $structure;
}
sub _dbh {
my $self = shift;
return $self->{dbh};
}
{
my $validator =
validation_for( params => [ { type => Str, optional => 1 } ] );
sub uuid {
my $self = shift;
if (@_) {
my ($new_uuid) = $validator->(@_);
$self->{uuid} = $new_uuid;
}
return $self->{uuid};
}
}
{
my $validator =
validation_for(
params => [ { type => InstanceOf ['DateTime'], optional => 1 } ] );
sub date_creation {
my $self = shift;
if (@_) {
my ($new_date_creation) = $validator->(@_);
$self->{date_creation} = $new_date_creation;
}
return $self->{date_creation};
}
}
{
my $validator = validation_for(
params => [
{ type => InstanceOf ['Peace::Model::Application'], optional => 1 }
]
);
sub application {
my $self = shift;
if (@_) {
my ($new_application) = $validator->(@_);
$self->{application} = $new_application;
}
unless ( exists $self->{application} ) {
my $application_uuid = $self->{application_uuid};
my $application_dao =
Peace::DAO::Application->new( dbh => $self->_dbh );
$self->{application} =
$application_dao->recover_by_uuid( uuid => $application_uuid );
}
return $self->{application};
}
}
{
my $validator = validation_for(
params => [
{
type => Str, optional => 1,
}
]
);
sub tag {
my $self = shift;
if (@_) {
my $new_tag = $validator->(@_);
$self->{tag} = $new_tag;
}
return $self->{tag};
}
}
{
my $validator = validation_for(
params => [
{ type => Str, optional => 1 },
]
);
sub name {
my $self = shift;
if (@_) {
my ($new_name) = $validator->(@_);
$self->{name} = $new_name;
}
return $self->{name};
}
}
1;
=encoding utf8
=head1 NAME
Peace::Model::Release - The release object representation.
=head1 SYNOPSIS
my $release = Peace::Model::Release->new(
application => $application,
tag => $tag,
name => $name,
);
=head1 DESCRIPTION
Describes a release from an application from Peace.
=head1 INSTANCE METHODS
Peace::Model::Release implements the following instance
methods:
=head2 new
my $release = Peace::Model::Release->new(
uuid => $uuid, # optional
date_creation => $date_creation, # optional
application => $application, # required or application_uuid should be passed.
application_uuid => $application_uuid, # required or application should be passed
dbh => $dbh, # needed if application_uuid is passed
tag => $tag,
name => $name,
);
=head1 METHODS
Peace::Model::Release implements the following methods:
=head2 uuid
my $uuid = $release->uuid;
$release->uuid($uuid);
Allows to retrieve and set the release uuid.
=head2 date_creation
my $date_creation = $release->date_creation;
$release->date_creation($date_creation);
Allows to retrieve and set the release date creation as a L<DateTime>.
=head2 application
my $application = $release->application;
$release->application($application);
Allows to retrieve and set the release application as a L<Peace::Model::Application>.
=head2 tag
my $tag = $release->tag;
$release->tag($tag);
Allows to retrieve and set the release tag.
=head2 name
my $name = $release->name
$release->name($name);
Allows to retrieve and set the release name.
=head1 SEE ALSO
L<Peace::Model::Application>, L<Peace::DAO::Release>
=cut