-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateTargetsFile.cs
More file actions
60 lines (52 loc) · 1.86 KB
/
Copy pathGenerateTargetsFile.cs
File metadata and controls
60 lines (52 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// ReSharper disable UnusedAutoPropertyAccessor.Global
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace NETMetaCoder.MSBuild
{
/// <summary>
/// An MSBuild task that reads file <c>NETMetaCoder.MSBuild-template.targets</c> and produces file
/// <c>NETMetaCoder.MSBuild.targets</c>.
///
/// The generated file is used by projects that depend on this library's NuGet package to load the necessary MSBuild
/// tasks.
/// </summary>
public sealed class GenerateTargetsFile : Task
{
/// <summary>
/// The file path to <c>NETMetaCoder.MSBuild-template.targets</c>.
/// </summary>
[Required]
public string TargetsFilePath { get; set; }
/// <summary>
/// The name of this library's package.
/// </summary>
[Required]
public string PackageId { get; set; }
/// <summary>
/// This library's package version.
/// </summary>
[Required]
public string Version { get; set; }
/// <summary>
/// The target framework moniker of this library.
/// </summary>
[Required]
public string TargetFrameworkMoniker { get; set; }
/// <inheritdoc cref="Task.Execute"/>
public override bool Execute()
{
if (!File.Exists(TargetsFilePath))
{
throw new FileNotFoundException($"\"{TargetsFilePath}\" is not a file.");
}
File.WriteAllText(
TargetsFilePath.Replace("-template", ""),
File.ReadAllText(TargetsFilePath)
.Replace("${PACKAGE_ID}", PackageId.ToLowerInvariant())
.Replace("${VERSION}", Version)
.Replace("${TARGET_FRAMEWORK_MONIKER}", TargetFrameworkMoniker));
return true;
}
}
}