Skip to content

Commit

Permalink
Merge pull request #100 from szaleq/fix/paths-on-windows
Browse files Browse the repository at this point in the history
Fix invalid paths on Windows
  • Loading branch information
BrianHenryIE authored Apr 26, 2024
2 parents a88e1a7 + e135480 commit 7df991f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
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)) {
// 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;
}
}

0 comments on commit 7df991f

Please sign in to comment.