pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/pkgtools/url2pkg/files pkgtools/url2pkg: document and ...



details:   https://anonhg.NetBSD.org/pkgsrc/rev/9cf8aa3d59a7
branches:  trunk
changeset: 399912:9cf8aa3d59a7
user:      rillig <rillig%pkgsrc.org@localhost>
date:      Sun Aug 18 17:34:13 2019 +0000

description:
pkgtools/url2pkg: document and clean up the code

diffstat:

 pkgtools/url2pkg/files/url2pkg.pl |  68 +++++++++++++++++++++-----------------
 pkgtools/url2pkg/files/url2pkg.t  |  20 +++++-----
 2 files changed, 47 insertions(+), 41 deletions(-)

diffs (209 lines):

diff -r 854cec1c4df5 -r 9cf8aa3d59a7 pkgtools/url2pkg/files/url2pkg.pl
--- a/pkgtools/url2pkg/files/url2pkg.pl Sun Aug 18 16:53:05 2019 +0000
+++ b/pkgtools/url2pkg/files/url2pkg.pl Sun Aug 18 17:34:13 2019 +0000
@@ -1,5 +1,5 @@
 #! @PERL5@
-# $NetBSD: url2pkg.pl,v 1.60 2019/08/18 16:18:04 rillig Exp $
+# $NetBSD: url2pkg.pl,v 1.61 2019/08/18 17:34:13 rillig Exp $
 #
 
 # Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -72,28 +72,6 @@
        return [$name, $op, $value];
 }
 
-sub add_section($$) {
-       my ($lines, $vars) = @_;
-
-       return if scalar(@$vars) == 0;
-
-       my $width = 0;
-       foreach my $var (@{$vars}) {
-               my ($name, $op, $value) = @$var;
-               next if $value eq "";
-               my $len = (length("$name$op\t") + 7) & -8;
-               $width = ($len > $width) ? $len : $width;
-       }
-
-       foreach my $var (@{$vars}) {
-               my ($name, $op, $value) = @$var;
-               next if $value eq "";
-               my $tabs = "\t" x (($width - length("$name$op") + 7) / 8);
-               push(@$lines, "$name$op$tabs$value");
-       }
-       push(@$lines, "");
-}
-
 sub read_lines($) {
        my ($filename) = @_;
 
@@ -125,6 +103,31 @@
        return scalar(@candidates) == 1 ? $candidates[0] : "";
 }
 
+# appends the given variable assignments to the lines, aligning the
+# variable values vertically.
+sub lines_add_vars($$) {
+       my ($lines, $vars) = @_;
+
+       return if scalar(@$vars) == 0;
+
+       my $width = 0;
+       foreach my $var (@$vars) {
+               my ($name, $op, $value) = @$var;
+               next if $value eq "";
+               my $len = (length("$name$op\t") + 7) & -8;
+               $width = ($len > $width) ? $len : $width;
+       }
+
+       foreach my $var (@$vars) {
+               my ($name, $op, $value) = @$var;
+               next if $value eq "";
+               my $tabs = "\t" x (($width - length("$name$op") + 7) / 8);
+               push(@$lines, "$name$op$tabs$value");
+       }
+       push(@$lines, "");
+}
+
+# changes the value of an existing variable in the lines.
 sub lines_set($$$) {
        my ($lines, $varname, $new_value) = @_;
 
@@ -142,7 +145,7 @@
        return false;
 }
 
-# appends the given value to the variable assignment.
+# appends to the value of an existing variable in the lines.
 sub lines_append($$$) {
        my ($lines, $varname, $value) = @_;
 
@@ -164,6 +167,7 @@
        return false;
 }
 
+# removes a variable assignment from the lines.
 sub lines_remove($$) {
        my ($lines, $varname) = @_;
 
@@ -179,6 +183,8 @@
        return false;
 }
 
