Skip to content

Commit

Permalink
Remove is_dir() convenience in ProjectComposerPackage
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Jan 18, 2025
1 parent 1aa29db commit ca76d96
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 38 deletions.
13 changes: 10 additions & 3 deletions src/Composer/Extra/StraussConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class StraussConfig implements
*
* @var ?string[]
*/
protected ?array $updateCallSites = null;
protected ?array $updateCallSites = array();

/**
* Packages to copy and (maybe) prefix.
Expand Down Expand Up @@ -398,9 +398,16 @@ public function getUpdateCallSites(): ?array
/**
* @param string[]|null $updateCallSites
*/
public function setUpdateCallSites(?array $updateCallSites): void
public function setUpdateCallSites($updateCallSites): void
{
$this->updateCallSites = $updateCallSites;
if (is_array($updateCallSites) && count($updateCallSites) === 1 && $updateCallSites[0] === true) {
// Setting `null` instructs Strauss to update call sites in the project's autoload key.
$this->updateCallSites = null;
} elseif (is_array($updateCallSites) && count($updateCallSites) === 1 && $updateCallSites[0] === false) {
$this->updateCallSites = array();
} else {
$this->updateCallSites = $updateCallSites;
}
}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/Composer/ProjectComposerPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ class ProjectComposerPackage extends ComposerPackage
* @param string $absolutePath
* @param ?array{files?:array<string>,classmap?:array<string>,"psr-4"?:array<string,string|array<string>>} $overrideAutoload
*/
public function __construct(string $absolutePath, ?array $overrideAutoload = null)
public function __construct(string $absolutePathFile, ?array $overrideAutoload = null)
{
$absolutePathFile = is_dir($absolutePath)
? rtrim($absolutePath, DIRECTORY_SEPARATOR) . '/composer.json'
: $absolutePath;
unset($absolutePath);

$composer = Factory::create(new NullIO(), $absolutePathFile, true);

parent::__construct($composer, $overrideAutoload);
Expand Down
16 changes: 8 additions & 8 deletions src/Console/Commands/DependenciesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected function loadProjectComposerPackage(): void
{
$this->logger->info('Loading package...');

$this->projectComposerPackage = new ProjectComposerPackage($this->workingDir);
$this->projectComposerPackage = new ProjectComposerPackage($this->workingDir . 'composer.json');

// TODO: Print the config that Strauss is using.
// Maybe even highlight what is default config and what is custom config.
Expand Down Expand Up @@ -293,14 +293,14 @@ protected function performReplacementsInComposerFiles(): void
$this->filesystem
);

$files = $fileEnumerator->compileFileListForPaths([$this->config->getVendorDirectory() . 'composer']);
$files = $fileEnumerator->compileFileListForPaths([$this->config->getVendorDirectory()]);

$composerPhpFileRelativePaths = array_map(
fn($file) => $file->getSourcePath($this->workingDir . $this->config->getVendorDirectory()),
$composerPhpFilePaths = array_map(
fn($file) => $file->getSourcePath(),
$files->getFiles()
);

$projectReplace->replaceInProjectFiles($this->discoveredSymbols, $composerPhpFileRelativePaths);
$projectReplace->replaceInProjectFiles($this->discoveredSymbols, $composerPhpFilePaths);
}

protected function performReplacementsInProjectFiles(): void
Expand Down Expand Up @@ -328,15 +328,15 @@ protected function performReplacementsInProjectFiles(): void

$phpFiles = $fileEnumerator->compileFileListForPaths($callSitePaths);

$phpFilesRelativePaths = array_map(
fn($file) => $file->getSourcePath($this->workingDir),
$phpFilesAbsolutePaths = array_map(
fn($file) => $file->getSourcePath(),
$phpFiles->getFiles()
);

// TODO: Warn when a file that was specified is not found
// $this->logger->warning('Expected file not found from project autoload: ' . $absolutePath);

$projectReplace->replaceInProjectFiles($this->discoveredSymbols, $phpFilesRelativePaths);
$projectReplace->replaceInProjectFiles($this->discoveredSymbols, $phpFilesAbsolutePaths);
}

protected function writeClassAliasMap(): void
Expand Down
14 changes: 7 additions & 7 deletions src/Console/Commands/ReplaceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,17 @@ protected function performReplacementsInProjectFiles(ReplaceConfigInterface $con
$this->filesystem
);

$phpFiles = $fileEnumerator->compileFileListForPaths($callSitePaths);

$phpFilesRelativePaths = array_map(
fn($file) => $file->getSourcePath($this->workingDir),
$phpFiles->getFiles()
);
$phpFilePaths = $fileEnumerator->compileFileListForPaths($callSitePaths);

// TODO: Warn when a file that was specified is not found (during config validation).
// $this->logger->warning('Expected file not found from project autoload: ' . $absolutePath);

$projectReplace->replaceInProjectFiles($this->discoveredSymbols, $phpFilesRelativePaths);
$phpFilesAbsolutePaths = array_map(
fn($file) => $file->getSourcePath(),
$phpFilePaths->getFiles()
);

$projectReplace->replaceInProjectFiles($this->discoveredSymbols, $phpFilesAbsolutePaths);
}


