Skip to content

Commit

Permalink
Merge pull request #92 from zlodes/dev-other-archive-extensions
Browse files Browse the repository at this point in the history
Added ability to use tar and tar.gz or any other skeleton archive format. Closes #91
  • Loading branch information
Jeroen-G committed Dec 22, 2019
2 parents 58b61d0 + dd409d4 commit 8aabcaa
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 29 deletions.
14 changes: 14 additions & 0 deletions src/ArchiveExtractors/Extractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace JeroenG\Packager\ArchiveExtractors;

abstract class Extractor
{
/**
* @param string $pathToArchive
* @param string $pathToDirectory
*
* @return void
*/
abstract public function extract(string $pathToArchive, string $pathToDirectory): void;
}
49 changes: 49 additions & 0 deletions src/ArchiveExtractors/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace JeroenG\Packager\ArchiveExtractors;

use Illuminate\Support\Arr;
use InvalidArgumentException;

final class Manager
{
/** @var Extractor[] */
protected $extractorsMap = [];

public function __construct()
{
$this->addExtractor('zip', new Zip())
->addExtractor('tar', new Tar())
->addExtractor('tar.gz', new TarGz());
}

/**
* @param string $archiveExtension
*
* @return Extractor
* @throws InvalidArgumentException
*/
public function getExtractor(string $archiveExtension): Extractor
{
$extractor = Arr::get($this->extractorsMap, $archiveExtension);

if (! $extractor) {
throw new InvalidArgumentException("There are no extractors for extension '{$archiveExtension}'!");
}

return $extractor;
}

/**
* @param string $archiveExtension
* @param Extractor $instance
*
* @return self
*/
public function addExtractor(string $archiveExtension, Extractor $instance): self
{
$this->extractorsMap[$archiveExtension] = $instance;

return $this;
}
}
17 changes: 17 additions & 0 deletions src/ArchiveExtractors/Tar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace JeroenG\Packager\ArchiveExtractors;

use PharData;

class Tar extends Extractor
{
/**
* {@inheritdoc}
*/
public function extract(string $pathToArchive, string $pathToDirectory): void
{
$archive = new PharData($pathToArchive);
$archive->extractTo($pathToDirectory);
}
}
35 changes: 35 additions & 0 deletions src/ArchiveExtractors/TarGz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace JeroenG\Packager\ArchiveExtractors;

use PharData;

class TarGz extends Extractor
{
/**
* {@inheritdoc}
*/
public function extract(string $pathToArchive, string $pathToDirectory): void
{
$pathToTarArchive = $this->extractTarPathFromGz($pathToArchive);
$archive = new PharData($pathToTarArchive);
$archive->extractTo($pathToDirectory);

unlink($pathToTarArchive);
}

/**
* Get the path to the tar within the gz folder.
*
* @param string $pathToArchive
* @return string
*/
private function extractTarPathFromGz(string $pathToArchive): string
{
$phar = new PharData($pathToArchive);
$phar->decompress();

// Remove .gz and return path.
return str_replace('.gz', '', $pathToArchive);
}
}
19 changes: 19 additions & 0 deletions src/ArchiveExtractors/Zip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace JeroenG\Packager\ArchiveExtractors;

use ZipArchive;

class Zip extends Extractor
{
/**
* {@inheritdoc}
*/
public function extract(string $pathToArchive, string $pathToDirectory): void
{
$archive = new ZipArchive;
$archive->open($pathToArchive);
$archive->extractTo($pathToDirectory);
$archive->close();
}
}
19 changes: 13 additions & 6 deletions src/Conveyor.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,20 @@ public function package($package = null)
*/
public function downloadSkeleton()
{
$this->download($zipFile = $this->makeFilename(), config('packager.skeleton'))
->extract($zipFile, $this->vendorPath())
->cleanUp($zipFile);
$skeletonArchiveUrl = config('packager.skeleton');
$extension = $this->getArchiveExtension($skeletonArchiveUrl);

$this->download($archive = $this->makeFilename($extension), $skeletonArchiveUrl)
->extract($archive, $this->tempPath())
->cleanUp($archive);

$firstInDirectory = scandir($this->vendorPath().'/temp')[2];
$extractedSkeletonLocation = $this->vendorPath().'/temp/'.$firstInDirectory;
$firstInDirectory = scandir($this->tempPath())[2];
$extractedSkeletonLocation = $this->tempPath().'/'.$firstInDirectory;
rename($extractedSkeletonLocation, $this->packagePath());
rmdir($this->vendorPath().'/temp');

if (is_dir($this->tempPath())) {
rmdir($this->tempPath());
}
}

