Skip to content

Commit

Permalink
Merge pull request #46 from SimonEast/august-updates
Browse files Browse the repository at this point in the history
Improvements for PHPloy v2: Two bugfixes, some tidy-up and new version number
  • Loading branch information
banago committed Aug 13, 2014
2 parents c080e21 + 9dda33e commit 83a9261
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 29 deletions.
100 changes: 74 additions & 26 deletions phploy
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
* @author Bruno De Barros <[email protected]>
* @author Fadion Dashi <[email protected]>
* @author Simon East <[email protected]>
* @author Mark Beech <[email protected]>
* @link http://wplancer.com
* @licence MIT Licence
* @version 2.0.0-beta3
* @version 2.0.0-beta4
*/

/**
Expand All @@ -32,7 +33,7 @@ class PHPloy
/**
* @var string $phployVersion
*/
protected $phployVersion = '2.0.0-beta3';
protected $phployVersion = '2.0.0-beta4';

/**
* @var string $revision
Expand Down Expand Up @@ -134,6 +135,9 @@ class PHPloy
protected $mainRepo;

/**
* When deploying the main (parent) repository, this is false.
* When deploying submodules, this gets set to the current submodule being deployed.
*
* @var bool|string $currentSubmoduleName
*/
protected $currentSubmoduleName = false;
Expand Down Expand Up @@ -191,7 +195,10 @@ class PHPloy
{
$this->parseOptions();

$this->output("\r\n-------------- phploy v{$this->phployVersion} ----------------\r\n");
$this->output("\r\n<bgGreen>---------------------------------------------------");
$this->output("<bgGreen>| phploy v{$this->phployVersion} |");
$this->output("<bgGreen>---------------------------------------------------<reset>\r\n");


if ($this->displayHelp) {
$this->displayHelp();
Expand All @@ -202,18 +209,17 @@ class PHPloy
return;
}

if (file_exists("$this->repo/.git")) {
if (!file_exists("$this->repo/.git"))
throw new Exception("'{$this->repo}' is not Git repository.");

if ($this->listFiles)
$this->output("<yellow>phploy is running in LIST mode. No remote files will be modified.\r\n");
if ($this->listFiles)
$this->output("<yellow>phploy is running in LIST mode. No remote files will be modified.\r\n");

$this->output('Scanning repository...');
$this->checkSubmodules($this->repo);
$this->deploy($this->revision);
$this->prepareServers();
$this->output('Scanning repository...');
$this->checkSubmodules($this->repo);
$this->deploy($this->revision);

} else {
throw new Exception("'{$this->repo}' is not Git repository.");
}
}

/**
Expand Down Expand Up @@ -392,10 +398,14 @@ class PHPloy
if ($name == 'quickmode') {
foreach ($options as $env => $creds) {
$options = parse_url($creds);
$options = array_merge($defaults, $options);
$this->servers[$env] = $options;
if ($options) {
$options = array_merge($defaults, $options);
$this->servers[$env] = $options;
} else {
$this->output("<yellow>Warning! The \"$env\" environment (\"$creds\") does not appear to be a valid URL.\r\nIf your username or password contain special characters, avoid using quickmode and specify each setting individually instead.");
}
}
break;
continue;
}

$options = array_merge($defaults, $options);
Expand All @@ -416,7 +426,10 @@ class PHPloy

$this->debug("<yellow>CONSOLE:<darkYellow> $command");

exec($command, $output);
exec($command, $output, $returnStatus);

if ($returnStatus != 0)
throw new Exception("PHPloy attempted to run the following console command but it failed:\r\n$command");

$this->debug('<darkYellow>' . implode("\r\n<darkYellow>", $output));

Expand All @@ -439,13 +452,19 @@ class PHPloy
}

/**
* Compare revisions and returns array of files to upload:
* Generates an array of changed files by comparing the remote revision hash
* with the local revision hash (via a `git diff`)
*
* Returns an array in the following format:
*
* array(
* 'upload' => $filesToUpload,
* 'delete' => $filesToDelete
* );
*
* If $this->currentSubmoduleName is set, the diff is performed on the appropriate submodule
* otherwise it's performed on the main parent repo.
*
* @param string $localRevision
* @return array
* @throws Exception if unknown git diff status
Expand Down Expand Up @@ -480,19 +499,29 @@ class PHPloy
} else if ($localRevision == 'HEAD') {
$command = 'diff --name-status '.$remoteRevision.'...'.$localRevision;
} else {
// Question: should there really be a space after ... here? Is that valid?
$command = 'diff --name-status '.$remoteRevision.'... '.$localRevision;
}

$output = $this->gitCommand($command);
try {
$output = $this->gitCommand($command);
} catch (Exception $e) {
$this->debug($e->getMessage());
throw new Exception("The git diff command failed. This is probably because the git revision found on the server is not present in your repository.\r\nTry doing a 'git pull' before deploying.\r\n<reset>If performing a 'pull' doesn't resolve the problem, then it may be that another developer has deployed a commit without pushing to your repository. You need access to this commit to be able to correctly deploy changed files.\r\n\r\nA workaround is to reset the revision hash stored on the server using:\r\nphploy --sync=\"your-revision-hash-here\" HOWEVER this will leave your server files in an inconsistent state unless you manually update the appropriate files.");
}

if (! empty($remoteRevision)) {
foreach ($output as $line) {
if ($line[0] == 'A' or $line[0] == 'C' or $line[0] == 'M') {
// Added (A), Modified (C), Unmerged (M)

// Added (A), Modified (C), Unmerged (M)
if ($line[0] == 'A' or $line[0] == 'C' or $line[0] == 'M') {
$filesToUpload[] = trim(substr($line, 1));
// TODO: we could possibly calculate & sum the file sizes here to display the proper upload progress later on

// Deleted (D)
} elseif ($line[0] == 'D') {
// Deleted (D)
$filesToDelete[] = trim(substr($line, 1));

} else {
throw new Exception("Unknown git-diff status: {$line[0]}");
}
Expand All @@ -501,7 +530,10 @@ class PHPloy
$filesToUpload = $output;
}

$filesToUpload = array_diff($filesToUpload, $this->filesToIgnore);
// Skip any files in the $this->filesToIgnore array
// (the array_values() call then ensures we have a numeric 0-based array with no gaps,
// so that file numbers display correctly)
$filesToUpload = array_values(array_diff($filesToUpload, $this->filesToIgnore));

return array(
'upload' => $filesToUpload,
Expand All @@ -516,8 +548,6 @@ class PHPloy
*/
public function deploy($revision = 'HEAD')
{
$this->prepareServers();

// Exit with an error if the specified server does not exist in deploy.ini
if ($this->server != '' && !array_key_exists($this->server, $this->servers))
throw new Exception("The server \"{$this->server}\" is not defined in {$this->deployIniFilename}.");
Expand Down Expand Up @@ -571,7 +601,7 @@ class PHPloy
}
if (!$this->listFiles) {
$this->output("\r\n<darkGreen>------ Deployment complete ------");
$this->output("<darkGreen>------ Deployment size: ".$this->humanFilesize($this->deploymentSize)." ------");
$this->output("<darkGreen>------ Deployment size: ".$this->humanFilesize($this->deploymentSize)." ------\r\n");
$this->deploymentSize = 0;
}

