ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/BuildSystem/TemplateInterface.pm
Revision: 1.3
Committed: Tue Nov 14 17:43:14 2006 UTC (18 years, 5 months ago) by sashby
Content type: text/plain
Branch: MAIN
Changes since 1.2: +8 -5 lines
Log Message:
Fix to make template dir path configurable

File Contents

# Content
1 #____________________________________________________________________
2 # File: TemplateInterface.pm
3 #____________________________________________________________________
4 #
5 # Author: Shaun Ashby <Shaun.Ashby@cern.ch>
6 # Update: 2004-07-01 14:03:46+0200
7 # Revision: $Id: TemplateInterface.pm,v 1.2 2004/12/10 13:41:37 sashby Exp $
8 #
9 # Copyright: 2004 (C) Shaun Ashby
10 #
11 #--------------------------------------------------------------------
12 package BuildSystem::TemplateInterface;
13 require 5.004;
14 use Exporter;
15 @ISA=qw(Exporter);
16 @EXPORT_OK=qw( );
17
18 sub new()
19 ###############################################################
20 # new #
21 ###############################################################
22 # modified : Thu Jul 1 14:04:01 2004 / SFA #
23 # params : #
24 # : #
25 # function : #
26 # : #
27 ###############################################################
28 {
29 my $proto=shift;
30 my $class=ref($proto) || $proto;
31 my $self={};
32
33 bless $self,$class;
34
35 # Process the environment hash and extract a
36 # list of vars that are needed for building.
37 # NB: This is done once only!
38 my %exportenv;
39 map
40 {
41 if ($_ =~ /^SCRAM/ || $_ =~ /(LOCAL|RELEASE)TOP$/)
42 {
43 $exportenv{$_} = $ENV{$_} if ($_ !~ /^SCRAMRT\_.*$/);
44 }
45 } keys %ENV;
46
47 # Add the environment information to the TEMPLATE_DATA hash:
48 $self->{TEMPLATE_DATA} = { 'environment' => \%exportenv };
49
50 # The filehandle for the generated Makefile:
51 my $makefile="$ENV{LOCALTOP}/$ENV{SCRAM_INTwork}/Makefile";
52
53 use FileHandle;
54 $self->{MAKEFILEFH} = FileHandle->new();
55 $self->{MAKEFILEFH}->open("> $makefile");
56
57 # Init and pass in the template location:
58 $self->_init(@_);
59 return $self;
60 }
61
62 sub _init()
63 {
64 my $self=shift;
65 my ($templatedir)=@_;
66
67 # Set the location where the templates may be found:
68 $self->template_dir($templatedir);
69 # Configure the template object:
70 $self->template_config();
71 # Create the new Template object:
72 $self->template_object();
73 return $self;
74 }
75
76 sub template_object()
77 {
78 my $self=shift;
79
80 # Instantiate a new Template object:
81 eval("use Template");
82
83 if ($@)
84 {
85 print "\nSCRAM Error: It appears that the module \"Template.pm\" is not installed.","\n";
86 print " Please check your installaion. If you are an administrator,","\n";
87 print " you can find the Perl Template Toolkit at www.cpan.org or at","\n";
88 print " the web site of the author (Andy Wardley):","\n";
89 print "\n";
90 print " www.template-toolkit.com","\n";
91 print "\n";
92 print " You should install version 2.xx (2.13 or better).","\n";
93 print "\nscram-developers\@cern.ch","\n\n";
94 exit(1);
95 }
96 else
97 {
98 $self->{TEMPLATE_OBJECT} = Template->new($self->{TEMPLATE_CONFIG});
99 }
100
101 return $self;
102 }
103
104 sub template_dir()
105 {
106 my $self=shift;
107 # Default template dir:
108 my $templatedir = $ENV{LOCALTOP}."/".$ENV{SCRAM_CONFIGDIR};
109 # Allow projects to override the template dir (subdir of config):
110 if (exists ($ENV{SCRAM_PROJECT_TEMPLATEDIR})) {
111 $templatedir.="/".$ENV{SCRAM_PROJECT_TEMPLATEDIR};
112 }
113 $self->{TEMPLATE_DIR} = $templatedir;
114 return $self;
115 }
116
117 sub template_config()
118 {
119 my $self=shift;
120 # Set up Template opts:
121 $self->{TEMPLATE_CONFIG} =
122 {
123 INCLUDE_PATH => [ "$self->{TEMPLATE_DIR}","$ENV{LOCALTOP}/$ENV{SCRAM_CONFIGDIR}" ],
124 PLUGIN_BASE => [ qw(SCRAM::Plugins BuildSystem::Template::Plugins) ],
125 EVAL_PERL => 1
126 };
127
128 return $self;
129 }
130
131 sub template_data()
132 {
133 my $self=shift;
134 my ($data) = @_;
135
136 # Set the things that we must set. The "data" key points
137 # to a DataCollector object. The "branch" data is a
138 # TreeItem object:
139 $self->{TEMPLATE} = $data->template();
140 # Add required data accessed by key:
141 $self->{TEMPLATE_DATA}->{branch} = $data;
142 }
143
144 sub run()
145 {
146 my $self=shift;
147 local *FH = $self->{MAKEFILEFH};
148
149 $self->{TEMPLATE_OBJECT}->process($self->{TEMPLATE},
150 $self->{TEMPLATE_DATA},
151 $self->{MAKEFILEFH} )
152 || die "SCRAM: Template error --> ",$self->{TEMPLATE_OBJECT}->error;
153 }
154
155 1;