Skip to content

Commit f8f3b97

Browse files
committed
Enable update of config.gc and config_H.gc using hashes
This avoids use of patch files against moving targets as versions change.
1 parent baf566e commit f8f3b97

File tree

3 files changed

+108
-3
lines changed

3 files changed

+108
-3
lines changed

lib/Perl/Dist/Strawberry.pm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ sub message {
422422

423423
sub resolve_name {
424424
my ($self, $name, $skip_canon) = @_;
425+
426+
# don't change references
427+
return $name if ref ($name);
428+
425429
if ($name =~ /<(package_url|dist_sharedir|image_dir)>/) {
426430
my $r = $self->global->{$1};
427431
$name =~ s/<(package_url|dist_sharedir|image_dir)>/$r/g if defined $r;

lib/Perl/Dist/Strawberry/Step.pm

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,15 @@ $self->boss->message(5, "PATCHING '$new' '$dst' '$dir' $tt_vars $no_backup\n");
293293
if ($dst =~ /\*$/) {
294294
warn "WE IS PATCHIN '$new'";
295295
}
296-
if (!-f $new) {
296+
if ($new eq 'config_H.gc' and ref($dst) =~ /HASH/) {
297+
$self->boss->message(5, "_patch_file: using hash of values to update config_H.gc'\n");
298+
$self->_update_config_H_gc ("$dir/win32/config_H.gc", $dst);
299+
}
300+
elsif ($new eq 'config.gc' and ref($dst) =~ /HASH/) {
301+
$self->boss->message(5, "_patch_file: using hash of values to update config.gc'\n");
302+
$self->_update_config_gc ("$dir/win32/config.gc", $dst);
303+
}
304+
elsif (!-f $new) {
297305
warn "ERROR: non-existing file '$new'";
298306
}
299307
elsif ($new =~ /\.tt$/) {
@@ -519,5 +527,96 @@ sub _apply_patch {
519527
}
520528
}
521529

530+
sub _update_config_H_gc {
531+
my ($self, $fname, $update_hash) = @_;
532+
533+
die "update hash arg is not a hash ref"
534+
if not ref($update_hash) =~ /HASH/;
535+
536+
open my $fh, $fname or die "Unable to open $fname, $!";
537+
538+
my $output;
539+
while (defined (my $line = <$fh>)) {
540+
$line =~ s/[\r\n]+$//;
541+
if ($line =~ /#define\s+(\w+)/ and exists $update_hash->{$1}) {
542+
my $key = $1;
543+
$line
544+
= !defined $update_hash->{$key} ? "/*#define $key\t\t/ **/"
545+
: $update_hash->{$key} eq 'define' ? "#define $key\t\t/* */"
546+
: "$update_hash->{$key}";
547+
}
548+
$output .= "$line\n";
549+
}
550+
551+
$fh->close;
552+
553+
554+
rename $fname, "$fname.orig" or die $!;
555+
open my $ofh, '>', $fname or die "Unable to open $fname to write to, $!";
556+
print {$ofh} $output;
557+
$ofh->close;
558+
559+
}
560+
561+
sub _update_config_gc {
562+
my ($self, $fname, $update_hash) = @_;
563+
564+
die "update hash arg is not a hash ref"
565+
if not ref($update_hash) =~ /HASH/;
566+
567+
open my $fh, $fname or die "Unable to open $fname, $!";
568+
569+
my @lines = (<$fh>);
570+
close $fh;
571+
572+
my %data;
573+
my @output;
574+
my @perl_lines; # lines starting with PERL
575+
576+
while (defined(my $line = shift @lines)) {
577+
$line =~ s/[\r\n]+$//;
578+
if ($line =~ /^#/) {
579+
# headers stay as they are
580+
push @output, $line;
581+
}
582+
elsif ($line =~ /^PERL/) {
583+
push @perl_lines, $line;
584+
}
585+
else {
586+
$line =~ m/^([\w]+)=(.+)$/;
587+
$data{$1} = $2;
588+
}
589+
}
590+
591+
# fix up quoting of values
592+
foreach my $val (values %$update_hash) {
593+
next if $val =~ /^'/; # assumes symmetry, i.e. opening and closing
594+
$val = "'$val'";
595+
}
596+
597+
@data{keys %$update_hash} = values %$update_hash;
598+
#foreach my $key (sort keys %$update_hash) {
599+
#
600+
#$self->boss->message(3, "Setting config, $key => $update_hash->{$key}");
601+
#$data{$key} = $update_hash->{$key};
602+
#}
603+
604+
my (@ucfirst_lines, @lcfirst_lines);
605+
foreach my $key (grep {/^[A-Z]/} keys %data) {
606+
push @ucfirst_lines, "$key=$data{$key}";
607+
}
608+
foreach my $key (grep {/^[_a-z]/} keys %data) {
609+
push @lcfirst_lines, "$key=$data{$key}";
610+
}
611+
push @output, (sort @ucfirst_lines), (sort @lcfirst_lines), @perl_lines;
612+
613+
rename $fname, "$fname.orig" or die $!;
614+
open my $ofh, '>', $fname or die "Unable to open $fname to write to, $!";
615+
say {$ofh} join "\n", @output;
616+
$ofh->close;
617+
618+
}
619+
620+
522621
1;
523622

lib/Perl/Dist/Strawberry/Step/InstallPerlCore.pm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ sub run {
7878
my $patch = $self->{config}->{patch};
7979
if ($patch) {
8080
while (my ($new, $dst) = each %$patch) {
81-
$self->_patch_file($self->boss->resolve_name($new), catfile($unpack_to, $perlsrc, $dst), catdir($unpack_to, $perlsrc), $tt_vars);
81+
# double pack refs, or update the names
82+
$dst = ref ($dst) ? $dst : catfile($unpack_to, $perlsrc, $dst);
83+
$self->_patch_file($self->boss->resolve_name($new), $dst, catdir($unpack_to, $perlsrc), $tt_vars);
8284
}
8385
}
8486

@@ -293,4 +295,4 @@ sub _strip_debug {
293295
}
294296
}
295297

296-
1;
298+
1;

0 commit comments

Comments
 (0)