mike-lischke / antlr-format

A tool to format ANTLR grammars
MIT License
5 stars 2 forks source link

Do not overwrite grammar if it is already formatted #11

Open jimidle opened 2 weeks ago

jimidle commented 2 weeks ago

When used as part of a build system, the fact that the formatter always writes the result of the format operation to file because a small but nagging issue. When the grammar is touched, the antlr-plugin will detect a change and will re-generate the target language, which then triggers a build of everything else affected by a grammar change.

A small change is suggested such that a grammar is not rewritten if the format does not change.

Suggested approaches:

Though if the original code is still accessible in memory after it has been formatted, then a simple string compare would do it of course (I have not looked at the code to determine how the original grammar is read).

mike-lischke commented 2 weeks ago

Wouldn't it be much simpler if the build system checks if the grammar has been touched (check last change date) and triggers the formatter only if so? After all it's an oranizational aspect, not related to formatting.

jimidle commented 2 weeks ago

The build system is mvn, so I can only execute the tool as there is no maven plugin. I will check to see if there is a way I can do it from Maven.

jimidle commented 2 weeks ago

There is no practical way for me to do this in Maven. Maven expects a plugin to do such things. I would need to write a shell script to do it based on some marker in target, which is fine I suppose but I would rather avoid external shell scripts. I know that this tool was not written for Maven of course, but it works fine with:

     <plugin>
        <groupId>com.github.eirslett</groupId>
        <artifactId>frontend-maven-plugin</artifactId>
        <version>1.15.0</version>
        <configuration>
          <nodeVersion>v22.3.0</nodeVersion>
          <installDirectory>${project.build.directory}</installDirectory>
        </configuration>
        <executions>
          <execution>
            <id>install node and npm</id>
            <phase>validate</phase>
            <goals>
              <goal>install-node-and-npm</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.3.0</version>
        <executions>
          <!-- npm install antlr-format-cli -->
          <execution>
            <id>npm install antlr-format antlr-format-cli</id>
            <phase>validate</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>${project.build.directory}/node/npm</executable>
              <arguments>
                <argument>install</argument>
                <argument>-g</argument>
                <argument>--save-dev</argument>
                <argument>antlr-format</argument>
                <argument>antlr-format-cli</argument>
              </arguments>
            </configuration>
          </execution>
          <!-- antlr-fmt -->
          <execution>
            <id>antlr-fmt</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>${project.build.directory}/bin/antlr-format</executable>
              <arguments>
                <argument>-v</argument>
                <argument>${basedir}/src/main/antlr4/**/*.g4</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
mike-lischke commented 2 weeks ago

I'm not fully convinced the tool should do this logic on its own - it's pretty specific. However, I'll keep the idea around for later consideration or maybe someone files a PR for it.

jimidle commented 2 weeks ago

I'm not fully convinced the tool should do this logic on its own - it's pretty specific. However, I'll keep the idea around for later consideration or maybe someone files a PR for it.

Well, I sent a PR for your consideration. In general *nix tools don't rewrite a file that they don't change to avoid IO and triggering other things unnecessarily.