package local::lib::profiles;

use strict;
use warnings;
use Params::Validate qw/validate
use local::lib;
use File::Copy;
use File::Path;
use File::Util;
use autodie;
use Carp;

=head1 NAME

local::lib::profiles - makes modules feel at home

=head1 VERSION

Version 0.03


our $VERSION = '0.03';


describe the module, working code example



local::lib::profiles is designed to extend upon the methodologies of
lib::local to make it simpler to create, manage, and discard local perl



These are the cmd line usages for the package

=head2 ll_mkprofile

Creates a profile under the LOCAL_LIB_HOME directory by the given

=head2 ll_rmprofile

Removes a profile that lives under the LOCAL_LIB_HOME directory 
by the given name.

=head2 ll_workon

Activates a previously created profile.

=head2 ll_deactivate

Deactivate the currently active profile. Note that this is only
available if an profile is active

=head2 ll_upgrade_profile

Replaces or create the Active scripts in a given local-lib profile.
This can be used to either turn a local-lib library into a full blown
profile or to take an existing profile and upgrade it to contain more
recent scripts

=head2 ll_cdprofile

Changes directory into the root of the profile



=head2 make_profile

Creates a new local lib profile in the supplied directory


sub make_profile {
    my %args = validate(@_,
            directory => {
                type => SCALAR
    # create base local lib

    # then upgrade it to a local lib profile
    upgrade_profile(directory => $args{directory});

} # end of subroutine make_profile

=head2 find_activate_script

Finds the path of the script


sub find_activate_script {
    my %args = validate(@_,


    my @lib_paths = split /:/, $ENV{PERL5LIB};

    foreach (@lib_paths)
        if (-e $_.'/local/lib/profiles/')
            return $_.'/local/lib/profiles/';

    die "No script found in library paths";
} # end of subroutine find_activate_script

=head2 upgrade_profile

Upgrades a local lib directory into a full profile


sub upgrade_profile {
    my %args = validate(@_,
            directory => {
                type => SCALAR

    my $activate_script = find_activate_script();

    File::Path::mkpath($args{directory} . '/bin');

        source => $activate_script,
        destination => $args{directory}.'/bin/ll_activate',
        profile_location => $args{directory}

} # end of subroutine upgrade_profile

=head2 deploy_support_file

takes a support file from a given local (like lib/ or the templates
directory), substitutes the content tokens that it finds, then writes the file
to the required destination


sub deploy_support_file {
    my %args = validate(@_,
            source => {
                type => SCALAR
            destination => {
                type => SCALAR
            profile_location => {
                type => SCALAR

    # File::Util cannot handle // in its file paths, so gotta replace them

    $args{source} =~ s/\/\//\//g;
    $args{destination} =~ s/\/\//\//g;

    #open the source of the file
    my $file = File::Util->new();
    my $support_file = $file->load_file($args{source});

    #substitute the tokens
    $support_file =~ s/__PERL_PROFILE__/$args{profile_location}/g;

    #write to its destination
        file => $args{destination},
        content => $support_file

} # end of subroutine deploy_support_file

=head1 AUTHOR

Please contribute patches to Github:

Christopher Mckay (cmckay), C<< <> >>

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

perldoc local::lib::profiles

=head1 BUGS

=over 4

=item Colons in profile names

This breaks the profile creation, just don't do it untill I fix it.



This code is largely modeled upon, and in places copied from, work by Doug 
Hellmann in his virtualenvwrapper python package


Copyright Doug Hellmann, All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Doug Hellmann not be used
in advertising or publicity pertaining to distribution of the software
without specific, written prior permission.

Copyright 2009 Christopher Mckay.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See for more information.


# End of local::lib::profiles