App-Virtualenv

 view release on metacpan or  search on metacpan

lib/App/Virtualenv.pm  view on Meta::CPAN

	$nondestructive = not defined($ENV{PERL_VIRTUAL_ENV}) if not defined($nondestructive);

	$ENV{PERL_VIRTUAL_ENV} = $ENV{_OLD_PERL_VIRTUAL_ENV} if defined($ENV{_OLD_PERL_VIRTUAL_ENV}) or not $nondestructive;
	undef $ENV{_OLD_PERL_VIRTUAL_ENV};

	$ENV{PATH} = $ENV{_OLD_PERL_VIRTUAL_PATH} if defined($ENV{_OLD_PERL_VIRTUAL_PATH}) or not $nondestructive;
	undef $ENV{_OLD_PERL_VIRTUAL_PATH};

	$ENV{PERL5LIB} = $ENV{_OLD_PERL_VIRTUAL_PERL5LIB} if defined($ENV{_OLD_PERL_VIRTUAL_PERL5LIB}) or not $nondestructive;
	undef $ENV{_OLD_PERL_VIRTUAL_PERL5LIB};

	$ENV{PERL_LOCAL_LIB_ROOT} = $ENV{_OLD_PERL_VIRTUAL_PERL_LOCAL_LIB_ROOT} if defined($ENV{_OLD_PERL_VIRTUAL_PERL_LOCAL_LIB_ROOT}) or not $nondestructive;
	undef $ENV{_OLD_PERL_VIRTUAL_PERL_LOCAL_LIB_ROOT};

	$ENV{PERL_MB_OPT} = $ENV{_OLD_PERL_VIRTUAL_PERL_MB_OPT} if defined($ENV{_OLD_PERL_VIRTUAL_PERL_MB_OPT}) or not $nondestructive;
	undef $ENV{_OLD_PERL_VIRTUAL_PERL_MB_OPT};

	$ENV{PERL_MM_OPT} = $ENV{_OLD_PERL_VIRTUAL_PERL_MM_OPT} if defined($ENV{_OLD_PERL_VIRTUAL_PERL_MM_OPT}) or not $nondestructive;
	undef $ENV{_OLD_PERL_VIRTUAL_PERL_MM_OPT};

	$ENV{PS1} = $ENV{_OLD_PERL_VIRTUAL_PS1} if defined($ENV{_OLD_PERL_VIRTUAL_PS1}) or not $nondestructive;
	undef $ENV{_OLD_PERL_VIRTUAL_PS1};

	return 1;
}

=head2 create($virtualenv_path, $empty)

creates Perl virtual environment

$virtualenv_path: I<new virtual environment path>

$empty: I<create empty virtual environment>

return value: I<virtual environment path if success, otherwise undef>

=cut
sub create
{
	my ($virtualenv_path, $empty) = @_;
	return unless defined($virtualenv_path) and length($virtualenv_path) > 0;
	$virtualenv_path = Cwd::realpath($virtualenv_path);
	say "Creating Perl virtual environment: $virtualenv_path";

	deactivate();
	$ENV{PERL_MM_USE_DEFAULT} = 1;
	$ENV{NONINTERACTIVE_TESTING} = 1;
	$ENV{AUTOMATED_TESTING} = 1;

	require local::lib;
	local::lib->import($virtualenv_path);

	activate($virtualenv_path);

	perl("-MCPAN", "-e exit(defined(CPAN::Shell->force('install', 'CPAN'))? 0: 1);") unless $empty;

	my $pkg_path = dirname(__FILE__);

	say "Copying... bin/activate";
	copy("$pkg_path/Virtualenv/activate", "$virtualenv_path/bin/activate");
	chmod(0644, "$virtualenv_path/bin/activate");

	say "Copying... bin/sh.pl";
	copy("$pkg_path/Virtualenv/sh.pl", "$virtualenv_path/bin/sh.pl");
	chmod(0755, "$virtualenv_path/bin/sh.pl");

	say "Copying... bin/perl.pl";
	file_put_contents("$virtualenv_path/bin/perl.pl", "#!".shellmeta($Config{perlpath})."\n".file_get_contents("$pkg_path/Virtualenv/perl.pl"));
	chmod(0755, "$virtualenv_path/bin/perl.pl");
	symlink("perl.pl", "$virtualenv_path/bin/perl");

	say "Copying... bin/virtualenv.pl";
	copy("$pkg_path/Virtualenv/virtualenv.pl", "$virtualenv_path/bin/virtualenv.pl");
	chmod(0755, "$virtualenv_path/bin/virtualenv.pl");
	symlink("virtualenv.pl", "$virtualenv_path/bin/virtualenv");

	return $virtualenv_path;
}

=head2 find_virtualenv_path($virtualenv_path)

finds Perl virtual environment path by $virtualenv_path argument or activated virtual environment or running script or PERL5LIB environment variable

$virtualenv_path: I<virtual environment path>

return value: I<best matching virtual environment path>

=cut
sub find_virtualenv_path
{
	my ($virtualenv_path) = @_;
	$virtualenv_path = $ENV{PERL_VIRTUAL_ENV} if not (defined($virtualenv_path) and length($virtualenv_path) > 0 and -d "$virtualenv_path/lib/perl5");
	$virtualenv_path = "${FindBin::Bin}/.." if not (defined($virtualenv_path) and length($virtualenv_path) > 0 and -d "$virtualenv_path/lib/perl5") and ${FindBin::Bin} !~ qr'^(/usr/|/bin/)' and -d "${FindBin::Bin}/../lib/perl5";
	for (split(":", defined($ENV{PERL5LIB})? $ENV{PERL5LIB}: ""))
	{
		last if defined($virtualenv_path) and length($virtualenv_path) > 0 and -d "$virtualenv_path/lib/perl5";
		$virtualenv_path = "$_/../..";
	}
	return if not (defined($virtualenv_path) and length($virtualenv_path) > 0 and -d "$virtualenv_path/lib/perl5");
	return $virtualenv_path;
}

=head2 activate2($virtualenv_path, $inform)

activates Perl virtual environment by find_virtualenv_path function

$virtualenv_path: I<virtual environment path>

$inform: I<informs activated virtual environment path to STDERR if new activated path differs old one, by default 0>

return value: I<activated best matching virtual environment path if success, otherwise undef>

=cut
sub activate2
{
	my ($virtualenv_path, $inform) = @_;
	my $old_virtualenv_path = $ENV{PERL_VIRTUAL_ENV};
	$virtualenv_path = activate(find_virtualenv_path($virtualenv_path));
	if ($inform)
	{
		if (defined($virtualenv_path))
		{
			say STDERR "Perl virtual environment path: $virtualenv_path" if not defined $old_virtualenv_path or $old_virtualenv_path ne $virtualenv_path;
		} else
		{
			say STDERR "Perl virtual environment is not activated";
		}
	}
	return $virtualenv_path;
}

=head2 getinc($virtualenv_path)

gets array ref of include paths given virtual environment path or sitelib paths



( run in 1.045 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )