Padre

 view release on metacpan or  search on metacpan

lib/Padre/Util.pm  view on Meta::CPAN

		# or share in the Padre-Plugin-Name/lib/Padre/Plugin/Name/share directory
		my $plugin_dir = File::Spec->catdir( $root, "Padre-Plugin-$plugin", 'share' );
		if ( -d $plugin_dir ) {
			return $plugin_dir;
		}
		$plugin_dir = File::Spec->catdir( $root, "Padre-Plugin-$plugin", 'lib', 'Padre', 'Plugin', $plugin, 'share' );
		return $plugin_dir;
	}

	# Rely on automatic handling of everything
	require File::ShareDir;
	if ($plugin) {
		return File::Spec->rel2abs( File::ShareDir::dist_dir("Padre-Plugin-$plugin") );
	} else {
		return File::Spec->rel2abs( File::ShareDir::dist_dir('Padre') );
	}
}

sub sharedir {
	File::Spec->catdir( share(), @_ );
}

sub sharefile {
	File::Spec->catfile( share(), @_ );
}

sub splash {
	my $original = Padre::Util::sharefile('padre-splash-ccnc.png');
	return -f $original ? $original : Padre::Util::sharefile('padre-splash.png');
}





######################################################################
# Logging and Debugging

# Returns the memory currently used by this application:
sub process_memory {
	if (Padre::Constant::UNIX) {
		open my $meminfo, '<', '/proc/self/stat' or return;
		my $rv = ( split( / /, <$meminfo> ) )[22];
		close $meminfo;
		return $rv;
	} elsif (Padre::Constant::WIN32) {
		require Padre::Util::Win32;
		return Padre::Util::Win32::GetCurrentProcessMemorySize();
	}
	return;
}

=pod

=head2 C<run_in_directory>

    Padre::Util::run_in_directory( $command, $directory );

Runs the provided C<command> in the C<directory>. On win32 platforms, executes
the command to provide *true* background process executions without window
popups on each execution. on non-win32 platforms, it runs a C<system>
command.

Returns 1 on success and 0 on failure.

=cut

sub run_in_directory {
	my ( $cmd, $directory ) = @_;

	# Make sure we execute from the correct directory
	if (Padre::Constant::WIN32) {
		require Padre::Util::Win32;
		my $retval = Padre::Util::Win32::ExecuteProcessAndWait(
			directory  => $directory,
			file       => 'cmd.exe',
			parameters => "/C $cmd",
		);
		return $retval ? 1 : 0;
	} else {
		require File::pushd;
		my $pushd  = File::pushd::pushd($directory);
		my $retval = system $cmd;
		return ( $retval == 0 ) ? 1 : 0;
	}
}

=pod

=head2 C<run_in_directory_two>

Plugin replacment for perl command qx{...} to avoid black lines in non *inux os

	qx{...};
	run_in_directory_two( cmd => '...');

optional parameters are dir and return type

	run_in_directory_two(cmd => '...', dir => $dir);
	run_in_directory_two(cmd => '...', dir => $dir, option => type);

also

	run_in_directory_two(cmd => '...', option => type);

return type 1 default, returns a string
return type 2 error only for testing
nb you might need to chomp result but thats for you.

return type 0 hash_ref

=over

=item example 1,

	Padre::Util::run_in_directory_two(cmd => 'svn --version --quiet');

	"1.6.12
	"

=item example 2,



( run in 2.085 seconds using v1.01-cache-2.11-cpan-364913b4093 )