Permalink
Browse files

Fix #356 - Use the full path (relative to the project root) for the p…

…ath to shared files/folder rather than just the basename
  • Loading branch information...
REBELinBLUE committed Oct 2, 2017
1 parent 539eb87 commit 2c35581b8553ecd8551e5a4dfed70d044db24581
@@ -192,11 +192,12 @@ private function getScriptForStep(array $tokens = [])
case Command::DO_INSTALL:
$release_path = $tokens['release_path'];
$shared_path = $tokens['shared_path'];
$project_path = $tokens['project_path'];
// Write configuration file to release dir, symlink shared files and run composer
return $this->process->setScript('deploy.steps.InstallComposerDependencies', $tokens)
->prependScript($this->configurationFileCommands($release_path))
->appendScript($this->shareFileCommands($release_path, $shared_path));
->appendScript($this->shareFileCommands($release_path, $shared_path, $project_path));
case Command::DO_ACTIVATE:
return $this->process->setScript('deploy.steps.ActivateNewRelease', $tokens);
case Command::DO_PURGE:
@@ -240,45 +241,57 @@ private function configurationFileCommands($release_dir)
*
* @param string $release_dir
* @param string $shared_dir
* @param string $project_dir
*
* @return string
*/
private function shareFileCommands($release_dir, $shared_dir)
private function shareFileCommands($release_dir, $shared_dir, $project_dir)
{
/** @var Collection $files */
$files = $this->deployment->project->sharedFiles;
if (!$files->count()) {
return '';
}
$script = '';
$files->each(function (SharedFile $shared) use (&$script, $release_dir, $shared_dir) {
$migration = '.deployer-migrated';
$backup_dir = $shared_dir . '.backup';
$script = $this->parser->parseFile('deploy.MigrateShared', [
'deployment' => $this->deployment->id,
'shared_dir' => $shared_dir,
'project_dir' => $project_dir,
'backup_dir' => $backup_dir,
'migration' => $migration,
]);
$files->each(function (SharedFile $shared) use (&$script, $release_dir, $shared_dir, $backup_dir) {
$pathinfo = pathinfo($shared->file);
$template = 'File';
$file = $shared->file;
if (starts_with($file, '/')) {
$file = substr($file, 1);
}
$file = ltrim($shared->file, '/1');
if (ends_with($file, '/')) {
$template = 'Directory';
$file = substr($file, 0, -1);
}
$filename = $pathinfo['filename'];
if (isset($pathinfo['extension'])) {
$filename .= '.' . $pathinfo['extension'];
$file = rtrim($file, '/');
}
$script .= $this->parser->parseFile('deploy.Share' . $template, [
'deployment' => $this->deployment->id,
'target_file' => $release_dir . '/' . $file,
'source_file' => $shared_dir . '/' . $filename,
'shared_dir' => $shared_dir,
'release_dir' => $release_dir,
'backup_dir' => $backup_dir,
'path' => $file,
'filename' => $pathinfo['basename'],
'parent_dir' => ltrim($pathinfo['dirname'], '/'),
]);
});
$script .= $this->parser->parseFile('deploy.MigrateSharedTimestamp', [
'migration' => $migration,
'shared_dir' => $shared_dir,
'release' => $this->deployment->release_id,
]);
return PHP_EOL . $script;
}
}
@@ -29,5 +29,12 @@ public function run()
'target_type' => 'project',
'target_id' => 1,
]);
SharedFile::create([
'name' => 'CSS',
'file' => 'resources/assets/css/console.css',
'target_type' => 'project',
'target_id' => 1,
]);
}
}
@@ -0,0 +1,12 @@
### Migrate shared directories/files - {{ deployment }}
SHARED_NEEDS_MIGRATING=false
if [ ! -f {{ shared_dir }}/{{ migration }} ]; then
echo ""
echo "Shared directory needs migrating - Backup created"
if [ ! -d {{ backup_dir }} ]; then
mv -f {{ shared_dir }} {{ backup_dir }}
fi
SHARED_NEEDS_MIGRATING=true
fi
@@ -0,0 +1,3 @@
if [ ! -f {{ shared_dir }}/{{ migration }} ]; then
echo '{{ release }}' > {{ shared_dir }}/{{ migraton }}
fi
@@ -1,14 +1,21 @@
### Create shared directories - {{ deployment }}
if [ -d {{ target_file }} ]; then
if [ ! -d {{ source_file }} ]; then
cp -pRn {{ target_file }} {{ source_file }}
fi
if [ ! -d {{ shared_dir }}/{{ path }} ]; then
mkdir -p {{ shared_dir }}/{{ path }}
rm -rf {{ target_file }}
if [ -d {{ release_dir }}/{{ path }} ]; then
cp -pRn {{ release_dir }}/{{ path }} {{ shared_dir }}/{{ parent_dir }}
fi
fi
if [ ! -d {{ source_file }} ]; then
mkdir {{ source_file }}
if [ -d {{ release_dir }}/{{ path }} ]; then
rm -rf {{ release_dir }}/{{ path }}
fi
ln -s {{ source_file }} {{ target_file }}
ln -s {{ shared_dir }}/{{ path }} {{ release_dir }}/{{ path }}
if [ $SHARED_NEEDS_MIGRATING ]; then
if [ -e {{ backup_dir }}/{{ filename }} ]; then
#rm -rf {{ shared_dir }}/{{ path }}
cp -RvfT {{ backup_dir }}/{{ filename }} {{ shared_dir }}/{{ path }}
fi
fi
@@ -1,14 +1,23 @@
### Created shared files - {{ deployment }}
if [ -f {{ target_file }} ]; then
if [ ! -f {{ source_file }} ]; then
cp -pRn {{ target_file }} {{ source_file }}
### Create shared files - {{ deployment }}
mkdir -p {{ shared_dir }}/{{ parent_dir }}
if [ -f {{ release_dir }}/{{ path }} ]; then
if [ ! -f {{ shared_dir }}/{{ path }} ]; then
cp -pRn {{ release_dir }}/{{ path }} {{ shared_dir }}/{{ path }}
fi
rm -rf {{ target_file }}
rm -f {{ release_dir }}/{{ path }}
fi
if [ ! -f {{ source_file }} ]; then
touch {{ source_file }}
if [ ! -f {{ shared_dir }}/{{ path }} ]; then
touch {{ shared_dir }}/{{ path }}
fi
ln -s {{ source_file }} {{ target_file }}
ln -s {{ shared_dir }}/{{ path }} {{ release_dir }}/{{ path }}
if [ $SHARED_NEEDS_MIGRATING ]; then
if [ -e {{ backup_dir }}/{{ filename }} ]; then
rm -rf {{ shared_dir }}/{{ path }}
cp -RvfT {{ backup_dir }}/{{ filename }} {{ shared_dir }}/{{ path }}
fi
fi
@@ -394,7 +394,7 @@ public function testIncludesSharedFiles()
$shared = new Collection();
$file = m::mock(SharedFile::class);
$file->shouldReceive('getAttribute')->with('file')->andReturn('/directory/');
$file->shouldReceive('getAttribute')->with('file')->andReturn('/a/path/to/directory/');
$shared->push($file);
$file = m::mock(SharedFile::class);
@@ -412,32 +412,58 @@ public function testIncludesSharedFiles()
$this->project->shouldReceive('getAttribute')->with('configFiles')->andReturn(new Collection());
$this->project->shouldReceive('getAttribute')->with('sharedFiles')->andReturn($shared);
$this->parser->shouldReceive('parseFile')->with('deploy.MigrateShared', [
'deployment' => 12312,
'shared_dir' => '/var/www/shared',
'project_dir' => '/var/www',
'backup_dir' => '/var/www/shared.backup',
'migration' => '.deployer-migrated',
])->andReturn($script);
// Test directory
$this->parser->shouldReceive('parseFile')->with('deploy.ShareDirectory', [
'deployment' => 12312,
'target_file' => '/var/www/releases/20170110155645/directory',
'source_file' => '/var/www/shared/directory',
'shared_dir' => '/var/www/shared',
'release_dir' => '/var/www/releases/20170110155645',
'path' => 'a/path/to/directory',
'filename' => 'directory',
'backup_dir' => '/var/www/shared.backup',
'parent_dir' => 'a/path/to',
])->andReturn($script);
// Test file with extension
$this->parser->shouldReceive('parseFile')->with('deploy.ShareFile', [
'deployment' => 12312,
'target_file' => '/var/www/releases/20170110155645/config/file/config.yml',
'source_file' => '/var/www/shared/config.yml',
'shared_dir' => '/var/www/shared',
'release_dir' => '/var/www/releases/20170110155645',
'path' => 'config/file/config.yml',
'filename' => 'config.yml',
'backup_dir' => '/var/www/shared.backup',
'parent_dir' => 'config/file',
])->andReturn($script);
// Test file without extension
$this->parser->shouldReceive('parseFile')->with('deploy.ShareFile', [
'deployment' => 12312,
'target_file' => '/var/www/releases/20170110155645/config/file/README',
'source_file' => '/var/www/shared/README',
'shared_dir' => '/var/www/shared',
'release_dir' => '/var/www/releases/20170110155645',
'path' => 'config/file/README',
'backup_dir' => '/var/www/shared.backup',
'filename' => 'README',
'parent_dir' => 'config/file',
])->andReturn($script);
$this->parser->shouldReceive('parseFile')->with('deploy.MigrateSharedTimestamp', [
'release' => 20170110155645,
'shared_dir' => '/var/www/shared',
'migration' => '.deployer-migrated',
])->andReturn($script);
$this->process->shouldReceive('setScript')
->with('deploy.steps.InstallComposerDependencies', m::type('array')) // FIXME: Should check tokens
->andReturnSelf();
->with('deploy.steps.InstallComposerDependencies', m::type('array')) // FIXME: Should check tokens
->andReturnSelf();
$this->process->shouldReceive('prependScript')->with('')->andReturnSelf();
$this->process->shouldReceive('appendScript')->with(PHP_EOL . $script . $script . $script)->andReturnSelf();
$this->process->shouldReceive('appendScript')->with(PHP_EOL . $script . $script . $script . $script . $script)->andReturnSelf();
$this->deployment->shouldReceive('getAttribute')->with('user')->andReturnNull();
$this->deployment->shouldReceive('getAttribute')->with('is_webhook')->andReturn(false);

0 comments on commit 2c35581

Please sign in to comment.