This repository has been archived by the owner on Nov 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macrodir
executable file
·128 lines (103 loc) · 3.36 KB
/
macrodir
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
#!/usr/bin/perl -w
# $Id: macrodir,v 1.11 2000/02/03 22:19:35 root Exp $
# Copyright (c) 1999-2000 Mark Summerfield. All Rights Reserved.
# May be used/distributed under the GPL.
use strict ;
use Cwd ;
use Getopt::Long ;
use File::Basename ;
use File::Find ;
use File::Path ;
use Symbol ;
use Text::MacroScript ;
use vars qw( $VERSION ) ;
$VERSION = '0.06' ;
my %Opt ;
&getoptions ;
my $Macro = Text::MacroScript->new(
-embedded => $Opt{'prep'} ? 0 : 1,
-file => [ $Opt{'file'} ],
) ;
find( \&wanted, scalar @ARGV ? @ARGV : '.' ) ;
sub wanted {
return unless /\.m$/o ;
my $file = $_ ;
my $newfile = $file ;
$newfile =~ s/\.m$//o ;
my $showdir = "$File::Find::dir/" ;
if( $Opt{'dir'} ) {
my $targetdir = $showdir ;
if( $targetdir =~ m,^\./,o ) {
$targetdir =~ s,^\./,$Opt{'dir'},o ;
}
else {
$targetdir =~ s/^$Opt{'root'}/$Opt{'dir'}/o ;
}
$targetdir .= '/' unless $targetdir =~ /\/$/o ;
$newfile = $targetdir . $newfile ;
$showdir = '' ;
}
if( $Opt{'force'} or ( not -e $newfile ) or
( (stat( $newfile ))[9] < (stat( $file ))[9] ) ) {
print STDERR "Expanding macros in $File::Find::name to $showdir$newfile\n"
if $Opt{'verbose'} ;
eval {
if( not $showdir ) { # Possibly new directory
my $path = dirname( $newfile ) ;
if( not -e $path ) {
print STDERR "Creating $path\n" if $Opt{'verbose'} ;
mkpath( $path ) ;
}
}
my $fh = gensym ;
open $fh, ">$newfile" or
die "Failed to open $newfile: $!\n" ;
print $fh $Macro->expand_file( $file ) ;
close $fh ;
} ;
warn $@ if $@ ;
}
elsif( $Opt{'verbose'} > 1 ) {
print STDERR "$showdir$newfile is up to date\n"
}
}
sub getoptions {
# Defaults.
$Opt{'dir'} = '' ;
$Opt{'force'} = 0 ;
$Opt{'file'} = 'macro' ;
$Opt{'prep'} = 0 ;
$Opt{'root'} = cwd ;
$Opt{'verbose'} = 1 ;
&help if defined $ARGV[0] and $ARGV[0] =~ /^(-h|--help)$/o ;
Getopt::Long::config 'no_ignore_case' ;
GetOptions( \%Opt,
'dir|d=s',
'force|f',
'file|F=s',
'prep|p',
'verbose|v:i',
) or &help ;
$Opt{'dir'} .= '/' if $Opt{'dir'} and $Opt{'dir'} !~ /\/$/o ;
}
sub help {
print STDERR <<__EOT__ ;
macrodir v $VERSION. Copyright (c) Mark Summerfield 1999-2000.
All rights reserved. May be used/distributed under the GPL.
usage: macrodir [options] <path>
-d --dir Put output files in <dir> instead of $Opt{'root'}
-f --force Force conversion [$Opt{'force'}]
-F --file Take macros from this file [$Opt{'file'}]
-h --help Show this screen and exit
-p --prep Operate as macro pre-processor instead of an embedded macro
expander
-v --verbose Verbose [$Opt{'verbose'}]
Loads the macros from file '$Opt{'file'}' in the current directory then expands
macros embedded in <: and :> in every .m file in the current directory and any
subdirectories.
Text::MacroScript now supplies a function relpath which returns the relative
path. (See html.macro example file for usage and Text::MacroScript.pm and
macro documentation.)
__EOT__
exit ;
}