-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ValidateStaticWebAssetsUniquePaths.cs
82 lines (71 loc) · 3.46 KB
/
ValidateStaticWebAssetsUniquePaths.cs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Build.Framework;
namespace Microsoft.AspNetCore.StaticWebAssets.Tasks
{
public class ValidateStaticWebAssetsUniquePaths : Task
{
private const string BasePath = "BasePath";
private const string RelativePath = "RelativePath";
private const string TargetPath = "TargetPath";
[Required]
public ITaskItem[] StaticWebAssets { get; set; }
[Required]
public ITaskItem[] WebRootFiles { get; set; }
public override bool Execute()
{
var assetsByWebRootPaths = new Dictionary<string, ITaskItem>(StringComparer.OrdinalIgnoreCase);
for (var i = 0; i < StaticWebAssets.Length; i++)
{
var contentRootDefinition = StaticWebAssets[i];
if (!EnsureRequiredMetadata(contentRootDefinition, BasePath) ||
!EnsureRequiredMetadata(contentRootDefinition, RelativePath))
{
return false;
}
else
{
var webRootPath = GetWebRootPath("/wwwroot",
contentRootDefinition.GetMetadata(BasePath),
contentRootDefinition.GetMetadata(RelativePath));
if (assetsByWebRootPaths.TryGetValue(webRootPath, out var existingWebRootPath))
{
if (!string.Equals(contentRootDefinition.ItemSpec, existingWebRootPath.ItemSpec, StringComparison.OrdinalIgnoreCase))
{
Log.LogError($"Conflicting assets with the same path '{webRootPath}' for content root paths '{contentRootDefinition.ItemSpec}' and '{existingWebRootPath.ItemSpec}'.");
return false;
}
}
else
{
assetsByWebRootPaths.Add(webRootPath, contentRootDefinition);
}
}
}
for (var i = 0; i < WebRootFiles.Length; i++)
{
var webRootFile = WebRootFiles[i];
var relativePath = webRootFile.GetMetadata(TargetPath);
var webRootFileWebRootPath = GetWebRootPath("", "/", relativePath);
if (assetsByWebRootPaths.TryGetValue(webRootFileWebRootPath, out var existingAsset))
{
Log.LogError($"The static web asset '{existingAsset.ItemSpec}' has a conflicting web root path '{webRootFileWebRootPath}' with the project file '{webRootFile.ItemSpec}'.");
return false;
}
}
return true;
}
// Normalizes /base/relative \base\relative\ base\relative and so on to /base/relative
private string GetWebRootPath(string webRoot, string basePath, string relativePath) => $"{webRoot}/{Path.Combine(basePath, relativePath.TrimStart('.').TrimStart('/')).Replace("\\", "/").Trim('/')}";
private bool EnsureRequiredMetadata(ITaskItem item, string metadataName)
{
var value = item.GetMetadata(metadataName);
if (string.IsNullOrEmpty(value))
{
Log.LogError($"Missing required metadata '{metadataName}' for '{item.ItemSpec}'.");
return false;
}
return true;
}
}
}