Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 1372726

Browse files
committed
Correct metadata additions and add errors about metadata
- related to #8419 and (more generally) #7947 - add errors for missing required metadata - add errors for duplicate `%(DocumentPath)` and `%(OutputPath)` metadata - remove `[Required]` for task inputs that may be `null` or empty - correct `%(DocumentPath)`s generated in `GetProjectReferenceMetadata` task - use this task
1 parent 0e9ef9d commit 1372726

File tree

6 files changed

+330
-29
lines changed

6 files changed

+330
-29
lines changed

src/Microsoft.Extensions.ApiDescription.Client/GetFileReferenceMetadata.cs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
namespace Microsoft.Extensions.ApiDescription.Client
1111
{
1212
/// <summary>
13-
/// Adds or corrects Namespace and OutputPath metadata in ServiceFileReference items.
13+
/// Adds or corrects ClassName, Namespace and OutputPath metadata in ServiceFileReference items. Also stores final
14+
/// metadata as SerializedMetadata.
1415
/// </summary>
1516
public class GetFileReferenceMetadata : Task
1617
{
@@ -23,7 +24,6 @@ public class GetFileReferenceMetadata : Task
2324
/// <summary>
2425
/// Default directory for OutputPath values.
2526
/// </summary>
26-
[Required]
2727
public string OutputDirectory { get; set; }
2828

2929
/// <summary>
@@ -49,14 +49,47 @@ public class GetFileReferenceMetadata : Task
4949
public override bool Execute()
5050
{
5151
var outputs = new List<ITaskItem>(Inputs.Length);
52+
var destinations = new HashSet<string>();
5253
foreach (var item in Inputs)
5354
{
5455
var newItem = new TaskItem(item);
5556
outputs.Add(newItem);
5657

5758
var codeGenerator = item.GetMetadata("CodeGenerator");
58-
var isTypeScript = codeGenerator.EndsWith("TypeScript", StringComparison.OrdinalIgnoreCase);
59+
if (string.IsNullOrEmpty("CodeGenerator"))
60+
{
61+
// This case occurs when user forgets to specify the required metadata. We have no default here.
62+
string type;
63+
if (!string.IsNullOrEmpty(item.GetMetadata("SourceProject")))
64+
{
65+
type = "ServiceProjectReference";
66+
}
67+
else if (!string.IsNullOrEmpty(item.GetMetadata("SourceUri")))
68+
{
69+
type = "ServiceUriReference";
70+
}
71+
else
72+
{
73+
type = "ServiceFileReference";
74+
}
75+
76+
Log.LogError(Resources.FormatInvalidEmptyMetadataValue("CodeGenerator", type, item.ItemSpec));
77+
}
5978

79+
var className = item.GetMetadata("ClassName");
80+
if (string.IsNullOrEmpty(className))
81+
{
82+
var filename = item.GetMetadata("Filename");
83+
className = $"{filename}Client";
84+
if (char.IsLower(className[0]))
85+
{
86+
className = char.ToUpper(className[0]) + className.Substring(startIndex: 1);
87+
}
88+
89+
MetadataSerializer.SetMetadata(newItem, "ClassName", className);
90+
}
91+
92+
var isTypeScript = codeGenerator.EndsWith("TypeScript", StringComparison.OrdinalIgnoreCase);
6093
var @namespace = item.GetMetadata("Namespace");
6194
if (string.IsNullOrEmpty(@namespace))
6295
{
@@ -67,20 +100,26 @@ public override bool Execute()
67100
var outputPath = item.GetMetadata("OutputPath");
68101
if (string.IsNullOrEmpty(outputPath))
69102
{
70-
var className = item.GetMetadata("ClassName");
71103
outputPath = $"{className}{(isTypeScript ? ".ts" : ".cs")}";
72104
}
73105

74106
outputPath = GetFullPath(outputPath);
75107
MetadataSerializer.SetMetadata(newItem, "OutputPath", outputPath);
76108

109+
if (!destinations.Add(outputPath))
110+
{
111+
// This case may occur when user is experimenting e.g. with multiple code generators or options.
112+
// May also occur when user accidentally duplicates OutputPath metadata.
113+
Log.LogError(Resources.FormatDuplicateFileOutputPaths(outputPath));
114+
}
115+
77116
// Add metadata which may be used as a property and passed to an inner build.
78117
newItem.SetMetadata("SerializedMetadata", MetadataSerializer.SerializeMetadata(newItem));
79118
}
80119

81120
Outputs = outputs.ToArray();
82121

83-
return true;
122+
return !Log.HasLoggedErrors;
84123
}
85124

86125
private string GetFullPath(string path)

src/Microsoft.Extensions.ApiDescription.Client/GetProjectReferenceMetadata.cs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Collections.Generic;
65
using System.IO;
76
using Microsoft.Build.Framework;
@@ -10,14 +9,14 @@
109
namespace Microsoft.Extensions.ApiDescription.Client
1110
{
1211
/// <summary>
13-
/// Adds or corrects DocumentPath and project-related metadata in ServiceProjectReference items.
12+
/// Adds or corrects DocumentPath and project-related metadata in ServiceProjectReference items. Also stores final
13+
/// metadata as SerializedMetadata.
1414
/// </summary>
1515
public class GetProjectReferenceMetadata : Task
1616
{
1717
/// <summary>
1818
/// Default directory for DocumentPath values.
1919
/// </summary>
20-
[Required]
2120
public string DocumentDirectory { get; set; }
2221

2322
/// <summary>
@@ -37,30 +36,53 @@ public class GetProjectReferenceMetadata : Task
3736
public override bool Execute()
3837
{
3938
var outputs = new List<ITaskItem>(Inputs.Length);
39+
var destinations = new HashSet<string>();
40+
4041
foreach (var item in Inputs)
4142
{
4243
var newItem = new TaskItem(item);
4344
outputs.Add(newItem);
4445

45-
var codeGenerator = item.GetMetadata("CodeGenerator");
46-
var isTypeScript = codeGenerator.EndsWith("TypeScript", StringComparison.OrdinalIgnoreCase);
46+
var documentGenerator = item.GetMetadata("DocumentGenerator");
47+
if (string.IsNullOrEmpty(documentGenerator))
48+
{
49+
// This case occurs when user overrides the default metadata.
50+
Log.LogError(Resources.FormatInvalidEmptyMetadataValue(
51+
"DocumentGenerator",
52+
"ServiceProjectReference",
53+
item.ItemSpec));
54+
}
55+
56+
var documentPath = item.GetMetadata("DocumentPath");
57+
if (string.IsNullOrEmpty(documentPath))
58+
{
59+
var filename = item.GetMetadata("Filename");
60+
var documentName = item.GetMetadata("DocumentName");
61+
if (string.IsNullOrEmpty(documentName))
62+
{
63+
documentName = "v1";
64+
}
65+
66+
documentPath = $"{filename}.{documentName}.json";
67+
}
68+
69+
documentPath = GetFullPath(documentPath);
70+
MetadataSerializer.SetMetadata(newItem, "DocumentPath", documentPath);
4771

48-
var outputPath = item.GetMetadata("OutputPath");
49-
if (string.IsNullOrEmpty(outputPath))
72+
if (!destinations.Add(documentPath))
5073
{
51-
var className = item.GetMetadata("ClassName");
52-
outputPath = className + (isTypeScript ? ".ts" : ".cs");
74+
// This case may occur when user is experimenting e.g. with multiple generators or options.
75+
// May also occur when user accidentally duplicates DocumentPath metadata.
76+
Log.LogError(Resources.FormatDuplicateProjectDocumentPaths(documentPath));
5377
}
5478

5579
// Add metadata which may be used as a property and passed to an inner build.
5680
newItem.SetMetadata("SerializedMetadata", MetadataSerializer.SerializeMetadata(newItem));
57-
outputPath = GetFullPath(outputPath);
58-
newItem.SetMetadata("OutputPath", outputPath);
5981
}
6082

6183
Outputs = outputs.ToArray();
6284

63-
return true;
85+
return !Log.HasLoggedErrors;
6486
}
6587

6688
private string GetFullPath(string path)

src/Microsoft.Extensions.ApiDescription.Client/GetUriReferenceMetadata.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public class GetUriReferenceMetadata : Task
1717
/// <summary>
1818
/// Default directory for DocumentPath metadata values.
1919
/// </summary>
20-
[Required]
2120
public string DocumentDirectory { get; set; }
2221

2322
/// <summary>
@@ -36,6 +35,7 @@ public class GetUriReferenceMetadata : Task
3635
public override bool Execute()
3736
{
3837
var outputs = new List<ITaskItem>(Inputs.Length);
38+
var destinations = new HashSet<string>();
3939
foreach (var item in Inputs)
4040
{
4141
var newItem = new TaskItem(item);
@@ -96,11 +96,18 @@ public override bool Execute()
9696

9797
documentPath = GetFullPath(documentPath);
9898
MetadataSerializer.SetMetadata(newItem, "DocumentPath", documentPath);
99+
100+
if (!destinations.Add(documentPath))
101+
{
102+
// This case may occur when user is experimenting e.g. with multiple code generators or options.
103+
// May also occur when user accidentally duplicates DocumentPath metadata.
104+
Log.LogError(Resources.FormatDuplicateUriDocumentPaths(documentPath));
105+
}
99106
}
100107

101108
Outputs = outputs.ToArray();
102109

103-
return true;
110+
return !Log.HasLoggedErrors;
104111
}
105112

106113
private string GetFullPath(string path)

src/Microsoft.Extensions.ApiDescription.Client/Properties/Resources.Designer.cs

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)