aboutsummaryrefslogtreecommitdiff
path: root/bin/feature-check.pl
blob: 2c36d81d3392406e8e683debbd9a8befcaa1db09 (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. =head1 DESCRIPTION
  50. B<feature-check> is a command-line tool
  51. to parse and examine a set of Redpill features,
  52. and print a summary on standard output.
  53. Usability of each feature is evaluated
  54. based on coverage and embedded annotations of its documentation.
  55. =head1 OPTIONS
  56. =head2 General
  57. =over 16
  58. =item B<-h>, B<--help>
  59. print help message and exit
  60. =item B<--man>
  61. print manual and exit
  62. =item B<-v>, B<--version>
  63. print version and copyright information and exit
  64. =back
  65. =cut
  66. push @OPT, qw(
  67. verbose
  68. help|h
  69. man
  70. version|v
  71. );
  72. GetOptions( \%OPT, @OPT ) or pod2usage(2);
  73. pod2usage(1) if ( $OPT{help} );
  74. pod2usage(-exitval => 0, -verbose => 2) if ($OPT{man});
  75. if ( $OPT{version} ) { version(); exit 0; }
  76. pod2usage( join "\n", map {"Unknown argument: $_"} @ARGV )
  77. if @ARGV;
  78. my $BASEDIR = path('../..');
  79. my @TARGET = qw(executive user expert);
  80. readonly \@TARGET;
  81. my @MATURITY = qw(unusable buggy incomplete usable good);
  82. readonly \@MATURITY;
  83. use enum qw(
  84. UNUSABLE=0 BUGGY INCOMPLETE USABLE GOOD
  85. EXECUTIVE=0 USER EXPERT
  86. );
  87. my %DOCFILE = (
  88. ADMIN => EXPERT,
  89. INTRO => USER,
  90. OVERVIEW => EXECUTIVE,
  91. README => EXECUTIVE,
  92. SETUP => EXPERT,
  93. USE => USER,
  94. );
  95. readonly \%DOCFILE;
  96. my $threshold = USABLE;
  97. $log->info("acceptance threshold: $MATURITY[$threshold]");
  98. my $graph = Graph::Easy::Parser->from_text('graph { flow: east; }');
  99. $graph->catch_messages(1);
  100. my $root = 'features';
  101. my $featureref = path("$BASEDIR")->visit( \&feature );
  102. #use DDP; p $featureref if $log->is_debug;
  103. my ( @row, %node );
  104. $node{$root} = $graph->add_node($root);
  105. for my $feature ( sort keys %$featureref ) {
  106. my @target = @{ $featureref->{$feature}{target} };
  107. next unless $featureref->{$feature}{maturity} >= $threshold;
  108. push @row, [ $feature, map { join ', ', sort keys %{ $target[$_]{docs} } if $target[$_]{maturity} >= $threshold } EXECUTIVE..EXPERT ]
  109. if $featureref->{$feature}{maturity} >= $threshold;
  110. next if defined $node{$feature};
  111. $node{$feature} = undef;
  112. }
  113. for my $feature ( sort keys %node ) {
  114. next if defined $node{$feature};
  115. if ( $feature =~ /^([^-]+).*\K-(?=[^-]+$)/ and defined $node{$`} and !defined $node{$feature} ) {
  116. ( undef, $node{$feature} ) = $graph->add_edge( $node{$`}, $feature );
  117. $node{$feature}->set_attribute( 'label', $' );
  118. }
  119. else {
  120. ( undef, $node{$feature} ) = $graph->add_edge( $node{$root}, $feature );
  121. }
  122. }
  123. my $table = Term::Table->new(
  124. header => [ 'feature', @TARGET ],
  125. rows => \@row,
  126. );
  127. say $_ for $table->render;
  128. print $graph->as_boxart();
  129. $log->error($_) for $graph->errors();
  130. $log->warn($_) for $graph->warnings();
  131. sub feature {
  132. my ($path, $state) = @_;
  133. my ( $feature, $docs, $maturity );
  134. return unless $path->is_dir;
  135. return if $path->basename =~ qr/\.$/;
  136. $feature = $path->basename;
  137. $log->trace("found feature: $feature");
  138. $docs = $path->visit( \&docfile );
  139. for my $target (EXECUTIVE..EXPERT) {
  140. my $maturity;
  141. if ( exists $docs->{$target} ) {
  142. $state->{$feature}{target}[$target]{docs} = $docs->{$target};
  143. $maturity = max map { $_->{maturity} } values %{ $docs->{$target} };
  144. }
  145. else {
  146. $maturity = UNUSABLE;
  147. }
  148. $state->{$feature}{target}[$target]{maturity} = $maturity;
  149. $log->debug("resolved feature $feature target $target maturity: $maturity");
  150. }
  151. if ( exists $state->{$feature}{target}[EXECUTIVE]{docs} && exists $state->{$feature}{target}[EXECUTIVE]{docs}{README} ) {
  152. $maturity = $state->{$feature}{target}[EXECUTIVE]{maturity};
  153. }
  154. elsif ( INCOMPLETE < $state->{$feature}{target}[EXECUTIVE]{maturity} ) {
  155. $maturity = INCOMPLETE;
  156. }
  157. else {
  158. $maturity = $state->{$feature}{target}[EXECUTIVE]{maturity};
  159. }
  160. $state->{$feature}{maturity} = $maturity;
  161. $log->warningf('skipping %s feature: %s', $MATURITY[$maturity], $feature)
  162. if $maturity < $threshold;
  163. }
  164. sub docfile {
  165. my ($path, $state) = @_;
  166. my ( $doc, $category, $content, $maturity );
  167. return unless $path->is_file;
  168. $doc = $path->basename('.md');
  169. return if $path->basename eq $doc;
  170. return unless exists $DOCFILE{$doc};
  171. $category = $DOCFILE{$doc}
  172. // return;
  173. $content = $path->slurp_utf8;
  174. if ( $content =~ qr/\bFIXME\b/ ) {
  175. $maturity = BUGGY;
  176. }
  177. elsif ( $content =~ qr/\bTODO\b/ ) {
  178. $maturity = INCOMPLETE;
  179. }
  180. else {
  181. $maturity //= USABLE;
  182. }
  183. $state->{$category}{$doc}{maturity} = $maturity;
  184. $log->trace("found docfile: $doc", { maturity => $maturity });
  185. }
  186. =head1 MATURITY
  187. =head2 Documentation file
  188. A documentation file is considered broken
  189. if it contains any B<FIXME> annotations,
  190. or otherwise considered unfinished
  191. if it contains any B<TODO> annotations.
  192. =head2 Targeted documentation
  193. A targeted documentation,
  194. for each of the Redpill targets B<executive>, B<user> and B<expert>,
  195. is as mature as the most mature of their files,
  196. or unusable if none exist for the target.
  197. =head2 Feature
  198. A feature is as mature as the most mature targeted documentation,
  199. except it is at most unfinished
  200. unless documentation complies with standard-readme specification.
  201. =head1 ENVIRONMENT
  202. =over 6
  203. =item NO_COLOR
  204. If defined, will disable color.
  205. Consulted before COLOR.
  206. =item COLOR
  207. Can be set to 0 to explicitly disable colors.
  208. The default is to use color when connected to a terminal.
  209. =item LOG_LEVEL
  210. =item QUIET
  211. =item VERBOSE
  212. =item DEBUG
  213. =item TRACE
  214. Used to emit varying details about discoveries to STDERR.
  215. See L<Log::Any::Adapter::Screen> for more details.
  216. =item LOG_PREFIX
  217. The default formatter groks these variables.
  218. See B<formatter> in L<Log::Any::Adapter::Screen> for more details.
  219. =back
  220. =encoding UTF-8
  221. =head1 AUTHOR
  222. Jonas Smedegaard C<< <dr@jones.dk> >>
  223. =head1 COPYRIGHT AND LICENSE
  224. Copyright 2021-2022 Jonas Smedegaard
  225. This program is free software:
  226. you can redistribute it and/or modify it
  227. under the terms of the GNU Affero General Public License
  228. as published by the Free Software Foundation,
  229. either version 3, or (at your option) any later version.
  230. This program is distributed in the hope that it will be useful,
  231. but WITHOUT ANY WARRANTY;
  232. without even the implied warranty
  233. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  234. See the GNU Affero General Public License for more details.
  235. You should have received a copy
  236. of the GNU Affero General Public License along with this program.
  237. If not, see <https://www.gnu.org/licenses/>.
  238. =cut
  239. 1;