File Coverage

blib/lib/App/DocKnot.pm
Criterion Covered Total %
statement 33 33 100.0
branch 1 2 50.0
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 46 47 97.8


line stmt bran cond sub pod time code
1             # Parent module for DocKnot.
2             #
3             # DocKnot provides various commands for generating documentation, web pages,
4             # and software releases. This parent module provides some internal helper
5             # functions used to load configuration and metadata. The normal entry point
6             # are the various submodules, or App::DocKnot::Command via docknot.
7             #
8             # SPDX-License-Identifier: MIT
9              
10             ##############################################################################
11             # Modules and declarations
12             ##############################################################################
13              
14             package App::DocKnot 3.01;
15              
16 9     9   112980 use 5.024;
  9         47  
17 9     9   39 use autodie;
  9         13  
  9         39  
18 9     9   38517 use warnings;
  9         16  
  9         311  
19              
20 9     9   3731 use File::BaseDir qw(config_files);
  9         9922  
  9         510  
21 9     9   2859 use File::ShareDir qw(module_file);
  9         145653  
  9         429  
22 9     9   56 use File::Spec;
  9         15  
  9         165  
23 9     9   3659 use JSON;
  9         56760  
  9         54  
24 9     9   3041 use Perl6::Slurp;
  9         5353  
  9         58  
25              
26             ##############################################################################
27             # Helper methods
28             ##############################################################################
29              
30             # Helper routine to return the path of a file from the application data.
31             # These data files are installed with App::DocKnot, but each file can be
32             # overridden by the user via files in $HOME/.config/docknot or
33             # /etc/xdg/docknot (or whatever $XDG_CONFIG_DIRS is set to).
34             #
35             # We therefore try File::BaseDir first (which handles the XDG paths) and fall
36             # back on using File::ShareDir to locate the data.
37             #
38             # $self - The App::DocKnot object
39             # @path - The relative path of the file as a list of components
40             #
41             # Returns: The absolute path to the application data
42             # Throws: Text exception on failure to locate the desired file
43             sub appdata_path {
44 118     118 1 288 my ($self, @path) = @_;
45              
46             # Try XDG paths first.
47 118         386 my $path = config_files('docknot', @path);
48              
49             # If that doesn't work, use the data that came with the module.
50 118 50       7176 if (!defined($path)) {
51 118         855 $path = module_file('App::DocKnot', File::Spec->catfile(@path));
52             }
53 118         28500 return $path;
54             }
55              
56             # Helper routine that locates an application data file, interprets it as JSON,
57             # and returns the resulting decoded contents. This uses the relaxed parsing
58             # mode, so comments and commas after data elements are supported.
59             #
60             # $self - The App::DocKnot object
61             # @path - The path of the file to load, as a list of components
62             #
63             # Returns: Anonymous hash or array resulting from decoding the JSON object
64             # Throws: slurp or JSON exception on failure to load or decode the object
65             sub load_appdata_json {
66 42     42 1 134 my ($self, @path) = @_;
67 42         179 my $path = $self->appdata_path(@path);
68 42         251 my $json = JSON->new;
69 42         168 $json->relaxed;
70 42         146 return $json->decode(scalar(slurp($path)));
71             }
72              
73             ##############################################################################
74             # Module return value and documentation
75             ##############################################################################
76              
77             1;
78             __END__