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

Customize latte linter #286

Closed
MartkCz opened this issue Mar 30, 2022 · 9 comments
Closed

Customize latte linter #286

MartkCz opened this issue Mar 30, 2022 · 9 comments

Comments

@MartkCz
Copy link
Contributor

MartkCz commented Mar 30, 2022

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);
@simPod
Copy link
Contributor

simPod commented Apr 20, 2022

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

@dg
Copy link
Member

dg commented Apr 20, 2022

It works this way:

$linter = new Latte\Tools\Linter($engine);
$linter->scanDirectory($path);

@mabar
Copy link

mabar commented Apr 20, 2022

@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');

@simPod
Copy link
Contributor

simPod commented Apr 20, 2022

So we cannot use the binary straight away, right?

@dg
Copy link
Member

dg commented Apr 20, 2022

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

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

@dg
Copy link
Member

dg commented Apr 20, 2022

So we cannot use the binary straight away, right?

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

@simPod
Copy link
Contributor

simPod commented Apr 20, 2022

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.

@dg
Copy link
Member

dg commented Apr 20, 2022

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.

@simPod
Copy link
Contributor

simPod commented Apr 20, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants