Twig-CS-Fixer icon indicating copy to clipboard operation
Twig-CS-Fixer copied to clipboard

Add phar?

Open garak opened this issue 9 months ago • 1 comments

Most tools similar to this one (e.g. phpstan, php-cs-fixer) make a phar version available for download. It would be nice to see the same here.

garak avatar May 05 '24 16:05 garak

Most tools similar to this one (e.g. phpstan, php-cs-fixer) make a phar version available for download. It would be nice to see the same here.

Hi, thanks for the suggestion. I never build a phar, how hard is it ? I'm open to PR.

Also I'm not sure a phar would be the better way to use this tool ; wouldn't it recreate issues like https://github.com/VincentLanglet/Twig-CS-Fixer/issues/206 ? I automatically add TokenParser based on dependencies installed in the same folder than the Twig-Cs-Fixer, would it still work if it was installed with a phar ?

VincentLanglet avatar May 05 '24 18:05 VincentLanglet

Building a PHAR distribution is easy with https://github.com/box-project/box

Twig-CS-Fixer does not require a specific config file, so we 've just to run the following command from root of this repo. box compile

Here is an example of execution :

Box version 4.6.2@29c3585 2024-04-23 19:35:41 UTC

 // Loading without a configuration file.

🔨  Building the PHAR "/shared/backups/forks/Twig-CS-Fixer/bin/twig-cs-fixer.phar"

? Checking Composer compatibility
    > Supported version detected
? No compactor to register
? Adding main file: /shared/backups/forks/Twig-CS-Fixer/bin/twig-cs-fixer
? Adding requirements checker
? Adding binary files
    > No file found
? Auto-discover files? Yes
? Exclude dev files? Yes
? Adding files
    > 494 file(s)
? Generating new stub
  - Using shebang line: #!/usr/bin/env php
  - Using banner:
    > Generated by Humbug Box 4.6.2@29c3585.
    >
    > @link https://github.com/humbug/box
? Dumping the Composer autoloader
? Removing the Composer dump artefacts
? No compression
? Setting file permissions to 0755
* Done.

No recommendation found.
No warning found.

 // PHAR: 537 files (2.26MB)
 // You can inspect the generated PHAR with the "info" command.

 // Memory usage: 23.03MB (peak: 23.23MB), time: 1sec

And if you want to have a manifest in your phar version, I suggest to use my application https://github.com/llaville/box-manifest that is compatible with official BOX v4.

Include it into your CI pipeline is also easy.

llaville avatar Jul 12 '24 09:07 llaville

Hi @llaville, would you be ok to provide a PR then ?

Big thanks

VincentLanglet avatar Jul 12 '24 10:07 VincentLanglet

@VincentLanglet Of course. Did you want to have the basic BOX support or the BOX with manifest(s) ?

Manifests are in different formats :

For examples, a console like this one

phar-manifest

and/or a SBOM contents like this one

[email protected] in /shared/backups/forks/Twig-CS-Fixer $ /shared/backups/php/sarif-php-sdk-2.0.phar --manifest sbom.json
{
    "$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json",
    "bomFormat": "CycloneDX",
    "specVersion": "1.5",
    "serialNumber": "urn:uuid:619b8aa9-0c11-423d-82ce-299fde3c60af",
    "version": 1,
    "metadata": {
        "timestamp": "2024-07-02T15:34:26Z",
        "tools": [
            {
                "vendor": "box-project",
                "name": "box",
                "version": "4.3.8@5534406"
            },
            {
                "vendor": "bartlett",
                "name": "box-manifest",
                "version": "3.5.1"
            }
        ],
        "properties": [
            {
                "name": "specVersion",
                "value": "1.5"
            },
            {
                "name": "bomFormat",
                "value": "CycloneDX"
            }
        ]
    },
    "components": [
        {
            "bom-ref": "pkg:composer/bamarni/[email protected]",
            "type": "application",
            "name": "composer-bin-plugin",
            "version": "1.8.2",
            "group": "bamarni",
            "purl": "pkg:composer/bamarni/[email protected]"
        },
        {
            "bom-ref": "pkg:composer/php-parallel-lint/[email protected]",
            "type": "library",
            "name": "php-console-color",
            "version": "v1.0.1",
            "group": "php-parallel-lint",
            "purl": "pkg:composer/php-parallel-lint/[email protected]"
        },
        {
            "bom-ref": "pkg:composer/php-parallel-lint/[email protected]",
            "type": "library",
            "name": "php-console-highlighter",
            "version": "v1.0.0",
            "group": "php-parallel-lint",
            "purl": "pkg:composer/php-parallel-lint/[email protected]"
        }
    ],
    "dependencies": [
        {
            "ref": "pkg:composer/bamarni/[email protected]"
        },
        {
            "ref": "pkg:composer/php-parallel-lint/[email protected]"
        },
        {
            "ref": "pkg:composer/php-parallel-lint/[email protected]"
        }
    ]
}

llaville avatar Jul 12 '24 10:07 llaville

Thanks a lot

Let's start with the basic box support first and we'll see later if manifest is needed/requested. :)

VincentLanglet avatar Jul 12 '24 10:07 VincentLanglet

Always difficult to identify a version (especially with PHAR distribution), but it's your choice. I'll propose a PR this afternoon.

llaville avatar Jul 12 '24 10:07 llaville

