Giter Club home page Giter Club logo

Comments (8)

sciloqi avatar sciloqi commented on July 3, 2024 2

Thank you both for your incredible work! Worked absolutely smooth ❤️

from kirby-focus.

FynnZW avatar FynnZW commented on July 3, 2024 2

I just tried it out and it worked perfectly!
Maybe this can be included as an cli command for this plugin?

from kirby-focus.

bnomei avatar bnomei commented on July 3, 2024 1

something like this?

<?php

declare(strict_types=1);

use Kirby\CLI\CLI;
use Kirby\Filesystem\F;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;

return [
    // Author: Bnomei
    // WARNING: this is a destructive command. Make a backup first!
    //
    // site/commands/focus-k4.php
    // php vendor/bin/kirby focus-k4 --dry-run
    //
    'description' => 'Migrate from Focus plugin to native K4',
    'args' => [
        'verbose' => [
            'shortPrefix' => 'v',
            'longPrefix' => 'verbose',
            'description' => 'Verbose output',
            'defaultValue' => false,
            'noValue' => true,
        ],
        'dryrun' => [
            'longPrefix' => 'dry-run',
            'description' => 'Dry run',
            'defaultValue' => false,
            'noValue' => true,
        ],
    ],
    'command' => static function (CLI $cli): void {
        // https://github.com/flokosiol/kirby-focus/issues/75
        $files = \Symfony\Component\Finder\Finder::create()
            ->in(kirby()->roots()->content())
            ->name('*.txt')
            ->files();
        $processed = 0;
        $replacements = 0;
        $isDryRun = $cli->arg('dryrun');
        $isVerbose = $cli->arg('verbose');

        foreach ($files as $file) {
            $path = $file->getPathname();
            $relpath = $file->getRelativePathname();
            $content = F::read($path);

            if (!Str::contains($content, 'Focus: ')) {
                if ($isVerbose) {
                    $cli->out("{$relpath}");
                }
                continue;
            }

            $content = preg_replace_callback('/^Focus: {(.*)}$/mi', function ($matches) use ($cli) {
                $json = json_decode('{' . $matches[1] . '}', true);
                if (!is_array($json)) {
                    return $matches[0]; // no change
                }
                $x = A::get($json, 'x', 0);
                $y = A::get($json, 'y', 0);
                $x = number_format($x * 100, 0);
                $y = number_format($y * 100, 0);

                return "Focus: {$x}% {$y}%";
            }, $content, -1, $count);

            if (!$isDryRun) {
                F::write($path, $content);
            }
            if ($count === 0 && $isVerbose) {
                $cli->out("⏩ [0] {$relpath}");
            }
            if ($count > 0) {
                $cli->out("✅ [{$count}] {$relpath}");
            }
            $processed++;
            $replacements += $count;
        }

        $cli->success("files: {$processed}, replacements: {$replacements}");
    }
];

from kirby-focus.

tobimori avatar tobimori commented on July 3, 2024 1

In general, Kirby plugins upload the vendor folder to git for people that don't use composer.

from kirby-focus.

flokosiol avatar flokosiol commented on July 3, 2024

Thank you so much for your kind words! @FynnZW

And also thank you for sharing your solution to migrate to Kirby v4. I'm sure it will help others.

from kirby-focus.

tobimori avatar tobimori commented on July 3, 2024

Maybe make a CLI command that migrates the content?

from kirby-focus.

flokosiol avatar flokosiol commented on July 3, 2024

I startet to integrate the command into the plugin structure, but I faced one problem. I got an error, because \Symfony\Component\Finder\Finder could not be found. After adding it to the composer.json file of the plugin everything went smooth, but not everyone uses Composer. What will happen to those folks? Anyone else running into this issue? Should be Composer setup mandatory if you want to use the command? What do you think?

And of course … THANK YOU @bnomei for taking the time to write the command and for sharing it! Very much appreciated!

And secondly, sorry for the late response 🙂

from kirby-focus.

medienbaecker avatar medienbaecker commented on July 3, 2024

Because I wanted to use it as a global command I replaced the Symfony stuff with a native RecursiveIteratorIterator:

<?php

declare(strict_types=1);

use Kirby\CLI\CLI;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;

return [
    // Author: Bnomei
    // WARNING: this is a destructive command. Make a backup first!
    //
    // site/commands/focus-k4.php
    // php vendor/bin/kirby focus-k4 --dry-run
    //
    'description' => 'Migrate from Focus plugin to native K4',
    'args' => [
        'verbose' => [
            'shortPrefix' => 'v',
            'longPrefix' => 'verbose',
            'description' => 'Verbose output',
            'defaultValue' => false,
            'noValue' => true,
        ],
        'dryrun' => [
            'longPrefix' => 'dry-run',
            'description' => 'Dry run',
            'defaultValue' => false,
            'noValue' => true,
        ],
    ],
    'command' => static function (CLI $cli): void {
        // https://github.com/flokosiol/kirby-focus/issues/75
        $directory = kirby()->roots()->content();
        $processed = 0;
        $replacements = 0;
        $isDryRun = $cli->arg('dryrun');
        $isVerbose = $cli->arg('verbose');

        $filenames = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), RecursiveIteratorIterator::SELF_FIRST);

        foreach ($filenames as $filename) {
            $filepath = $filename->getPathname();
            $relpath = str_replace($directory . '/', '', $filepath);
            if ($filename->isDir() || pathinfo($filepath, PATHINFO_EXTENSION) !== 'txt') {
                continue;
            }
            $content = file_get_contents($filepath);

            if (!Str::contains($content, 'Focus: ')) {
                if ($isVerbose) {
                    $cli->out("{$relpath}");
                }
                continue;
            }

            $content = preg_replace_callback('/^Focus: {(.*)}$/mi', function ($matches) use ($cli) {
                $json = json_decode('{' . $matches[1] . '}', true);
                if (!is_array($json)) {
                    return $matches[0]; // no change
                }
                $x = A::get($json, 'x', 0);
                $y = A::get($json, 'y', 0);
                $x = number_format($x * 100, 0);
                $y = number_format($y * 100, 0);

                return "Focus: {$x}% {$y}%";
            }, $content, -1, $count);

            if (!$isDryRun) {
                file_put_contents($filepath, $content);
            }
            if ($count === 0 && $isVerbose) {
                $cli->out("⏩ [0] {$relpath}");
            }
            if ($count > 0) {
                $cli->out("✅ [{$count}] {$relpath}");
            }
            $processed++;
            $replacements += $count;
        }

        $cli->success("files processed: {$processed}, replacements: {$replacements}");
    }
];

from kirby-focus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.