aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarsten Heimrich <karsten.heimrich@qt.io>2025-12-02 14:26:16 +0100
committerKarsten Heimrich <karsten.heimrich@qt.io>2025-12-03 19:44:34 +0000
commit672206ec491f95ef4707e960aa312e1fba9a17b7 (patch)
tree4096a3ae69f3bf42757ced6dfee6ab60bb02b4d8
parenta8e6493452303a0d730b1f21618d6b8d4d351302 (diff)
Introduce QtTemplateFile task to replace placeholder tokens in files
Change-Id: I5f4bb86c5d11e19f639542e281ecc7ee82d55689 Reviewed-by: Miguel Costa <miguel.costa@qt.io>
-rw-r--r--qt_template.targets61
-rw-r--r--qtdotnet.sln1
2 files changed, 62 insertions, 0 deletions
diff --git a/qt_template.targets b/qt_template.targets
new file mode 100644
index 0000000..5807bc8
--- /dev/null
+++ b/qt_template.targets
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+
+ <!--
+ QtTemplateFile: Explicitly replaces specified tokens in a template file and writes the result
+ to OutputFileName.
+
+ Tokens: ITaskItem[]
+ - ItemSpec = string to be replaced (e.g., "__PACKAGE_VERSION__")
+ - Metadata "ReplacementValue" = target value
+ -->
+
+ <UsingTask TaskName="QtTemplateFile"
+ TaskFactory="RoslynCodeTaskFactory"
+ AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
+ <ParameterGroup>
+ <Template ParameterType="System.String" Required="true" />
+ <OutputFileName ParameterType="System.String" Required="true" />
+ <Tokens ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="false" />
+ </ParameterGroup>
+ <Task>
+ <Using Namespace="System" />
+ <Using Namespace="System.IO" />
+ <Using Namespace="System.Collections.Generic" />
+ <Using Namespace="Microsoft.Build.Framework" />
+ <Code Type="Fragment" Language="cs"><![CDATA[
+
+ const string MetadataReplacementValue = "ReplacementValue";
+ try {
+ if (File.Exists(Template)) {
+ string content = File.ReadAllText(Template);
+
+ if (Tokens != null) {
+ foreach (ITaskItem taskItem in Tokens) {
+ var token = taskItem.ItemSpec;
+ if (string.IsNullOrEmpty(token))
+ continue;
+ content = content.Replace(token,
+ taskItem.GetMetadata(MetadataReplacementValue) ?? "");
+ }
+ }
+
+ var dir = Path.GetDirectoryName(OutputFileName);
+ if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
+ Directory.CreateDirectory(dir);
+
+ File.WriteAllText(OutputFileName, content);
+ Log.LogMessage(MessageImportance.Low,
+ "Template '{0}' processed and written to '{1}'.", Template, OutputFileName);
+ } else {
+ Log.LogError("Template file '{0}' does not exist.", Template);
+ }
+ } catch (System.Exception ex) {
+ Log.LogErrorFromException(ex, true);
+ }
+
+ ]]></Code>
+ </Task>
+ </UsingTask>
+
+</Project>
diff --git a/qtdotnet.sln b/qtdotnet.sln
index 8001579..aa2aa87 100644
--- a/qtdotnet.sln
+++ b/qtdotnet.sln
@@ -36,6 +36,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution", "solution", "{0F
HOW-TO debug generated code.md = HOW-TO debug generated code.md
HOW-TO versions.md = HOW-TO versions.md
NuGet.Config = NuGet.Config
+ qt_template.targets = qt_template.targets
qt_version.props = qt_version.props
README.md = README.md
EndProjectSection