Available with standard BOX version to build a PHAR version (without manifest).

See available results on my fork https://github.com/llaville/Twig-CS-Fixer/actions/runs/9907085104/job/27369999556

I've just added steps to build the PHAR, but not how to reuse it (upload artfifact or not)

It's up-to-you to decide whatever you want.

llaville avatar Jul 12 '24 11:07 llaville

Phar is available in the 3.0.0-rc-1 version https://github.com/VincentLanglet/Twig-CS-Fixer/releases/tag/3.0.0-rc-1

Could you try it and tell me if anything is wrong @garak @llaville ?

Thanks

VincentLanglet avatar Jul 18 '24 21:07 VincentLanglet

It seems it does not work with Twig components

File is invalid: Unknown "props" tag.

File is invalid: Unexpected "component" tag (expecting

File is invalid: Unexpected token "spread operator" of value "...".

But it's maybe related to 3.0 ?

Or does it require to be run from a specific path ?

smnandre avatar Jul 18 '24 23:07 smnandre

On my side, I found no issues, but I run PHAR version only on copy of my fork of this repo:

  • [x] Identify application version

github com_VincentLanglet_Twig-CS-Fixer_releases_tag_3 0 0-rc-1

Works as expected

twigcs-fixer-version

  • [x] Uses absolute path on checkstyle and junit report only

Works as expected

console

While junit and checkstyle reports used absolute paths, but not github that still continue to use relative path.

llaville avatar Jul 19 '24 02:07 llaville

It seems it does not work with Twig components

File is invalid: Unknown "props" tag.

File is invalid: Unexpected "component" tag (expecting

File is invalid: Unexpected token "spread operator" of value "...".

But it's maybe related to 3.0 ?

Or does it require to be run from a specific path ?

Hi @smnandre, I assume the TokenParser are not automatically added when using the PHAR https://github.com/VincentLanglet/Twig-CS-Fixer/blob/main/src/Environment/StubbedEnvironment.php#L124-L154 Maybe class_exists returns false (?).

If you follow https://github.com/VincentLanglet/Twig-CS-Fixer/blob/main/docs/configuration.md#token-parser-twig-extension--node-visitors to add the token parser manually it should fix the issue.

But I'm not sure if there is a way to still auto-detect those classes...

VincentLanglet avatar Jul 19 '24 06:07 VincentLanglet

It works for me

garak avatar Jul 19 '24 07:07 garak

But I'm not sure if there is a way to still auto-detect those classes...

A "solution" might be to remove the code https://github.com/VincentLanglet/Twig-CS-Fixer/blob/main/src/Runner/Linter.php#L71-L75

I would stop to validate the twig file before linting/fixing it... I try it in https://github.com/VincentLanglet/Twig-CS-Fixer/pull/274

VincentLanglet avatar Jul 19 '24 07:07 VincentLanglet

I have no need to use the phar one, and that would not shock me if the PHAR version is released without support for external Parser ;)

smnandre avatar Jul 19 '24 17:07 smnandre

I have no need to use the phar one, and that would not shock me if the PHAR version is released without support for external Parser ;)

Sure, but it could also make sens to not trying to validating/parsing the twig (and let symfony doing it https://symfony.com/doc/7.2/templates.html#linting-twig-templates). Unless someone use NodeVisitor, I don't need it, so I made it optional.

New rc is ready with those changes https://github.com/VincentLanglet/Twig-CS-Fixer/releases/tag/3.0.0-rc-2

VincentLanglet avatar Jul 20 '24 07:07 VincentLanglet

No regression found for me on PHAR RC2 distrib !

llaville avatar Jul 20 '24 10:07 llaville

@VincentLanglet I've a recommendation on PHAR building distribution.

Even if BOX project is a zero config application and works in most of case, we can improve the artifact built with a little config option : learn more at https://github.com/box-project/box/blob/main/doc/configuration.md#compression-algorithm-compression

Currently, default is compression is none, that means we have a huge PHAR copy. But if you create on root of your project a box.json.dist default file with following entry

{
    "compression": "GZ"
}

The artifact built will decreased from 2.2 Mb to 682 kB. It may be cool for final stable version 3.0.0

llaville avatar Jul 20 '24 11:07 llaville

I'll try it @llaville https://github.com/VincentLanglet/Twig-CS-Fixer/pull/279

Also, I found an issue with the PHAR, maybe you could help me on it.

If I write a custom rules in my project-src folder and use the config to add

$ruleset = (new Ruleset())->addRule(new \App\Cs\Twig\CustomRule());

I get a Class not found error, which doesn't occur when I use the tool directly from composer.

Can I avoid this ?

VincentLanglet avatar Jul 20 '24 16:07 VincentLanglet

@VincentLanglet Of course you can avoid this, but it will need to add a --bootstrap option (IMO).

Reason came from your autoloader that is not included when you build the PHAR artifact.

By adding an option to include a script before execution, we can define a custom autoloader that goal is to load additional resources like your custom rules.

Lot of projects used this possibility. Checks

PHPUnit implement such feature with --bootstrap <file> PHPLint implement such feature with --bootstrap <file> PHPStan implement such feature with --autoload-file <file>

PS: I've a similar behavior in one of my project to load resources : see https://github.com/llaville/umlwriter/blob/4.2/docs/03_Cookbook/03_Custom_autoloader.md

llaville avatar Jul 20 '24 17:07 llaville