Skip to content

Commit

Permalink
fix[build]: scan also META.json for additional needed CPAN packages.
Browse files Browse the repository at this point in the history
  • Loading branch information
sidey79 committed Mar 6, 2024
1 parent 0db2d23 commit b555882
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 7 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ jobs:
perl-version: "5.38"
install-modules-with: cpanm
install-modules-args: --notest
install-modules: PPI Perl::PrereqScanner::NotQuiteLite File::Find::Rule List::MoreUtils

install-modules: PPI Perl::PrereqScanner::NotQuiteLite File::Find::Rule List::MoreUtils CPAN::Meta Module::CPANfile CPAN::Meta::Merge Scalar::Util

- name: clone 3rdparty repositories at github
run: |
Expand All @@ -55,17 +55,20 @@ jobs:
- name: create cpanfile from local cloned 3rdparty repositories
run: |
scan-perl-prereqs-nqlite -save_cpanfile -suggests -private_re "^(FHEM::|Win32::|YAF$|OW$|RTypes$|RRDs$|SetExtensions$|HttpUtils$|UPnP::ControlPoint$|FritzBoxUtils$|configDB$|RESIDENTStk$|SHC_datafields$|TcpServerUtils$|Blocking$|uConv$|ZWLib$|UpNp:Common|HttpUtils$|Unit$|GD$|DevIo$|AttrTemplate$|ProtoThreads$|$FHEM_MODULES)" ./3rdparty
perl scripts/parse-METAJson.pl ./3rdparty
- uses: actions/upload-artifact@v4
with:
name: cpanfile-3rdParty
path: cpanfile
overwrite: true

- name: create cpanfile from FHEM dependencies
- name: create cpanfile from FHEM svn dependencies
run: |
rm cpanfile
scan-perl-prereqs-nqlite -save_cpanfile -suggests -private_re "^(FHEM::|Win32::|YAF$|OW$|RTypes$|RRDs$|SetExtensions$|HttpUtils$|UPnP::ControlPoint$|FritzBoxUtils$|configDB$|RESIDENTStk$|SHC_datafields$|TcpServerUtils$|Blocking$|uConv$|ZWLib$|UpNp:Common|HttpUtils$|Unit$|GD$|DevIo$|AttrTemplate$|ProtoThreads$|$FHEM_MODULES)" ./src/fhem/trunk
perl scripts/parse-METAJson.pl ./src/fhem/trunk
- uses: actions/upload-artifact@v4
with:
name: cpanfile-FHEM
Expand Down Expand Up @@ -226,7 +229,7 @@ jobs:
- name: Run build in unittests
run: |
CONTAINER=$(docker run -d -ti --health-interval=10s --health-timeout=8s --health-start-period=10s --health-retries=5 -e CPAN_PKGS=Test2::Suite ${{ fromJSON(steps.meta.outputs.json).tags[0] }} )
CONTAINER=$(docker run -d -ti --health-interval=10s --health-timeout=8s --health-start-period=10s --health-retries=5 ${{ fromJSON(steps.meta.outputs.json).tags[0] }} )
sleep 15;
until [ "$(/usr/bin/docker inspect -f {{.State.Health.Status}} $CONTAINER)" == "healthy" ];
do sleep 1;
Expand Down
2 changes: 1 addition & 1 deletion scripts/get-Packages.pl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use PPI;
use File::Find::Rule;
use List::MoreUtils qw(uniq);
use Data::Dumper;
# use Data::Dumper;


my @directories = @ARGV;
Expand Down
136 changes: 136 additions & 0 deletions scripts/parse-METAJson.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/bin/perl
use strict;
use warnings;
use CPAN::Meta;
use Module::CPANfile;
# use Data::Dumper;
use CPAN::Meta::Merge;
use Scalar::Util qw/blessed/;
use File::Find::Rule;
use JSON;

my @directories = @ARGV;

#my $filename = @ARGV # path must be provided to our script
my @JSONlines;
my $jsonString;
#my $line;
## Load existing requirements from cpanfile
my $cpanfile = Module::CPANfile->load('cpanfile');

my $newCPANFile;
# Alle Perl-Moduldateien im Verzeichnisbaum finden
foreach my $directory (@directories) {


my @files = File::Find::Rule->file()->name('*.pm')->in($directory);

foreach my $filename (@files) {
$jsonString="";
@JSONlines=();

print qq[\n try processing file $filename ...];
open(my $fh, '<', $filename) or die "can not open file: $!";
my $inside_for = 0;

while (<$fh>) {
if (/^=for :application\/json;q=META.json/) {
$inside_for = 1;
next;
}
if ($inside_for) {
last if /^=end :application\/json;q=META.json/; # Ende des =for-Abschnitts
$_ =~ s/\R//g;

push @JSONlines, $_;

}
}
close($fh);
if (!@JSONlines)
{
print "aborting, no META.json found\n";
next;
}

$jsonString = join '', @JSONlines;

## Script breaks here, because we may have no version field which is requred to pass here

my $MetaHash;
eval {
$MetaHash = from_json($jsonString) ;
1;
} or do {
print q[[ failed ]]. $@;
next;
};

#print Dumper $MetaHash;

# fix missing version information
my @fixups = ();

if (!exists($MetaHash->{version}) )
{
push(@fixups, q[version] );

$MetaHash->{version} = "1";

}
if (!exists($MetaHash->{name}) )
{
push(@fixups, q[name] );
$MetaHash->{name} = $filename;
}

if (!exists($MetaHash->{'meta-spec'}) || !exists($MetaHash->{'meta-spec'}{'version'}) )
{
push(@fixups, q[meta-spec] );
$MetaHash->{'meta-spec'}{'version'} = 2;
}

if (scalar @fixups > 0)
{
print q[ fixups: ];
print join ", ", @fixups;
print q[ ];
}


my $moduleMeta;
eval {
$moduleMeta = CPAN::Meta->load_json_string(to_json($MetaHash));
1;
} or do {
print q[[ failed ]]. $@;
next;
};


# merge requirements

my $prereqs_hash = $cpanfile->prereqs->with_merged_prereqs($moduleMeta->effective_prereqs)->as_string_hash;
my $struct = { %{$moduleMeta->as_struct}, prereqs => $prereqs_hash };

#print $moduleMeta->meta_spec->{version};

my $mergedMeta = CPAN::Meta->new($struct);
$newCPANFile = Module::CPANfile->from_prereqs( $mergedMeta->prereqs );

$cpanfile = $newCPANFile;
print qq[$filename was processed successfull\n];
}
}

# save our new cpanfile
if (defined $newCPANFile)
{
$newCPANFile->save('cpanfile');
print qq[\n\nResult: cpanfile was saved\n];
}


# example usage
# perl scripts/parse-METAJson.pl src/FHEM/trunk/fhem/FHEM/ 3rdParty/
#

0 comments on commit b555882

Please sign in to comment.