<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-platform-parent</artifactId>
        <version>24.9-SNAPSHOT</version>
    </parent>

    <artifactId>vaadin-gradle-plugin</artifactId>
    <packaging>jar</packaging>
    <name>Vaadin Gradle plugin</name>

    <prerequisites>
        <maven>3.0</maven>
    </prerequisites>

    <properties>
        <flow.version>24.9-SNAPSHOT</flow.version>
        <hilla.version>24.9-SNAPSHOT</hilla.version>

        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <gradle.executable>./gradlew</gradle.executable>
        <gradle.publish.key>${gradlePublishKey}</gradle.publish.key>
        <gradle.publish.secret>${gradlePublishSecret}</gradle.publish.secret>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>hilla-gradle-plugin</artifactId>
            <version>${hilla.version}</version>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>flow-gradle-plugin</artifactId>
            <version>${flow.version}</version>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-prod-bundle</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- Dev bundle is needed to copy package-lock file from it to the project, -->
        <!-- this speeds up the production bundle build -->
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-dev-bundle</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Remove the generated files into /src/main/**, when clean the project
                 Fixes https://github.com/vaadin/platform/issues/777 -->
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>${project.basedir}/src/main</directory>
                            <includes>
                                <include>**</include>
                            </includes>
                            <followSymlinks>false</followSymlinks>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.vaadin</groupId>
                                    <artifactId>hilla-gradle-plugin</artifactId>
                                    <version>${hilla.version}</version>
                                    <classifier>sources</classifier>
                                    <includes>**/*.kt</includes>
                                    <outputDirectory>target/temp</outputDirectory>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.vaadin</groupId>
                                    <artifactId>hilla-gradle-plugin</artifactId>
                                    <version>${hilla.version}</version>
                                    <excludes>**/*.class,META-INF/**</excludes>
                                    <outputDirectory>src/main/resources</outputDirectory>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.vaadin</groupId>
                                    <artifactId>hilla-gradle-plugin</artifactId>
                                    <version>${hilla.version}</version>
                                    <includes>**/lifecycle-mapping-metadata.xml</includes>
                                    <outputDirectory>src/main/resources</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>com.coderplus.maven.plugins</groupId>
                <artifactId>copy-rename-maven-plugin</artifactId>
                <version>1.0.1</version>
                <executions>
                    <execution>
                        <id>rename-hilla-plugin-to-vaadin</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>rename</goal>
                        </goals>
                        <configuration>
                            <sourceFile>src/main/kotlin/com/vaadin/gradle/plugin/HillaPlugin.kt</sourceFile>
                            <destinationFile>src/main/kotlin/com/vaadin/gradle/plugin/VaadinPlugin.kt</destinationFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>3.0.2</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.groovy</groupId>
                        <artifactId>groovy-all</artifactId>
                        <version>4.0.18</version>
                        <type>pom</type>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>replace-package-declaration-in-src-files</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <properties>
                                <property>
                                    <name>basedir</name>
                                    <value>${project.basedir}</value>
                                </property>
                            </properties>
                            <scripts>
                                <script>
                                    <![CDATA[
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.Path
import java.util.stream.Collectors

def baseDirPath = basedir

// Define the directory to search for Kotlin files
String directoryPath = "${baseDirPath}/src/main/kotlin/com/vaadin/gradle/plugin"
String oldPackage = 'package com.vaadin.hilla.gradle.plugin'
String newPackage = 'package com.vaadin.gradle.plugin'
String oldPluginClassDef = 'public class HillaPlugin : Plugin<Project> {'
String newPluginClassDef = 'public class VaadinPlugin : Plugin<Project> {'
String oldPluginCreateEngineConf = 'HillaPlugin.createEngineConfiguration'
String newPluginCreateEngineConf = 'VaadinPlugin.createEngineConfiguration'

// Use NIO to walk through the directory
Files.walk(Paths.get(directoryPath))
    // Filter to include only Kotlin files
    .filter { path -> path.toString().endsWith('.kt') }
    // Collect paths to process them
    .collect(Collectors.toList())
    // Iterate over each file
    .each { Path path ->
        // Read the content of the file
        String content = new String(Files.readAllBytes(path))
        // Replace the old package declaration with the new one
        String updatedContent = content.replace(oldPackage, newPackage)
        // Replace the old plugin class definition with the new one
        updatedContent = updatedContent.replace(oldPluginClassDef, newPluginClassDef)
        // Replace the old plugin method calls definition with the new one
        updatedContent = updatedContent.replace(oldPluginCreateEngineConf, newPluginCreateEngineConf)
        // Write the updated content back to the file
        Files.write(path, updatedContent.getBytes())
        println "Updated package declaration in file: ${path}"
    }
                                    ]]>
                                </script>
                            </scripts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- execute Gradle clean build -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>gradle</id>
                        <phase>prepare-package</phase>
                        <configuration>
                            <executable>${gradle.executable}</executable>
                            <arguments>
                                <argument>clean</argument>
                                <argument>build</argument>
                                <argument>javadocJar</argument>
                                <argument>-x</argument>
                                <argument>functionalTest</argument>
                                <argument>-S</argument>
                            </arguments>
                        </configuration>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- copy generated JARs -->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>copy-gradle-jars</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>build/libs/</directory>
                                    <includes>
                                        <include>**/*.jar</include>
                                    </includes>
                                </resource>
                            </resources>
                            <overwrite>true</overwrite>
                        </configuration>
                    </execution>
                    <execution>
                        <!-- for copying extracted src files from target/temp to new package -->
                        <id>move-src-from-hilla-package-to-vaadin</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/src/main/kotlin/com/vaadin/gradle/plugin</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}/target/temp/com/vaadin/hilla/gradle/plugin</directory>
                                    <includes>
                                        <include>**/*.kt</include>
                                    </includes>
                                </resource>
                            </resources>
                            <overwrite>true</overwrite>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- attach copied JARs to build -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attach-artifact</goal>
                        </goals>
                        <configuration>
                            <artifacts>
                                <artifact>
                                    <file>target/${project.artifactId}-${project.version}-sources.jar</file>
                                    <type>jar</type>
                                    <classifier>sources</classifier>
                                </artifact>
                                <artifact>
                                    <file>target/${project.artifactId}-${project.version}-javadoc.jar</file>
                                    <type>jar</type>
                                    <classifier>javadoc</classifier>
                                </artifact>
                            </artifacts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <profiles>
        <profile>
            <id>windows_profile</id>
            <activation>
                <os>
                    <family>Windows</family>
                </os>
            </activation>
            <properties>
                <gradle.executable>${basedir}/gradlew.bat</gradle.executable>
            </properties>
        </profile>
    </profiles>

</project>
