1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#!/usr/bin/perl
# Bootstrapper for ChordPro config.
# Author : Johan Vromans
# Created On : Mon Jun 3 08:14:35 2024
# Last Modified By: Johan Vromans
# Last Modified On: Fri Sep 26 20:45:43 2025
# Update Count : 56
# Status : Unknown, Use with caution!
################ Common stuff ################
use v5.26;
use feature 'signatures';
no warnings 'experimental::signatures';
use FindBin;
use lib "$FindBin::Bin/../lib";
use lib "$FindBin::Bin/../CPAN";
use lib "$FindBin::Bin/../lib/ChordPro/lib";
# Package name.
my $my_package = 'ChordPro';
# Program name and version.
my ($my_name, $my_version) = qw( cfgboot 0.01 );
################ Command line parameters ################
use Getopt::Long 2.13;
# Command line options.
my $output;
my $verbose = 1; # verbose processing
my $debug = 0; # debugging
my $trace = 0; # trace (show process)
my $test = 0; # test mode.
# Process command line options.
app_options();
$trace |= ($debug || $test);
################ The Process ################
use JSON::Relaxed qw();
use JSON::XS qw();
use File::LoadLines;
use Encode qw(decode_utf8);
binmode STDOUT => ':utf8';
binmode STDERR => ':utf8';
my $parser = JSON::Relaxed::Parser->new
( booleans => [ $Types::Serialiser::false, $Types::Serialiser::true ] );
my $file = shift;
my $opts = { split => 0, fail => "soft" };
my $json = loadlines( $file, $opts );
die( "$file: $opts->{error}\n") if $opts->{error};
my $data = $parser->decode($json);
if ( $parser->is_error ) {
warn( "$file: JSON error: ", $parser->err_msg, "\n" );
next;
}
my $writer = JSON::XS->new->canonical->utf8(0)->pretty($test)->convert_blessed;
if ( $output && $output ne "-" ) {
open( my $fd, '>:utf8', $output )
or die("$output: $!\n");
select $fd;
}
print <<'EOD';
#! perl #### THIS IS A GENERATED FILE. DO NO MODIFY
package ChordPro::Config::Data;
use JSON::XS qw();
use JSON::Relaxed::Parser qw();
use feature qw(state);
EOD
print "# Config version.\nour \$VERSION = ", $data->{meta}->{_configversion}, ";\n\n";
print <<'EOD';
sub config {
state $pp = JSON::XS->new->utf8
->boolean_values( $JSON::Boolean::false, $JSON::Boolean::true );
$pp->decode( <<'EndOfJSON' );
EOD
print ( $writer->encode($data), "\n" );
print <<EOD;
EndOfJSON
}
1;
EOD
################ Subroutines ################
sub app_options() {
my $help = 0; # handled locally
my $ident = 0; # handled locally
# Process options, if any.
if ( !GetOptions(
'output=s' => \$output,
'ident' => \$ident,
'verbose+' => \$verbose,
'quiet' => sub { $verbose = 0 },
'trace' => \$trace,
'help|?' => \$help,
'debug' => \$debug
) or $help) {
app_usage(2);
}
app_ident() if $ident;
app_usage(2) unless @ARGV == 1;
}
sub app_ident() {
print STDERR ("This is $my_package [$my_name $my_version]\n");
print STDERR ("JSON::Relaxed version $JSON::Relaxed::VERSION\n");
}
sub app_usage( $exit ) {
app_ident();
print STDERR <<EndOfUsage;
Usage: $0 [options] config.json
--output=XXX optional output file
--ident shows identification
--help shows a brief help message and exits
--verbose provides more verbose information
--quiet runs as silently as possible
EndOfUsage
exit $exit if defined $exit && $exit != 0;
}
|