2013-11-18 08:03:46 +01:00
|
|
|
<?php
|
2012-06-02 14:10:08 +02:00
|
|
|
namespace wdm\debian;
|
|
|
|
|
2015-05-26 10:15:42 +02:00
|
|
|
use RuntimeException;
|
2012-06-02 14:10:08 +02:00
|
|
|
use wdm\debian\control\StandardFile;
|
|
|
|
|
2013-11-18 08:03:46 +01:00
|
|
|
class Packager
|
2012-06-02 14:10:08 +02:00
|
|
|
{
|
|
|
|
private $_control;
|
|
|
|
private $_mountPoints = array();
|
|
|
|
private $_outputPath;
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2012-06-04 11:03:35 +02:00
|
|
|
private $_preInst;
|
|
|
|
private $_postInst;
|
|
|
|
private $_preRM;
|
|
|
|
private $_postRM;
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2012-06-04 11:03:35 +02:00
|
|
|
/**
|
|
|
|
* Set the control file
|
2013-11-18 08:03:46 +01:00
|
|
|
*
|
2012-06-04 11:03:35 +02:00
|
|
|
* This file contains the base package information
|
2013-11-18 08:03:46 +01:00
|
|
|
*
|
2012-06-04 11:03:35 +02:00
|
|
|
* @param StandardFile $control
|
|
|
|
* @return \wdm\debian\Packager
|
|
|
|
*/
|
2012-06-02 14:10:08 +02:00
|
|
|
public function setControl(StandardFile $control)
|
|
|
|
{
|
|
|
|
$this->_control = $control;
|
|
|
|
return $this;
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2015-04-12 11:27:56 +02:00
|
|
|
/**
|
|
|
|
* Return the actual control file
|
|
|
|
*
|
|
|
|
* @return StandardFile
|
|
|
|
*/
|
|
|
|
public function getControl()
|
|
|
|
{
|
|
|
|
return $this->_control;
|
|
|
|
}
|
|
|
|
|
2012-06-04 11:03:35 +02:00
|
|
|
/**
|
|
|
|
* Pre install script
|
2013-11-18 08:03:46 +01:00
|
|
|
*
|
2012-06-04 11:03:35 +02:00
|
|
|
* @param string $path The absolute path of your pre-install script
|
|
|
|
*/
|
|
|
|
public function setPreInstallScript($path)
|
|
|
|
{
|
|
|
|
$this->_preInst = $path;
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2012-06-04 11:03:35 +02:00
|
|
|
/**
|
|
|
|
* Post install script
|
2013-11-18 08:03:46 +01:00
|
|
|
*
|
2012-06-04 11:03:35 +02:00
|
|
|
* @param string $path The absolute path of your post-install script
|
|
|
|
*/
|
|
|
|
public function setPostInstallScript($path)
|
|
|
|
{
|
|
|
|
$this->_postInst = $path;
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2012-06-04 11:03:35 +02:00
|
|
|
/**
|
|
|
|
* Pre remove script
|
2013-11-18 08:03:46 +01:00
|
|
|
*
|
2012-06-04 11:03:35 +02:00
|
|
|
* @param string $path The absolute path of your pre-remove script
|
|
|
|
*/
|
|
|
|
public function setPreRemoveScript($path)
|
|
|
|
{
|
|
|
|
$this->_preRM = $path;
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2012-06-04 11:03:35 +02:00
|
|
|
/**
|
|
|
|
* Post remove script
|
2013-11-18 08:03:46 +01:00
|
|
|
*
|
2012-06-04 11:03:35 +02:00
|
|
|
* @param string $path The absolute path of your post-remove script
|
|
|
|
*/
|
|
|
|
public function setPostRemoveScript($path)
|
|
|
|
{
|
|
|
|
$this->_postRM = $path;
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2015-04-12 13:12:26 +02:00
|
|
|
/**
|
|
|
|
* @deprecated See addMount instead
|
|
|
|
*/
|
2012-06-02 14:10:08 +02:00
|
|
|
public function mount($sourcePath, $destinationPath)
|
2015-04-12 13:12:26 +02:00
|
|
|
{
|
|
|
|
return $this->addMount($sourcePath, $destinationPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addMount($sourcePath, $destinationPath)
|
2012-06-02 14:10:08 +02:00
|
|
|
{
|
|
|
|
$this->_mountPoints[$sourcePath] = $destinationPath;
|
|
|
|
return $this;
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2012-06-02 14:10:08 +02:00
|
|
|
public function setOutputPath($path)
|
|
|
|
{
|
|
|
|
$this->_outputPath = $path;
|
|
|
|
return $this;
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2015-04-12 13:12:26 +02:00
|
|
|
public function getOutputPath()
|
|
|
|
{
|
|
|
|
return $this->_outputPath;
|
|
|
|
}
|
|
|
|
|
2012-06-02 14:10:08 +02:00
|
|
|
public function run()
|
|
|
|
{
|
2015-04-12 13:12:26 +02:00
|
|
|
if (file_exists($this->getOutputPath())) {
|
|
|
|
$iterator = new \DirectoryIterator($this->getOutputPath());
|
2012-06-02 14:10:08 +02:00
|
|
|
foreach ($iterator as $path) {
|
2015-05-26 03:55:20 +02:00
|
|
|
if ($path != '.' && $path != '..') {
|
2015-05-26 10:15:42 +02:00
|
|
|
throw new RuntimeException("OUTPUT DIRECTORY MUST BE EMPTY! Something exists, exit immediately!");
|
2012-06-02 14:10:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2015-05-26 10:15:42 +02:00
|
|
|
if (!file_exists($this->getOutputPath())) {
|
|
|
|
mkdir($this->getOutputPath(), 0777);
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2012-06-02 14:10:08 +02:00
|
|
|
foreach ($this->_mountPoints as $path => $dest) {
|
2015-04-12 13:12:26 +02:00
|
|
|
$this->_pathToPath($path, $this->getOutputPath() . DIRECTORY_SEPARATOR . $dest);
|
2012-06-02 14:10:08 +02:00
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2015-04-12 13:12:26 +02:00
|
|
|
mkdir($this->getOutputPath() . "/DEBIAN", 0777);
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2015-04-12 13:12:26 +02:00
|
|
|
file_put_contents($this->getOutputPath() . "/DEBIAN/control", (string)$this->_control);
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2012-06-04 11:03:35 +02:00
|
|
|
if ($this->_preInst) {
|
2015-04-12 13:12:26 +02:00
|
|
|
$dest = $this->getOutputPath() . "/DEBIAN/preinst";
|
2012-06-04 11:03:35 +02:00
|
|
|
$this->_copy($this->_preInst, $dest);
|
|
|
|
chmod($dest, 0755);
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2012-06-04 11:03:35 +02:00
|
|
|
if ($this->_postInst) {
|
2015-04-12 13:12:26 +02:00
|
|
|
$dest = $this->getOutputPath() . "/DEBIAN/postinst";
|
2013-11-18 08:03:46 +01:00
|
|
|
$this->_copy($this->_postInst, $dest);
|
2012-06-04 11:03:35 +02:00
|
|
|
chmod($dest, 0755);
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2012-06-04 11:03:35 +02:00
|
|
|
if ($this->_preRM) {
|
2015-04-12 13:12:26 +02:00
|
|
|
$dest = $this->getOutputPath() . "/DEBIAN/prerm";
|
2013-11-18 08:03:46 +01:00
|
|
|
$this->_copy($this->_preRM, $dest);
|
2012-06-04 11:03:35 +02:00
|
|
|
chmod($dest, 0755);
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2012-06-04 11:03:35 +02:00
|
|
|
if ($this->_postRM) {
|
2015-04-12 13:12:26 +02:00
|
|
|
$dest = $this->getOutputPath() . "/DEBIAN/postrm";
|
2013-11-18 08:03:46 +01:00
|
|
|
$this->_copy($this->_postRM, $dest);
|
2012-06-04 11:03:35 +02:00
|
|
|
chmod($dest, 0755);
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2012-06-02 14:10:08 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
|
|
|
private function _pathToPath($path, $dest)
|
2012-06-02 14:10:08 +02:00
|
|
|
{
|
2013-11-18 08:03:46 +01:00
|
|
|
if (is_dir($path)) {
|
2012-06-02 14:10:08 +02:00
|
|
|
$iterator = new \DirectoryIterator($path);
|
|
|
|
foreach ($iterator as $element) {
|
|
|
|
if ($element != '.' && $element != '..') {
|
|
|
|
$fullPath = $path . DIRECTORY_SEPARATOR . $element;
|
|
|
|
if (is_dir($fullPath)) {
|
|
|
|
$this->_pathToPath($fullPath, $dest . DIRECTORY_SEPARATOR . $element);
|
|
|
|
} else {
|
|
|
|
$this->_copy($fullPath, $dest . DIRECTORY_SEPARATOR . $element);
|
|
|
|
}
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
}
|
|
|
|
} else if (is_file($path)) {
|
|
|
|
$this->_copy($path, $dest);
|
2012-06-02 14:10:08 +02:00
|
|
|
}
|
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
|
|
|
private function _copy($source, $dest)
|
2012-06-02 14:10:08 +02:00
|
|
|
{
|
2013-11-18 08:03:46 +01:00
|
|
|
$destFolder = dirname($dest);
|
|
|
|
if (!file_exists($destFolder)) {
|
|
|
|
mkdir($destFolder, 0777, true);
|
|
|
|
}
|
2016-06-21 14:19:46 +02:00
|
|
|
if (is_link($source)) {
|
|
|
|
symlink(readlink($source), $dest);
|
2016-06-21 14:33:34 +02:00
|
|
|
return; // don't set perms on symlink targets
|
2016-06-21 14:19:46 +02:00
|
|
|
} else {
|
2017-04-19 12:12:32 +02:00
|
|
|
if (!copy($source, $dest)) {
|
2016-06-21 14:22:27 +02:00
|
|
|
echo "Error: failed to copy: $source -> $dest \m";
|
|
|
|
return;
|
|
|
|
}
|
2016-06-21 14:19:46 +02:00
|
|
|
}
|
|
|
|
if (fileperms($source) != fileperms($dest)) {
|
2015-05-21 19:07:23 +02:00
|
|
|
chmod($dest, fileperms($source));
|
2016-06-21 14:19:46 +02:00
|
|
|
}
|
2012-06-02 14:10:08 +02:00
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2012-06-02 14:10:08 +02:00
|
|
|
public function build($debPackageName = false)
|
|
|
|
{
|
|
|
|
if (!$debPackageName) {
|
2017-07-18 12:07:15 +02:00
|
|
|
$control = $this->getControl();
|
|
|
|
$name = $control['Package'];
|
|
|
|
$version = $control['Version'];
|
|
|
|
$arch = $control['Architecture'];
|
|
|
|
$debPackageName = "{$name}_{$version}_{$arch}.deb";
|
2012-06-02 14:10:08 +02:00
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2015-04-12 13:12:26 +02:00
|
|
|
$command = "dpkg -b {$this->getOutputPath()} {$debPackageName}";
|
2013-11-18 08:03:46 +01:00
|
|
|
|
2014-12-03 23:24:42 +01:00
|
|
|
return $command;
|
2012-06-02 14:10:08 +02:00
|
|
|
}
|
2013-11-18 08:03:46 +01:00
|
|
|
}
|