Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Do the right thing when trying to replace a file that does not exist

  • Loading branch information...
commit e71293f398ed3a11d3470d4b507b31cc77431c89 1 parent 0fb3fc4
@stuartherbert authored
Showing with 85 additions and 80 deletions.
  1. +85 −80 src/php/Phix_Project/ComponentManager/Entities/ComponentFolder.php
View
165 src/php/Phix_Project/ComponentManager/Entities/ComponentFolder.php
@@ -80,16 +80,16 @@ class ComponentFolder
* @var string
*/
public $buildPropertiesFile = null;
-
+
/**
* The path to the package.xml file in the component's root folder
* @var string
*/
public $packageXmlFile = null;
-
+
/**
* The current state of the folder
- * @var int
+ * @var int
*/
public $state = self::STATE_UNKNOWN;
@@ -134,7 +134,7 @@ public function loadFolderState()
return;
}
}
-
+
// is this for a component.type that we support?
if ($properties['component.type'] !== static::COMPONENT_TYPE)
{
@@ -176,12 +176,12 @@ public function getStateAsText()
public function copyFilesFromDataFolder($files, $dest='')
{
// make sure we catch silly programmer errors
- Contract::Preconditions(function() use($files, $dest)
+ Contract::Preconditions(function() use($files, $dest)
{
// make sure we're happy with $files
Contract::RequiresValue($files, is_array($files), '$files must be an array');
Contract::RequiresValue($files, count($files) > 0, '$files must not be an empty array');
-
+
// make sure we're happy with $dest
Contract::RequiresValue($dest, is_string($dest), '$dest must be a string');
if (strlen($dest) > 0)
@@ -189,19 +189,19 @@ public function copyFilesFromDataFolder($files, $dest='')
Contract::RequiresValue($dest, substr($dest,-1, 1) == '/', '$dest must end with a / character');
}
});
-
+
$taskQueue = new TaskQueue();
-
+
foreach ($files as $filename)
{
$srcFile = $this->pathToDataFolder . '/' . $filename;
$destFile = $this->folder . '/' . $dest . basename($filename);
-
+
$cpTask = new Files_CpTask();
$cpTask->initWithFilesOrFolders($srcFile, $destFile);
$taskQueue->queueTask($cpTask);
}
-
+
$taskQueue->executeTasks();
}
@@ -212,20 +212,20 @@ public function copyFileFromDataFolderWithNewName($file, $dest)
{
Contract::RequiresValue($file, is_string($file), '$file must be a string');
Contract::RequiresValue($file, strlen($file) > 0, '$file cannot be an empty string');
-
+
Contract::RequiresValue($dest, is_string($dest), '$dest must be a string');
Contract::RequiresValue($dest, strlen($dest) > 0, '$dest cannot be an empty string');
});
-
+
$srcFile = $this->pathToDataFolder . '/' . $file;
$destFile = $this->folder . '/' . $dest;
-
+
$taskQueue = new TaskQueue();
-
+
$cpTask = new Files_CpTask();
$cpTask->initWithFilesOrFolders($srcFile, $destFile);
$taskQueue->queueTask($cpTask);
-
+
$taskQueue->executeTasks();
}
@@ -236,35 +236,35 @@ public function replaceFolderContentsFromDataFolder($src, $dest='')
{
Contract::RequiresValue($src, is_string($src), '$src must be a string');
Contract::RequiresValue($src, strlen($src) > 0, '$src cannot be an empty string');
-
+
Contract::RequiresValue($dest, is_string($dest), '$dest must be a string');
});
-
+
$srcFolder = $this->pathToDataFolder . '/' . $src;
$destFolder = $this->folder . '/' . $dest;
// queue up the work we need to do
$taskQueue = new TaskQueue();
-
+
$rmTask = new Files_RmTask();
$rmTask->initWithFileOrFolder($destFolder);
$taskQueue->queueTask($rmTask);
-
+
$mkdirTask = new Files_MkdirTask();
$mkdirTask->initWithFolder($destFolder);
$taskQueue->queueTask($mkdirTask);
-
+
$cpTask = new Files_CpTask();
$cpTask->initWithFilesOrFolders($src, $dest);
$taskQueue->queueTask($cpTask);
-
+
// execute the tasks!
- //
+ //
// if there are problems, an exception will automatically
// be thrown
$taskQueue->executeTasks();
}
-
+
public function renameOrReplaceFileFromDataFolder($src, $oldName, $oldChecksum)
{
// make sure we catch silly programmer errors
@@ -277,39 +277,44 @@ public function renameOrReplaceFileFromDataFolder($src, $oldName, $oldChecksum)
Contract::RequiresValue($oldChecksum, is_string($oldChecksum), '$oldChecksum must be a string');
Contract::RequiresValue($oldChecksum, strlen($oldChecksum) == 32, '$oldChecksum must be an md5sum');
});
-
+
$newFile = $this->pathToDataFolder . '/' . $src;
$fileToMoveFrom = $this->folder . '/' . $oldName;
$fileToMoveTo = $this->folder . '/' . $src;
-
- // queue up the work we need to do
- $taskQueue = new TaskQueue();
-
- $renameTask = new Files_RenameIfNotModifiedTask();
- $renameTask->initWithFileAndChecksum($fileToMoveFrom, $fileToMoveTo, $oldChecksum);
- $taskQueue->queueTask($renameTask);
-
- $renameFailed = false;
- try
- {
- $taskQueue->executeTasks();
- }
- catch(E5xx_TaskFailedException $e)
- {
- $renameFailed = true;
- }
-
- // did it work?
- if (!$renameFailed)
+
+ if (file_exists($fileToMoveFrom))
{
- // yes
- return true;
+ // queue up the work we need to do
+ $taskQueue = new TaskQueue();
+
+ $renameTask = new Files_RenameIfNotModifiedTask();
+ $renameTask->initWithFileAndChecksum($fileToMoveFrom, $fileToMoveTo, $oldChecksum);
+ $taskQueue->queueTask($renameTask);
+
+ $renameFailed = false;
+ try
+ {
+ $taskQueue->executeTasks();
+ }
+ catch(E5xx_TaskFailedException $e)
+ {
+ $renameFailed = true;
+ }
+
+ // did it work?
+ if (!$renameFailed)
+ {
+ // yes
+ return true;
+ }
}
-
+
// if we get here, the old file has been modified, and
- // cannot be moved. we need to copy the new version of
- // the file into the right place
-
+ // cannot be moved ... or the old file is missing
+ //
+ // we need to copy the new version of the file into the
+ // right place
+
$this->copyFilesFromDataFolder(array($src));
return false;
}
@@ -321,10 +326,10 @@ public function copyFolders($src, $dest='')
{
Contract::RequiresValue($src, is_string($src), '$src must be a string');
Contract::RequiresValue($src, strlen($src) > 0, '$src cannot be an empty string');
-
+
Contract::RequiresValue($dest, is_string($dest), '$dest must be a string');
});
-
+
if ($src{0} !== DIRECTORY_SEPARATOR)
{
$srcFolder = $this->pathToDataFolder . '/' . $src;
@@ -337,19 +342,19 @@ public function copyFolders($src, $dest='')
// queue up the work we need to do
$taskQueue = new TaskQueue();
-
+
$rmTask = new Files_RmTask();
$rmTask->initWithFileOrFolder($destFolder);
$taskQueue->queueTask($rmTask);
-
+
$mkdirTask = new Files_MkdirTask();
$mkdirTask->initWithFolder($destFolder);
$taskQueue->queueTask($mkdirTask);
-
+
$cpTask = new Files_CpTask();
$cpTask->initWithFilesOrFolders($srcFolder, $destFolder);
$taskQueue->queueTask($cpTask);
-
+
// execute the tasks!
//
// if there are problems, an exception will automatically
@@ -365,7 +370,7 @@ public function enableExecutionOf($file)
Contract::RequiresValue($file, is_string($file), '$file must be a string');
Contract::RequiresValue($file, strlen($file) > 0, '$file cannot be an empty string');
});
-
+
if ($file{0} !== DIRECTORY_SEPARATOR)
{
$fqFile = $this->folder . '/' . $file;
@@ -377,18 +382,18 @@ public function enableExecutionOf($file)
// queue up the work we need to do
$taskQueue = new TaskQueue();
-
+
$chmodTask = new Files_ChmodTask();
$chmodTask->initWithFileAndMode($fqFile, 0755);
$taskQueue->queueTask($chmodTask);
-
+
// execute the task
//
// if there are problems, an exception will automatically
// be thrown
$taskQueue->executeTasks();
}
-
+
public function regexFile($file, $regex, $replace)
{
// catch any silly programmer errors
@@ -396,7 +401,7 @@ public function regexFile($file, $regex, $replace)
{
Contract::RequiresValue($file, is_string($file), '$file must be a string');
Contract::RequiresValue($file, strlen($file) > 0, '$file cannot be an empty string');
-
+
if (is_array($regex))
{
Contract::ForAll($regex, function($value){ Contract::RequiresValue($value, is_string($value), '$regex array can only contain strings'); });
@@ -406,7 +411,7 @@ public function regexFile($file, $regex, $replace)
Contract::RequiresValue($regex, is_string($regex), '$regex must be a string or an array');
Contract::RequiresValue($regex, strlen($regex) > 0, '$regex cannot be an empty string');
}
-
+
if (is_array($replace))
{
Contract::ForAll($replace, function($value){ Contract::RequiresValue($value, is_string($value), '$replace array can only contain strings'); });
@@ -417,16 +422,16 @@ public function regexFile($file, $regex, $replace)
Contract::RequiresValue($replace, strlen($replace) > 0, '$replace cannot be an empty string');
}
});
-
+
$srcFile = $this->folder . '/' . $file;
-
+
// queue up the work we need to do
$taskQueue = new TaskQueue();
-
+
$regexTask = new Files_RegexTask();
$regexTask->initWithFileAndRegex($srcFile, $regex, $replace);
$taskQueue->queueTask($regexTask);
-
+
// execute the tasks!
//
// if there are problems, an exception will automatically
@@ -456,16 +461,16 @@ public function addBuildProperty($property, $value, $after=null)
{
Contract::RequiresValue($property, is_string($property), '$property must be a string');
Contract::RequiresValue($property, strlen($property) > 0, '$property cannot be an empty string');
-
+
Contract::RequiresValue($value, is_string($value) || is_int($value) || is_float($value), '$value must be a string, integer or float');
});
-
+
if (!$this->testHasBuildProperties())
{
return false;
}
- $buildProperties = file_get_contents($this->buildPropertiesFile);
+ $buildProperties = file_get_contents($this->buildPropertiesFile);
if ($this->hasBuildProperty($property, $buildProperties))
{
$buildProperties = preg_replace('|^' . $property . '=.*$|m', $property . '=' . $value, $buildProperties);
@@ -488,14 +493,14 @@ public function hasBuildProperty($property, $buildProperties)
{
Contract::RequiresValue($property, is_string($property), '$property must be a string');
Contract::RequiresValue($property, strlen($property) > 0, '$property cannot be an empty string');
-
+
Contract::RequiresValue($buildProperties, is_string($buildProperties), '$buildProperties must be a string');
Contract::RequiresValue($buildProperties, strlen($buildProperties) > 0, '$buildProperties cannot be an empty string');
});
-
- return preg_match('|' . $property . '=|', $buildProperties);
+
+ return preg_match('|' . $property . '=|', $buildProperties);
}
-
+
public function setBuildProperty($property, $value)
{
// catch silly programmer errors
@@ -503,10 +508,10 @@ public function setBuildProperty($property, $value)
{
Contract::RequiresValue($property, is_string($property), '$property must be a string');
Contract::RequiresValue($property, strlen($property) > 0, '$property cannot be an empty string');
-
+
Contract::RequiresValue($value, is_string($value) || is_int($value) || is_float($value), '$value must be a string, integer or float');
});
-
+
if (!$this->testHasBuildProperties())
{
return false;
@@ -523,7 +528,7 @@ public function setBuildProperty($property, $value)
}
file_put_contents($this->buildPropertiesFile, $buildProperties);
}
-
+
public function testHasPackageXml()
{
if (file_exists($this->packageXmlFile))
@@ -533,22 +538,22 @@ public function testHasPackageXml()
return false;
}
-
+
public function loadPackageXml()
{
if (!$this->testHasPackageXml())
{
return false;
}
-
+
return simplexml_load_file($this->packageXmlFile);
}
-
+
public function savePackageXml(SimpleXMLElement $xmlNode)
{
file_put_contents($this->packageXmlFile, $xmlNode->asXML());
}
-
+
public function upgradeComponent($targetVersion, $thisVersion = null)
{
// work out which version we are upgrading from
Please sign in to comment.
Something went wrong with that request. Please try again.