Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 232 lines (207 sloc) 7.214 kb
#!/usr/bin/env php
<?php
/**
* baskit - sandboxed build environment for PHP, PEAR and Phing
*
* @package Baskit
* @version 1.0.4
* @author Wes Mason <wes.mason@isotoma.com>
* @copyright 2011 Isotoma Limited
* @license http://opensource.org/licenses/Apache-2.0 Apache License, Version 2.0
*/
/**
* PEAR::Console_GetOpt
*/
require 'Console/Getopt.php';
// Setup all the paths
$home = getenv('HOME') . '/';
$cwd = getcwd() . '/';
$parts = $cwd . 'parts/';
$var = $cwd . 'var/';
$bin = $cwd . 'bin/';
$cache = $cwd . 'download_cache/';
$data_dir = $cwd; // For local checkout usage, overridden by PEAR installer
$php_dir = $cwd; // For local checkout usage, overridden by PEAR installer
$baskit_data_dir = "${data_dir}/baskit";
$baskit_php_dir = "${php_dir}/baskit";
//
// Read the command line
$cg = new Console_Getopt();
$args = $cg->readPHPArgv();
// Get the options
$ret = $cg->getopt($args, 'b:c:', array('baskitversion=', 'channel='));
// Check for errors and die with an error message if there was a problem
if (PEAR::isError($ret)) {
echo 'Error in command line: ' . $ret->getMessage() . PHP_EOL;
exit(1);
}
// Defaults
$baskit_version = null; // This version
$default_channel = null; // No channel override
$phing_version = null;
// Check for these values in the *project* build properties
if (file_exists($cwd . 'build.properties')) {
$project_build_properties = parse_ini_file($cwd . 'build.properties');
if (isset($project_build_properties['baskit.version'])) {
$baskit_version = $project_build_properties['baskit.version'];
}
if (isset($project_build_properties['baskit.channel'])) {
$default_channel = $project_build_properties['baskit.channel'];
}
if (isset($project_build_properties['baskit.phing.version'])) {
$phing_version = $project_build_properties['baskit.phing.version'];
}
}
// Options from command line override project properties
$opts = $ret[0];
if (sizeof($opts) > 0) {
foreach ($opts as $o) {
switch ($o[0]) {
case 'b':
case '--baskitversion':
if ($o[1]) {
$baskit_version = $o[1];
}
break;
case 'p':
case '--phingversion':
if ($o[1]) {
$phing_version = $o[1];
}
break;
case 'c':
case '--channel':
if ($o[1]) {
$default_channel = $o[1];
}
break;
}
}
}
// Get current version from build properties
$build_properties = parse_ini_file(dirname(__FILE__) . '/build.properties');
$current_version = trim($build_properties['baskit.package.version']);
// Create dynamic paths for environment
if (!file_exists($parts)) {
mkdir($parts);
echo "Created ${parts}" . PHP_EOL;
}
if (!file_exists($bin)) {
mkdir($bin);
echo "Created ${bin}" . PHP_EOL;
}
if (!file_exists($cache)) {
mkdir($cache);
echo "Created ${cache}" . PHP_EOL;
}
if (!file_exists($var)) {
mkdir($var);
echo "Created ${var}" . PHP_EOL;
}
// Move any existing ~/.pearrc out of the way, temporarily
$moved_rc = false;
if (file_exists("${home}.pearrc")) {
rename("${home}.pearrc", "${home}.pearrc_");
$moved_rc = true;
}
// Use the system isntalled PEAR to generate a local copy
echo 'Creating sandboxed ~/.pearrc' . PHP_EOL;
exec("pear config-create ${parts} ${home}.pearrc 2>&1", $out);
echo implode(PHP_EOL, $out) . PHP_EOL;
exec('pear install -o PEAR 2>&1', $out);
echo implode(PHP_EOL, $out) . PHP_EOL;
rename("${home}.pearrc", "${cwd}.pearrc");
if ($moved_rc) {
rename("${home}.pearrc_", "${home}.pearrc");
}
if (file_exists("${bin}pear")) {
unlink("${bin}pear");
}
// Create a wrapper around the local PEAR to reference the right .pearrc
// in the project path rather than looking in $HOME
file_put_contents("${bin}pear", "#!/bin/bash\n${parts}/pear/pear -c ${cwd}.pearrc \$@");
chmod("${bin}pear", 0755);
echo 'Created bin/pear' . PHP_EOL;
// Make local pear aware of baskit's PEAR channel
$channel = $default_channel ? $default_channel : 'baskit.h.isotoma.com';
exec("${bin}pear channel-discover ${channel} 2>&1", $out);
echo implode(PHP_EOL, $out) . PHP_EOL;
// Install Phing locally and wrap it's script
if (!$default_channel) {
exec("${bin}pear channel-discover pear.phing.info 2>&1", $out);
}
echo implode(PHP_EOL, $out) . PHP_EOL;
$channel = $default_channel ? $default_channel : 'phing';
$phing_install_version = $phing_version ? '-' . $phing_version : '';
exec("${bin}pear install ${channel}/phing${phing_install_version} 2>&1", $out);
echo implode(PHP_EOL, $out) . PHP_EOL;
if (file_exists("${bin}phing")) {
unlink("${bin}phing");
}
copy("${parts}pear/phing", "${bin}phing");
$phing_bin = file_get_contents("${bin}phing");
$phing_bin = str_replace('phing.php', 'phing_wrapper.php', $phing_bin);
file_put_contents("${bin}phing", $phing_bin);
chmod("${bin}phing", 0755);
$paths = implode(PATH_SEPARATOR, array(
$parts,
"${parts}pear/php",
$cwd,
));
file_put_contents(
"${parts}pear/php/phing_wrapper.php",
"#!/usr/bin/env php\n<?php\nset_include_path('${paths}');\ninclude '${parts}pear/php/phing.php';"
);
echo 'Created bin/phing' . PHP_EOL;
file_put_contents(
"${bin}php",
"#!/bin/sh\nphp -d include_path=${paths} \$@"
);
chmod("${bin}php", 0755);
echo 'Created bin/php' . PHP_EOL;
/**
* Function to recursively copy a directory.
*/
function copy_directory($source, $destination) {
if ($source == '.' || $source == '..') {
return;
}
if (is_dir($source)) {
@mkdir($destination);
$directory = dir($source);
while (false !== ($read_directory = $directory->read())) {
if ($read_directory == '.' || $read_directory == '..') {
continue;
}
$path_dir = $source . '/' . $read_directory;
if (is_dir($path_dir)) {
copy_directory($path_dir, $destination . '/' . $read_directory);
continue;
}
copy($path_dir, $destination . '/' . $read_directory);
}
$directory->close();
} else {
copy($source, $destination);
}
}
// Make sure we're not bootstrapping baskit itself, or there's already a
// locally checkout version.
if (!file_exists("${cwd}baskit") && !is_file("${cwd}baskit")) {
// Are we installing a specific version?
// Or is thst version the same as this one?
if (!$baskit_version || $baskit_version == $current_version) {
copy_directory($baskit_data_dir, "${cwd}baskit");
copy_directory($baskit_php_dir, "${cwd}baskit");
echo "baskit copied to ${cwd}baskit" . PHP_EOL;
} else {
// PEAR install and symlink specified version
$channel = $default_channel ? $default_channel : 'baskit';
exec("${bin}pear install ${channel}/baskit-${baskit_version} 2>&1", $out);
echo implode(PHP_EOL, $out) . PHP_EOL;
symlink("${parts}baskit", "${cwd}baskit");
echo "baskit-${baskit_version} symlinked to ${cwd}baskit" . PHP_EOL;
}
}
echo 'Done.' . PHP_EOL;
exit(0);
Jump to Line
Something went wrong with that request. Please try again.