/**
Expand All @@ -87,6 +93,7 @@ public function downloadFromGithub($origin, $piece, $branch)
$this->download($zipFile = $this->makeFilename(), $origin)
->extract($zipFile, $this->vendorPath())
->cleanUp($zipFile);

rename($this->vendorPath().'/'.$piece.'-'.$branch, $this->packagePath());
}

Expand Down
88 changes: 65 additions & 23 deletions src/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace JeroenG\Packager;

use GuzzleHttp\Client;
use JeroenG\Packager\ArchiveExtractors\Manager;
use RuntimeException;
use ZipArchive;

trait FileHandler
{
Expand All @@ -28,6 +28,16 @@ public function vendorPath()
return $this->packagesPath().'/'.$this->vendor();
}

/**
* Get the path to store a vendor's temporary files.
*
* @return string $path
*/
public function tempPath()
{
return $this->vendorPath().'/temp';
}

/**
* Get the full package path.
*
Expand All @@ -39,13 +49,15 @@ public function packagePath()
}

/**
* Generate a random temporary filename for the package zipfile.
* Generate a random temporary filename for the package archive file.
*
* @param string $extension
*
* @return string
*/
public function makeFilename()
public function makeFilename($extension = 'zip')
{
return getcwd().'/package'.md5(time().uniqid()).'.zip';
return getcwd().'/package'.md5(time().uniqid()).'.'.$extension;
}

/**
Expand Down Expand Up @@ -101,48 +113,48 @@ public function removeDir($path)
}

/**
* Download the temporary Zip to the given file.
* Download the archive to the given file by url.
*
* @param string $zipFile
* @param string $source
* @param string $filePath
* @param string $sourceFileUrl
* @return $this
*/
public function download($zipFile, $source)
public function download($filePath, $sourceFileUrl)
{
$client = new Client(['verify' => config('packager.curl_verify_cert')]);
$response = $client->get($source);
file_put_contents($zipFile, $response->getBody());
$response = $client->get($sourceFileUrl);
file_put_contents($filePath, $response->getBody());

return $this;
}

/**
* Extract the zip file into the given directory.
* Extract the downloaded archive into the given directory.
*
* @param string $zipFile
* @param string $directory
* @param string $archiveFilePath
* @param string $directory
* @return $this
*/
public function extract($zipFile, $directory)
public function extract($archiveFilePath, $directory)
{
$archive = new ZipArchive;
$archive->open($zipFile);
$archive->extractTo($directory);
$archive->close();
$extension = $this->getArchiveExtension($archiveFilePath);
$extractorManager = new Manager();
$extractor = $extractorManager->getExtractor($extension);
$extractor->extract($archiveFilePath, $directory);

return $this;
}

/**
* Clean-up the Zip file.
* Remove the archive.
*
* @param string $zipFile
* @param string $pathToArchive
* @return $this
*/
public function cleanUp($zipFile)
public function cleanUp($pathToArchive)
{
@chmod($zipFile, 0777);
@unlink($zipFile);
@chmod($pathToArchive, 0777);
@unlink($pathToArchive);

return $this;
}
Expand Down Expand Up @@ -173,6 +185,9 @@ public function renameFiles($manifest = null)
}
}

/**
* Remove the rules files if present.
*/
public function cleanUpRules()
{
$ruleFiles = ['rules.php', 'rewriteRules.php'];
Expand All @@ -183,4 +198,31 @@ public function cleanUpRules()
}
}
}

/**
* Based on the extension a different archive extractor is used.
*
* @param string $archiveFilePath
*
* @return string
*/
protected function getArchiveExtension($archiveFilePath): string
{
$pathParts = pathinfo($archiveFilePath);
$extension = $pathParts['extension'];

// Here we check if it actually is supposed to be .tar.gz/.tar.xz
if (in_array($extension, ['gz', 'xz'])) {
$subExtension = pathinfo($pathParts['filename'], PATHINFO_EXTENSION);

if ($subExtension) {
$extension = implode('.', [
$subExtension,
$extension,
]);
}
}

return $extension;
}
}
2 changes: 2 additions & 0 deletions src/PackagerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace JeroenG\Packager;

use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\ServiceProvider;

/**
Expand Down Expand Up @@ -34,6 +35,7 @@ class PackagerServiceProvider extends ServiceProvider

/**
* Bootstrap the application events.
* @throws BindingResolutionException
*/
public function boot()
{
Expand Down
28 changes: 28 additions & 0 deletions tests/SkeletonArchiveExtractorsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace JeroenG\Packager\Tests;

use Illuminate\Config\Repository;
use Illuminate\Support\Facades\Artisan;

class SkeletonArchiveExtractorsTest extends TestCase
{
public function test_new_package_is_created_with_tar_gz_skeleton()
{
Artisan::call('packager:new', ['vendor' => 'MyVendor', 'name' => 'MyPackage']);

$this->seeInConsoleOutput('Package created successfully!');
}

protected function getEnvironmentSetUp($app)
{
/** @var Repository $config */
$config = $app['config'];

// Change the extension in github archive url form .zip to .tar.gz
$originalZipUrl = $config->get('packager.skeleton');
$tarGzUrl = str_replace('.zip', '.tar.gz', $originalZipUrl);

$config->set('packager.skeleton', $tarGzUrl);
}
}

0 comments on commit 8aabcaa

Please sign in to comment.