Bump for 3.6-28
[LibreOffice.git] / solenv / bin / modules / SourceConfigHelper.pm
blob6b0a5c9f169ca62686aad6b832ee7be194369426
1 #*************************************************************************
3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 # Copyright 2000, 2010 Oracle and/or its affiliates.
7 # OpenOffice.org - a multi-platform office productivity suite
9 # This file is part of OpenOffice.org.
11 # OpenOffice.org is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU Lesser General Public License version 3
13 # only, as published by the Free Software Foundation.
15 # OpenOffice.org is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU Lesser General Public License version 3 for more details
19 # (a copy is included in the LICENSE file that accompanied this code).
21 # You should have received a copy of the GNU Lesser General Public License
22 # version 3 along with OpenOffice.org. If not, see
23 # <http://www.openoffice.org/license.html>
24 # for a copy of the LGPLv3 License.
26 #*************************************************************************
28 #*************************************************************************
30 # SourceConfigHelper - Perl extension for parsing general info databases
32 # usage: see below
34 #*************************************************************************
36 package SourceConfigHelper;
38 use strict;
40 use RepositoryHelper;
41 use SourceConfig;
42 use Cwd qw (cwd);
43 use Carp;
45 my $debug = 0;
46 my @source_config_list; # array of sourceconfig objects
48 #-----------------------------------------------------------------------
49 # Constants
50 #-----------------------------------------------------------------------
52 use constant SOURCE_CONFIG_NONE => 0;
53 use constant SOURCE_CONFIG_CURRENT_FIRST => 1;
54 use constant SOURCE_CONFIG_ENVIRONMENT_FIRST => 2;
55 use constant SOURCE_CONFIG_CURRENT_ONLY => 3;
56 use constant SOURCE_CONFIG_ENVIRONMENT_ONLY => 4;
58 use constant SOURCE_CONFIG_DEFAULT => SOURCE_CONFIG_CURRENT_FIRST;
60 ##### profiling #####
62 ##### ctor #####
64 sub new {
65 my $proto = shift;
66 my $class = ref($proto) || $proto;
67 my $init_action = shift;
68 my $self = {};
69 my $SourceConfigCurrent;
70 my $SourceConfigEnvironment;
72 $init_action = SOURCE_CONFIG_DEFAULT if (!defined ($init_action));
73 if (!eval ($init_action) or ($init_action < SOURCE_CONFIG_NONE) or ($init_action > SOURCE_CONFIG_ENVIRONMENT_ONLY)) {
74 croak("wrong initial parameter: $init_action\n");
77 if ($init_action != SOURCE_CONFIG_NONE) {
78 my $repositoryHash_ref = {};
79 if ($init_action != SOURCE_CONFIG_ENVIRONMENT_ONLY) {
80 my $initial_directory = cwd();
81 my $result = is_repository($initial_directory, $repositoryHash_ref);
82 if ($result) {
83 $SourceConfigCurrent = SourceConfig->new($repositoryHash_ref->{REPOSITORY_ROOT});
86 if ($init_action != SOURCE_CONFIG_CURRENT_ONLY) {
87 my $source_config = $ENV{SOURCE_ROOT_DIR} . '/' . SourceConfig::SOURCE_CONFIG_FILE_NAME;
88 if (-f $source_config) {
89 $SourceConfigEnvironment = SourceConfig->new($source_config);
93 # fill array
95 if (($init_action == SOURCE_CONFIG_CURRENT_FIRST) or ($init_action == SOURCE_CONFIG_CURRENT_ONLY)) {
96 if (defined ($SourceConfigCurrent)) {
97 push (@source_config_list, $SourceConfigCurrent);
99 if ($init_action == SOURCE_CONFIG_CURRENT_FIRST) {
100 if (defined ($SourceConfigEnvironment)) {
101 push (@source_config_list, $SourceConfigEnvironment);
105 elsif (($init_action == SOURCE_CONFIG_ENVIRONMENT_FIRST) or ($init_action == SOURCE_CONFIG_ENVIRONMENT_ONLY)) {
106 if (defined ($SourceConfigEnvironment)) {
107 push (@source_config_list, $SourceConfigEnvironment);
109 if ($init_action == SOURCE_CONFIG_ENVIRONMENT_FIRST) {
110 if (defined ($SourceConfigCurrent)) {
111 push (@source_config_list, $SourceConfigCurrent);
117 $self->{SOURCE_CONFIG_LIST} = \@source_config_list;
119 bless($self, $class);
120 return $self;
123 ##### methods #####
125 ############################################################################################
127 sub add_SourceConfig {
128 my $self = shift;
129 my $source_config = shift;
130 push (@{$self->{SOURCE_CONFIG_LIST}}, $source_config);
133 ############################################################################################
135 sub get_SourceConfigList {
136 my $self = shift;
137 return @{$self->{SOURCE_CONFIG_LIST}};
140 ############################################################################################
142 sub has_SourceConfig {
143 my $self = shift;
144 my $result = 0;
145 my $count = @{$self->{SOURCE_CONFIG_LIST}};
146 $result = 1 if ($count > 0);
147 return $result;
150 ############################################################################################
152 sub get_module_path {
153 my $self = shift;
154 my $module = shift;
155 my $function = \&SourceConfig::get_module_path;
156 my $result;
157 $result = $self->get_StringResult ($function, $module);
158 return $result;
161 ############################################################################################
163 sub get_active_modules {
164 my $self = shift;
165 my $parameter; # empty
166 my $function = \&SourceConfig::get_active_modules;
167 my $array_ref;
168 $array_ref = $self->get_ArrayResult ($function, $parameter);
169 return @$array_ref;
172 ############################################################################################
174 sub get_repositories {
175 my $self = shift;
176 my $parameter; # empty
177 my $function = \&SourceConfig::get_repositories;
178 my $array_ref;
179 $array_ref = $self->get_ArrayResult ($function, $parameter);
180 return @$array_ref;
183 ############################################################################################
185 sub get_module_repository {
186 my $self = shift;
187 my $module = shift;
188 my $function = \&SourceConfig::get_module_repository;
189 my $result;
190 $result = $self->get_StringResult ($function, $module);
191 return $result;
194 ############################################################################################
196 sub is_active {
197 my $self = shift;
198 my $module = shift;
199 my $function = \&SourceConfig::is_active;
200 my $result_ref;
201 my $is_active = 0;
202 $result_ref = $self->get_ResultOfList ($function, $module);
203 my $count = @$result_ref;
204 if ($count>0) {
205 foreach my $active (@$result_ref) {
206 if ($active) {
207 $is_active = $active;
211 return $is_active;
214 ##### private methods #####
216 ############################################################################################
218 # is_repository () : check if the directory is a valid repository
220 # input: - directory
221 # - hash reference, where the output will be stored
223 # output: 0 = FALSE, the directory is no valid repository
224 # 1 = TRUE, the repository root can be found in $repositoryHash_ref->{REPOSITORY_ROOT}
226 ############################################################################################
228 sub is_repository {
229 my $directory = shift;
230 my $repositoryHash_ref = shift;
231 $repositoryHash_ref->{INITIAL_DIRECTORY} = $directory;
232 $repositoryHash_ref->{REPOSITORY_ROOT} = undef;
233 my $result = RepositoryHelper::search_via_build_lst($repositoryHash_ref);
234 return $result;
237 ############################################################################################
239 # get_ResultOfList(): give back an array reference from all SourceConfig Objects results
241 # input: - function : reference to the called function of each SourceConfig Object
242 # - parameter : parameter for the called function
244 # output: result : array of all results
246 ############################################################################################
248 sub get_ResultOfList {
249 my $self = shift;
250 my $function = shift;
251 my $parameter = shift;
252 my @result;
253 foreach my $source_config (@{$self->{SOURCE_CONFIG_LIST}}) {
254 push (@result, &$function ($source_config, $parameter));
256 return \@result;
259 ############################################################################################
261 # get_StringResult(): give back the first defined result from all SourceConfig Objects
263 # input: - function : reference to the called function of each SourceConfig Object
264 # - parameter : parameter for the called function
266 # output: result : scalar variable (string), undef if no result
268 ############################################################################################
270 sub get_StringResult {
271 my $self = shift;
272 my $function = shift;
273 my $parameter = shift;
274 my $result_ref;
275 $result_ref = $self->get_ResultOfList ($function, $parameter);
276 my $count = @$result_ref;
277 if ($count>0) {
278 my $value;
279 my $i = 0;
280 while (($i < $count) and !defined ($value)) { # search the first defined result
281 $value = $$result_ref[$i];
282 $i++;
284 return $value;
286 return undef;
289 ############################################################################################
291 # get_StringResult(): give back a sorted and uniqe array reference of the results
292 # from all SourceConfig Objects
294 # input: - function : reference to the called function of each SourceConfig Object
295 # - parameter : parameter for the called function
297 # output: result : sorted and uniqe array reference
299 ############################################################################################
301 sub get_ArrayResult {
302 my $self = shift;
303 my $function = shift;
304 my $parameter = shift;
305 my $result_ref;
306 my @modules;
307 $result_ref = $self->get_ResultOfList ($function, $parameter);
308 my $count = @$result_ref;
309 if ($count>0) {
310 my %moduleHash;
311 foreach my $module (@$result_ref) {
312 $moduleHash{$module}++;
314 @modules = sort keys %moduleHash;
316 return \@modules;
319 ##### finish #####
321 1; # needed by use or require
323 __END__
325 =head1 NAME
327 SourceConfigHelper - Perl extension for handling with SourceConfigObjetcs
329 =head1 SYNOPSIS
331 # example that will read source_config file and return the active repositories
333 use SourceConfigHelper;
335 # Create a new instance:
336 $a = SourceConfigHelper->new();
338 # Get repositories for the actual workspace:
339 $a->get_repositories();
341 =head1 DESCRIPTION
343 SourceConfigHelper is a perl extension to handle more than one objects of SourceConfig
344 to set up a search order for modules.
346 Methods:
348 SourceConfigHelper::new()
350 Creates a new instance of SourceConfigHelper. Can be initialized by: default - empty or with a constant of search order. default: the source_config will be taken first from the current repository and second from the environment
351 Possible parameters are:
352 SourceConfigHelper::SOURCE_CONFIG_NONE - no SourceConfig Object will be created
353 SourceConfigHelper::SOURCE_CONFIG_CURRENT_FIRST - use the current repository first
354 SourceConfigHelper::SOURCE_CONFIG_ENVIRONMENT_FIRST - use the repository of the environment first
355 SourceConfigHelper::SOURCE_CONFIG_CURRENT_ONLY - use only the current repository
356 SourceConfigHelper::SOURCE_CONFIG_ENVIRONMENT_ONLY - use only the repository of the environment
358 SourceConfigHelper::get_repositories()
360 Returns sorted list of active repositories for the actual workspace
362 SourceConfigHelper::get_active_modules()
364 Returns a sorted list of active modules
366 SourceConfigHelper::get_all_modules()
368 Returns sorted list of all modules in active repositories.
370 SourceConfigHelper::get_module_path($module)
372 Returns absolute module path. If the module is not active or don't exists, "undef" will be returned.
374 SourceConfigHelper::get_module_repository($module)
376 Returns the module's repository. If the module is not active or don't exists, "undef" will be returned.
378 SourceConfigHelper::is_active()
380 Returns 1 (TRUE) if a module is active
381 Returns 0 (FALSE) if a module is not active
383 SourceConfigHelper::add_SourceConfig($SourceConfigObject)
385 Add the SourceConfigObject to the end of the list
387 SourceConfigHelper::get_SourceConfigList()
389 Return an array of SourceConfigObjects
391 SourceConfigHelper::has_SourceConfig()
393 Returns 1 (TRUE) if one or more SourceConfig Objects is in the list
394 Returns 0 (FALSE) if no SourceConfig Object is in the list (can happen if there is no valid repository)
396 =head2 EXPORT
398 SourceConfigHelper::new()
399 SourceConfigHelper::get_repositories()
400 SourceConfigHelper::get_active_modules()
401 SourceConfigHelper::get_all_modules()
402 SourceConfigHelper::get_module_path($module)
403 SourceConfigHelper::get_module_repository($module)
404 SourceConfigHelper::is_active($module)
405 SourceConfigHelper::add_SourceConfig($SourceConfigObject)
406 SourceConfigHelper::get_SourceConfigList()
407 SourceConfigHelper::has_SourceConfig()
409 =head1 AUTHOR
411 Kurt Zenker, kz@openoffice.org
413 =head1 SEE ALSO
415 perl(1).
417 =cut