Here is a fairly simple package. I tried to boil it down as much as I could for you. It’s untested and I had to move some things around to make it simpler, so consider it an alpha version, but it should get you started. I assumed that you wanted to put all your snippets in a category. It’s generally a good idea.
<?php
/**
* Minimal Build Script
*
*/
global $modx;
/* set this to the directory above your _build directory */
$root = dirname(dirname(__FILE__)).'/';
/* Set File Locations
* -- source_assets is for files you want to transfer that need to be accessible via URL
* or have no security issues
*
* -- source_core is for files you want to protect and that don't need to be accessible
* via URL (e.g. .php files)
*
* -- data is for sources files used in the build that won't be transfered (which could be all of your files)
*
* Assumes a directory structure like this:
*
* yourcomponent
* _build
* build.transport.php -- this file
* build.config.php -- file with the modx config settings (not used here)
* data -- files used in build that won't be transferred in the package
* yoursnippet1.props.php -- properties file
* yoursnippet2.props.php
* transport.snippets.php -- code to create the snippet array (not used here)
* transport.chunks.php -- code to create the chunk array (not used here)
* transport.resources.php -- code to create the resources array (not used here)
* etc. for other elements (plugins, system settings, users, etc.
* assets -- files to transfer below the assets dir -- whole dir is transferred
* components
* yourcomponent
* css
* yourcomponent.css
* js
* yourcomponent.js
* core -- files to transfer below the core dir -- whole dir is transferred
* components
* yourcomponent
* classes -- class files (if any -- not used here)
* yourcomponent.class.php
* docs
* readme.txt
* license.txt
* changelog.txt
* lexicon
* en
* default.inc.php -- language file
* templates (if any)
* yourcomponent.tpl -- Tpl chunk
* yourcomponenttpl2.tpl -- Tpl chunk
* yoursnippet1.inc.php -- source code of snippet1
* yoursnippet2.inc.php -- source code of snippet2
*
*
*
*/
$sources= array (
'root' => $root,
'build' => $root . '_build/',
'data' => $root . '_build/data/',
'lexicon' => $root . 'core/components/yourcomponentname/lexicon/',
'docs' => $root . 'core/components/yourcomponentname/docs/',
'source_assets' => $root . 'assets/components/yourcomponentname',
'source_core' => $root . 'core/components/yourcomponentname',
);
unset($root);
$packageNamespace = 'yourcomponentname';
/* This example assumes that you are creating 2 snippets with one namespace,
* a lexicon, and 2 file resolvers (core files, assets files).
*
* The elements will all go in a single category.
* Note: this code won't work without a category.
*
* You'll need to modify it if your situation
* is different.
*
* A snippet with no files to be transferred doesn't need a file
* resolver so you can comment out that part of the code.
*
* If you have no lexicon, you can comment out that part of
* the code.
*/
/* The name of the package as it will appear in Package Management
* will be a combination of the name, version, and release
*/
define('PKG_NAME','YourComponentName');
define('PKG_NAME_LOWER','yourcomponentname');
define('PKG_VERSION','1.0.0');
define('PKG_RELEASE','beta1');
/* set the name of the category the elements will go in */
define('PKG_CATEGORY','yourcategory');
/* set modx constants (edit the first) */
define('MODX_CORE_PATH', '/path/to/modx/core/');
define('MODX_CONFIG_KEY', 'config');
/* let this script run outside of MODx */
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
$modx= new modX();
$modx->initialize('mgr');
echo '<pre>'; /* used for nice formatting for log messages */
$modx->setLogLevel(modX::LOG_LEVEL_INFO);
$modx->setLogTarget('ECHO');
/* $modx->setDebug(true); */
$modx->log(modX::LOG_LEVEL_INFO,'Building ' . PKG_NAME . '<br />');
$modx->log(modX::LOG_LEVEL_INFO,'Initializing Package Builder<br />');
/* Initialize the Package Builder - don't change anything here */
$modx->loadClass('transport.modPackageBuilder','',false, true);
$builder = new modPackageBuilder($modx);
$builder->createPackage(PKG_NAME_LOWER,PKG_VERSION,PKG_RELEASE);
$builder->registerNamespace(PKG_NAME_LOWER,false,true,'{core_path}components/'.PKG_NAME_LOWER.'/');
/* create category -- skip if you don't want to put your stuff in a category */
$modx->log(modX::LOG_LEVEL_INFO,'Creating Category<br />');
$attr = array(
xPDOTransport::UNIQUE_KEY => 'category',
xPDOTransport::PRESERVE_KEYS => false,
xPDOTransport::UPDATE_OBJECT => true,
xPDOTransport::RELATED_OBJECTS => true,
xPDOTransport::RELATED_OBJECT_ATTRIBUTES => array (
'Snippets' => array (
xPDOTransport::PRESERVE_KEYS => false,
xPDOTransport::UPDATE_OBJECT => true,
xPDOTransport::UNIQUE_KEY => 'name',
),
'Chunks' => array (
xPDOTransport::PRESERVE_KEYS => false,
xPDOTransport::UPDATE_OBJECT => true,
xPDOTransport::UNIQUE_KEY => 'name',
),
)
);
$categoryObject= $modx->newObject('modCategory');
$categoryObject->set('id',1);
$categoryObject->set('category',PKG_CATEGORY);
/* create snippet objects */
$modx->log(modX::LOG_LEVEL_INFO,'Creating Snippets<br />');
$snippets = array();
/* first snippet */
$modx->log(modX::LOG_LEVEL_INFO,'Creating Snippet1<br />');
$snippets[1]= $modx->newObject('modSnippet');
$snippets[1]->fromArray(array(
'id' => 1,
'name' => 'YourSnippet1',
'description' => 'YourSnippet1 '.PKG_VERSION.'-'.PKG_RELEASE
.' - Description goes here',
),'',true,true);
$snippets[1]->setContent(file_get_contents($sources['source_core'] . '/yoursnippet1.inc.php'));
/* code to add snippet properties to snippet1 */
/* $properties = include $sources['data'].'properties.yoursnippet1.php';
$snippets[1]->setProperties($properties);
unset($properties); */
/* second snippet */
$modx->log(modX::LOG_LEVEL_INFO,'Creating Snippet2<br />');
$snippets[2]= $modx->newObject('modSnippet');
$snippets[2]->fromArray(array(
'id' => 2,
'name' => 'yoursnippet2',
'description' => 'YourSnippet2 '.PKG_VERSION.'-'.PKG_RELEASE
. 'Description of snippet 2',
),'',true,true);
$snippets[2]->setContent(file_get_contents($sources['source_core'] . '/yoursnippet2.inc.php'));
/* code to add snippet properties to snippet2 */
/* $properties = include $sources['data'].'properties.yoursnippet2.php';
$snippets[2]->setProperties($properties);
unset($properties); */
/* repeat above sections for more snippets */
/* chunks work just like snippets, but with 'modChunk'.
Put them in a $chunks array */
/* add snippets to category (if there is one) */
$categoryObject->addMany($snippets,'Snippets');
/* add chunks to category
$categoryObject->addMany($chunks,'Chunks');
*/
/* build category */
$modx->log(modX::LOG_LEVEL_INFO,'Creating Vehicle<br />');
$vehicle = $builder->createVehicle($categoryObject,$attr);
$modx->log(modX::LOG_LEVEL_INFO,'Adding Resolver to transfer assets files<br />');
$vehicle->resolve('file',array(
'source' => $sources['source_assets'],
'target' => "return MODX_ASSETS_PATH . 'components/';",
));
$modx->log(modX::LOG_LEVEL_INFO,'Adding Resolver to transfer core files<br />');
$vehicle->resolve('file',array(
'source' => $sources['source_core'],
'target' => "return MODX_CORE_PATH . 'components/';",
));
$modx->log(modX::LOG_LEVEL_INFO,'Adding File Resolvers to package<br />');
$builder->putVehicle($vehicle);
/* done building package */
/* now pack in the license file and readme */
$modx->log(modX::LOG_LEVEL_INFO,'Adding readme.txt and licence to package<br />');
$builder->setPackageAttributes(array(
'readme' => file_get_contents($sources['docs'] . 'readme.txt'),
'license' => file_get_contents($sources['docs'] . 'license.txt'),
));
$modx->log(modX::LOG_LEVEL_INFO,'Adding Lexicon strings<br />');
/* load lexicon strings */
$builder->buildLexicon($sources['lexicon']);
$modx->log(modX::LOG_LEVEL_INFO,'Creating package ZIP<br />');
/* zip up the package */
$builder->pack();
$modx->log(modX::LOG_LEVEL_INFO,'Package completed.<br />');
exit();