toolbox/update-all-config.pl: quiet means quiet
[girocco.git] / toolbox / lint-all-readme.pl
bloba969af80d93edd8f950e2703b4e53e70297cb2ea
1 #!/usr/bin/perl
3 # lint-all-readme.pl - lint all projects' explicit README files
4 # Copyright (C) 2021 Kyle J. McKay.
5 # All rights reserved.
6 # License GPLv2+: GNU GPL version 2 or later.
7 # www.gnu.org/licenses/gpl-2.0.html
8 # This is free software: you are free to change and redistribute it.
9 # There is NO WARRANTY, to the extent permitted by law.
11 use strict;
12 use warnings;
13 use vars qw($VERSION);
14 BEGIN {*VERSION = \'1.0.0'}
15 use File::Basename qw(basename);
16 use lib "__BASEDIR__";
17 use Girocco::Config;
18 use Girocco::Util;
19 use Girocco::CLIUtil;
20 use Girocco::Project;
21 my $bn; BEGIN {$bn = basename(__FILE__)}
23 exit(&main(@ARGV)||0);
25 our $help;
26 BEGIN {$help = <<'HELP'}
27 Usage: %s [--help] [--dry-run]
28 --help show this help
29 --dry-run show projects that need updating but don't update them
30 -P/--progress show progress on STDERR (default if STDERR is a tty)
32 Exit status will always be non-zero if any readme files fail to lint.
33 HELP
35 sub lint_project_readmes {
36 my ($dryrun, $show_progress) = @_;
37 my %allprojs = map({$_ => 1} Girocco::Project::get_full_list());
38 my @allprojs = sort({lc($a) cmp lc($b) || $a cmp $b} keys(%allprojs));
39 my @outdated = ();
40 my @badlint = ();
41 my $bd = $Girocco::Config::reporoot . '/';
42 my $progress = Girocco::CLIUtil::Progress->new(
43 $show_progress ? scalar(@allprojs) : 0,
44 "Checking project readme files");
45 my $cnt = 0;
46 foreach (@allprojs) {
47 ++$cnt;
48 $progress->update($cnt);
49 my $pd = $bd . $_ . '.git';
50 -d $pd or next; # just ignore any phantoms
51 my $proj = undef;
52 eval { $proj = Girocco::Project->load($_); 1; } && $proj or
53 next; # just ignore unloadable projects
54 my $readme = $proj->{README};
55 defined($readme) or $readme = "";
56 chomp($readme);
57 my ($cnt, $err) = $proj->_lint_readme(0);
58 if ($cnt) {
59 push(@badlint, $_);
60 $progress->emit("$_: error: $err");
61 next;
63 my $newreadme = $proj->{README};
64 defined($newreadme) or $newreadme = "";
65 chomp($newreadme);
66 $readme eq $newreadme and next;
67 if ($dryrun) {
68 push(@outdated, $_);
69 $progress->emit("$_: needs update");
70 } else {
71 push(@outdated, $_);
72 $proj->_property_fput("READMEDATA", $proj->{READMEDATA}, 1);
73 $proj->_property_fput("README", $proj->{README}, -e "$pd/README.html");
74 $proj->_property_fput("rmtype", $proj->{rmtype}, 1);
75 $proj->_set_changed;
76 $progress->emit("$_: updated");
79 return {count => scalar(@allprojs), outdated => \@outdated,
80 badlint => \@badlint};
83 sub dohelp {
84 my $fd = shift;
85 my $ec = shift;
86 printf $fd "%s version %s\n", $bn, $VERSION;
87 printf $fd $help, $bn;
88 exit $ec;
91 sub main {
92 local *ARGV = \@_;
93 my ($dryrun, $help);
94 my $progress = -t STDERR;
96 shift, $dryrun=1, redo if @ARGV && $ARGV[0] =~ /^(?:-n|--dry-run)$/i;
97 shift, $help=1, redo if @ARGV && $ARGV[0] =~ /^(?:-h|--help)$/i;
98 shift, $progress=1, redo if @ARGV && $ARGV[0] =~ /^(?:-P|--progress)$/i;
99 shift, $progress=0, redo if @ARGV && $ARGV[0] =~ /^(?:--no-progress)$/i;
101 !@ARGV && !$help or dohelp($help ? \*STDOUT : \*STDERR, !$help);
102 nice_me(18);
103 my $results = lint_project_readmes(!!$dryrun, $progress);
104 printf "Total: %d %s: %d Lintfail: %d\n",
105 $results->{count},
106 $dryrun ? "Outdated" : "Updated", scalar(@{$results->{outdated}}),
107 scalar(@{$results->{badlint}});
108 exit @{$results->{badlint}} ? 1 : 0;