ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/BuildSystem/TemplateInterface.pm
Revision: 1.4
Committed: Mon Dec 4 14:21:23 2006 UTC (18 years, 5 months ago) by sashby
Content type: text/plain
Branch: MAIN
CVS Tags: V1_1_0, v110p1, V110p6, V110p5, V110p4, V110p3, before110xmlBRmerge
Branch point for: v200branch
Changes since 1.3: +11 -9 lines
Log Message:
Added mods from Shahzad for the template location.

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.3 2006/11/14 17:43:14 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 my ($templatedir)=@_;
108 my $dir = $ENV{LOCALTOP}."/".$ENV{SCRAM_CONFIGDIR};
109 if ((exists $ENV{SCRAM_PROJECT_TEMPLATEDIR}) &&
110 ($ENV{SCRAM_PROJECT_TEMPLATEDIR} !~ /^\s*$/)) {
111 $dir = $ENV{SCRAM_PROJECT_TEMPLATEDIR};
112 }
113 $templatedir ||= $dir;
114 $self->{TEMPLATE_DIR} = $templatedir;
115 return $self;
116 }
117
118 sub template_config()
119 {
120 my $self=shift;
121 # Set up Template opts:
122 $self->{TEMPLATE_CONFIG} =
123 {
124 INCLUDE_PATH => [ "$self->{TEMPLATE_DIR}","$ENV{LOCALTOP}/$ENV{SCRAM_CONFIGDIR}" ],
125 PLUGIN_BASE => [ qw(SCRAM::Plugins BuildSystem::Template::Plugins) ],
126 EVAL_PERL => 1,
127 ABSOLUTE => 1
128 };
129
130 return $self;
131 }
132
133 sub template_data()
134 {
135 my $self=shift;
136 my ($data) = @_;
137
138 # Set the things that we must set. The "data" key points
139 # to a DataCollector object. The "branch" data is a
140 # TreeItem object:
141 $self->{TEMPLATE} = $data->template();
142 # Add required data accessed by key:
143 $self->{TEMPLATE_DATA}->{branch} = $data;
144 }
145
146 sub run()
147 {
148 my $self=shift;
149 local *FH = $self->{MAKEFILEFH};
150
151 $self->{TEMPLATE_OBJECT}->process($self->{TEMPLATE},
152 $self->{TEMPLATE_DATA},
153 $self->{MAKEFILEFH} )
154 || die "SCRAM: Template error --> ",$self->{TEMPLATE_OBJECT}->error;
155 }
156
157 1;