-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAProfile.pm
executable file
·167 lines (128 loc) · 3.68 KB
/
AProfile.pm
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/perl
package AProfile;
use AProfile::Config;
use feature ":5.10";
use strict;
use utf8;
use Carp;
use Exporter 'import';
=pod
=head1 NAME
AProfile - A Feather for the project config objects.
=head1 DESCRIPTION
The module manages the different possibly parallel configurations.
By default it exports the 'active' project in a $AConf variable.
=head1 METHODS
=over 4
=item - I<new()>
The constructor. It reads the projectlist.
=cut
our @EXPORT=qw($AConf);
my $self;
my $Projectlist="$ENV{PWD}/AProfile/Projects.lst";
sub new {
return $self if $self;
$self={};
bless $self,__PACKAGE__;
return $self;
}
=item - I<active_config( 'project' )>
Sets/gets the active project to 'project'. If does not exist reads its
path from AProfile/Projects.lst file, which is a simple key:'value' config file.
Returns the 'project' configuration object or the active project if nothing is passed.
object.
=cut
sub active_config {
if ( !$self->{projectlist} ) {
if ( -f $Projectlist ) {
$self->{projectlist} = $self->_read_projectlist();
} else { # we have no Projectslist, so this is a first run
$_[1] = 'default_project';
}
}
$_[1]||='default_project';
$self->{active}=$self->get_config($_[1]);
$AProfile::AConf=$self->{projects}{$_[1]};
return $self->{projects}{$_[1]};
}
=item - I<get_config( 'project' )>
Gets the project named 'project' but keeps 'active' untouched. If does not exist reads its
path from AProfile/Projects.lst file, which is a simple key:'value' config file.
Returns the 'project' configuration object. If nothing is passed the active project config
is returned.
=cut
sub get_config {
my ($self,$project)=@_;
return $self->{active} unless $project;
given ( $self->{projects}{$project} ) {
when ( defined ) { # we have it, so it must be registered in Projectlist.lst
1; # TODO: load config and initialize all the data there
}
# we have no registry in the Projectlist.lst, so make a new file and
# register that.
default {
my $file=$self->{projectlist}{$project};
my $conf=AProfile::Config->new({ name=>$project, file=>$file });
# add the new entry
$self->{projects}{$project}=$conf;
$self->_write_projectlist();
}
};
return $self->{projects}{$project};
}
=item - I<schema( 'project'? )>
Returns the schema for the active project config, or when a project name is
passed, the schema belonging to that project. If project profile is not yet
loaded, it gets loaded.
=cut
sub get_schema {
my ($self,$proj) =@_;
return $self->{active}->schema unless $proj;
if ( !$self->{projectlist}{$proj} ) {
$self->get_config($proj);
}
$self->{projectlist}{$proj}->get_schema;
}
=item - I<get_projectnames>
Returns an ARRAY of projectnames.
=cut
sub get_projectnames {
return keys %{$_[0]->{projects}};
}
sub _write_projectlist {
my ($self,$file)=@_;
open PROJ,'>',$Projectlist or do {
carp "Cannot open or create AProfile/Projects.lst";
return undef;
};
foreach ( keys %{$self->{projects}} ) {
my $file=$self->{projects}{$_}->projectfile;
my $line="'$_':'$file'";
print PROJ $line;
}
close PROJ;
}
sub _read_projectlist {
open PROJ,$Projectlist or do {
carp "No AProfile/Projects.lst";
return undef;
};
my $data;
while ( <PROJ> ) {
chomp;
next if /^\s*\n$ | ^\s*#/; # empty lines and comments skip
s/'//g;
my ($k,$v)=split(/\s?:\s?/);
$data->{$k}=$v;
}
close PROJ;
return $data;
}
1;
__END__
=pod
=back
=head1 BUGS, WARNINGS and TODO
1. The module stores the project singletons but does not do great reference counting management.
It means that dead configs may wander about. These configs are comparatively
small amounts of data this is unlikely a harm, but nevertheless, not too elegant solution.