aboutsummaryrefslogtreecommitdiff
path: root/bin/feature-check.pl
blob: e6f3514396a7d23548159a82578b4bc3b58617a9 (plain)
  1. #!/usr/bin/perl
  2. use v5.14;
  3. use utf8;
  4. use warnings;
  5. use open qw( :std :encoding(UTF-8) );
  6. use Getopt::Long 2.24 qw(:config gnu_getopt);
  7. use IO::Interactive qw(is_interactive);
  8. my $USE_COLOR;
  9. BEGIN {
  10. $USE_COLOR = !(
  11. exists $ENV{NO_COLOR}
  12. or ( $ENV{COLOR} and !$ENV{COLOR} )
  13. or !is_interactive
  14. );
  15. $Pod::Usage::Formatter = 'Pod::Text::Color' if $USE_COLOR;
  16. }
  17. use Pod::Usage;
  18. my $COPYRIGHT;
  19. use Pod::Constants
  20. -trim => 1,
  21. 'COPYRIGHT AND LICENSE' =>
  22. sub { ($COPYRIGHT) = s/C<< (.*) >>/$1/gr; $COPYRIGHT =~ s/©/©/g };
  23. use List::Util qw(max);
  24. use Readonly::Tiny;
  25. use Path::Tiny;
  26. use Log::Any qw($log);
  27. use Log::Any::Adapter;
  28. use Term::Table;
  29. use Graph::Easy;
  30. use Graph::Easy::Parser;
  31. # TODO: handle options --quiet/--verbose/--debug
  32. # TODO: handle output scope, with options --threshold/--all
  33. # TODO: handle custom basepath (with builtin as default), taken as argument
  34. # TODO: handle multiple basepaths
  35. # TODO: check scripts (not only documentation)
  36. Log::Any::Adapter->set( 'Screen', use_color => $USE_COLOR );
  37. =head1 NAME
  38. feature-check - examine maturity and availability status of Redpill features
  39. =head1 VERSION
  40. Version v0.1.0
  41. =cut
  42. our $VERSION = 'v0.1.0';
  43. my $progname = path($0)->basename;
  44. our %OPT = ();
  45. my @OPT = ();
  46. =head1 SYNOPSIS
  47. feature-check [ --help | --version ]
  48. feature-check [OPTION...]
  49. feature-check --format graphviz | fdp -Tx11
  50. =head1 DESCRIPTION
  51. B<feature-check> is a command-line tool
  52. to parse and examine a set of Redpill features,
  53. and print a summary on standard output.
  54. Usability of each feature is evaluated
  55. based on coverage and embedded annotations of its documentation.
  56. =head1 OPTIONS
  57. =head2 Rendering
  58. =over 16
  59. =item B<--format>
  60. Output format, either of I<table> I<ascii> I<ansi> I<svg> I<graphviz>
  61. S<(default value: I<ansi>)>
  62. =back
  63. =cut
  64. push @OPT, qw(
  65. format=s
  66. );
  67. $OPT{'format'} = 'ansi';
  68. =head2 General
  69. =over 16
  70. =item B<-h>, B<--help>
  71. print help message and exit
  72. =item B<--man>
  73. print manual and exit
  74. =item B<-v>, B<--version>
  75. print version and copyright information and exit
  76. =back
  77. =cut
  78. push @OPT, qw(
  79. verbose
  80. help|h
  81. man
  82. version|v
  83. );
  84. GetOptions( \%OPT, @OPT ) or pod2usage(2);
  85. pod2usage(1) if ( $OPT{help} );
  86. pod2usage(-exitval => 0, -verbose => 2) if ($OPT{man});
  87. if ( $OPT{version} ) { version(); exit 0; }
  88. pod2usage( join "\n", map {"Unknown argument: $_"} @ARGV )
  89. if @ARGV;
  90. my $BASEDIR = path('../..');
  91. my @TARGET = qw(executive user expert);
  92. readonly \@TARGET;
  93. my @MATURITY = qw(unusable buggy incomplete usable good);
  94. readonly \@MATURITY;
  95. use enum qw(
  96. UNUSABLE=0 BUGGY INCOMPLETE USABLE GOOD
  97. EXECUTIVE=0 USER EXPERT
  98. );
  99. my %DOCFILE = (
  100. ADMIN => EXPERT,
  101. INTRO => USER,
  102. OVERVIEW => EXECUTIVE,
  103. README => EXECUTIVE,
  104. SETUP => EXPERT,
  105. USE => USER,
  106. );
  107. readonly \%DOCFILE;
  108. my $threshold = USABLE;
  109. $log->info("acceptance threshold: $MATURITY[$threshold]");
  110. my $graph = Graph::Easy::Parser->from_text('graph { flow: east; }');
  111. $graph->catch_messages(1);
  112. my $root = 'features';
  113. my $featureref = path("$BASEDIR")->visit( \&feature );
  114. #use DDP; p $featureref if $log->is_debug;
  115. my ( @row, %node );
  116. $node{$root} = $graph->add_node($root);
  117. for my $feature ( sort keys %$featureref ) {
  118. my @target = @{ $featureref->{$feature}{target} };
  119. next unless $featureref->{$feature}{maturity} >= $threshold;
  120. push @row, [ $feature, map { join ', ', sort keys %{ $target[$_]{docs} } if $target[$_]{maturity} >= $threshold } EXECUTIVE..EXPERT ]
  121. if $featureref->{$feature}{maturity} >= $threshold;
  122. next if defined $node{$feature};
  123. $node{$feature} = undef;
  124. }
  125. for my $feature ( sort keys %node ) {
  126. next if defined $node{$feature};
  127. if ( $feature =~ /^([^-]+).*\K-(?=[^-]+$)/ and defined $node{$`} and !defined $node{$feature} ) {
  128. ( undef, $node{$feature} ) = $graph->add_edge( $node{$`}, $feature );
  129. $node{$feature}->set_attribute( 'label', $' );
  130. }
  131. else {
  132. ( undef, $node{$feature} ) = $graph->add_edge( $node{$root}, $feature );
  133. }
  134. }
  135. my $table = Term::Table->new(
  136. header => [ 'feature', @TARGET ],
  137. rows => \@row,
  138. );
  139. for ( $OPT{format} ) {
  140. map { say $_ } $table->render when 'table';
  141. print $graph->as_ascii() when 'ascii';
  142. print $graph->as_boxart() when 'ansi';
  143. print $graph->as_svg() when 'svg';
  144. print $graph->as_graphviz() when 'graphviz';
  145. default { $log->fatal("unsupported format:$_") && exit 1 }
  146. }
  147. $log->error($_) for $graph->errors();
  148. $log->warn($_) for $graph->warnings();
  149. sub feature {
  150. my ($path, $state) = @_;
  151. my ( $feature, $docs, $maturity );
  152. return unless $path->is_dir;
  153. return if $path->basename =~ qr/\.$/;
  154. $feature = $path->basename;
  155. $log->trace("found feature: $feature");
  156. $docs = $path->visit( \&docfile );
  157. for my $target (EXECUTIVE..EXPERT) {
  158. my $maturity;
  159. if ( exists $docs->{$target} ) {
  160. $state->{$feature}{target}[$target]{docs} = $docs->{$target};
  161. $maturity = max map { $_->{maturity} } values %{ $docs->{$target} };
  162. }
  163. else {
  164. $maturity = UNUSABLE;
  165. }
  166. $state->{$feature}{target}[$target]{maturity} = $maturity;
  167. $log->debug("resolved feature $feature target $target maturity: $maturity");
  168. }
  169. if ( exists $state->{$feature}{target}[EXECUTIVE]{docs} && exists $state->{$feature}{target}[EXECUTIVE]{docs}{README} ) {
  170. $maturity = $state->{$feature}{target}[EXECUTIVE]{maturity};
  171. }
  172. elsif ( INCOMPLETE < $state->{$feature}{target}[EXECUTIVE]{maturity} ) {
  173. $maturity = INCOMPLETE;
  174. }
  175. else {
  176. $maturity = $state->{$feature}{target}[EXECUTIVE]{maturity};
  177. }
  178. $state->{$feature}{maturity} = $maturity;
  179. $log->warningf('skipping %s feature: %s', $MATURITY[$maturity], $feature)
  180. if $maturity < $threshold;
  181. }
  182. sub docfile {
  183. my ($path, $state) = @_;
  184. my ( $doc, $category, $content, $maturity );
  185. return unless $path->is_file;
  186. $doc = $path->basename('.md');
  187. return if $path->basename eq $doc;
  188. return unless exists $DOCFILE{$doc};
  189. $category = $DOCFILE{$doc}
  190. // return;
  191. $content = $path->slurp_utf8;
  192. if ( $content =~ qr/\bFIXME\b/ ) {
  193. $maturity = BUGGY;
  194. }
  195. elsif ( $content =~ qr/\bTODO\b/ ) {
  196. $maturity = INCOMPLETE;
  197. }
  198. else {
  199. $maturity //= USABLE;
  200. }
  201. $state->{$category}{$doc}{maturity} = $maturity;
  202. $log->trace("found docfile: $doc", { maturity => $maturity });
  203. }
  204. =head1 MATURITY
  205. =head2 Documentation file
  206. A documentation file is considered broken
  207. if it contains any B<FIXME> annotations,
  208. or otherwise considered unfinished
  209. if it contains any B<TODO> annotations.
  210. =head2 Targeted documentation
  211. A targeted documentation,
  212. for each of the Redpill targets B<executive>, B<user> and B<expert>,
  213. is as mature as the most mature of their files,
  214. or unusable if none exist for the target.
  215. =head2 Feature
  216. A feature is as mature as the most mature targeted documentation,
  217. except it is at most unfinished
  218. unless documentation complies with standard-readme specification.
  219. =head1 ENVIRONMENT
  220. =over 6
  221. =item NO_COLOR
  222. If defined, will disable color.
  223. Consulted before COLOR.
  224. =item COLOR
  225. Can be set to 0 to explicitly disable colors.
  226. The default is to use color when connected to a terminal.
  227. =item LOG_LEVEL
  228. =item QUIET
  229. =item VERBOSE
  230. =item DEBUG
  231. =item TRACE
  232. Used to emit varying details about discoveries to STDERR.
  233. See L<Log::Any::Adapter::Screen> for more details.
  234. =item LOG_PREFIX
  235. The default formatter groks these variables.
  236. See B<formatter> in L<Log::Any::Adapter::Screen> for more details.
  237. =back
  238. =encoding UTF-8
  239. =head1 AUTHOR
  240. Jonas Smedegaard C<< <dr@jones.dk> >>
  241. =head1 COPYRIGHT AND LICENSE
  242. Copyright 2021-2022 Jonas Smedegaard
  243. This program is free software:
  244. you can redistribute it and/or modify it
  245. under the terms of the GNU Affero General Public License
  246. as published by the Free Software Foundation,
  247. either version 3, or (at your option) any later version.
  248. This program is distributed in the hope that it will be useful,
  249. but WITHOUT ANY WARRANTY;
  250. without even the implied warranty
  251. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  252. See the GNU Affero General Public License for more details.
  253. You should have received a copy
  254. of the GNU Affero General Public License along with this program.
  255. If not, see <https://www.gnu.org/licenses/>.
  256. =cut
  257. 1;