|
1 #!perl |
|
2 # exportall |
|
3 # |
|
4 # Copyright (c) 2008 - 2010 Accenture. All rights reserved. |
|
5 # This component and the accompanying materials are made available |
|
6 # under the terms of the "Eclipse Public License v1.0" |
|
7 # which accompanies this distribution, and is available |
|
8 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
9 # |
|
10 # Initial Contributors: |
|
11 # Accenture - Initial contribution |
|
12 # |
|
13 |
|
14 # Description: |
|
15 # exportall - A tool to export a whole folder of files from a bld.inf |
|
16 |
|
17 use strict; |
|
18 use Cwd; |
|
19 use Getopt::Long; |
|
20 use File::Copy; |
|
21 use File::Basename; |
|
22 use File::Path; |
|
23 |
|
24 sub ProcessCommandLine(); |
|
25 sub DisplayHelp(); |
|
26 sub FindSourceFiles(); |
|
27 sub DoCopy($$); |
|
28 |
|
29 my %options; |
|
30 my $sourceDir; |
|
31 my $destDir; |
|
32 |
|
33 ProcessCommandLine(); |
|
34 |
|
35 my $sourceFiles = FindSourceFiles(); |
|
36 my @destFiles = @$sourceFiles; |
|
37 map { s|^\Q$sourceDir\E(.*)|$destDir$1| } @destFiles; |
|
38 map { s|\\\\|\\| } @destFiles; # get rid of double \\ |
|
39 |
|
40 if ($options{what}) { |
|
41 if (@destFiles > 0) { |
|
42 local $, = "\n"; |
|
43 print @destFiles; |
|
44 print "\n"; |
|
45 } |
|
46 } elsif ($options{clean}) { |
|
47 map {unlink} @destFiles; |
|
48 } else { |
|
49 local $, = "\n"; |
|
50 map { my $src =$_; |
|
51 s|^\Q$sourceDir\E(.*)|$destDir$1|; |
|
52 DoCopy($src, $_); |
|
53 } @$sourceFiles; |
|
54 } |
|
55 |
|
56 sub ProcessCommandLine() { |
|
57 my $help; |
|
58 GetOptions('h|help' => \$help, |
|
59 'v|verbose' => \$options{verbose}, |
|
60 'w|what' => \$options{what}, |
|
61 'c|clean' => \$options{clean}) or DisplayHelp(); |
|
62 |
|
63 DisplayHelp() if $help; |
|
64 |
|
65 if (scalar(@ARGV)!=2) { |
|
66 print "ERROR: Invalid number of arguments.\n"; |
|
67 DisplayHelp(); |
|
68 } |
|
69 $sourceDir = shift(@ARGV); |
|
70 $destDir = shift(@ARGV); |
|
71 print STDERR "Exporting from $sourceDir to $destDir\n" if ($options{verbose}); |
|
72 } |
|
73 |
|
74 sub DoCopy($$) { |
|
75 my ($src, $dest) = @_; |
|
76 my $destDir = dirname($dest); |
|
77 unless (-d $destDir) { |
|
78 mkpath $destDir or die "Can't create $destDir: $!"; |
|
79 } |
|
80 if (-e $dest) { |
|
81 unlink $dest; |
|
82 } |
|
83 copy($src, $dest) or die "Can't copy $src to $_: $!" |
|
84 } |
|
85 |
|
86 sub FindSourceFiles() { |
|
87 my @sourceFiles; |
|
88 my @dirStack; |
|
89 push @dirStack, $sourceDir; |
|
90 while (scalar(@dirStack)) { |
|
91 my $dir = pop(@dirStack); |
|
92 opendir DIR, "$dir"; |
|
93 my @dirContents = readdir(DIR); |
|
94 closedir(DIR); |
|
95 push @sourceFiles, (map {"$dir\\$_"} (grep {-f "$dir\\$_"} @dirContents)); |
|
96 push @dirStack, (map {"$dir\\$_"} (grep {(-d "$dir\\$_") && !(m|^\.\.?|)} @dirContents)); |
|
97 } |
|
98 return \@sourceFiles; |
|
99 } |
|
100 |
|
101 sub DisplayHelp() { |
|
102 require Pod::Text; |
|
103 print "\n"; |
|
104 my $parser = Pod::Text->new(); |
|
105 $parser->parse_from_file($0); |
|
106 exit; |
|
107 } |
|
108 |
|
109 __END__ |
|
110 |
|
111 =head1 NAME |
|
112 |
|
113 exportall - A tool to export an entire directory tree from a project. Designed to be invoked from a custom makefile. |
|
114 |
|
115 =head1 SYNOPSIS |
|
116 |
|
117 exportall [options] sourceDir destDir |
|
118 |
|
119 options: |
|
120 |
|
121 =over |
|
122 |
|
123 =item -h |
|
124 |
|
125 Show help |
|
126 |
|
127 =item -v |
|
128 |
|
129 Verbose |
|
130 |
|
131 =item -w |
|
132 |
|
133 Print list of files to be exported, for a C<-what>. |
|
134 |
|
135 =item -c |
|
136 |
|
137 Clean. Deletes all files that aer exported. |
|
138 |
|
139 =back |
|
140 |
|
141 =head1 COPYRIGHT |
|
142 |
|
143 Copyright (c) 2008-2010 Accenture. All rights reserved. |
|
144 |
|
145 =cut=cut |