<?xml version="1.0" encoding="utf-8"?>
<!--
 Copyright (C) 2025 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
-->

<!--
///////////////////////////////////////////////////////////////////////////////////////////////////
// Generate imports of QML static plugins
// -->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!--
  /////////////////////////////////////////////////////////////////////////////////////////////////
  // Import pre-requisites
  // -->
  <Import
    Condition="'$(QtMsBuildTargets_BeforeQmlStatic)' != ''"
    Project="$(QtMsBuildTargets_BeforeQmlStatic)"/>

  <!--
  /////////////////////////////////////////////////////////////////////////////////////////////////
  /// TARGET QtQmlStaticGatherQmlPaths
  ///  => runs once
  ///  => skipped if there are no QRC files in the project
  /////////////////////////////////////////////////////////////////////////////////////////////////
  // Gathers the QML paths within all RCC files.
  // -->
  <Target
    Name="QtQmlStaticGatherQmlPaths"
    Condition="'@(QtRcc)' != ''"
    Outputs="@(QtQmlPaths)">
    <ListQrc QrcFilePath="%(QtRcc.FullPath)">
      <Output TaskParameter="Result" ItemName="QtRccPaths" />
    </ListQrc>
    <ItemGroup>
      <QtQmlPaths Condition="'%(QtRccPaths.Extension)' == '.qml'"
        Include="%(QtRccPaths.Identity)" />
    </ItemGroup>
  </Target>

  <!--
  /////////////////////////////////////////////////////////////////////////////////////////////////
  /// TARGET QtQmlStaticGenerateImportFile
  ///  => runs once
  ///  => skipped if there are no QRC files in the project
  ///  => targets that must run first: QtQmlStaticGatherQmlPaths
  /////////////////////////////////////////////////////////////////////////////////////////////////
  // Generates a QML file containing all import statements in all QML files.
  // -->
  <Target
    Name="QtQmlStaticGenerateImportFile"
    Condition="'@(QtRcc)' != ''"
    DependsOnTargets="QtQmlStaticGatherQmlPaths"
    Inputs="@(QtRcc);@(QtQmlPaths)"
    Outputs="$(QtQmlStaticImportFile)">
    <ReadLinesFromFile Condition="'@(QtQmlPaths)' != ''" File="%(QtQmlPaths.Identity)">
      <Output TaskParameter="Lines" ItemName="QtQmlSourceText" />
    </ReadLinesFromFile>

    <ItemGroup>
      <QtQmlImportFileSource
        Include="$([System.Text.RegularExpressions.Regex]::Matches('%(QtQmlSourceText.Identity)',
        '^\s*import\s+[^\n]*'))" />
      <QtQmlImportFileSource Include="QmlObject { }" />
    </ItemGroup>

    <ReadLinesFromFile Condition="Exists('$(QtQmlStaticImportFile)')"
      File="$(QtQmlStaticImportFile)">
      <Output TaskParameter="Lines" ItemName="QtPrevQmlImportFileSource" />
    </ReadLinesFromFile>

    <PropertyGroup>
      <QtQmlImportFileSource
        >@(QtQmlImportFileSource->'%(Identity)')</QtQmlImportFileSource>
      <QtPrevQmlImportFileSource
        >@(QtPrevQmlImportFileSource->'%(Identity)')</QtPrevQmlImportFileSource>
    </PropertyGroup>

    <WriteLinesToFile Condition="'$(QtQmlImportFileSource)' != '$(QtPrevQmlImportFileSource)'"
      File="$(QtQmlStaticImportFile)" Lines="@(QtQmlImportFileSource)" Overwrite="true" />
  </Target>

  <!--
  /////////////////////////////////////////////////////////////////////////////////////////////////
  // Import dependants
  // -->
  <Import
    Condition="'$(QtMsBuildTargets_AfterQmlStatic)' != ''"
    Project="$(QtMsBuildTargets_AfterQmlStatic)"/>

</Project>
