Config-FileManager
view release on metacpan or search on metacpan
lib/Config/FileManager.pm view on Meta::CPAN
This method get/set the name of the tool for which the config file is.
Usage:
my $toolname = $cfg->toolname();
$cfg->toolname("tool name");
=cut
sub toolname($;$) {
my $self = shift;
if (@_) {
my $toolname = shift;
$self->{toolname} = $toolname;
$self->{full_path} = undef;
}
return $self->{toolname};
}
=item * Method C<filename>
This method get/set the file basename of the config file
Usage:
my $filename = $cfg->filename();
$cfg->filename("file name");
=cut
sub filename($;$) {
my $self = shift;
if (@_) {
my $filename = shift;
$self->{filename} = basename $filename;
$self->{full_path} = undef;
}
return $self->{filename};
}
=item * Method C<paths>
lib/Config/FileManager.pm view on Meta::CPAN
This method get/set the paths where the config file should be found.
The special __APPDIR__ path is OS dependant (see I<File::HomeDir> module documentation).
Usage:
my @paths = $cfg->paths();
$cfg->paths(qw(list ./of/paths));
=cut
sub paths($;@) {
my $self = shift;
if (@_) {
my @p = @{$_[0]};
@{$self->{paths}} = @p;
$self->{full_path} = undef;
}
return @{$self->{paths}};
}
=item * Method C<interactive>
This method get/set the value of interactive...
Usage:
my $v = $cfg->interactive();
$cfg->interactive(0); # or $cfg->interactive(1);
=cut
sub interactive($;$) {
my $self = shift;
if (@_) {
my $val = shift;
$self->{interactive} = $val;
}
return $self->{interactive};
}
=item * Method C<version>
This method get/set the current version of the config file
Usage:
my $vers = $cfg->version();
$cfg->version("0.1.2");
=cut
sub version($;$) {
my $self = shift;
if (@_) {
my $version = shift;
$self->{version} = $version;
@{$self->{allVersions}} = ();
}
return $self->{version};
}
=item * Method C<versions>
This method returns (and prior computes if required) the array of all version's strings from the newest to the oldest.
Usage:
$cfg->versions();
=cut
sub versions($) {
my $self = shift;
if (!@{$self->{allVersions}}) {
my $cur_ver = $self->{version};
my $old_ver;
do {
if (ref($cur_ver) eq 'HASH') {
my @k = keys %{$cur_ver};
$cur_ver = $k[0];
}
$cur_ver =~ s/^to v//;
lib/Config/FileManager.pm view on Meta::CPAN
You can (should) use '__VERSION__' instead of giving it explicitely.
In such case, it will be replaced by the corresponding version string.
Usage:
my $cfg_txt = $cfg->defaultContent();
$cfg->defaultContent("# the default config content of the current version");
=cut
sub defaultContent($;$) {
my $self = shift;
if (@_) {
my $defaultContent = shift;
$self->{defaultContent} = $defaultContent;
}
return $self->{defaultContent};
}
=item * Method C<addPatch>
lib/Config/FileManager.pm view on Meta::CPAN
$cfg->addPatch(
"from" => "some version",
"to" => "previous version",
"diffs" => '
@@ -1,1 +0,0 @@
-# blablabla
');
=cut
sub addPatch($%) {
my $self = shift;
my %params = @_;
for my $required (qw(from to diffs)) {
croak "Required parameter '$required' not passed to addPatch method."
unless exists $params{$required};
}
croak "A patch already exists from version ".$params{"from"}."."
unless !defined($self->{patches}->{"patch from v".$params{"from"}});
$self->{patches}->{"patch from v".$params{"from"}}{"to v".$params{"to"}}= $params{"diffs"};
@{$self->{allVersions}} = ();
lib/Config/FileManager.pm view on Meta::CPAN
This method gets (and prior computes if required) the path where the config file is.
If no config file is found, then the default current config file is created in the "correct" place.
Usage:
$cfg->getPath();
=cut
sub getPath($) {
my $self = shift;
return $self->{full_path} if defined($self->{full_path});
# No config file already defined
# check all given paths in the given order
my @p = $self->paths;
foreach my $path (@p) {
#print "DBG:path=$path\n";
my $appdir = File::HomeDir->my_dist_data($self->toolname) || "";
$path =~ s/^__APPDIR__$/$appdir/;
lib/Config/FileManager.pm view on Meta::CPAN
This method check if the current config file is up-to-date and proposes an update if it is not.
The update tries to preserve custom user's settings.
Usage:
$cfg->update();
=cut
sub update($) {
my $self = shift;
# Check if config file is up-to-date
open (CFG_FILE, "<".$self->getPath) or croak "Unable to open config file [".$self->getPath."]: $!";
my @versions = $self->versions;
my $current_user_config_version = $versions[-1];
my $current_user_config = "";
while (<CFG_FILE>) {
$current_user_config .= $_;
if (/^#.*configuration file (\d+.\d+.\d+.*)$/) {
lib/Config/FileManager.pm view on Meta::CPAN
This method gets the default config content of the given version. If ommited, then uses the current version.
Usage:
$cfg->getDefaultContent();
$cfg->getDefaultContent("a given version");
=cut
sub getDefaultContent($;$) {
my $self = shift;
my $wanted_version = shift || $self->version;
my $computed_version = $self->version;
my $computed_config = $self->defaultContent;
$computed_config =~ s/^\s+//;
$computed_config =~ s/\s+$/\n/;
my $error = undef;
eval {
# print "looking for version $wanted_version\n";
if (!grep(/^$wanted_version$/, $self->versions)) {
( run in 3.475 seconds using v1.01-cache-2.11-cpan-65fba6d93b7 )