Skip to content

Instantly share code, notes, and snippets.

@patpawlowski
Last active August 17, 2017 12:51
Show Gist options
  • Save patpawlowski/8ffb644b49343446fedc532dc0f11527 to your computer and use it in GitHub Desktop.
Save patpawlowski/8ffb644b49343446fedc532dc0f11527 to your computer and use it in GitHub Desktop.
PHP Script convert a list of files into an installable sugar package
<?php
/**
* Created by NetBeans.
* User: patpawlowski
* Date: Dec 30, 2016 at 4:19:08 PM
* File: SugarPackager.php
* find custom/ -type f
* Curl: curl https://gitlab.w-systems.com/snippets/11/raw > SugarPackager.php
*
* Description: You can run this file in a folder two folders down from the root
* of a Sugar installation. For instance: <sugar root>/packages/package1 It will
* look for a file named file_list.txt with a file and path included on each line.
*
* Example file_list.txt file:
* custom/modules/Emails/logic_hooks.php
* custom/modules/Emails/LogicHooks/createPropertiesRelationship.php
* custom/modules/Emails/metadata/studio.php
*
* An easy way to get the list is by capturing the output of a git status and then
* cleaning it up a bit with some global replaces.
*
* Running 'php SugarPackager.php "INC0048757 ACME, Inc. - Create a means to link Emails to Properties"
* will then copy all the files from two folders up "../../" to the local folder
* and then create a manifest.php file based on the template included at the bottom
* of this file. The first word of the attribute passed in "INC0048757" will have
* "tcx_" prepended to it and will become the name of the $manifest and the id of the
* $installdefs. The entire string will become the $manifest description.
*
*/
ini_set('display_errors', 1);
class SugarPackager {
private $FileList = array();
private $Manifest = '';
private $FileCopyText = '';
private $Name = '';
private $Filename = '';
public function run($Folder = '') {
$this->getFileList();
$this->copyFiles();
$this->loadManifestTemplate();
$this->createFileCopyText();
$this->updateManifest();
$this->saveManifest();
$this->zipFiles();
}
private function getFileList() {
$this->FileList = file('file_list.txt');
}
private function copyFiles() {
foreach($this->FileList as $File){
if($this->isCommnet($File)) {continue;}
$File = trim($File);
$Source = "../../".$File;
echo "Copying $Source \n\t to $File\n";
@mkdir(dirname($File), 0777, true);
copy($Source, $File);
}
}
// private function loadManifest() {
// echo "Loading manifest template\n";
// $this->Manifest = file_get_contents('manifest.tmp.php');
// }
private function saveManifest() {
echo "Saving manifest\n";
file_put_contents('manifest.php', $this->Manifest);
}
private function createFileCopyText() {
echo "Creating File Copy Text\n";
foreach($this->FileList as $File){
if($this->isCommnet($File)) {continue;}
$File = trim($File);
$this->FileCopyText .= " array(
'from' => '<basepath>/$File',
'to' => '$File',
),\n";
}
$this->Manifest = str_replace('{copyfiles}', $this->FileCopyText, $this->Manifest);
}
private function updateManifest() {
global $argv;
$Params = explode(' ', $argv[1]);
$this->Name = 'tcx_'.$Params[0];
$this->Manifest = str_replace('{name}', $this->Name, $this->Manifest);
$this->Manifest = str_replace('{description}', $argv[1], $this->Manifest);
$this->Manifest = str_replace('{datetime}', date(DATE_ATOM), $this->Manifest);
$this->Manifest = str_replace('{version}', $this->getVersion(), $this->Manifest);
}
private function getVersion() {
$i = 0;
while(true){
$i++;
if(!file_exists($this->Name."v".$i.".zip")){
$this->Filename = $this->Name."v".$i.".zip";
echo "Filename: ".$this->Filename.PHP_EOL;
return $i;
}
}
}
private function zipFiles() {
echo "Zipping files into ". $this->Filename.PHP_EOL;
exec("zip -r {$this->Filename} custom modules post_install_actions.php manifest.php ");
}
private function isCommnet($string) {
$c = substr(trim($string), 0, 1);
if($c === '#') {return true;}
if($c === ';') {return true;}
if($c === '') {return true;}
return false;
}
private function loadManifestTemplate(){
$this->Manifest = "<?php
/**
* Created by NetBeans.
* User: patpawlowski
* Date: Jan 17, 2017 at 11:32:04 AM
* File: tcx_INC0048757.php
*/
\$manifest = array(
'built_in_version' => '7.7.1.0',
'acceptable_sugar_versions' =>
array(
0 => '7.*.*.*',
),
'acceptable_sugar_flavors' =>
array(
0 => 'CE',
1 => 'PRO',
2 => 'ENT',
3 => 'ULT',
),
'readme' => '',
'key' => 'tcx',
'author' => 'Ticomix',
'description' => '{description}',
'icon' => '',
'is_uninstallable' => true,
'name' => '{name}',
'published_date' => '{datetime}',
'type' => 'module',
'version' => {version},
// 'remove_tables' => 'prompt',
);
\$installdefs = array(
'id' => '{name}',
'copy' =>
array(
{copyfiles}
// array(
// 'from' => '<basepath>/custom',
// 'to' => 'custom',
// ),
),
// 'pre_execute' => array(
// 0 => '<basepath>/pre_install_actions.php',
// ),
// 'post_execute' => array(
// 0 => '<basepath>/post_install_actions.php',
// ),
);
";
}
}
$SugarPackager = new SugarPackager();
$SugarPackager->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment