Skip to content

Commit

Permalink
fix(scaffolder): Fix scaffolding files from within the phar-archive.
Browse files Browse the repository at this point in the history
  • Loading branch information
stmh committed Sep 2, 2022
1 parent 8967400 commit 1b9c4a8
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 12 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1",
"drupal/coder": "^8.3",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-symfony": "^1.0"
"phpstan/phpstan-symfony": "^1.0",
"phpspec/prophecy-phpunit": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
54 changes: 53 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Configuration/ConfigurationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public function resolveRelativeInheritanceRefs(
$item = $child->getValue();

// Skip urls and absolute paths:
if ($item[0] === '/' || Utilities::isHttpUrl($item)) {
if ($item[0] === '/' || Utilities::isHttpUrl($item) || Utilities::isPharUrl($item)) {
continue;
}

Expand Down Expand Up @@ -549,7 +549,7 @@ public function readHttpResource(string $resource)
if (!$this->offlineMode) {
try {
$this->logger->info(sprintf('Read remote file from `%s`', $resource));
$url = parse_url($resource);
$url = Utilities::parseUrl($resource);
$url['path'] = urlencode($url['path']);
$url['path'] = str_replace('%2F', '/', $url['path']);
$resource = http_build_url($url);
Expand Down
2 changes: 1 addition & 1 deletion src/Method/ResticMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ protected function ensureKnownHosts(
): void {
$repository = $host_config['restic']['repository'];
if (substr($repository, 0, 5) == 'sftp:') {
$a = parse_url($repository);
$a = Utilities::parseUrl($repository);
$known_hosts = [
sprintf("%s:%d", $a['host'], $a['port'] ?? 22)
];
Expand Down
4 changes: 3 additions & 1 deletion src/Scaffolder/Scaffolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public function scaffold(
$root_path = dirname($url);
try {
if (!Utilities::isHttpUrl($url)) {
$fullpath = realpath($url);
$is_phar = Utilities::isPharUrl($url);
$fullpath = $is_phar ? $url : realpath($url);
if (empty($fullpath)) {
throw new \RuntimeException(sprintf('Could not find file at `%s`!', $url));
}
Expand Down Expand Up @@ -384,6 +385,7 @@ public function getLocalScaffoldFile($name)
? Phar::running() . '/config/scaffold'
: realpath(__DIR__ . '/../../config/scaffold/');

print_r($rootFolder);
return $rootFolder . '/' . $name;
}

Expand Down
29 changes: 27 additions & 2 deletions src/Utilities/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,9 +603,26 @@ public static function getTempFileName(HostConfig $host_config, $str): string
basename($str);
}

/**
* Custom parse_url implementation, as parse_url does not support phar-scheme.
*/
public static function parseUrl($url)
{
if (self::isPharUrl($url)) {
return [
'scheme' => 'phar',
'path' => str_replace('phar://', '//', $url),
];
}
return parse_url($url);
}


public static function resolveRelativePaths(string $url): string
{
$result = parse_url($url);
$result = self::parseUrl($url);
print_r($url);
print_r($result);
$filename = $result['path'];
$path = [];
$parts = explode('/', $filename);
Expand Down Expand Up @@ -645,7 +662,7 @@ public static function buildUrl($components)
if (!empty($components['username']) && !empty($components['password'])) {
$url .= $components['username'] . ':' . $components['password'] . '@';
}
if (!empty($components['scheme'])) {
if (!empty($components['host'])) {
$url .= $components['host'];
}
if (!empty($components['port'])) {
Expand All @@ -670,4 +687,12 @@ public static function isHttpUrl($url): bool
{
return (substr($url, 0, 4) === 'http') && (strpos($url, '://') !== false);
}

/**
* Returns true if url is a phar url.
*/
public static function isPharUrl($url): bool
{
return (substr($url, 0, 4) === 'phar') && (strpos($url, '://') !== false);
}
}
3 changes: 3 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
"phpspec/prophecy": {
"version": "1.8.0"
},
"phpspec/prophecy-phpunit": {
"version": "v2.0.1"
},
"phpstan/phpdoc-parser": {
"version": "1.2.0"
},
Expand Down
18 changes: 18 additions & 0 deletions tests/UtilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,22 @@ public function testArgumentsParsing()
$args = Utilities::parseArguments("password=aFQd=BDq_ys9j72frDgM");
$this->assertEquals("aFQd=BDq_ys9j72frDgM", $args['password']);
}

public function testRelativePharUrls()
{
$url = 'phar:///usr/local/bin/phab/config/scaffold/mbb/./mbb-base.yml';
$this->assertEquals(
'phar:///usr/local/bin/phab/config/scaffold/mbb/mbb-base.yml',
Utilities::resolveRelativePaths($url)
);
}

public function testRelativeFileUrls()
{
$url = 'file:///usr/local/bin/phab/config/scaffold/mbb/./mbb-base.yml';
$this->assertEquals(
'file:///usr/local/bin/phab/config/scaffold/mbb/mbb-base.yml',
Utilities::resolveRelativePaths($url)
);
}
}
21 changes: 17 additions & 4 deletions tests/WorkspaceCreateUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,45 @@
use Phabalicious\Command\WorkspaceCreateCommand;
use Phabalicious\Command\WorkspaceUpdateCommand;
use Phabalicious\Configuration\ConfigurationService;
use Phabalicious\Configuration\Storage\Node;
use Phabalicious\Method\LocalMethod;
use Phabalicious\Method\MethodFactory;
use Phabalicious\Method\ScriptMethod;
use Phabalicious\Method\TaskContext;
use Phabalicious\Method\TaskContextInterface;
use Phabalicious\Scaffolder\Options;
use Phabalicious\Scaffolder\Scaffolder;
use Phabalicious\Utilities\Utilities;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Yaml\Yaml;

class WorkspaceCreateUpdateTest extends PhabTestCase
{

use ProphecyTrait;

/** @var Application */
protected $application;

protected $configuration;

public function setup(): void
{
$this->application = new Application();
$this->application->setVersion(Utilities::FALLBACK_VERSION);
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();

$configuration = new ConfigurationService($this->application, $logger);
$method_factory = new MethodFactory($configuration, $logger);
$this->configuration = new ConfigurationService($this->application, $logger);
$method_factory = new MethodFactory($this->configuration, $logger);
$method_factory->addMethod(new LocalMethod($logger));
$method_factory->addMethod(new ScriptMethod($logger));

$this->application->add(new WorkspaceCreateCommand($configuration, $method_factory));
$this->application->add(new WorkspaceUpdateCommand($configuration, $method_factory));
$this->application->add(new WorkspaceCreateCommand($this->configuration, $method_factory));
$this->application->add(new WorkspaceUpdateCommand($this->configuration, $method_factory));
}

private function prepareTarget()
Expand Down

0 comments on commit 1b9c4a8

Please sign in to comment.