b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | |
| 4 | my $error; |
| 5 | my %state; |
| 6 | |
| 7 | sub usage() { |
| 8 | die <<EOF; |
| 9 | Usage: $0 <file> <command> [<arguments>] |
| 10 | |
| 11 | Commands: |
| 12 | add-hash <variable> <value> |
| 13 | fix-hash <variable> <value> |
| 14 | rename-var <variable> <name> |
| 15 | |
| 16 | EOF |
| 17 | } |
| 18 | |
| 19 | sub set_var($) { |
| 20 | my $var = shift; |
| 21 | |
| 22 | $state{var} = $var; |
| 23 | if ($var =~ /(.*):(.*)/) { |
| 24 | $state{template} = $1; |
| 25 | $state{var} = $2; |
| 26 | $state{related_var} = "URL"; |
| 27 | } else { |
| 28 | $state{context} = 1; |
| 29 | $state{related_var} = "PKG_SOURCE_URL"; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | my %check_command = ( |
| 34 | "add-hash" => sub { |
| 35 | set_var($ARGV[0]); |
| 36 | |
| 37 | $state{value} = $ARGV[1]; |
| 38 | length($ARGV[1]) == 64 or die "Invalid hash value\n"; |
| 39 | }, |
| 40 | "fix-hash" => sub { |
| 41 | set_var($ARGV[0]); |
| 42 | |
| 43 | $state{value} = $ARGV[1]; |
| 44 | $state{prev_value} = $ARGV[2]; |
| 45 | |
| 46 | length($ARGV[1]) == 64 or die "Invalid hash value\n"; |
| 47 | }, |
| 48 | "rename-var" => sub { |
| 49 | set_var($ARGV[0]); |
| 50 | $state{new_var} = $ARGV[1]; |
| 51 | $state{new_var} =~ s/.*://g; |
| 52 | }, |
| 53 | ); |
| 54 | |
| 55 | sub check_context($) { |
| 56 | my $line = shift; |
| 57 | return unless $state{template}; |
| 58 | |
| 59 | $state{next} and do { |
| 60 | $state{context} = 1; |
| 61 | undef $state{next}; |
| 62 | return; |
| 63 | }; |
| 64 | |
| 65 | if (not $state{context}) { |
| 66 | $line =~ /^\s*define\s+$state{template}/ and $state{next} = 1; |
| 67 | } else { |
| 68 | $line =~ /^\s*endef/ and do { |
| 69 | $state{done} = 1; |
| 70 | undef $state{context}; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | my %commands = ( |
| 76 | "add-hash" => sub { |
| 77 | my $line = shift; |
| 78 | check_context($line); |
| 79 | return $line unless $state{context}; |
| 80 | |
| 81 | # skip existing hash variable |
| 82 | return "" if $line =~ /^(\s*)$state{var}(\s*):?=(\s*)(.*)\n/; |
| 83 | |
| 84 | # insert md5sum after related variable |
| 85 | return $line unless $line =~ /^(\s*)$state{related_var}(\s*):?=(\s*)(.*)\n/; |
| 86 | return "$line$1$state{var}$2:=$3$state{value}\n"; |
| 87 | }, |
| 88 | "fix-hash" => sub { |
| 89 | my $line = shift; |
| 90 | check_context($line); |
| 91 | return $line unless $state{context}; |
| 92 | return $line unless $line =~ /^(\s*)$state{var}(\s*):?=(\s*)(.*)$state{prev_value}(.*)\n/; |
| 93 | $state{done} = 1; |
| 94 | $4 =~ /\$/ and do { |
| 95 | warn "$state{var} contains a reference to another variable, can't fix automatically\n"; |
| 96 | return $line; |
| 97 | }; |
| 98 | return "$1$state{var}$2:=$3$state{value}\n"; |
| 99 | }, |
| 100 | "rename-var" => sub { |
| 101 | my $line = shift; |
| 102 | check_context($line); |
| 103 | return $line unless $state{context}; |
| 104 | return $line unless $line =~ /^(\s*)$state{var}(\s*:?=.*)\n/; |
| 105 | return "$1$state{new_var}$2\n"; |
| 106 | }, |
| 107 | ); |
| 108 | |
| 109 | my $file = shift @ARGV; |
| 110 | my $command = shift @ARGV; |
| 111 | |
| 112 | ($file and $command and $check_command{$command}) or usage; |
| 113 | &{$check_command{$command}}(); |
| 114 | |
| 115 | -f $file or die "File $file not found\n"; |
| 116 | |
| 117 | open IN, "<${file}" or die "Cannot open input file\n"; |
| 118 | open OUT, ">${file}.new" or die "Cannot open output file\n"; |
| 119 | |
| 120 | my $cmd = $commands{$command}; |
| 121 | while (my $line = <IN>) { |
| 122 | $line = &$cmd($line) unless $state{done}; |
| 123 | print OUT $line; |
| 124 | last if $error; |
| 125 | } |
| 126 | |
| 127 | close OUT; |
| 128 | close IN; |
| 129 | |
| 130 | $error and do { |
| 131 | unlink "${file}.new"; |
| 132 | exit 1; |
| 133 | }; |
| 134 | |
| 135 | rename "${file}.new", "$file"; |