Skip to content

Add output dir configuration option #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 12, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ potentially very difficult to debug due to dissimilar or unsupported package ver
- [Usage](#usage)
- [Configuration](docs/configuration.md#configuration)
- [Prefix](docs/configuration.md#prefix)
- [Output directory](docs/configuration.md#output-directory)
- [Finders and paths](docs/configuration.md#finders-and-paths)
- [Patchers](docs/configuration.md#patchers)
- [Excluded files](docs/configuration.md#excluded-files)
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
"psr-4": {
"Humbug\\PhpScoper\\": "src/"
},
"classmap": [
"vendor-hotfix/"
],
"files": [
"src/functions.php"
]
Expand Down
11 changes: 11 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Configuration

- [Prefix](#prefix)
- [Output directory](#output-directory)
- [Finders and paths](#finders-and-paths)
- [Patchers](#patchers)
- [Excluded files](#excluded-files)
Expand All @@ -25,6 +26,7 @@ use Isolated\Symfony\Component\Finder\Finder;

return [
'prefix' => null, // string|null
'output-dir' => null, // string|null
'finders' => [], // list<Finder>
'patchers' => [], // list<callable(string $filePath, string $prefix, string $contents): string>

Expand Down Expand Up @@ -52,6 +54,15 @@ The prefix to be used to isolate the code. If `null` or `''` (empty string) is g
then a random prefix will be automatically generated.


### Output directory

The base output directory where the prefixed files will be generated. If `null`
is given, `build` is used.

This setting will be overridden by the command line option of the same name if
present.


### Finders and paths

By default, when running `php-scoper add-prefix`, it will prefix all relevant
Expand Down
16 changes: 14 additions & 2 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ final class Configuration
private readonly string $prefix;

/**
* @param non-empty-string|null $path Absolute path to the configuration file loaded.
* @param non-empty-string $prefix The prefix applied.
* @param non-empty-string|null $path Absolute canonical path to the configuration file loaded.
* @param non-empty-string|null $outputDir Absolute canonical path to the output directory.
* @param non-empty-string $prefix The prefix applied.
* @param array<string, array{string, string}> $filesWithContents Array of tuple with the
* first argument being the file path and the second
* its contents
Expand All @@ -40,13 +41,15 @@ final class Configuration
*/
public function __construct(
private ?string $path,
private ?string $outputDir,
string $prefix,
private array $filesWithContents,
private array $excludedFilesWithContents,
private Patcher $patcher,
private SymbolsConfiguration $symbolsConfiguration
) {
self::validatePrefix($prefix);

$this->prefix = $prefix;
}

Expand All @@ -58,13 +61,22 @@ public function getPath(): ?string
return $this->path;
}

/**
* @return non-empty-string|null Absolute canonical path
*/
public function getOutputDir(): ?string
{
return $this->outputDir;
}

/**
* @param non-empty-string $prefix
*/
public function withPrefix(string $prefix): self
{
return new self(
$this->path,
$this->outputDir,
$prefix,
$this->filesWithContents,
$this->excludedFilesWithContents,
Expand Down
14 changes: 14 additions & 0 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function create(?string $path = null, array $paths = []): Configuration
self::validateConfigKeys($config);

$prefix = self::retrievePrefix($config);
$outputDir = self::retrieveOutputDir($config);

$excludedFiles = null === $path
? []
Expand All @@ -99,6 +100,7 @@ public function create(?string $path = null, array $paths = []): Configuration

return new Configuration(
$path,
$outputDir,
$prefix,
$filesWithContents,
self::retrieveFilesWithContents($excludedFiles),
Expand All @@ -122,6 +124,7 @@ public function createWithPaths(Configuration $config, array $paths): Configurat

return new Configuration(
$config->getPath(),
$config->getOutputDir(),
$config->getPrefix(),
[
...$config->getFilesWithContents(),
Expand All @@ -139,6 +142,7 @@ public function createWithPrefix(Configuration $config, string $prefix): Configu

return new Configuration(
$config->getPath(),
$config->getOutputDir(),
$prefix,
$config->getFilesWithContents(),
$config->getExcludedFilesWithContents(),
Expand Down Expand Up @@ -226,6 +230,16 @@ private static function retrievePrefix(array $config): string
return '' === $prefix ? self::generateRandomPrefix() : $prefix;
}

/**
* @return non-empty-string|null
*/
private static function retrieveOutputDir(array $config): ?string
{
$outputDir = trim((string) ($config[ConfigurationKeys::OUTPUT_DIR_KEYWORD] ?? ''));

return '' === $outputDir ? null : $outputDir;
}

/**
* @return array<(callable(string,string,string): string)|Patcher>
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Configuration/ConfigurationKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
final class ConfigurationKeys
{
public const PREFIX_KEYWORD = 'prefix';
public const OUTPUT_DIR_KEYWORD = 'output-dir';
public const EXCLUDED_FILES_KEYWORD = 'exclude-files';
public const FINDER_KEYWORD = 'finders';
public const PATCHERS_KEYWORD = 'patchers';
Expand All @@ -38,6 +39,7 @@ final class ConfigurationKeys

public const KEYWORDS = [
self::PREFIX_KEYWORD,
self::OUTPUT_DIR_KEYWORD,
self::EXCLUDED_FILES_KEYWORD,
self::FINDER_KEYWORD,
self::PATCHERS_KEYWORD,
Expand Down
25 changes: 16 additions & 9 deletions src/Console/Command/AddPrefixCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ final class AddPrefixCommand implements Command, CommandAware
private const STOP_ON_FAILURE_OPT = 'stop-on-failure';
private const CONFIG_FILE_OPT = 'config';
private const NO_CONFIG_OPT = 'no-config';

private const DEFAULT_OUTPUT_DIR = 'build';

private bool $init = false;

public function __construct(
Expand Down Expand Up @@ -90,7 +93,6 @@ public function getConfiguration(): CommandConfiguration
'o',
InputOption::VALUE_REQUIRED,
'The output directory in which the prefixed code will be dumped.',
'build',
),
new InputOption(
self::FORCE_OPT,
Expand Down Expand Up @@ -134,12 +136,14 @@ public function execute(IO $io): int
$cwd = getcwd();

$paths = $this->getPathArguments($io, $cwd);
$outputDir = $this->getOutputDir($io, $cwd);
$config = $this->retrieveConfig($io, $paths, $cwd);

$outputDir = $this->canonicalizePath(
$this->getOutputDir($io, $config),
$cwd,
);
$this->checkOutputDir($io, $outputDir);

$config = $this->retrieveConfig($io, $paths, $cwd);

$this->getScoper()->scope(
$io,
$config,
Expand All @@ -154,12 +158,15 @@ public function execute(IO $io): int
/**
* @return non-empty-string
*/
private function getOutputDir(IO $io, string $cwd): string
private function getOutputDir(IO $io, Configuration $configuration): string
{
return $this->canonicalizePath(
$io->getOption(self::OUTPUT_DIR_OPT)->asString(),
$cwd,
);
$commandOutputDir = $io->getOption(self::OUTPUT_DIR_OPT)->asString();

if ('' !== $commandOutputDir) {
return $commandOutputDir;
}

return $configuration->getOutputDir() ?? self::DEFAULT_OUTPUT_DIR;
}

private function checkOutputDir(IO $io, string $outputDir): void
Expand Down
6 changes: 5 additions & 1 deletion src/scoper.inc.php.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ use Isolated\Symfony\Component\Finder\Finder;
// the PHP-Scoper analysis.

return [
// The prefix configuration. If a non null value is be used, a random prefix
// The prefix configuration. If a non-null value is used, a random prefix
// will be generated instead.
//
// For more see: https://github.com/humbug/php-scoper/blob/master/docs/configuration.md#prefix
'prefix' => null,

// The base output directory for the prefixed files.
// This will be overridden by the 'output-dir' command line option if present.
'output-dir' => null,

// By default when running php-scoper add-prefix, it will prefix all relevant code found in the current working
// directory. You can however define which files should be scoped by defining a collection of Finders in the
// following configuration key.
Expand Down
2 changes: 2 additions & 0 deletions tests/Configuration/ConfigurationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function test_it_can_create_a_complete_configuration(): void

return [
'prefix' => 'MyPrefix',
'output-dir' => 'dist',
'exclude-files' => ['file1', 'file2'],
'patchers' => [],
'finders' => [],
Expand Down Expand Up @@ -127,6 +128,7 @@ public function test_it_can_create_a_complete_configuration(): void

self::assertSame($this->tmp.DIRECTORY_SEPARATOR.'scoper.inc.php', $configuration->getPath());
self::assertSame('MyPrefix', $configuration->getPrefix());
self::assertSame('dist', $configuration->getOutputDir());
self::assertSame([], $configuration->getFilesWithContents());
self::assertSame(
[
Expand Down
6 changes: 5 additions & 1 deletion tests/Configuration/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function test_it_validates_the_prefix(
$this->expectExceptionMessage($expectedExceptionMessage);

new Configuration(
null,
null,
$prefix,
[],
Expand All @@ -53,6 +54,7 @@ public function test_it_can_create_a_new_instance_with_a_different_prefix(): voi
{
$values = [
'/path/to/config',
'/path/to/outputDir',
'initialPrefix',
['/path/to/fileA' => ['/path/to/fileA', 'fileAContent']],
['/path/to/fileB' => ['/path/to/fileB', 'fileBContent']],
Expand All @@ -68,7 +70,7 @@ public function test_it_can_create_a_new_instance_with_a_different_prefix(): voi
$newConfig = $config->withPrefix('newPrefix');

$expectedNewConfigValues = $values;
$expectedNewConfigValues[1] = 'newPrefix';
$expectedNewConfigValues[2] = 'newPrefix';

self::assertStateIs($config, ...$values);
self::assertStateIs($newConfig, ...$expectedNewConfigValues);
Expand All @@ -90,13 +92,15 @@ public static function prefixProvider(): iterable
private static function assertStateIs(
Configuration $configuration,
?string $expectedPath,
?string $expectedOutputDir,
string $expectedPrefix,
array $expectedFilesWithContents,
array $expectedExcludedFilesWithContents,
Patcher $expectedPatcher,
SymbolsConfiguration $expectedSymbolsConfiguration
): void {
self::assertSame($expectedPath, $configuration->getPath());
self::assertSame($expectedOutputDir, $configuration->getOutputDir());
self::assertSame($expectedPrefix, $configuration->getPrefix());
self::assertEqualsCanonicalizing(
$expectedFilesWithContents,
Expand Down
1 change: 1 addition & 0 deletions tests/Scoper/ScoperFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function test_it_can_create_a_scoper(): void

$factory->createScoper(
new Configuration(
null,
null,
'_Humbug',
[],
Expand Down
Loading