Skip to content
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

Fix invalid paths on Windows #100

Merged
merged 4 commits into from
Apr 26, 2024
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
6 changes: 3 additions & 3 deletions src/Composer/ComposerPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ public function __construct(Composer $composer, array $overrideAutoload = null)
$this->relativePath = $this->packageName;
$this->packageAbsolutePath = realpath($vendorDirectory . DIRECTORY_SEPARATOR . $this->packageName) . DIRECTORY_SEPARATOR;
// If the package is symlinked, the path will be outside the working directory.
} elseif (0 !== strpos($absolutePath, getcwd()) && 1 === preg_match('/.*\/([^\/]*\/[^\/]*)\/[^\/]*/', $vendorDirectory, $output_array)) {
} elseif (0 !== strpos($absolutePath, getcwd()) && 1 === preg_match('/.*[\/\\\\]([^\/\\\\]*[\/\\\\][^\/\\\\]*)[\/\\\\][^\/\\\\]*/', $vendorDirectory, $output_array)) {
$this->relativePath = $output_array[1];
} elseif (1 === preg_match('/.*\/([^\/]*\/[^\/]*)\/composer.json/', $composerJsonFileAbsolute, $output_array)) {
// Not every package gets installed to a folder matching its name (crewlabs/unsplash).
} elseif (1 === preg_match('/.*[\/\\\\]([^\/\\\\]+[\/\\\\][^\/\\\\]+)[\/\\\\]composer.json/', $composerJsonFileAbsolute, $output_array)) {
Comment on lines +126 to +128
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BrianHenryIE Only now I realised why the relative paths were not created correctly: the regexes only worked for forward slash paths. Here is the fix to make it work with backslash paths also.

// Not every package gets installed to a folder matching its name (crewlabs/unsplash).
$this->relativePath = $output_array[1];
}

Expand Down
5 changes: 3 additions & 2 deletions src/FileEnumerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use BrianHenryIE\Strauss\Composer\ComposerPackage;
use BrianHenryIE\Strauss\Composer\Extra\StraussConfig;
use BrianHenryIE\Strauss\Helpers\Path;
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
use RecursiveDirectoryIterator;
Expand Down Expand Up @@ -125,7 +126,7 @@ public function compileFileList(): DiscoveredFiles
$this->addFile($dependency, $namespaceRelativePath, $type);
} elseif (is_dir($sourceAbsolutePath)) {
// trailingslashit(). (to remove duplicates).
$sourcePath = rtrim($sourceAbsolutePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
$sourcePath = Path::normalize($sourceAbsolutePath);

// $this->findFilesInDirectory()
$finder = new Finder();
Expand All @@ -139,7 +140,7 @@ public function compileFileList(): DiscoveredFiles
continue;
}

$namespaceRelativePath = rtrim($namespaceRelativePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
$namespaceRelativePath = Path::normalize($namespaceRelativePath);

$this->addFile(
$dependency,
Expand Down
11 changes: 11 additions & 0 deletions src/Helpers/Path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace BrianHenryIE\Strauss\Helpers;

class Path
{
public static function normalize(string $path): string
{
return rtrim(preg_replace('#[\\\/]+#', DIRECTORY_SEPARATOR, $path), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
}
Loading