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

Commit 7084b67

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 1646345 commit 7084b67

File tree

6 files changed

+313
-19
lines changed

6 files changed

+313
-19
lines changed

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

Lines changed: 30 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,33 @@ 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+
Log.LogError(Resources.FormatInvalidEmptyMetadataValue("CodeGenerator"));
63+
}
5964

65+
var className = item.GetMetadata("ClassName");
66+
if (string.IsNullOrEmpty(className))
67+
{
68+
var filename = item.GetMetadata("Filename");
69+
className = $"{filename}Client";
70+
if (char.IsLower(className[0]))
71+
{
72+
className = char.ToUpper(className[0]) + className.Substring(startIndex: 1);
73+
}
74+
75+
MetadataSerializer.SetMetadata(newItem, "ClassName", className);
76+
}
77+
78+
var isTypeScript = codeGenerator.EndsWith("TypeScript", StringComparison.OrdinalIgnoreCase);
6079
var @namespace = item.GetMetadata("Namespace");
6180
if (string.IsNullOrEmpty(@namespace))
6281
{
@@ -67,20 +86,26 @@ public override bool Execute()
6786
var outputPath = item.GetMetadata("OutputPath");
6887
if (string.IsNullOrEmpty(outputPath))
6988
{
70-
var className = item.GetMetadata("ClassName");
7189
outputPath = $"{className}{(isTypeScript ? ".ts" : ".cs")}";
7290
}
7391

7492
outputPath = GetFullPath(outputPath);
7593
MetadataSerializer.SetMetadata(newItem, "OutputPath", outputPath);
7694

95+
if (!destinations.Add(outputPath))
96+
{
97+
// This case may occur when user is experimenting e.g. with multiple code generators or options.
98+
// May also occur when user accidentally duplicates OutputPath metadata.
99+
Log.LogError(Resources.FormatDuplicateFileOutputPaths(outputPath));
100+
}
101+
77102
// Add metadata which may be used as a property and passed to an inner build.
78103
newItem.SetMetadata("SerializedMetadata", MetadataSerializer.SerializeMetadata(newItem));
79104
}
80105

81106
Outputs = outputs.ToArray();
82107

83-
return true;
108+
return !Log.HasLoggedErrors;
84109
}
85110

86111
private string GetFullPath(string path)

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

Lines changed: 31 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,50 @@ 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("DocumentGenerator"));
51+
}
52+
53+
var documentPath = item.GetMetadata("DocumentPath");
54+
if (string.IsNullOrEmpty(documentPath))
55+
{
56+
var filename = item.GetMetadata("Filename");
57+
var documentName = item.GetMetadata("DocumentName");
58+
if (string.IsNullOrEmpty(documentName))
59+
{
60+
documentName = "v1";
61+
}
62+
63+
documentPath = $"{filename}.{documentName}.json";
64+
}
65+
66+
documentPath = GetFullPath(documentPath);
67+
MetadataSerializer.SetMetadata(newItem, "DocumentPath", documentPath);
4768

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

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

6180
Outputs = outputs.ToArray();
6281

63-
return true;
82+
return !Log.HasLoggedErrors;
6483
}
6584

6685
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)