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