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

TASK: Script: Rename all import style from "./style.css" to style.module.css #3217

Merged
merged 4 commits into from
Mar 8, 2023

Conversation

mhsdesign
Copy link
Member

@mhsdesign mhsdesign commented Oct 9, 2022

Currently any build stack gets confused by our "normal" style.css files, as they are not named style.module.css.
Sure we can work against that but its clearly better to stick to the conventions

Renamed "108" files
Replaced "124" imports

FYI the build works perfectly ;)

No worries it has been done by a script - and can be done again ^^

Show me the wizard who did this:

<?php

/*
 * Script to rename all import style from "./style.css" to style.module.css
 *
 * !!! Operates on ./packages in the current __DIR__
 *
 * Build for linux - should work on Mac - don't even try it on windoofs
 */

function renameStrategy(string $filename, string $extension): string {
    return $filename . '.module.css';
}

(function () {
    die("enable me");

    $regex = <<<'REGEX'
    /import[ ]+[a-zA-Z0-9]+[ ]+from[ ]+["'](?P<imported>.*\.css)["']/
    REGEX;

    $fileIterator = recursiveIterator(__DIR__ . "/packages");

    /**
     * Indexed by realpath of 'from' to rename. Value is 'to' be renamed to.
     * @var array<string,string> $filesToRename
     */
    $filesToRename = [];

    $replacedImports = 0;

    foreach (iterateOverFilesAndSelectByFileEnding($fileIterator, [".js", ".ts"]) as $file) {
        $contents = file_get_contents($file);

        $currentFileDir = dirname($file);

        $newContents = preg_replace_callback($regex, function ($matches) use ($currentFileDir, $file, &$filesToRename, &$replacedImports) {
            $rawImported = $matches["imported"];

            if (str_starts_with($rawImported, "@neos-project/")) {
                // package import `@neos-project/neos-ui-guest-frame/src/style.css`
                $import = substr_replace($rawImported, __DIR__ . "/packages/", 0, \strlen("@neos-project/"));
            } else {
                // relative import `../../style.css`
                $import = $currentFileDir . '/' . $rawImported;
            }

            if (!file_exists($import)) {
                echo <<<MSG
                --------
                import '$rawImported';
                File $import does not exist or this simplified dependency resolution does not work.
                It was imported in $file
                --------
                MSG;
                return $matches[0];
            }

            [
                'basename' => $basename,
                'dirname' => $dirname,
                'extension' => $extension,
                'filename' => $filename
            ] = pathinfo($import);

            $newFileNameWithExtension = renameStrategy($filename, $extension);
            $newFilePath = $dirname . '/' . $newFileNameWithExtension;


            $importRealPath = realpath($import);

            if (isset($filesToRename[$importRealPath]) === false) {
                $filesToRename[$importRealPath] = $newFilePath;
            }

            $replacedImports++;

            return str_replace($basename, $newFileNameWithExtension, $matches[0]);
        }, $contents, -1, $count);

        if ($count) {
            $success = file_put_contents($file, $newContents);
            if ($success === false) {
                echo <<<MSG
                --------
                $count could not be written to  $file
                --------
                MSG;
            }
        }
    }

    $renamedFiles = 0;
    foreach ($filesToRename as $from => $to) {
        $success = rename($from, $to);
        if (!$success) {
            echo <<<MSG
            --------
            Rename from $from to $to failed
            --------
            MSG;
        }
        $renamedFiles ++;
    }

    echo <<<MSG
    --------
    Renamed "$renamedFiles" files
    Replaced "$replacedImports" imports
    --------
    MSG;
})();

function recursiveIterator(string $basePath): \Iterator {
    $recursiveDirectoryIterator = new \RecursiveDirectoryIterator($basePath);
    return new \RecursiveIteratorIterator($recursiveDirectoryIterator);
}

/**
 * @param \Iterator|\SplFileInfo[] $fileIterator
 */
function iterateOverFilesAndSelectByFileEnding(\Iterator $fileIterator, array $fileEndings): \Generator {
    foreach ($fileIterator as $fileInfo) {
        if ($fileInfo->isDir()) {
            continue;
        }
        if (str_contains($fileInfo->getPath(), "/node_modules/")) {
            continue;
        }
        if (str_contains($fileInfo->getPath(), "/dist/")) {
            continue;
        }
        $pathAndFilename = $fileInfo->getPathname();
        foreach ($fileEndings as $ending) {
            if (str_ends_with($pathAndFilename, $ending)) {
                yield $pathAndFilename;
            }
        }
    }
}

@mhsdesign mhsdesign mentioned this pull request Oct 9, 2022
54 tasks
@mhsdesign
Copy link
Member Author

mhsdesign commented Oct 19, 2022

Must be regenerated

@mhsdesign mhsdesign changed the title TASK: Rename all import style from "./style.css" to style.module.scss TASK: Script: Rename all import style from "./style.css" to style.module.scss Oct 19, 2022
@crydotsnake crydotsnake added the Task Stale Something that would be cool to clean up but is not beeing done label Nov 16, 2022
Base automatically changed from feature/esbuild to 8.2 November 24, 2022 19:45
@mhsdesign mhsdesign changed the base branch from 8.2 to 8.3 December 2, 2022 08:55
@mhsdesign mhsdesign marked this pull request as draft February 14, 2023 20:32
@mhsdesign mhsdesign changed the title TASK: Script: Rename all import style from "./style.css" to style.module.scss TASK: Script: Rename all import style from "./style.css" to style.module.css Feb 20, 2023
@mhsdesign mhsdesign mentioned this pull request Feb 20, 2023
36 tasks
@mhsdesign mhsdesign removed the Task Stale Something that would be cool to clean up but is not beeing done label Feb 20, 2023
@mhsdesign mhsdesign force-pushed the feature/esbuild-scss-modules branch from 49b9c80 to 517d236 Compare February 22, 2023 17:27
@mhsdesign mhsdesign marked this pull request as ready for review February 22, 2023 17:27
Comment on lines -80 to -81
includeFilter: /\.css$/,
excludeFilter: /@ckeditor\/|@fortawesome\/fontawesome-svg-core\/|styleHostOnly\.css|normalize\.css/,
Copy link
Member Author

Choose a reason for hiding this comment

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

thats why we dit it, we dont need that ugly logic anymore as we rely on conventions

@markusguenther markusguenther merged commit 6c83d1a into 8.3 Mar 8, 2023
@markusguenther markusguenther deleted the feature/esbuild-scss-modules branch March 8, 2023 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

3 participants