From 64273cc51bcd7efb60e7c1636ee75696a791db22 Mon Sep 17 00:00:00 2001 From: Peter Powell Date: Fri, 24 Mar 2017 21:00:01 +0000 Subject: Rename read_configure_cache to read_config_file and move to common. --- make/common.pm | 16 +++++++++++++++- make/configure.pm | 17 ++--------------- 2 files changed, 17 insertions(+), 16 deletions(-) (limited to 'make') diff --git a/make/common.pm b/make/common.pm index b1608db56..d5a2f06c7 100644 --- a/make/common.pm +++ b/make/common.pm @@ -1,7 +1,7 @@ # # InspIRCd -- Internet Relay Chat Daemon # -# Copyright (C) 2013-2014 Peter Powell +# Copyright (C) 2013-2017 Peter Powell # # This file is part of InspIRCd. InspIRCd is free software: you can # redistribute it and/or modify it under the terms of the GNU General Public @@ -34,6 +34,7 @@ use File::Spec::Functions qw(rel2abs); our @EXPORT = qw(create_directory get_cpu_count get_version + read_config_file module_installed); sub create_directory($$) { @@ -107,4 +108,17 @@ sub get_cpu_count { return $count; } +sub read_config_file($) { + my $path = shift; + my %config; + open(my $fh, $path) or return %config; + while (my $line = <$fh>) { + next if $line =~ /^\s*($|\#)/; + my ($key, $value) = ($line =~ /^(\S+)(?:\s(.*))?$/); + $config{$key} = $value; + } + close $fh; + return %config; +} + 1; diff --git a/make/configure.pm b/make/configure.pm index 59657bfc4..ed03f5b24 100644 --- a/make/configure.pm +++ b/make/configure.pm @@ -1,7 +1,7 @@ # # InspIRCd -- Internet Relay Chat Daemon # -# Copyright (C) 2012-2014 Peter Powell +# Copyright (C) 2012-2017 Peter Powell # Copyright (C) 2008 Robin Burchell # Copyright (C) 2007-2008 Craig Edwards # Copyright (C) 2008 Thomas Stagner @@ -52,7 +52,6 @@ our @EXPORT = qw(CONFIGURE_CACHE_FILE run_test test_file test_header - read_configure_cache write_configure_cache get_compiler_info find_compiler @@ -180,7 +179,7 @@ EOH sub cmd_update { print_error "You have not run $0 before. Please do this before trying to update the generated files." unless -f CONFIGURE_CACHE_FILE; say 'Updating...'; - my %config = read_configure_cache(); + my %config = read_config_file(CONFIGURE_CACHE_FILE); my %compiler = get_compiler_info($config{CXX}); my %version = get_version $config{DISTRIBUTION}; parse_templates(\%config, \%compiler, \%version); @@ -215,18 +214,6 @@ sub test_header($$;$) { return !$?; } -sub read_configure_cache { - my %config; - open(CACHE, CONFIGURE_CACHE_FILE) or return %config; - while (my $line = ) { - next if $line =~ /^\s*($|\#)/; - my ($key, $value) = ($line =~ /^(\S+)(?:\s(.*))?$/); - $config{$key} = $value; - } - close(CACHE); - return %config; -} - sub write_configure_cache(%) { unless (-e CONFIGURE_DIRECTORY) { print_format "Creating <|GREEN ${\CONFIGURE_DIRECTORY}|> ...\n"; -- cgit v1.3.1-10-gc9f91 From c185edf71cdd9aa4b72ffd9059534d8ac0cb1249 Mon Sep 17 00:00:00 2001 From: Peter Powell Date: Fri, 24 Mar 2017 21:44:33 +0000 Subject: Extract core logic of write_configure_cache to write_config_file. --- make/common.pm | 14 ++++++++++++++ make/configure.pm | 7 +------ 2 files changed, 15 insertions(+), 6 deletions(-) (limited to 'make') diff --git a/make/common.pm b/make/common.pm index d5a2f06c7..f0174e0b3 100644 --- a/make/common.pm +++ b/make/common.pm @@ -31,10 +31,13 @@ use Exporter qw(import); use File::Path qw(mkpath); use File::Spec::Functions qw(rel2abs); +use make::console; + our @EXPORT = qw(create_directory get_cpu_count get_version read_config_file + write_config_file module_installed); sub create_directory($$) { @@ -121,4 +124,15 @@ sub read_config_file($) { return %config; } +sub write_config_file($%) { + my $path = shift; + my %config = @_; + open(my $fh, '>', $path) or print_error "unable to write to $path: $!"; + while (my ($key, $value) = each %config) { + $value //= ''; + say $fh "$key $value"; + } + close $fh; +} + 1; diff --git a/make/configure.pm b/make/configure.pm index ed03f5b24..a10493318 100644 --- a/make/configure.pm +++ b/make/configure.pm @@ -222,12 +222,7 @@ sub write_configure_cache(%) { print_format "Writing <|GREEN ${\CONFIGURE_CACHE_FILE}|> ...\n"; my %config = @_; - open(CACHE, '>', CONFIGURE_CACHE_FILE) or print_error "unable to write ${\CONFIGURE_CACHE_FILE}: $!"; - while (my ($key, $value) = each %config) { - $value //= ''; - say CACHE "$key $value"; - } - close(CACHE); + write_config_file CONFIGURE_CACHE_FILE, %config; } sub get_compiler_info($) { -- cgit v1.3.1-10-gc9f91 From 5c88e3df25dde6aa6ff849263a79ffda6144cf4d Mon Sep 17 00:00:00 2001 From: Peter Powell Date: Fri, 24 Mar 2017 21:55:29 +0000 Subject: Replace module_installed with eval. --- make/common.pm | 9 +-------- modulemanager | 4 ++-- 2 files changed, 3 insertions(+), 10 deletions(-) (limited to 'make') diff --git a/make/common.pm b/make/common.pm index f0174e0b3..6ca280bec 100644 --- a/make/common.pm +++ b/make/common.pm @@ -37,8 +37,7 @@ our @EXPORT = qw(create_directory get_cpu_count get_version read_config_file - write_config_file - module_installed); + write_config_file); sub create_directory($$) { my ($location, $permissions) = @_; @@ -90,12 +89,6 @@ sub get_version { return %version; } -sub module_installed($) { - my $module = shift; - eval("use $module;"); - return !$@; -} - sub get_cpu_count { my $count = 1; if ($^O =~ /bsd/) { diff --git a/modulemanager b/modulemanager index 2681e8326..9f9c03344 100755 --- a/modulemanager +++ b/modulemanager @@ -26,10 +26,10 @@ use warnings FATAL => qw(all); use make::common; BEGIN { - unless (module_installed("LWP::Simple")) { + unless (eval "use LWP::Simple; 1") { die "Your system is missing the LWP::Simple Perl module!"; } - unless (module_installed("Crypt::SSLeay") || module_installed("IO::Socket::SSL")) { + unless (eval "use Crypt::SSLeay; 1" || eval "use IO::Socket::SSL; 1") { die "Your system is missing the Crypt::SSLeay or IO::Socket::SSL Perl modules!"; } -- cgit v1.3.1-10-gc9f91 From b6a69e98ee6c08b874def397ee6a490d4ab8863c Mon Sep 17 00:00:00 2001 From: Peter Powell Date: Fri, 24 Mar 2017 23:11:40 +0000 Subject: Add a nice API for command line interfaces to make::console. --- make/console.pm | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 5 deletions(-) (limited to 'make') diff --git a/make/console.pm b/make/console.pm index 84fbaae4a..0d3c1b38d 100644 --- a/make/console.pm +++ b/make/console.pm @@ -1,7 +1,7 @@ # # InspIRCd -- Internet Relay Chat Daemon # -# Copyright (C) 2014 Peter Powell +# Copyright (C) 2014-2017 Peter Powell # # This file is part of InspIRCd. InspIRCd is free software: you can # redistribute it and/or modify it under the terms of the GNU General Public @@ -27,11 +27,14 @@ use feature ':5.10'; use strict; use warnings FATAL => qw(all); +use Class::Struct qw(struct); +use Exporter qw(import); use File::Path qw(mkpath); use File::Spec::Functions qw(rel2abs); -use Exporter qw(import); -our @EXPORT = qw(print_format +our @EXPORT = qw(command + execute_command + print_format print_error print_warning prompt_bool @@ -39,8 +42,9 @@ our @EXPORT = qw(print_format prompt_string); my %FORMAT_CODES = ( - DEFAULT => "\e[0m", - BOLD => "\e[1m", + DEFAULT => "\e[0m", + BOLD => "\e[1m", + UNDERLINE => "\e[4m", RED => "\e[1;31m", GREEN => "\e[1;32m", @@ -48,6 +52,13 @@ my %FORMAT_CODES = ( BLUE => "\e[1;34m" ); +my %commands; + +struct 'command' => { + 'callback' => '$', + 'description' => '$', +}; + sub __console_format($$) { my ($name, $data) = @_; return $data unless -t STDOUT; @@ -111,4 +122,38 @@ sub prompt_string($$$) { return $answer ? $answer : $default; } +sub command($$$) { + my ($name, $description, $callback) = @_; + $commands{$name} = command->new; + $commands{$name}->callback($callback); + $commands{$name}->description($description); +} + +sub command_alias($$) { + my ($source, $target) = @_; + command $source, undef, sub(@) { + execute_command $target, @_; + }; +} + +sub execute_command(@) { + my $command = defined $_[0] ? lc shift : 'help'; + if ($command eq 'help') { + print_format "<|GREEN Usage:|> $0 <<|UNDERLINE COMMAND|>> [<|UNDERLINE OPTIONS...|>]\n\n"; + print_format "<|GREEN Commands:|>\n"; + for my $key (sort keys %commands) { + next unless defined $commands{$key}->description; + my $name = sprintf "%-15s", $key; + my $description = $commands{$key}->description; + print_format " <|BOLD $name|> # $description\n"; + } + exit 0; + } elsif (!$commands{$command}) { + print_error "no command called <|BOLD $command|> exists!", + "See <|BOLD $0 help|> for a list of commands."; + } else { + return $commands{$command}->callback->(@_); + } +} + 1; -- cgit v1.3.1-10-gc9f91