Skip to content

Instantly share code, notes, and snippets.

@itsjavi
Created June 6, 2016 17:29
Show Gist options
  • Select an option

  • Save itsjavi/cd610f6b1e62426745baf4d641e290a7 to your computer and use it in GitHub Desktop.

Select an option

Save itsjavi/cd610f6b1e62426745baf4d641e290a7 to your computer and use it in GitHub Desktop.
Generate a PHAR using composer and box
#! /usr/bin/env php
<?php
// Usage: composer-box <package-name>:<version> [<config>] [<output>]
ini_set('phar.readonly', false);
$WORKING_DIR = realpath(dirname(__FILE__));
$ROOT_DIR = realpath($WORKING_DIR . '/../');
$TMP_DIR = "${ROOT_DIR}/tmp";
$BUILD_DIR = "${ROOT_DIR}/build";
$PACKAGE_NAME = isset($argv[1]) ? $argv[1] : false;
$PACKAGE_CONFIG = isset($argv[2]) ? $argv[2] : false;
$OUTPUT_FILENAME = isset($argv[3]) ? $argv[3] : false;
if (!$PACKAGE_NAME) {
print 'Package name not specified' . "\n";
exit(1);
}
// Parse version
$PACKAGE_NAME_ARR = explode(':', $PACKAGE_NAME, 2);
$PACKAGE_VERSION = (isset($PACKAGE_NAME_ARR[1]) ? $PACKAGE_NAME_ARR[1] : 'dev-master');
$PACKAGE_NAME = $PACKAGE_NAME_ARR[0];
$PACKAGE_TMP_DIR = $TMP_DIR . '/' . $PACKAGE_NAME;
$PACKAGE_DIR = $PACKAGE_TMP_DIR . "/vendor/${PACKAGE_NAME}";
$DEFAULT_OUTPUT_FILENAME = $BUILD_DIR . '/' . (str_replace('/', '_', basename($PACKAGE_NAME))) . '.phar';
// Get config json
if ($PACKAGE_CONFIG) {
if (is_readable($PACKAGE_CONFIG)) { // is file ?
$PACKAGE_CONFIG = file_get_contents($PACKAGE_CONFIG);
}
if ($PACKAGE_CONFIG === false || json_decode($PACKAGE_CONFIG, true) === null) { // is not json ?
print 'Invalid package config provided' . "\n";
exit(1);
}
}
// Cleam tmp folder and make it the current working dir
print "Cleaning up temporary folder ...\n";
if (is_dir($TMP_DIR)) {
passthru("rm -rf ${PACKAGE_TMP_DIR}");
}
// Create tmp folder for package
passthru("mkdir -p ${PACKAGE_TMP_DIR}");
chdir($PACKAGE_TMP_DIR);
// Add composer.json
file_put_contents($PACKAGE_TMP_DIR . '/composer.json', json_encode([
'require' => [
$PACKAGE_NAME => $PACKAGE_VERSION,
],
'minimum-stability' => 'dev',
'prefer-stable' => true,
], JSON_UNESCAPED_SLASHES));
// Install package at specified version, skipping the unnecessary parts
$composer_cmd = "composer install --no-dev --no-scripts --ignore-platform-reqs --no-autoloader -q";
print "Fetching package ${PACKAGE_NAME}:${PACKAGE_VERSION} ...\n";
passthru($composer_cmd, $return_status);
if ($return_status != 0) {
print 'Composer command failed: ' . $composer_cmd . "\n";
exit((int)$return_status);
}
chdir($PACKAGE_DIR);
// Install package dependencies
$composer_cmd = "composer install";
passthru($composer_cmd, $return_status);
if ($return_status != 0) {
print 'Composer command failed: ' . $composer_cmd . "\n";
exit((int)$return_status);
}
// Add box.json if present, and check it
$config_file = $PACKAGE_DIR . '/box.json';
// Read original package config, if any
if (is_readable($config_file)) {
$ORIG_CONFIG = json_decode(file_get_contents($config_file), true);
} else {
$ORIG_CONFIG = [];
}
// Avoid using original output file
if (isset($ORIG_CONFIG['output'])) {
unset($ORIG_CONFIG['output']);
}
// Resolve config
if (!$PACKAGE_CONFIG) {
$PACKAGE_CONFIG = $ORIG_CONFIG;
} else {
$PACKAGE_CONFIG = array_replace_recursive($ORIG_CONFIG, json_decode($PACKAGE_CONFIG, true));
}
// Resolve output file
if ($OUTPUT_FILENAME) {
$PACKAGE_CONFIG['output'] = $OUTPUT_FILENAME;
} else {
$OUTPUT_FILENAME = $PACKAGE_CONFIG['output'] = (
isset($PACKAGE_CONFIG['output']) ? $PACKAGE_CONFIG['output'] : $DEFAULT_OUTPUT_FILENAME
);
}
// Set config defaults
$PACKAGE_CONFIG = array_replace_recursive([
"chmod" => "0755",
"directories" => ["src"],
"files" => [
"LICENSE",
],
"finder" => [
"name" => ["*.php", "*.xsd", "LICENSE"],
"exclude" => ["Tests", "tests"],
"in" => "vendor",
],
"compactors" => "Herrera\\Box\\Compactor\\Php",
"main" => "bin/" . strtolower(basename($PACKAGE_NAME)),
"stub" => true,
], $PACKAGE_CONFIG);
// Write or override config
file_put_contents($config_file, json_encode($PACKAGE_CONFIG, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
if (!is_readable($config_file)) {
print $config_file . 'does not exist in the package folder' . "\n";
exit(1);
}
// Build phar
$box_cmd = 'box build -v';
passthru($box_cmd, $return_status);
if ($return_status != 0) {
print 'Box command failed: ' . $box_cmd . "\n";
exit((int)$return_status);
}
// Make phar executable
passthru('chmod +x "' . $OUTPUT_FILENAME . '"');
// Cleanup again
print "Cleaning up temporary folder ...\n";
if (is_dir($TMP_DIR)) {
passthru("rm -rf ${TMP_DIR}");
}
// Add version file
file_put_contents($OUTPUT_FILENAME . '.version', "${PACKAGE_VERSION}");
// Finish
print "${PACKAGE_NAME}:${PACKAGE_VERSION} PHAR generated in: " . $OUTPUT_FILENAME . "\n";
exit(0);
?>
@itsjavi
Copy link
Author

itsjavi commented Jun 6, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment