| 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 v8.0.1; | ||||||||||
| 15 | |||||||||||
| 16 | 19 | ▶ | use 5.024; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 19 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 19 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 19 | ▶ | use File::BaseDir qw(config_files); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 19 | ▶ | use File::ShareDir qw(module_file); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 19 | ▶ | use Kwalify qw(validate); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 19 | ▶ | use Path::Tiny qw(path); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 19 | ▶ | use YAML::XS (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | # This function must be in the App::DocKnot module so that File::ShareDir | ||||||||||
| 39 | # works properly and searches the correct module path. | ||||||||||
| 40 | # | ||||||||||
| 41 | # @path - The relative path of the file as a list of components | ||||||||||
| 42 | # | ||||||||||
| 43 | # Returns: The absolute path to the application data | ||||||||||
| 44 | # Throws: Text exception on failure to locate the desired file | ||||||||||
| 45 | 170 | ▶ | sub appdata_path { | ||||||||
|
Subroutine
appdata_path: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 46 | 170 | my ($self, @path) = @_; | |||||||||
| 47 | |||||||||||
| 48 | # Try XDG paths first. | ||||||||||
| 49 | 170 | my $path = config_files('docknot', @path); | |||||||||
| 50 | |||||||||||
| 51 | # If that doesn't work, use the data that came with the module. | ||||||||||
| 52 | 170 | ▶ | if (!defined($path)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 170 | $path = module_file('App::DocKnot', path(@path)->stringify()); | |||||||||
| 54 | } | ||||||||||
| 55 | 170 | return $path; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | # Load a YAML file with schema checking. | ||||||||||
| 59 | # | ||||||||||
| 60 | # $path - Path to the YAML file to load | ||||||||||
| 61 | # $schema - Name of the schema file against which to check it | ||||||||||
| 62 | # | ||||||||||
| 63 | # Returns: Contents of the file as a hash | ||||||||||
| 64 | # Throws: YAML::XS exception on invalid file | ||||||||||
| 65 | # Text exception on schema mismatch | ||||||||||
| 66 | 63 | ▶ | sub load_yaml_file { | ||||||||
|
Subroutine
load_yaml_file: called CC=3 SCAR=12.6
Pod
documented
| |||||||||||
| 67 | 63 | my ($self, $path, $schema) = @_; | |||||||||
| 68 | |||||||||||
| 69 | # Tell YAML::XS to use real booleans. Otherwise, Kwalify is unhappy with | ||||||||||
| 70 | # data elements set to false. | ||||||||||
| 71 | 63 | local $YAML::XS::Boolean = 'JSON::PP'; | |||||||||
| 72 | |||||||||||
| 73 | # Load the metadata and check it against the schema. YAML::XS for some | ||||||||||
| 74 | # reason puts a newline before the system error part of an error message | ||||||||||
| 75 | # when loading a file, so clean up the error a bit. | ||||||||||
| 76 | 63 | my $schema_path = $self->appdata_path('schema', $schema . '.yaml'); | |||||||||
| 77 | 63 | my ($data_ref, $schema_ref); | |||||||||
| 78 | 63 | eval { | |||||||||
| 79 | 63 | $data_ref = YAML::XS::LoadFile($path); | |||||||||
| 80 | 63 | $schema_ref = YAML::XS::LoadFile($schema_path); | |||||||||
| 81 | }; | ||||||||||
| 82 | 63 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 0 | my $error = lcfirst($@); | |||||||||
| 84 | 0 | chomp($error); | |||||||||
| 85 | 0 | $error =~ s{ \n }{ }xms; | |||||||||
| 86 | 0 | die "$error\n"; | |||||||||
| 87 | } | ||||||||||
| 88 | 63 | eval { validate($schema_ref, $data_ref) }; | |||||||||
| 89 | 63 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 0 | my $errors = $@; | |||||||||
| 91 | 0 | chomp($errors); | |||||||||
| 92 | 0 | die "schema validation for $path failed:\n$errors\n"; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | # Return the verified contents. | ||||||||||
| 96 | 63 | return $data_ref; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | ############################################################################## | ||||||||||
| 100 | # Module return value and documentation | ||||||||||
| 101 | ############################################################################## | ||||||||||
| 102 | |||||||||||
| 103 | 19 | 1; | |||||||||
| 104 | __END__ | ||||||||||