aboutsummaryrefslogtreecommitdiff
path: root/bin/feature-check.pl
blob: 154bb2a291fe3f49b38f432627716936a2bea5aa (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. my $root = 'redpill';
  100. my $featureref = path("$BASEDIR")->visit( \&feature );
  101. #use DDP; p $featureref if $log->is_debug;
  102. my ( @row, %node );
  103. $node{$root} = $graph->add_node($root);
  104. for my $feature ( sort keys %$featureref ) {
  105. my @target = @{ $featureref->{$feature}{target} };
  106. next unless $featureref->{$feature}{maturity} >= $threshold;
  107. push @row, [ $feature, map { join ', ', sort keys %{ $target[$_]{docs} } if $target[$_]{maturity} >= $threshold } EXECUTIVE..EXPERT ]
  108. if $featureref->{$feature}{maturity} >= $threshold;
  109. next if defined $node{$feature};
  110. $node{$feature} = undef;
  111. }
  112. for my $feature ( sort keys %node ) {
  113. next if defined $node{$feature};
  114. if ( $feature =~ /^([^-]+).*\K-(?=[^-]+$)/ and defined $node{$`} and !defined $node{$feature} ) {
  115. ( undef, $node{$feature} ) = $graph->add_edge( $node{$`}, $feature );
  116. $node{$feature}->set_attribute( 'label', $' );
  117. }
  118. else {
  119. ( undef, $node{$feature} ) = $graph->add_edge( $node{$root}, $feature );
  120. }
  121. }
  122. my $table = Term::Table->new(
  123. header => [ 'feature', @TARGET ],
  124. rows => \@row,
  125. );
  126. say $_ for $table->render;
  127. print $graph->as_boxart();
  128. sub feature {
  129. my ($path, $state) = @_;
  130. my ( $feature, $docs, $maturity );
  131. return unless $path->is_dir;
  132. return if $path->basename =~ qr/\.$/;
  133. $feature = $path->basename;
  134. $log->trace("found feature: $feature");
  135. $docs = $path->visit( \&docfile );
  136. for my $target (EXECUTIVE..EXPERT) {
  137. my $maturity;
  138. if ( exists $docs->{$target} ) {
  139. $state->{$feature}{target}[$target]{docs} = $docs->{$target};
  140. $maturity = max map { $_->{maturity} } values %{ $docs->{$target} };
  141. }
  142. else {
  143. $maturity = UNUSABLE;
  144. }
  145. $state->{$feature}{target}[$target]{maturity} = $maturity;
  146. $log->debug("resolved feature $feature target $target maturity: $maturity");
  147. }
  148. if ( exists $state->{$feature}{target}[EXECUTIVE]{docs} && exists $state->{$feature}{target}[EXECUTIVE]{docs}{README} ) {
  149. $maturity = $state->{$feature}{target}[EXECUTIVE]{maturity};
  150. }
  151. elsif ( INCOMPLETE < $state->{$feature}{target}[EXECUTIVE]{maturity} ) {
  152. $maturity = INCOMPLETE;
  153. }
  154. else {
  155. $maturity = $state->{$feature}{target}[EXECUTIVE]{maturity};
  156. }
  157. $state->{$feature}{maturity} = $maturity;
  158. $log->warningf('skipping %s feature: %s', $MATURITY[$maturity], $feature)
  159. if $maturity < $threshold;
  160. }
  161. sub docfile {
  162. my ($path, $state) = @_;
  163. my ( $doc, $category, $content, $maturity );
  164. return unless $path->is_file;
  165. $doc = $path->basename('.md');
  166. return if $path->basename eq $doc;
  167. return unless exists $DOCFILE{$doc};
  168. $category = $DOCFILE{$doc}
  169. // return;
  170. $content = $path->slurp_utf8;
  171. if ( $content =~ qr/\bFIXME\b/ ) {
  172. $maturity = BUGGY;
  173. }
  174. elsif ( $content =~ qr/\bTODO\b/ ) {
  175. $maturity = INCOMPLETE;
  176. }
  177. else {
  178. $maturity //= USABLE;
  179. }
  180. $state->{$category}{$doc}{maturity} = $maturity;
  181. $log->trace("found docfile: $doc", { maturity => $maturity });
  182. }
  183. =head1 MATURITY
  184. =head2 Documentation file
  185. A documentation file is considered broken
  186. if it contains any B<FIXME> annotations,
  187. or otherwise considered unfinished
  188. if it contains any B<TODO> annotations.
  189. =head2 Targeted documentation
  190. A targeted documentation,
  191. for each of the Redpill targets B<executive>, B<user> and B<expert>,
  192. is as mature as the most mature of their files,
  193. or unusable if none exist for the target.
  194. =head2 Feature
  195. A feature is as mature as the most mature targeted documentation,
  196. except it is at most unfinished
  197. unless documentation complies with standard-readme specification.
  198. =head1 ENVIRONMENT
  199. =over 6
  200. =item NO_COLOR
  201. If defined, will disable color.
  202. Consulted before COLOR.
  203. =item COLOR
  204. Can be set to 0 to explicitly disable colors.
  205. The default is to use color when connected to a terminal.
  206. =item LOG_LEVEL
  207. =item QUIET
  208. =item VERBOSE
  209. =item DEBUG
  210. =item TRACE
  211. Used to emit varying details about discoveries to STDERR.
  212. See L<Log::Any::Adapter::Screen> for more details.
  213. =item LOG_PREFIX
  214. The default formatter groks these variables.
  215. See B<formatter> in L<Log::Any::Adapter::Screen> for more details.
  216. =back
  217. =encoding UTF-8
  218. =head1 AUTHOR
  219. Jonas Smedegaard C<< <dr@jones.dk> >>
  220. =head1 COPYRIGHT AND LICENSE
  221. Copyright © 2021 Jonas Smedegaard
  222. This program is free software:
  223. you can redistribute it and/or modify it
  224. under the terms of the GNU Affero General Public License
  225. as published by the Free Software Foundation,
  226. either version 3, or (at your option) any later version.
  227. This program is distributed in the hope that it will be useful,
  228. but WITHOUT ANY WARRANTY;
  229. without even the implied warranty
  230. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  231. See the GNU Affero General Public License for more details.
  232. You should have received a copy
  233. of the GNU Affero General Public License along with this program.
  234. If not, see <https://www.gnu.org/licenses/>.
  235. =cut
  236. 1;