File Coverage

blib/lib/Enbld/Target/Symlink.pm
Criterion Covered Total %
statement 47 48 97.9
branch 13 16 81.2
condition n/a
subroutine 6 6 100.0
pod 0 2 0.0
total 66 72 91.6


line stmt bran cond sub pod time code
1             package Enbld::Target::Symlink;
2              
3 2     2   7 use strict;
  2         2  
  2         50  
4 2     2   5 use warnings;
  2         3  
  2         43  
5              
6 2     2   6 use File::Spec;
  2         2  
  2         41  
7 2     2   6 use File::Path qw/make_path/;
  2         2  
  2         1077  
8              
9             require Enbld::Home;
10              
11              
12             # directory list for symbolic link
13             our $dir_list = {
14             'bin' => {
15             'dir' => 'bin',
16             'omit' => undef,
17             },
18              
19             'sbin' => {
20             'dir' => 'sbin',
21             'omit' => undef,
22             },
23              
24             'lib' => {
25             'dir' => 'lib',
26             'omit' => [ 'pkgconfig', 'perl5' ],
27             },
28            
29             'pkgconfig' => {
30             'dir' => File::Spec->catdir( 'lib', 'pkgconfig' ),
31             'omit' => undef,
32             },
33            
34             'include' => {
35             'dir' => 'include',
36             'omit' => undef,
37             },
38              
39             'script' => {
40             'dir' => 'script',
41             'omit' => undef,
42             },
43              
44             'support-files' => {
45             'dir' => 'support-files',
46             'omit' => undef,
47             },
48              
49             'man-man1' => {
50             'dir' => File::Spec->catdir( 'man', 'man1' ),
51             'omit' => undef,
52             },
53              
54             'man-man3' => {
55             'dir' => File::Spec->catdir( 'man', 'man3' ),
56             'omit' => undef,
57             },
58              
59             'share-man1' => {
60             'dir' => File::Spec->catdir( 'share', 'man', 'man1' ),
61             'omit' => undef,
62             },
63              
64             'share-man3' => {
65             'dir' => File::Spec->catdir( 'share', 'man', 'man3' ),
66             'omit' => undef,
67             },
68              
69             'share' => {
70             'dir' => 'share',
71             'omit' => [ 'emacs', 'info', 'man', 'doc', 'aclocal' ],
72             },
73              
74             'doc' => {
75             'dir' => File::Spec->catdir( 'share', 'doc' ),
76             'omit' => undef,
77             },
78              
79             'site-lisp' => {
80             'dir' => File::Spec->catdir( 'share', 'emacs', 'site-lisp' ),
81             'omit' => undef,
82             },
83              
84             'aclocal' => {
85             'dir' => File::Spec->catdir( 'share', 'aclocal' ),
86             'omit' => undef,
87             },
88             };
89              
90             sub delete_symlink {
91 28     28 0 80 my ( $pkg, $old_depository ) = @_;
92              
93 28         44 foreach my $dir ( sort keys %{ $dir_list } ) {
  28         490  
94             my $installed = File::Spec->catdir(
95             Enbld::Home->home,
96             $dir_list->{$dir}{dir}
97 420         1351 );
98              
99 420 100       4340 next unless ( -d $installed );
100            
101 8         326 opendir( my $dh, $installed );
102 8         234 my @list = grep { !/^\.{1,2}$/ } readdir $dh;
  24         152  
103 8         64 closedir $dh;
104              
105 8         89 chdir $installed;
106            
107 8         25 foreach my $file ( @list ) {
108 8 50       92 next unless ( -l $file );
109              
110 8         70 my $link = readlink( $file );
111            
112 8 100       94 if ( index( $link, $old_depository ) == 0 ) {
113 5         322 unlink $file;
114             }
115             }
116             }
117             }
118              
119             sub create_symlink {
120 27     27 0 78 my ( $pkg, $new_depository ) = @_;
121              
122 27         50 foreach my $dir ( sort keys %{$dir_list} ) {
  27         229  
123             my $link_from_path =
124 405         1552 File::Spec->catdir( $new_depository, $dir_list->{$dir}{dir} );
125              
126 405 100       4822 next unless ( -d $link_from_path );
127            
128 27         897 opendir( my $dh, $link_from_path );
129 27         500 my @list = grep { !/^\.{1,2}$/ } readdir $dh;
  81         394  
130 27         166 closedir $dh;
131            
132             my $link_to_path = File::Spec->catdir(
133             Enbld::Home->home,
134             $dir_list->{$dir}{dir}
135 27         216 );
136            
137 27 100       286 unless ( -d $link_to_path ) {
138 20         4144 make_path( $link_to_path );
139             }
140              
141 27         80 foreach my $file ( @list ) {
142 27 50       64 next if ( grep {/^$file$/ } @{$dir_list->{$dir}{omit}} );
  0         0  
  27         147  
143            
144 27         321 my $symlink = File::Spec->catfile( $link_to_path, $file );
145            
146 27 100       482 if ( -l $symlink ) {
147 3         171 unlink $symlink;
148             }
149            
150 27 50       240 unless ( -e $symlink ) {
151 27         1176 symlink File::Spec->catfile( $link_from_path, $file ), $symlink;
152             }
153             }
154             }
155             }
156              
157              
158             1;