-
Notifications
You must be signed in to change notification settings - Fork 0
/
dewi.in
2397 lines (2068 loc) · 64.3 KB
/
dewi.in
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!@@PERL5@@
# Copyright (c) 2010-2018
# Frank Terbeck <[email protected]>, All rights reserved.
# Terms for redistribution and use can be found in `LICENCE'.
package Dewi::Exceptions::EarlyExit;
sub new {
my ($class) = @_;
$self = { config => { do_deploy => 0,
be_silent => 0,
output_to_stderr => 1,
exit => undef },
output => [ ] };
bless $self, $class;
return $self;
}
sub add_config {
my ($self, $hr) = @_;
$self->{config} = { %{ $self->{config} }, %{ $hr } };
return $self->{config};
}
sub add_output {
my ($self, $output) = @_;
if (ref $output eq q{ARRAY}) {
push @{ $self->{output} }, @{ $output };
} else {
push @{ $self->{output} }, $output;
}
return $self->{output};
}
sub print_output {
my ($self) = @_;
my $printer = sub { return $self->print_to_stderr() ? warn @_ : print @_ };
foreach my $line (@{ $self->{output} }) {
$printer->($line, qq{\n});
}
return $#{ $self->{output} };
}
sub be_silent {
my ($self) = @_;
return $self->{config}->{be_silent};
}
sub do_deploy {
my ($self) = @_;
return $self->{config}->{do_deploy};
}
sub do_exit {
my ($self) = @_;
return $self->{config}->{exit};
}
sub print_to_stderr {
my ($self) = @_;
return $self->{config}->{output_to_stderr};
}
package Dewi::Storage::WithFallback;
# Basic key/value storage with fallback support.
sub new {
my ($class, $self) = @_;
$self = {} unless (defined $self);
bless $self, $class;
return $self;
}
# Gets a $key from the internal storage. If it is not defined in this
# instance's internal storage, try the fallback instance instead. Returns undef
# in case no defined value could be found for the $key in any of the queried
# instances.
sub get {
my ($self, $key) = @_;
my $result = $self->{getter}->($self, $key);
return $result if (defined $result);
return if (not defined $self->{fallback});
return $self->{fallback}->{getter}->($self->{fallback}, $key);
}
# Set a $key to a $value in an instances internal data store.
sub set {
my ($self, $key, $value) = @_;
return $self->{setter}->($self, $key, $value);
}
# Set the fallback instance to another WithFallback class instance.
sub set_fallback {
my ($self, $fb) = @_;
$self->{fallback} = $fb;
}
package Dewi::Storage::Options;
use parent -norequire, "Dewi::Storage::WithFallback";
# This is a fairly simple key/value store with support for getting data from
# other Dewi::Storage::WithFallback storages. This one also has support for
# interpreting values as boolean data when retrieving data from the storage.
#
# This data structure is used for implementing options and user options, of
# which there are global and package-local variants of both.
# This class uses $self->{data} as its internal storage. It is a hash ref to
# arbitrary types of values.
sub setopt {
my ($self, $key, $value) = @_;
$self->{data}->{$key} = $value;
return $value;
}
sub getopt {
my ($self, $key) = @_;
return $self->{data}->{$key};
}
# The following three sub-routines implement the interpretation of Perl scalar
# values as boolean values.
sub is_bool_false {
my ($value) = @_;
return 1 if (!defined $value
|| $value eq 'no'
|| $value eq 'no_thanks'
|| $value eq 'off'
|| $value eq 'false'
|| $value eq '0');
return 0;
}
sub is_bool_true {
my ($value) = @_;
return 0 if (!defined $value);
return 1 if ($value eq 'yes'
|| $value eq 'yes_please'
|| $value eq 'on'
|| $value eq 'true'
|| $value eq '1');
return 0;
}
sub getopt_bool {
my ($self, $key) = @_;
my $value;
$value = $self->get($key);
return 0 if (is_bool_false($value));
return 1 if (is_bool_true($value));
warn "Unknown value for boolean option \"$key\" ($value).";
warn " Assuming false.\n";
return 0;
}
sub new {
my ($class, $data) = @_;
my $self = $class->SUPER::new($data);
$self->{data} = {};
$self->{getter} = \&getopt;
$self->{setter} = \&setopt;
return $self;
}
package Dewi::Storage::Hooks;
use parent -norequire, "Dewi::Storage::WithFallback";
# This is a second kind of key-value store. But instead of accepting arbitrary
# Perl data as values, it maintains a list of code refs, to be used as hooks.
# The key to the list of values is the name of the hook to be run. This is
# obviously used for implementing hooks in dewi. It has methods like push,
# add, remove_first, remove_last and remove_all to manipulate the list of
# hooks. There are also get and set methods that modify the list of hooks
# directly.
#
# As with options, hooks have global and package-local variants, where the
# package-local variant overrides the list from the global variant.
# Push a new hook to the end of the current list.
sub push {
my ($self, $key, $value) = @_;
my $list = $self->safe_gethooks($key);
push @{ $list }, $value;
$self->sethooks($key, $list);
return $list;
}
# Add a new hook to the start of the current list.
sub add {
my ($self, $key, $value) = @_;
my $list = $self->safe_gethooks($key);
unshift @{ $list }, $value;
$self->sethooks($key, $list);
return $list;
}
# Get a list of hooks, possibly from fallback instances. If none were found,
# return an empty array ref.
sub safe_gethooks {
my ($self, $key) = @_;
my $list = $self->get($key);
return $list if (defined $list);
return [];
}
# Set the data-set for a $key to an empty array ref. If $key is not specified,
# clear the data-sets of *all* keys of *THIS* instance. This method does not
# decent into fallback instances.
sub clear {
my ($self, $key) = @_;
if (defined $key) {
$self->sethooks($key, []);
return $self->{data}->{$key};
}
foreach my $name (keys %{ $self->{data} }) {
$self->clear($name);
}
return $self->{data};
}
# Entirely delete the data-set for $key. If $key is not specified, the entire
# internal storage of *THIS* instance is set to an empty hash ref.
sub delete {
my ($self, $key) = @_;
if (defined $key) {
delete $self->{data}->{$key};
} else {
$self->{data} = {};
}
return $self->{data};
}
sub gethooks {
my ($self, $key) = @_;
return $self->{data}->{$key};
}
sub sethooks {
my ($self, $key, $value) = @_;
$self->{data}->{$key} = $value;
return $value;
}
sub new {
my ($class, $data) = @_;
my $self = $class->SUPER::new($data);
$self->{data} = undef;
$self->{getter} = \&gethooks;
$self->{setter} = \&sethooks;
return $self;
}
package DewiFile;
use strict;
use warnings qw(all);
use English '-no_match_vars';
use File::Find;
use File::Glob qw{ bsd_glob };
use File::Spec;
use File::Basename;
use Getopt::Long;
use Cwd;
use Scalar::Util qw{ blessed };
my $reg_calls = 0;
our (%DEWI, $local_options, $user_settings, $local_hooks);
# output
sub verbose {
DewiInternal::verbose(@_);
return 1;
}
sub debug {
DewiInternal::debug(@_);
return 1;
}
# option handling
sub set_opt {
my ($key, $value) = @_;
return DewiInternal::set_opt($key, $value);
}
sub set_opt_default {
my ($key, $value) = @_;
return DewiInternal::set_opt_default($key, $value);
}
sub get_opt {
my ($key) = @_;
return DewiInternal::get_opt($key);
}
sub get_opt_default {
my ($key) = @_;
return DewiInternal::get_opt_default($key);
}
sub get_opt_bool {
my ($key) = @_;
return DewiInternal::get_opt_bool($key);
}
sub get_opt_bool_default {
my ($key) = @_;
return DewiInternal::get_opt_bool_default($key);
}
# user definable settings
sub user_set {
my ($key, $value) = @_;
return DewiInternal::user_set($key, $value);
}
sub user_set_default {
my ($key, $value) = @_;
return DewiInternal::user_set_default($key, $value);
}
sub user_get {
my ($key) = @_;
return DewiInternal::user_get($key);
}
sub user_get_default {
my ($key) = @_;
return DewiInternal::user_get_default($key);
}
sub user_get_bool {
my ($key) = @_;
return DewiInternal::user_get_bool($key);
}
sub user_get_bool_default {
my ($key) = @_;
return DewiInternal::user_get_bool_default($key);
}
# helpers
# hooks
sub __add_hook {
my ($store, $h) = @_;
my ($ev);
goto ERROR if (ref($h) ne 'HASH');
$h->{type} = 'perl' if (!defined $h->{type});
goto ERROR if (!defined $h->{code} || !defined $h->{event});
if ($h->{type} ne 'perl' && $h->{type} ne 'shell-inline'
&& $h->{type} ne 'shell-file')
{
print "The type parameter needs to be one of these:\n";
print " - perl\n";
print " - shell-file\n";
print " - shell-inline\n";
print "\n";
goto ERROR;
}
if ($h->{type} eq 'perl' && ref($h->{code}) ne 'CODE') {
print "add_hook(): When `type' is \"perl\","
. " `code' needs to be a coderef.\n\n";
goto ERROR;
}
if ($h->{type} eq 'shell-file' && !-f ref($h->{code})) {
print "add_hook(): When `type' is \"shell-file\","
. " `code' needs to be an existing file.\n\n";
goto ERROR;
}
$ev = $h->{event};
delete($h->{event});
DewiInternal::register_hook($ev, $h);
return 1;
ERROR:
print "usage: add_hook(<hash-ref>);\n";
print "See dewifile(5) for details.\n";
exit 1;
}
sub add_hook {
my ($h) = @_;
my $store = (defined $local_hooks) ? $local_hooks : $DewiInternal::hooks;
return __add_hook($store, $h);
}
sub add_hook_default {
my ($h) = @_;
return __add_hook($DewiInternal::hooks, $h);
}
sub clear_hook {
my ($hook) = @_;
my $store = (defined $local_hooks) ? $local_hooks : $DewiInternal::hooks;
return $store->clear($hook);
}
sub clear_hook_default {
my ($hook) = @_;
return $DewiInternal::hooks->clear($hook);
}
sub delete_hook {
my ($hook) = @_;
my $store = (defined $local_hooks) ? $local_hooks : $DewiInternal::hooks;
return $store->delete($hook);
}
sub delete_hook_default {
my ($hook) = @_;
return $DewiInternal::hooks->delete($hook);
}
# predefined hook code
sub nuke_dead_links_register {
my ($dir, $glob) = @_;
if (!defined $dir || !defined $glob) {
die "Usage: nuke_dead_links_register('<dir>', '<glob>');\n";
}
my $store = (defined $local_options) ? $local_options : $DewiInternal::options;
my $cfg = $store->get(q{nuke-dead-links-cfg});
$cfg = {} unless (defined $cfg);
$cfg->{$dir} = $glob;
$store->set(q{nuke-dead-links-cfg}, $cfg);
$store->set(q{nuke-dead-links-verbose}, 1)
unless (defined $store->get(q{nuke-dead-links-verbose}));
return 1;
}
sub nuke_dead_links {
my $odir = cwd();
my $store = (defined $local_options) ? $local_options : $DewiInternal::options;
my $cfg = $store->get(q{nuke-dead-links-cfg});
if (!(defined $cfg && (ref $cfg eq q{HASH}))) {
print qq{nuke_dead_links(): Missing configuration!\n};
return 0;
}
DIR: foreach my $dir (sort keys %{ $cfg }) {
my $key = $dir;
$dir =~ s!^~!$ENV{HOME}!;
$dir =~ s/\/+$//;
chdir($dir) or do { print "Couldn't change to $dir: $ERRNO\n";
next DIR; };
print "Nuking dead symbolic links in $dir/...\n"
if ($store->getopt_bool(q{nuke-dead-links-verbose}));
my @dls = grep { -l && ! -e } bsd_glob($cfg->{$key});
foreach my $dl (sort @dls) {
print "nuke_dead_links(): Removing dead link: $dl\n";
unlink $dl || print " Failed to remove dead link: $ERRNO\n";
}
chdir($odir) or die "Couldn't change to $odir: $ERRNO\n";
}
return 1;
}
# request empty directories
sub deploy_directory {
if ($#_ != 0) {
die "usage: deploy_directory('directory_name');\n";
}
my ($dir) = @_;
my $new = {};
$new->{destination} = DewiInternal::expand_dir($dir);
$new->{deploydir} = 'yes';
debug("deploy_directory(): \"" . $new->{destination} . "\"\n");
push @DewiInternal::regfiles, $new;
}
# predefined `glob' code
sub regularfiles {
my ($glob) = @_;
return grep { -f } bsd_glob($glob);
}
sub recursivefiles {
my ($data) = @_;
my (@files, $regex);
if (ref($data) ne 'HASH') {
my $save = $data;
$data = {};
$data->{basedir} = $save;
}
$data->{basedir} = '.' if (!defined $data->{basedir});
$data->{regex} = '.' if (!defined $data->{regex});
$regex = qr{$data->{regex}};
my $findcb = sub {
return if !-f;
return if (!m/$regex/);
push @files, $File::Find::name;
};
find($findcb, $data->{basedir});
return @files;
}
# predefined `post-glob' code
sub remove_tilde {
# throw away stuff that matches "*~"
my (@ret);
foreach my $file (@_) {
if ($file !~ m/~$/) {
push @ret, $file
} else {
debug("Weeding out backup file: `$file'\n");
}
}
return @ret;
}
sub remove_hashes {
# throw away stuff that matches "#*#"
my (@ret);
foreach my $file (@_) {
my $name = basename($file);
if ($name !~ m/^#.+#$/) {
push @ret, $file
} else {
debug("Weeding out temporary file: `$file'\n");
}
}
return @ret;
}
# predefined `transform' code
sub notransform {
return $_[0];
}
sub makedotfile {
return '.' . $_[0];
}
# predefined `destination' code
sub recursive_dirname {
my ($prefix, $data) = @_;
return expand_path(merge_path($prefix, $data->{srcdir}));
}
# predefined `filter' code
sub print_filter {
my ($line) = @_;
return $line;
}
sub simple_filter_init {
my ($file) = @_;
unless (defined $file) {
warn qq{Usage: simple_filter_init('<file>');\n};
return 0;
}
$file = DewiInternal::expand_dir($file);
my $store = (defined $local_options) ? $local_options : $DewiInternal::options;
my $cfg = $store->get(q{simple-filter-cfg});
$cfg = {} unless (defined $cfg);
open my $in, q{<}, $file or do {
warn "Could not open file ($file): $ERRNO\n";
return 0;
};
while (my $line = <$in>) {
my ($tag, $repl);
chomp $line;
next if ($line =~ m/^\s*#/ || $line =~ m/^\s*$/);
if (($tag, $repl) = $line =~ m/^([^:]+):(.*)$/) {
$cfg->{$tag} = $repl;
} else {
warn "Unknown line in filter file: \"$line\"\n";
return 0;
}
}
close $in;
$store->set(q{simple-filter-cfg}, $cfg);
return 1;
}
sub simple_filter {
my ($line) = @_;
my $store = (defined $local_options) ? $local_options : $DewiInternal::options;
my $cfg = $store->get(q{simple-filter-cfg});
return $line unless (defined $cfg && ref $cfg eq q{HASH});
foreach my $tag (sort keys %{ $cfg }) {
my $repl = $cfg->{$tag};
$line =~ s/$tag/$repl/g;
}
return $line;
}
# path name helpers
sub merge_path {
if ($#_ != 1) {
die "Usage: merge_path(<part_one>, <part_two>);\n";
}
DewiInternal::merge_name(@_);
}
sub expand_path {
if ($#_ != 0) {
die "Usage: merge_path(<directory>);\n";
}
DewiInternal::expand_dir(@_);
}
# Miscellaneous helpers
sub find_cmd {
my ($cmd, $args) = @_;
my $path;
if (defined $args->{path}) {
$path = $args->{path};
} else {
$path = [ split /:/, $ENV{PATH} ];
}
if (defined $args->{more}) {
push @{ $path }, @{ $args->{more} };
}
foreach my $e (@{ $path }) {
my $rc = DewiInternal::expand_dir($e) . "/$cmd";
return $rc if (-f $rc);
}
return $args->{fallback} if (defined $args->{fallback});
return;
}
# The `register()' subroutine
sub __register_defaults {
# This sets default values for the different meaningful keys and
# also does some error-checking on values where it makes sense.
my ($h) = @_;
if (!defined $h->{glob}) {
die "Cannot call register() without `glob' argument.\n";
}
if (ref($h->{glob}) eq 'CODE' && !defined &{ $h->{glob} }) {
die
"register(): Unknown coderef in `glob'.\n".
" Call number $reg_calls.\n";
}
if (!defined $h->{destination}) {
$h->{destination} = '~/';
debug("register(): Setting default `destination': ~/\n");
}
if (!defined $h->{method}) {
$h->{method} = 'copy';
debug("register(): Setting default `method': copy\n");
}
if (!DewiInternal::method_exists($h->{method})) {
die
"register(): Unknown `method' parameter: \"" . $h->{method} . "\"\n".
" Call number $reg_calls.\n";
}
if (!defined $h->{transform}) {
$h->{transform} = \¬ransform;
debug("register(): Setting default `transform': `notransform'\n");
}
if (!defined &{ $h->{transform} }) {
die
"register(): Unknown coderef in `transform'.\n".
" Call number $reg_calls.\n";
}
if (defined $h->{post_glob} && !defined &{ $h->{post_glob} }) {
die
"register(): Unknown coderef in `post_glob'.\n".
" Call number $reg_calls.\n";
}
if (!defined $h->{globarg}) {
$h->{globarg} = '';
debug("register(): Setting default `globarg': (empty string)\n");
}
if ($h->{method} eq 'template') {
if (!defined $h->{options}) {
debug("register(): Setting default `options' for Template.\n");
$h->{options} = { };
}
if (!defined $h->{data}) {
debug("register(): Setting default `data' for Template.\n");
$h->{data} = { };
}
}
if ($h->{method} eq 'filtered') {
if (!defined $h->{filtertype}) {
debug("register(): Setting default `filtertype': perl\n");
$h->{filtertype} = 'perl';
}
if (!defined $h->{filter}) {
$h->{filtertype} = 'perl';
debug("register(): Setting default `filter': \&print_filter\n");
$h->{filter} = \&print_filter;
}
if ($h->{filtertype} eq 'perl' && !defined &{ $h->{filter} }) {
die "register(): perl filter: Unknown coderef.\n".
" Call number $reg_calls.\n";
}
if ($h->{filtertype} eq 'shell-file' && !-e $h->{filter}) {
die "register(): shell-file filter: File does not exist `" .
$h->{filter} . "'.\n".
" Call number $reg_calls.\n";
}
} elsif (defined $h->{concatenate}) {
die "register(): `concatenate' only allowed with method `filtered'.\n"
. " Call number $reg_calls.\n";
}
if (defined $h->{intercat}) {
my $ic = $h->{intercat};
if (!defined $ic->{type} || $ic->{type} eq 'perl') {
foreach my $cb (qw{firstpre firstpost
lastpre lastpost
otherpre otherpost})
{
if ((!defined $ic->{$cb.'type'} || $ic->{$cb.'type'} eq 'perl')
&& defined $ic->{$cb} && !defined &{ $ic->{$cb} }) {
die "register(): concatenate callback `$cb' does not point"
. " to a valid Perl subroutine."
. " Call number $reg_calls.\n";
}
}
}
}
return 1;
}
sub __register {
my ($h) = @_;
my (@files);
if (ref($h->{glob}) eq 'CODE') {
@files = DewiInternal::run_coderef(
q{globbing callback}, $h->{glob}, $h->{globarg});
} elsif (ref($h->{glob}) eq 'ARRAY') {
foreach my $entry (@{ $h->{glob} }) {
my @f = bsd_glob($entry);
if (defined $h->{post_glob_single}
&& ref $h->{post_glob_single} eq 'CODE') {
@f = DewiInternal::run_coderef(
q{post_glob_single callback}, $h->{post_glob_single}, @f);
}
push @files, @f;
}
} else {
@files = bsd_glob($h->{glob});
}
if (defined $h->{post_glob} && ref $h->{post_glob} eq 'CODE') {
@files = DewiInternal::run_coderef(
q{post_glob callback}, $h->{post_glob}, @files);
}
if (ref($h->{destination}) ne 'CODE') {
$h->{destination} = DewiInternal::expand_dir($h->{destination});
}
foreach my $path (@files) {
my $new = {};
my ($volume,$directories,$file) = File::Spec->splitpath( $path );
$new->{path} = $path;
$directories =~ s/\/+$//;
$new->{srcdir} = $directories;
$new->{name} = $file;
if (defined $h->{concatenate}) {
$new->{concatenate} = DewiInternal::run_coderef(
q{transform callback}, $h->{transform}, $h->{concatenate});
$new->{transformed} = $new->{concatenate};
} else {
$new->{transformed} = DewiInternal::run_coderef(
q{transform callback}, $h->{transform}, $file);
}
if (ref($h->{destination}) eq 'CODE') {
my %copy = %{ $new };
$new->{destination} = DewiInternal::run_coderef(
q{destination callback}, $h->{destination}, $h->{destarg}, \%copy );
} else {
$new->{destination} = $h->{destination};
}
$new->{method} = $h->{method};
if ($new->{method} eq 'template') {
$new->{options} = $h->{options};
$new->{data} = $h->{data};
}
$new->{filter} = $h->{filter};
$new->{filtertype} = $h->{filtertype};
$new->{intercat} = $h->{intercat};
$new->{mergedname} =
DewiInternal::merge_name($new->{destination}, $new->{transformed});
debug(
"register(): \"" . $new->{path} . "\"\n" .
" Name: \"" . $new->{name} . "\"\n");
debug(
" Concatenate: \"" . $new->{concatenate} . "\"\n")
if (defined $new->{concatenate});
debug(
" Transformed: \"" . $new->{transformed} . "\"\n" .
" Source: \"" . $new->{srcdir} . "\"\n" .
" Destination: \"" . $new->{destination} . "\"\n" .
" Merged-Name: \"" . $new->{mergedname} . "\"\n" .
" Method: " . $new->{method} . "\n");
push @DewiInternal::regfiles, $new;
}
return 1;
}
sub register {
$reg_calls++;
my ($arg) = @_;
my $type = ref $arg;
if ($type eq '') {
my ($nodot, $symlink) = (0, 0);
my $p = Getopt::Long::Parser->new();
$p->configure(qw{ require_order no_gnu_compat no_ignore_case });
$p->getoptionsfromarray(\@_,
q{no-dotfile} => \$nodot,
q{symlink} => \$symlink);
die "usage: register( OPTION(s), ..., FILENAME );\n" if ($#_ != 0);
debug("register(): --no-dotfile option used in trivial call.\n") if ($nodot != 0);
debug("register(): --symlink option used in trivial call.\n") if ($symlink != 0);
my ($arg) = @_;
my $hr = { glob => "$arg",
method => (($symlink == 0) ? "copy" : "symlink"),
transform => (($nodot == 0) ? \&makedotfile : \¬ransform) };
__register_defaults($hr);
__register($hr);
} elsif ($type eq 'HASH') {
die "usage: register( { foo => val0, bar => val1, ... } );\n" if ($#_ != 0);
__register_defaults($arg);
__register($arg);
} else {
die
"The argument to the register function must be either one\n".
"or more scalar values or exactly one hash reference.\n";
}
return 1;
}
# Dewifile reader
sub read_dewifile {
my ($file) = @_;
my ($d, $rc);
if (!-e $file) {
warn qq{dewi: $file does not exist!\n};
return 0;
}
$file = DewiInternal::merge_name(Cwd::abs_path(Cwd::getcwd()), $file)
unless (File::Spec->file_name_is_absolute($file));
$rc = do $file;
if (blessed($EVAL_ERROR)) {
my $ev = $EVAL_ERROR;
if ($ev->isa(q{Dewi::Exceptions::EarlyExit})) {
$ev->print_output();
warn q{dewi: Early exit from package: } . $DEWI{PACKAGE} . qq{\n}
unless ($ev->be_silent());
if (defined $ev->do_exit()) {
exit $ev->do_exit();
}
return $ev->do_deploy();
} else {
warn qq{read_dewifile: Unknown exception: $EVAL_ERROR\n};
return 0;
}
} elsif (!defined $rc && $EVAL_ERROR) {
warn qq{Could not parse $file:\n - Reason: $EVAL_ERROR\n};
return 0;
} elsif (!defined $rc) {
if ($EVAL_ERROR eq '') {
warn qq{$file empty?\n};
} else {
warn qq{Could not read $file:\n - Reason: $ERRNO\n};
}
return 0;
} elsif ($rc != 1) {
warn qq{Reading $file did not return 1.\n}
." While this is not a fatal problem, it is good practice, to let\n"
." perl script files return 1. Just put a '1;' into the last line\n"
." of this file to get rid of this warning.\n";
}
return 1;
}
# place holder function for the bootstrapping functionality
sub dewifile_is_empty {
print
"Empty package: " . $DEWI{PACKAGE} . "\n".
"This Dewifile is empty. Is was probably created by dewi's bootstrap mode.\n".
"You will need to register the files you want dewi to deploy. This is\n".
"merely a placeholder. Thanks for your attention.\n";
}
# a glorified '1;' for the end of Dewifiles
sub end {
return 1;
}
sub early_exit {
my $exception = Dewi::Exceptions::EarlyExit->new( { arguments => \@_ } );
foreach my $argument (@_) {
my $type = ref $argument;
if ($type eq q{HASH}) {
$exception->add_config($argument);
} elsif ($type eq q{ARRAY} || $type eq q{}) {
$exception->add_output($argument);
} else {
warn qq{usage: early_exit( [ARRAYREF|HASHREF|SCALAR]... )\n};
exit 1;
}
}
die $exception;
}
sub callsite {
return $main::callsite;
}
###########################################################################
package DewiInternal;
use strict;
use warnings qw(all);
use English '-no_match_vars';
use Cwd;
use File::Basename;
use File::Copy;
use File::Spec;
use Scalar::Util qw{ blessed };
my %optional_features = (
external_filters => "tobeseen",
templates => "tobeseen"
);
eval {require IPC::Run3;};
if ($EVAL_ERROR) {
$optional_features{external_filters} = 'missing';
} else {
$optional_features{external_filters} = 'gotit';
}
eval {require Template;};
if ($EVAL_ERROR) {
$optional_features{templates} = 'missing';
} else {
$optional_features{templates} = 'gotit';
}
our $NAME = 'dewi';
our $MAJOR_VERSION = @@MAJOR_VERSION@@;
our $MINOR_VERSION = @@MINOR_VERSION@@;
our $SUFFIX_VERSION = @@VERSION_SUFFIX@@;
our $FULL_VERSION = @@FULL_VERSION@@;
our $VERSION = $MAJOR_VERSION . '.' . $MINOR_VERSION . $SUFFIX_VERSION;
# Default set of options. This will be the fallback storage for per-package
# option sets as well.
our $options = Dewi::Storage::Options->new();
$options->set(q{filter_always}, q{no});
$options->set(q{debug}, q{no});
$options->set(q{dryrun}, q{no});
$options->set(q{verbose}, q{no});
# The user-settings work exactly like $options do. But the namespace it
# entirely for the user to control. Dewi will never do near it internally.
our $user_settings = Dewi::Storage::Options->new();