Skip to content

Commit

Permalink
sync.pl: Find and prefer .twinklerc in the twinkle directory
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Amorymeltzer committed Feb 10, 2020
1 parent 120f725 commit e5b8b20
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 36 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.pl]
indent_style = space
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.mediawiki-bot-*
alltwinkle.js
.twinklerc
/.settings
/.project
jsl.conf
Expand Down
2 changes: 1 addition & 1 deletion DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 47 additions & 35 deletions sync.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <files...>",
Expand Down Expand Up @@ -65,15 +73,6 @@
# Make sure we know what we're doing before doing it
forReal();

# Build file->page hashes
my %deploys;
while (<DATA>) {
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({
Expand All @@ -86,6 +85,14 @@

### Main loop to parse options
if ($opt->mode eq 'deploy') {
# Build file->page hashes from __DATA__
my %deploys;
while (<DATA>) {
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) {
Expand All @@ -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);
}
}

Expand Down

0 comments on commit e5b8b20

Please sign in to comment.