-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleAutoloader.pm
More file actions
405 lines (346 loc) · 10.7 KB
/
Copy pathModuleAutoloader.pm
File metadata and controls
405 lines (346 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package ModuleAutoloader;
use strict;
use warnings;
use FindBin;
use Try::Tiny;
use Getopt::Long qw(GetOptions);
use Data::Dumper;
Getopt::Long::Configure qw(pass_through);
$ModuleAutoloader::VERSION = "1.1.1";
my $debugLib = '';
my $libPath = $FindBin::Bin;
my $debugConfig = '';
my $configPath = $FindBin::Bin . '/moduleAutoloaderConfig';
my $relativeBasePath = $FindBin::Bin;
GetOptions (
"Autoload-DebugLib" => \$debugLib,
"Autoload-LibPath=s" => \$libPath,
"Autoload-DebugConfig" => \$debugConfig,
"Autoload-ConfigPath=s" => \$configPath,
"Autoload-RelativeBasePath=s" => \$relativeBasePath,
);
my $currentFileDirectory = " ";
BEGIN
{
foreach my $dir (@INC) {
next if (!defined $dir);
next if (ref($dir));
if (-f $dir."/ModuleAutoloader.pm") {
$currentFileDirectory = $dir."/";
last;
}
}
push(@INC, $currentFileDirectory."/moduleAutoloaderLib");
push(@INC, $currentFileDirectory."/moduleAutoloaderLib/Exception");
push(@INC, $currentFileDirectory."/moduleAutoloaderLib/Explorer");
push(@INC, $currentFileDirectory."/moduleAutoloaderLib/Hash-Merge-0.200/lib/");
}
use Explorer;
use YamlException;
use DirectoryException;
use autouse 'YAML' => qw(LoadFile);
use Hash::Merge qw(merge);
my $yamlLibEnable = 0;
my %configuration = ();
my @included = [];
INIT
{
ModuleAutoloader::loadLibrary();
ModuleAutoloader::loadConfiguration();
ModuleAutoloader::processConfiguration();
}
sub loadLibrary
{
my ($libInfo) = @_;
my $explorer = Explorer->new();
my @librariesDir;
my $exception = undef;
$libInfo->{"path"} = $libPath if (!defined $libInfo->{"path"});
$libInfo->{"recursive"} = 1 if (!defined $libInfo->{"recursive"});
$libInfo->{"error"} = "Autoloader unable to automatically discover itself libraries." if (!defined $libInfo->{"error"});
try {
@librariesDir = @{$explorer->searchDirectoriesContaining({
directories => [$libInfo->{path}],
pattern => "\.(pm|t|pl)\$",
recursive => $libInfo->{recursive}
})};
} catch {
my $exception = DirectoryException->new({
message => $libInfo->{error},
code => "980a50f41",
previous => $_
});
print Dumper $exception;
die $exception;
};
foreach my $library (@librariesDir){
if ($library =~ /YAML/i ) {
$yamlLibEnable = 1;
}
ModuleAutoloader::pushInc($library);
}
}
sub loadConfiguration
{
my $self = shift;
my $explorer = Explorer->new();
my @configFile;
try {
@configFile = @{$explorer->searchFiles({
directories => [$configPath],
pattern => "\.(yaml|yml)\$",
recursive => 1
})};
} catch {
my $exception = DirectoryException->new({
message => "Autoloader unable to reach configuration directory.",
code => "980a50f42",
previous => $_
});
print Dumper $exception;
die $exception;
};
my $merger = Hash::Merge->new('RIGHT_PRECEDENT');
foreach my $yamlFile (@configFile){
$yamlFile = $configPath . "/" . $yamlFile;
print "Loading yaml file : " . $yamlFile . "\n" if ($debugConfig);
my $fileContent;
try {
$fileContent = LoadFile($yamlFile);
} catch {
my $exception = YamlException->new({
message => "Autoloader unable to load file $yamlFile.",
code => "980a50f43",
previous => $_
});
print Dumper $exception;
die $exception;
};
if ($fileContent->{autoloader}) {
print "Configuration data find. Loading \n" if ($debugConfig);
%configuration = %{$merger->merge(\%configuration, $fileContent->{autoloader})};
}
}
}
sub processConfiguration
{
my $default = ModuleAutoloader::searchDefault(\%configuration);
ModuleAutoloader::includeConfigurationDirectories({
configuration => \%configuration,
defaults => $default,
});
}
sub searchDefault
{
my $configuration = shift;
my $default;
$default->{recursive} = "false";
$default->{strict} = "false";
$default->{basePath} = "/";
if (!$configuration->{default}) {
return $default;
}
$default->{recursive} = $configuration->{default}->{recursive} if (defined $configuration->{default}->{recursive});
$default->{strict} = $configuration->{default}->{strict} if (defined $configuration->{default}->{strict});
$default->{basePath} = $configuration->{default}->{basePath} if (defined $configuration->{default}->{basePath});
return $default;
}
sub includeConfigurationDirectories
{
my ($args) = @_;
my $configuration = $args->{"configuration"};
my $defaults = $args->{"defaults"};
if (!$configuration->{"directories"}) {
return;
}
my $directories = $configuration->{directories};
foreach my $dirKey (keys($directories)) {
try {
if ($directories->{$dirKey}->{dir}) {
print "Include $dirKey \n" if ($debugConfig);
my $recursive = 0;
if (defined $directories->{$dirKey}->{recursive}) {
my $recursiveValue = $directories->{$dirKey}->{recursive};
$recursive = 1 if ($recursiveValue eq "true");
$recursive = 0 if ($recursiveValue eq "false");
} else {
$recursive = 1 if ($defaults->{recursive} eq "true");
$recursive = 0 if ($defaults->{recursive} eq "false");
}
my $strict = 0;
if (defined $directories->{$dirKey}->{strict}) {
my $strictValue = $directories->{$dirKey}->{strict};
$strict = 1 if ($strictValue eq "true");
$strict = 0 if ($strictValue eq "false");
} else {
$strict = 1 if ($defaults->{strict} eq "true");
$strict = 0 if ($defaults->{strict} eq "false");
}
my $realPath = ModuleAutoloader::getPath({
"path" => $directories->{$dirKey}->{dir},
"defaults" => $defaults,
});
ModuleAutoloader::loadLibrary({
"path" => $realPath,
"recursive" => $recursive,
"error" => "Autoloader unable to load $dirKey",
});
ModuleAutoloader::pushInc($realPath) if $strict;
} else {
my $exception = YamlException->new({
message => "Yaml format error for '$dirKey' element. Need 'dir' key with directory path",
code => "980a50f44",
previous => $_
});
print Dumper $exception;
die $exception;
}
} catch {
my $exception = YamlException->new({
message => "Yaml format error for '$dirKey' element. Need 'dir' key with directory path",
code => "980a50f44",
previous => $_
});
print Dumper $exception;
die $exception;
};
}
}
sub pushInc
{
my $lib = shift;
if(grep(/^$lib$/, @included)) {
print "Library directory already added to INC : $lib\n" if $debugLib;
return;
}
print "Library directory added to INC : $lib\n" if $debugLib;
push(@included, $lib);
push(@INC, $lib);
}
sub import {
my $pkg = shift;
my ($messages) = @_;
if ( defined $messages ) {
if (ref($messages) eq "ARRAY"){
foreach my $message (@{$messages}) {
$pkg->applyMessage($message);
}
} else {
$pkg->applyMessage($messages);
}
}
return;
}
sub applyMessage
{
my $pkg = shift;
my $messages = shift;
if ($messages =~ /DebugConfig=/){
my ($state) = ($messages =~ /DebugConfig=([01])/);
$debugConfig = ($state eq "0") ? 0 : 1;
} elsif ($messages =~ /DebugLib=/){
my ($state) = ($messages =~ /DebugLib=([01])/);
$debugLib = ($state eq "0") ? 0 : 1;
} elsif ($messages =~ /LibPath=/){
($libPath) = ($messages =~ /LibPath=(.+)/);
} elsif ($messages =~ /ConfigPath=/){
($configPath) = ($messages =~ /ConfigPath=(.+)/);
} elsif ($messages =~ /RelativeBasePath=/){
($relativeBasePath) = ($messages =~ /RelativeBasePath=(.+)/);
}
}
sub getPath
{
my ($args) = @_;
my $path = $args->{"path"};
my $default = $args->{"defaults"};
if (ModuleAutoloader::isRelative({"path"=>$path, "defaults"=>$default})) {
if ($path =~ /^\//) {
return $relativeBasePath . $path;
} else {
return $relativeBasePath . '/' . $path;
}
} else {
return $path;
}
}
sub isRelative
{
my ($args) = @_;
my $path = $args->{"path"};
my $default = $args->{"defaults"};
my $basePath = $default->{"basePath"};
return !($path =~ /^$basePath/);
}
1;
__END__
=begin markdown
# ModuleAutoloader
The ModuleAutoloader package is used to load dynamicaly
the include path of a perl script..
Use [strict](http://perldoc.perl.org/strict.html)
Use [warnings](http://perldoc.perl.org/warnings.html)
Use [FindBin](http://perldoc.perl.org/FindBin.html)
Use [Try::Tiny](http://search.cpan.org/~ether/Try-Tiny-0.24/lib/Try/Tiny.pm)
Use [Getopt::Long](http://perldoc.perl.org/Getopt/Long.html)
Use [Data::Dumper](http://perldoc.perl.org/Data/Dumper.html)
Version: 1.1.1
Date: 2016/04/11
Author: Matthieu vallance <matthieu.vallance@cscfa.fr>
Module: [ModuleAutoloader](./ModuleAutoloader.md)
License: MIT
### Options
option | action
------ | ------
Autoload-DebugLib | print each imported library
Autoload-LibPath=s | define the ModuleAutoloaderLib path
Autoload-DebugConfig | print each configuration file informations
Autoload-ConfigPath=s | define the ModuleAutoloaderConfig path
### Configuration :
The Autoloader allow you to define the configuration into a yaml file write into the ConfigPath directory.
The default ConfigPath directory is './moduleAutoloaderConfig'.
The configuration file must finish by '.yaml' or '.yml' and contain the 'autoloader' key.
The allowed things for the configuration are the default recursivity and a set of directory to load. See the following example :
```yaml
autoloader:
default:
recursive: true # The default recursivity for the import directories (false as default)
strict: true # define that the directories must be strict included (false as default)
basePath: "/" # define the root base path to process real path of relative directories
directories:
firstKey: # Configuration key (user defined, no one requirement)
dir: "/perl/some/directory/to/load"
secondKey:
dir: "/perl/some/other/directory/to/load"
recursive: false # override the default recursivity for the current dir
strict: false # override the default strict for the current dir
thirdKey: # Configuration key (user defined, no one requirement)
dir: "relative/directory/to/load" #
```
The 'strict' element force the autoloader to inject the directory as it without check that it contain any pm|t|pl file.
Note you can use a relative path since version 1.1.0. The relative path is parsed as real path before include.
The real path take at relative base path the ModuleAutoloader.pm file path as default. This is override by the Autoload-RelativeBasePath
option, or by the RelativeBasePath use message. See example for usage.
### Example of use :
```perl
use strict;
use warnings;
use FindBin;
BEGIN
{
push @INC, $FindBin::Bin;
}
use ModuleAutoloader;
```
If you want to use specific path, you can import parameters as in this example :
```perl
use strict;
use warnings;
use Getopt::Long;
BEGIN
{
push @INC, $FindBin::Bin;
}
use ModuleAutoloader ["DebugLib=1", "DebugConfig=1", "LibPath=/some/path", "ConfigPath=/some/other/path", "RelativeBasePath=$FindBin::Bin/"];
```
=end markdown