Expand Down Expand Up @@ -698,14 +728,18 @@ class PHPloy

$this->ftpDebug("Changing directory to $path (to test if exists)");
if (! @ftp_chdir($this->connection, $path)) {

$this->ftpDebug('Could not change to remote directory. Will now attempt to create it...');
if (! ftp_mkdir($this->connection, $path)) {
$ret = false;

// Should this possibly raise an exception?
// Currently just exits the function and proceeds onto next server (if deploying to multiple)
$this->output(" <red>Failed to create '$path'.");
$this->output(" <red>Directory could not be created. Please check if a file with the same name exists in the server and delete it.");

return;

} else {
$this->output(" Created directory '$path'.");
$pathsThatExist[$path] = true;
Expand Down Expand Up @@ -964,14 +998,28 @@ class Ansi {

if (!static::$enabled) {
// Strip tags (replace them with an empty string)
return str_replace(array_keys(static::$tags), '', $string);
return static::stripTags($string);
}

// We always add a <reset> at the end of each string so that any output following doesn't continue the same styling
$string .= '<reset>';
return str_replace(array_keys(static::$tags), static::$tags, $string);
}

/**
* Removes all occurances of ANSI tags from a string
* (used when static::$enabled == false or can be called directly if outputting strings to a text file)
*
* Note: the reason we don't use PHP's strip_tags() here is so that we only remove the ANSI-related ones
*
* @param string $string Text possibly containing ANSI tags such as <red>, <bold> etc.
* @return string Stripped of all valid ANSI tags
*/
public static function stripTags($string)
{
return str_replace(array_keys(static::$tags), '', $string);
}

public static function isWindows()
{
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
Expand Down
17 changes: 14 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# PHPloy
**Version 2.0.0-beta3**
**Version 2.0.0-beta4**

PHPloy is a incremental Git FTP deployment tool. By keeping track of the state of the remote server(s) it deploys only the files that were committed since the last deployment. PHPloy supports submodules, sub-submodules, deploying to multiple servers and rollbacks.

Expand Down Expand Up @@ -78,10 +78,12 @@ The `deploy.ini` file hold your credentials and it must be in the root directory

[quickmode]
; If that seemed too long for you, you can use quickmode instead
staging = ftp://example:[email protected]:21/path/to/installation
production = ftp://example:[email protected]:21/path/to/installation
staging = ftp://user:[email protected]:21/path/to/installation
production = ftp://user:[email protected]:21/path/to/installation


Quickmode will *not* work if your username contains `:`, `/`, or if your password contains `/`. In these cases specify each item individually.

The first time it's executed, PHPloy will assume that your deployment server is empty, and will upload ALL the files of your project. If the remote server already has a copy of the files, you can specify which revision it is on using the `--sync` command (see below).


Expand Down Expand Up @@ -160,11 +162,20 @@ The people that have brought PHPloy to you are:
* [Baki Goxhaj](https://twitter.com/banago) - lead developer
* [Bruno De Barros](https://twitter.com/terraduo) - initial inspiration
* [Fadion Dashi](https://twitter.com/jonidashi) - contributor
* Mark Beech - contributor
* [Simon East](https://twitter.com/SimoEast) - contributor, Windows support


## Version history

v2.0.0-beta4 (13 Aug 2014)

* Feature: PHPloy version header more prominent - makes it easier to see where deployments started when scrolling back through long console output
* Feature: Log deployment size and show on deploy completion
* Bugfix: phploy would ignore any environments *after* quickmode
* Bugfix: file upload counter sometimes incorrect
* More informative error messages in several cases

v2.0.0-beta3 (26 May 2014)

* Colored console output is now *optional* and disabled by default on Windows unless Ansicon is detected. (Colors can be disabled through the `--no-colors` command-line option.)
Expand Down

0 comments on commit 83a9261

Please sign in to comment.