Skip to content
This repository has been archived by the owner on May 31, 2021. It is now read-only.

handle shebang #93

Merged
merged 1 commit into from Jun 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/SensioLabs/Melody/Handler/FileHandler.php
Expand Up @@ -36,6 +36,11 @@ public function createResource($filename)
sprintf('file://%s', realpath($filename))
);

return new LocalResource($filename, file_get_contents($filename), $metadata);
$content = file_get_contents($filename);
if ('#!' === substr($content, 0, 2)) {
$content = explode("\n", $content."\n", 2)[1];
}

return new LocalResource($filename, $content, $metadata);
}
}
23 changes: 2 additions & 21 deletions src/SensioLabs/Melody/Runner/Runner.php
Expand Up @@ -27,11 +27,7 @@ public function __construct($vendorDir = 'vendor')

public function getProcess(Script $script, $dir)
{
if ($script->getResource() instanceof LocalResource) {
$bootstrap = $this->getLocalBootstrap($script->getResource());
} else {
$bootstrap = $this->getRemoteBootstrap($script->getResource());
}
$bootstrap = $this->getBootstrap($script->getResource());

$file = sprintf('%s/%s', $dir, self::BOOTSTRAP_FILENAME);

Expand Down Expand Up @@ -62,22 +58,7 @@ public function getProcess(Script $script, $dir)
return $process;
}

private function getLocalBootstrap(LocalResource $resource)
{
$template = <<<'TEMPLATE'
{{ head }}

require '{{ script_filename }}';

TEMPLATE;

return strtr($template, array(
'{{ head }}' => $this->getHead(),
'{{ script_filename }}' => $resource->getFilename(),
));
}

private function getRemoteBootstrap(Resource $resource)
private function getBootstrap(Resource $resource)
{
$template = <<<TEMPLATE
{{ head }}
Expand Down
2 changes: 1 addition & 1 deletion src/SensioLabs/Melody/Tests/Fixtures/shebang.php
@@ -1,4 +1,4 @@
#!/usr/local/bin/melody run
#!/usr/bin/env -S melody run
<?php
<<<CONFIG
packages:
Expand Down
10 changes: 10 additions & 0 deletions src/SensioLabs/Melody/Tests/Handler/FileHandlerTest.php
Expand Up @@ -45,4 +45,14 @@ public function testCreateResource()
$this->assertInstanceOf('SensioLabs\Melody\Resource\Resource', $resource);
$this->assertSame(file_get_contents($filename), $resource->getContent());
}

public function testCreatedResourceDontContainsShebang()
{
$filename = __DIR__.'/../Fixtures/shebang.php';

$resource = $this->handler->createResource($filename);

$this->assertInstanceOf('SensioLabs\Melody\Resource\Resource', $resource);
$this->assertNotRegExp('/^#![^\n]+\n/u', $resource->getContent());
}
}
1 change: 1 addition & 0 deletions src/SensioLabs/Melody/Tests/IntegrationTest.php
Expand Up @@ -39,6 +39,7 @@ public function testRunWithShebang()
$this->assertContains('Loading composer repositories with package information', $output);
$this->assertContains('Updating dependencies (including require-dev)', $output);
$this->assertContains('Installing twig/twig (v1.16.0)', $output);
$this->assertNotContains('#!/usr/bin/env -S melody run', $output);
$this->assertContains('Hello world', $output);
}

Expand Down