Another simplification
[MPC.git] / vs_postclean.pl
blob2b0adfa6af7fac2a7051b31f0a6392400f2b9dd5
1 #!/usr/bin/env perl
2 eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}'
3 & eval 'exec perl -w -S $0 $argv:q'
4 if 0;
6 # ******************************************************************
7 # Author: Chad Elliott
8 # Date: 7/10/2008
9 # Description: Visual Studio doesn't support a post clean build step,
10 # so this script will do it.
11 # ******************************************************************
13 # ******************************************************************
14 # Pragma Section
15 # ******************************************************************
17 use strict;
18 use FileHandle;
19 use File::Basename;
20 use Cwd;
22 # ******************************************************************
23 # Data Section
24 # ******************************************************************
26 my $version = '1.0';
28 # ******************************************************************
29 # Subroutine Section
30 # ******************************************************************
32 sub read_proj {
33 my($cfg, $file) = @_;
34 my $fh = new FileHandle();
35 my $cmd;
37 if (open($fh, $file)) {
38 my $cfg_ok;
39 my $next_name;
40 my $next_command;
41 while(<$fh>) {
42 ## Locate the start of a Configuration section
43 if (/<Configuration\s*$/) {
44 $next_name = 1;
46 ## Next, find the configuration name
47 elsif ($next_name && /Name="(.+)"/) {
48 $cfg_ok = ($1 eq $cfg);
49 $next_name = undef;
51 ## Next, find the post clean event
52 elsif ($cfg_ok && /Name="VCPostCleanEventTool"/) {
53 $next_command = 1;
55 ## And finally, get the postclean command line
56 elsif ($next_command && /CommandLine="(.+)"/) {
57 $cmd = $1;
58 last;
61 close($fh);
64 ## Convert frequently used XML sequences to plain characters.
65 $cmd =~ s/&amp;/&/g;
66 $cmd =~ s/&quot;/\"/g;
67 $cmd =~ s/&gt;/>/g;
68 $cmd =~ s/&lt;/</g;
70 ## Return the command line (undef if there was no postclean)
71 return $cmd;
74 sub clean_proj {
75 my($cfg, $file) = @_;
77 ## Read the postclean command from the project
78 my $cmd = read_proj($cfg, $file);
80 ## Move to the directory of the project and run the command
81 if (defined $cmd) {
82 my $current_dir = getcwd();
83 if (chdir(dirname($file))) {
84 system($cmd);
85 chdir($current_dir);
87 else {
88 ## We'll only warn about files that we can't deal with.
89 print "WARNING: Unable to postclean $file\n";
94 sub clean_sln {
95 my($cfg, $file) = @_;
96 my $fh = new FileHandle();
98 ## For a solution, just read in and clean each project file we find.
99 if (open($fh, $file)) {
100 while (<$fh>) {
101 if (/^Project\([^)]+\)\s*=\s*"[^\"]+",\s*"([^\"]+)"/) {
102 clean_proj($cfg, $1);
105 close($fh);
109 # ******************************************************************
110 # Main Section
111 # ******************************************************************
113 if ($#ARGV == -1) {
114 print "PostClean v$version\n",
115 "Usage: ", basename($0), " [CFG=<configuration>] <project or solution files>\n\n",
116 "Executes the MPC generated VCPostCleanEventTool commands in a project.\n",
117 "These events are ignored by VisualStudio and must be handled by an outside\n",
118 "tool (i.e., this script).\n";
119 exit(0);
122 ## Determine the project or solution configuration (defaulting to the
123 ## default created by MPC).
124 my $cfg = 'Debug|Win32';
125 if ($ARGV[0] =~ /^CFG=(.+)/) {
126 $cfg = $1;
127 shift(@ARGV);
130 foreach my $file (@ARGV) {
131 if ($file =~ /\.sln$/) {
132 clean_sln($cfg, $file);
134 else {
135 ## It's not a solution file, we'll assume it's a project
136 clean_proj($cfg, $file);