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.02;
15              
16 9     9   105584 use 5.024;
  9         45  
17 9     9   38 use autodie;
  9         16  
  9         40  
18 9     9   39052 use warnings;
  9         17  
  9         286  
19              
20 9     9   3815 use File::BaseDir qw(config_files);
  9         10041  
  9         495  
21 9     9   2893 use File::ShareDir qw(module_file);
  9         147190  
  9         425  
22 9     9   57 use File::Spec;
  9         15  
  9         161  
23 9     9   3649 use JSON;
  9         56747  
  9         43  
24 9     9   3090 use Perl6::Slurp;
  9         5440  
  9         49  
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 320 my ($self, @path) = @_;
45              
46             # Try XDG paths first.
47 118         377 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         826 $path = module_file('App::DocKnot', File::Spec->catfile(@path));
52             }
53 118         28547 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 158 my ($self, @path) = @_;
67 42         154 my $path = $self->appdata_path(@path);
68 42         254 my $json = JSON->new;
69 42         149 $json->relaxed;
70 42         136 return $json->decode(scalar(slurp($path)));
71             }
72              
73             ##############################################################################
74             # Module return value and documentation
75             ##############################################################################
76              
77             1;
78             __END__