Expand Down
3 changes: 3 additions & 0 deletions src/Helpers/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public function findAllFilesAbsolutePaths(string $workingDir, array $relativeFil

public function isDir(string $path): bool
{
if (strpos($path, '/.') === strlen($path) - 2) {
$path = rtrim($path, '.');
}
$attributes = $this->getAttributes($path);
return $attributes instanceof DirectoryAttributes;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Pipeline/FileEnumerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ public function compileFileListForDependencies(array $dependencies): DiscoveredF
foreach ($namespace_relative_paths as $namespaceRelativePath) {
$sourceAbsolutePath = $dependency->getPackageAbsolutePath() . $namespaceRelativePath;

if (is_file($sourceAbsolutePath)) {
$this->addFileWithDependency($dependency, $namespaceRelativePath, $type);
} elseif ($this->filesystem->isDir($sourceAbsolutePath)) {
if ($this->filesystem->isDir($sourceAbsolutePath)) {
// trailingslashit(). (to remove duplicates).
$sourcePath = Path::normalize($sourceAbsolutePath);

Expand All @@ -145,6 +143,8 @@ public function compileFileListForDependencies(array $dependencies): DiscoveredF
$type
);
}
} else {
$this->addFileWithDependency($dependency, $namespaceRelativePath, $type);
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/Pipeline/Prefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,30 @@ public function replaceInFiles(DiscoveredSymbols $discoveredSymbols, array $file

/**
* @param DiscoveredSymbols $discoveredSymbols
* @param string[] $relativeFilePaths
* @param string[] $absoluteFilePathsArray
*
* @return void
* @throws FilesystemException
*/
public function replaceInProjectFiles(DiscoveredSymbols $discoveredSymbols, array $relativeFilePaths): void
public function replaceInProjectFiles(DiscoveredSymbols $discoveredSymbols, array $absoluteFilePathsArray): void
{
foreach ($relativeFilePaths as $workingDirRelativeFilepath) {
if (! $this->filesystem->fileExists($this->workingDir . $workingDirRelativeFilepath)) {
foreach ($absoluteFilePathsArray as $workingDirRelativeFilepath) {
$fileAbsolutePath = 0 === strpos($workingDirRelativeFilepath, $this->workingDir)
? $workingDirRelativeFilepath
: $this->workingDir . $workingDirRelativeFilepath;

if (! $this->filesystem->fileExists($fileAbsolutePath)) {
continue;
}

// Throws an exception, but unlikely to happen.
$contents = $this->filesystem->read($this->workingDir . $workingDirRelativeFilepath);
$contents = $this->filesystem->read($fileAbsolutePath);

$updatedContents = $this->replaceInString($discoveredSymbols, $contents);

if ($updatedContents !== $contents) {
$this->changedFiles[ $workingDirRelativeFilepath ] = null;
$this->filesystem->write($this->workingDir . $workingDirRelativeFilepath, $updatedContents);
$this->filesystem->write($fileAbsolutePath, $updatedContents);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/CopierIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testsPrepareTarget()

exec('composer install');

$projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir);
$projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir . 'composer.json');

$dependencies = array_map(function ($element) {
$composerFile = $this->testsWorkingDir . 'vendor'. DIRECTORY_SEPARATOR . $element . '/composer.json';
Expand Down Expand Up @@ -110,7 +110,7 @@ public function testsCopy()

exec('composer install');

$projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir);
$projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir . 'composer.json');

$dependencies = array_map(function ($element) {
$composerFile = $this->testsWorkingDir . 'vendor'. DIRECTORY_SEPARATOR . $element . '/composer.json';
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/FileEnumeratorIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testBuildFileList()

exec('composer install');

$projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir);
$projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir . 'composer.json');

// Only one because we haven't run "flat dependency list".
$dependencies = array_map(function ($element) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/FileScannerIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testOne()

exec('composer install');

$projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir);
$projectComposerPackage = new ProjectComposerPackage($this->testsWorkingDir . 'composer.json');

$dependencies = array_map(function ($element) {
$composerFile = $this->testsWorkingDir . 'vendor'. DIRECTORY_SEPARATOR . $element . '/composer.json';
Expand Down
3 changes: 2 additions & 1 deletion tests/Issues/StraussIssue108Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public function test_correct_directory_permission()
"."
]
}
}
},
"update_call_sites": true
}
}
}
Expand Down

0 comments on commit ca76d96

Please sign in to comment.