From e5b8b20255b2a8d35c14a59792353dad03681978 Mon Sep 17 00:00:00 2001 From: Amory Meltzer Date: Tue, 4 Feb 2020 20:58:57 -0500 Subject: [PATCH] sync.pl: Find and prefer .twinklerc in the twinkle directory Accordingly, add `.twinklerc` to .gitignore. Also adjust `.editorconfig` for perl files and tweak so that perl only builds the __DATA__ hash if deploying and only parses files/base if pulling or pushing. --- .editorconfig | 3 ++ .gitignore | 1 + DEVELOPER.md | 2 +- sync.pl | 82 +++++++++++++++++++++++++++++---------------------- 4 files changed, 52 insertions(+), 36 deletions(-) diff --git a/.editorconfig b/.editorconfig index 5438a6932..ef4551df9 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,3 +11,6 @@ end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true + +[*.pl] +indent_style = space diff --git a/.gitignore b/.gitignore index 85222d1b2..bb370776b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .mediawiki-bot-* alltwinkle.js +.twinklerc /.settings /.project jsl.conf diff --git a/DEVELOPER.md b/DEVELOPER.md index 8373bdf1f..539a9d398 100644 --- a/DEVELOPER.md +++ b/DEVELOPER.md @@ -66,7 +66,7 @@ The program depends on a few Perl modules, namely [`MediaWiki::API`][MediaWiki:: You may prefer to install them through your operating system's packaing tool (e.g. `apt-get install libgetopt-long-descriptive-perl`) although you can install them through cpanm too. -When running the program, you can enter your credentials on the command line using the `--username` and `--password` parameters, but it is recommended to save them in a file called `~/.twinklerc` using the following format: +When running the program, you can enter your credentials on the command line using the `--username` and `--password` parameters, but it is recommended to save them in a `.twinklerc` file, either in this directory or in your `~` home, using the following format: username = username password = password diff --git a/sync.pl b/sync.pl index a57028fd4..c7cfa2cb4 100755 --- a/sync.pl +++ b/sync.pl @@ -7,6 +7,7 @@ use English qw(-no_match_vars); use utf8; +use FindBin qw($Bin); use Config::General qw(ParseConfig); use Getopt::Long::Descriptive; use Git::Repository; @@ -18,9 +19,16 @@ # username = Jimbo Wales # lang = en # etc. +my $rc = '.twinklerc'; +# Check script directory and ~ (home) for config file, preferring the former +my @dotLocales = map { $_.q{/}.$rc } ($Bin, $ENV{HOME}); my %conf; -my $config_file = "$ENV{HOME}/.twinklerc"; -%conf = ParseConfig($config_file) if -e -f -r $config_file; +foreach my $dot (@dotLocales) { + if (-e -f -r $dot) { + %conf = ParseConfig($dot); + last; + } +} my ($opt, $usage) = describe_options( "$PROGRAM_NAME %o ", @@ -65,15 +73,6 @@ # Make sure we know what we're doing before doing it forReal(); -# Build file->page hashes -my %deploys; -while () { - chomp; - my @map = split; - $deploys{$map[0]} = $map[1]; -} -# Remove 'modules/' from ARGV input filenames -my %pages = map {+(my $s = $_) =~ s/modules\///; $_ => "$opt->{base}/$s"} @ARGV; # Open API and log in before anything else my $mw = MediaWiki::API->new({ @@ -86,6 +85,14 @@ ### Main loop to parse options if ($opt->mode eq 'deploy') { + # Build file->page hashes from __DATA__ + my %deploys; + while () { + chomp; + my @map = split; + $deploys{$map[0]} = $map[1]; + } + # Follow order when deploying, useful mainly for keeping twinkle.js and # morebits.js first with make deploy foreach my $file (@ARGV) { @@ -96,31 +103,36 @@ my $page = $deploys{$file}; next if saltNPepa($page, $file); } -} elsif ($opt->mode eq 'pull') { - while (my ($file, $page) = each %pages) { - my $wikiPage = checkPage($page); - next if !$wikiPage; - print "Grabbing $page"; - my $text = $wikiPage->{q{*}}."\n"; # MediaWiki doesn't have trailing newlines - # Might be faster to check this using git and eof, but here makes sense - if ($text eq read_text($file)) { - print colored ['blue'], "... No changes found, skipping\n"; - next; - } else { - print "\n"; - write_text($file, $text); +} else { + # Remove 'modules/' from ARGV input filenames + my %pages = map {+(my $s = $_) =~ s/modules\///; $_ => "$opt->{base}/$s"} @ARGV; + + if ($opt->mode eq 'pull') { + while (my ($file, $page) = each %pages) { + my $wikiPage = checkPage($page); + next if !$wikiPage; + print "Grabbing $page"; + my $text = $wikiPage->{q{*}}."\n"; # MediaWiki doesn't have trailing newlines + # Might be faster to check this using git and eof, but here makes sense + if ($text eq read_text($file)) { + print colored ['blue'], "... No changes found, skipping\n"; + next; + } else { + print "\n"; + write_text($file, $text); + } + } + # Show a summary of any changes + my $cmd = $repo->command(diff => '--stat', '--color'); + my $s = $cmd->stdout; + while (<$s>) { + print; + } + $cmd->close; + } elsif ($opt->mode eq 'push') { + while (my ($file, $page) = each %pages) { + next if saltNPepa($page, $file); } - } - # Show a summary of any changes - my $cmd = $repo->command(diff => '--stat', '--color'); - my $s = $cmd->stdout; - while (<$s>) { - print; - } - $cmd->close; -} elsif ($opt->mode eq 'push') { - while (my ($file, $page) = each %pages) { - next if saltNPepa($page, $file); } }