+# removes a variable assignment from the lines if its value is the
+# expected one.
 sub lines_remove_if($$$) {
        my ($lines, $varname, $expected_value) = @_;
 
@@ -421,7 +427,7 @@
        while (defined(my $line = <CONF>)) {
                # "checksum cargo-package-name cargo-package-version
                if ($line =~ m/("checksum)\s(\S+)\s(\S+)/) {
-                       push(@build_vars, var("CARGO_CRATE_DEPENDS", "=", "$2-$3"));
+                       push(@build_vars, var("CARGO_CRATE_DEPENDS", "+=", "$2-$3"));
                }
        }
        close(CONF);
@@ -578,7 +584,7 @@
        push(@lines, "# \$" . "NetBSD\$");
        push(@lines, "");
 
-       add_section(\@lines, [
+       lines_add_vars(\@lines, [
                var("GITHUB_PROJECT", "=", $github_project),
                var("DISTNAME", "=", $distname),
                var("CATEGORIES", "=", $categories),
@@ -588,7 +594,7 @@
                var("DIST_SUBDIR", "=", $dist_subdir),
        ]);
 
-       add_section(\@lines, [
+       lines_add_vars(\@lines, [
                var("MAINTAINER", "=", get_maintainer()),
                var("HOMEPAGE", "=", $homepage),
                var("COMMENT", "=", "TODO: Short description of the package"),
@@ -620,7 +626,7 @@
        my @initial_lines = generate_initial_package_Makefile_lines($url);
        my @current_lines = read_lines("Makefile");
 
-       # don't risk to overwrite any changes by the package developer.
+       # don't risk to overwrite any changes made by the package developer.
        if (join('\n', @current_lines) ne join('\n', @initial_lines)) {
                splice(@$lines, -2, 0, "# TODO: Migrate MASTER_SITES to PYPI");
                return;
@@ -738,10 +744,10 @@
        push(@depend_vars, map { var("BUILD_DEPENDS", "+=", $_) } @build_depends);
        push(@depend_vars, map { var("DEPENDS", "+=", $_) } @depends);
        push(@depend_vars, map { var("TEST_DEPENDS", "+=", $_) } @test_depends);
-       add_section(\@lines, \@depend_vars);
+       lines_add_vars(\@lines, \@depend_vars);
 
-       add_section(\@lines, \@build_vars);
-       add_section(\@lines, \@extra_vars);
+       lines_add_vars(\@lines, \@build_vars);
+       lines_add_vars(\@lines, \@extra_vars);
 
        push(@lines, @bl3_lines);
        push(@lines, map { $_ = ".include \"$_\"" } @includes);
diff -r 854cec1c4df5 -r 9cf8aa3d59a7 pkgtools/url2pkg/files/url2pkg.t
--- a/pkgtools/url2pkg/files/url2pkg.t  Sun Aug 18 16:53:05 2019 +0000
+++ b/pkgtools/url2pkg/files/url2pkg.t  Sun Aug 18 17:34:13 2019 +0000
@@ -1,5 +1,5 @@
 # -*- perl -*-
-# $NetBSD: url2pkg.t,v 1.4 2019/08/18 16:18:04 rillig Exp $
+# $NetBSD: url2pkg.t,v 1.5 2019/08/18 17:34:13 rillig Exp $
 
 require "url2pkg.pl";
 
@@ -8,10 +8,10 @@
 use strict;
 use warnings;
 
-sub test_add_section__simple() {
+sub test_lines_add_vars__simple() {
        my $lines = [];
 
-       add_section($lines, [
+       lines_add_vars($lines, [
                var("1", "=", "one"),
                var("6", "=", "six"),
        ]);
@@ -23,10 +23,10 @@
        ]);
 }
 
-sub test_add_section__alignment() {
+sub test_lines_add_vars__alignment() {
        my $lines = [];
 
-       add_section($lines, [
+       lines_add_vars($lines, [
                var("short", "=", "value"),
                var("long_name", "=", "value # comment"),
        ]);
@@ -38,10 +38,10 @@
        ]);
 }
 
-sub test_add_section__operators() {
+sub test_lines_add_vars__operators() {
        my $lines = [];
 
-       add_section($lines, [
+       lines_add_vars($lines, [
                var("123456", "+=", "value"),
        ]);
 
@@ -111,9 +111,9 @@
        my $pkgsrcdir = $ENV{"PKGSRCDIR"} or die;
        chdir("$pkgsrcdir/pkgtools/url2pkg") or die;
 
-       test_add_section__simple();
-       test_add_section__alignment();
-       test_add_section__operators();
+       test_lines_add_vars__simple();
+       test_lines_add_vars__alignment();
+       test_lines_add_vars__operators();
        test_lines_append__not_found();
        test_lines_append__only_comment();
        test_lines_append__value_with_comment();



Home | Main Index | Thread Index | Old Index