Skip to content

Customize latte linter #286

Closed
Closed

Description

@MartkCz

I want to use custom latte macros, now I must copy whole file latte-lint.php to my file then edit it. It's not good for future updates.

I would imagine usage like this:

require __DIR__ . '/vendor/autoload.php';

$booting = new Booting();

$engine = $booting->boot()
	->createContainer()
	->getByType(Engine::class)

LatteLinter::run($engine);

Activity

  1. simPod commented on Apr 20, 2022

    @simPod
    Contributor

    Or just some option like ./latte-lint --ignore-tags webpack,potato src

  2. added a commit that references this issue on Apr 20, 2022
    48ef723
  3. dg commented on Apr 20, 2022

    @dg
    Member

    It works this way:

    $linter = new Latte\Tools\Linter($engine);
    $linter->scanDirectory($path);
  4. mabar commented on Apr 20, 2022

    @mabar

    @dg It took some trial and error to make it work with Nette application, but here we go:

    use App\Bootstrap;
    use Latte\Loaders\StringLoader;
    use Latte\Tools\Linter;
    use Nette\Bridges\ApplicationLatte\TemplateFactory;
    
    require __DIR__ . '/../vendor/autoload.php';
    
    $configurator = Bootstrap::boot();
    $container = $configurator->createContainer();
    
    // We need TemplateFactory and not LatteFactory because it adds some macros
    $factory = $container->getService('latte.templateFactory');
    assert($factory instanceof TemplateFactory);
    
    $template = $factory->createTemplate();
    $engine = $template->getLatte();
    
    // StringLoader is required
    // Otherwise Latte\RuntimeException: Missing template file '<whole template file here>' is thrown
    $engine->setLoader(new StringLoader());
    
    $linter = new Linter($engine);
    $linter->scanDirectory(dirname(__DIR__) . '/app');
  5. simPod commented on Apr 20, 2022

    @simPod
    Contributor

    So we cannot use the binary straight away, right?

  6. dg commented on Apr 20, 2022

    @dg
    Member

    I'll modify it to set StringLoader directly in the scanDirectory().

    Otherwise, Linter automatically adds macros for forms and applications when available.

  7. added a commit that references this issue on Apr 20, 2022
    c7a3a31
  8. dg commented on Apr 20, 2022

    @dg
    Member

    So we cannot use the binary straight away, right?

    If you don't use custom macros, you can.

  9. simPod commented on Apr 20, 2022

    @simPod
    Contributor

    If you don't use custom macros

    This is what the issue is about


    For anyone trying to set this up in CI:

    touch latte-lint
    chmod +x latte-lint

    I took the snippet above, changed it to this:

    #!/usr/bin/env php
    <?php
    
    declare(strict_types=1);
    
    use Latte\Tools\Linter;
    use Nette\Bridges\ApplicationLatte\TemplateFactory;
    
    require __DIR__ . '/vendor/autoload.php';
    
    $container = require __DIR__ . '/src/bootstrap.php';
    
    $factory = $container->getService('latte.templateFactory');
    assert($factory instanceof TemplateFactory);
    
    $template = $factory->createTemplate();
    $engine = $template->getLatte();
    
    $linter = new Linter($engine);
    
    $success = $linter->scanDirectory(__DIR__ . '/src');
    
    exit($success ? 0 : 1);

    and had to set database to be lazy since this initializes the app lazy: true

    Also had to mimic parameters.neon which is the unversioned file I use to parametrize my ~3-line app (env file). (My bootstrap file has $configurator->addConfig(__DIR__ . '/config/parameters.neon');)

    This is how it looks in GA:

          - name: "Run Latte Lint"
            run: |
              touch src/config/parameters.neon
              printf "parameters:\n\tdatabase:\n\t\tdriver: mysql\n\t\thost: 127.0.0.1\n\t\tdbname: hey\n\t\tuser: just\n\t\tpassword: work\n\t\tport: 3306\n\n\tsentry:\n\t\tdsn: ''" > src/config/parameters.neon
              ./latte-lint

    I guess I could rewrite the app bootstrapping from scratch to adapt this but I just followed nette docs and it's not worth it since the project is "just a small app".

    Still, I'd rather had a binary I could pass few params to but it is what it is.

    Too much work to ignore/learn one macro it seems but I guess I don't have a big picture. Hope it helps.

  10. dg commented on Apr 20, 2022

    @dg
    Member

    Feel free to suggest a better way. The point is that ignoring goes against the intent of the linter. Linter has to validate the use, content etc of custom macros too.

  11. simPod commented on Apr 20, 2022

    @simPod
    Contributor

    Sure, I know it needs the container to get the whole context. But I had to rebootstrap the whole app in CI as shown above even though I wanted to just catch latte deprecations before breaking production. The vendor/bin/latte-lint worked great out of the box, except it gave me 2 more errors on unknown webpack macro.

    This vendor/bin/latte-lint --ignore-tags webpack,potato src looked much simpler for my use case and those ignored tags would have been my responsibility now.

  12. added a commit that references this issue on May 7, 2022
    5e49c82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions