public void SerializeMetadata_ReturnsExpectedString_WhenEscaping(string escapedValue)
        {
            // Arrange
            var identity       = "../files/azureMonitor.json";
            var expectedResult = $"Identity={identity}|Value={escapedValue}";
            var metadata       = new SortedDictionary <string, string>(StringComparer.Ordinal)
            {
                { "Value", escapedValue }
            };
            var input = new TaskItem(identity, metadata);

            // Act
            var result = MetadataSerializer.SerializeMetadata(input);

            // Assert
            Assert.Equal(expectedResult, result);
        }
        public void DeserializeMetadata_ReturnsExpectedTask()
        {
            // Arrange
            var identity = "../files/azureMonitor.json";
            var input    = $"Identity={identity}|ClassName=azureMonitorClient|" +
                           "CodeGenerator=NSwagCSharp|FirstForGenerator=true|Namespace=ConsoleClient|" +
                           "Options=|OriginalItemSpec=../files/azureMonitor.json|" +
                           "OutputPath=C:\\dd\\dnx\\AspNetCore\\artifacts\\obj\\ConsoleClient\\azureMonitorClient.cs";

            var expectedMetadata = new SortedDictionary <string, string>(StringComparer.Ordinal)
            {
                { "ClassName", "azureMonitorClient" },
                { "CodeGenerator", "NSwagCSharp" },
                { "FirstForGenerator", "true" },
                { "Namespace", "ConsoleClient" },
                { "Options", "" },
                { "OriginalItemSpec", identity },
                { "OutputPath", "C:\\dd\\dnx\\AspNetCore\\artifacts\\obj\\ConsoleClient\\azureMonitorClient.cs" },
            };

            // Act
            var item = MetadataSerializer.DeserializeMetadata(input);

            // Assert
            Assert.Equal(identity, item.ItemSpec);
            var metadata = Assert.IsAssignableFrom <IDictionary <string, string> >(item.CloneCustomMetadata());

            // The dictionary CloneCustomMetadata returns doesn't provide a useful KeyValuePair enumerator.
            var orderedMetadata = new SortedDictionary <string, string>(StringComparer.Ordinal);

            foreach (var key in metadata.Keys)
            {
                orderedMetadata.Add(key, metadata[key]);
            }

            Assert.Equal(expectedMetadata, orderedMetadata);
        }
        public void SerializeMetadata_ReturnsExpectedString_WithLegacyItem_WhenEscapingIdentity(
            string value,
            string escapedValue)
        {
            // Arrange
            var metadata = new SortedDictionary <string, string>(StringComparer.Ordinal)
            {
                { "Value", "a value" }
            };
            var input = new Mock <ITaskItem>(MockBehavior.Strict);

            input.SetupGet(i => i.ItemSpec).Returns(value).Verifiable();
            input.Setup(i => i.CloneCustomMetadata()).Returns(metadata).Verifiable();

            var expectedResult = $"Identity={escapedValue}|Value=a value";

            // Act
            var result = MetadataSerializer.SerializeMetadata(input.Object);

            // Assert
            Assert.Equal(expectedResult, result);
            input.VerifyGet(i => i.ItemSpec, Times.Once);
            input.Verify(i => i.CloneCustomMetadata(), Times.Once);
        }
        /// <inheritdoc />
        public override bool Execute()
        {
            var outputs        = new List <ITaskItem>(Inputs.Length);
            var codeGenerators = new HashSet <string>();
            var destinations   = new HashSet <string>();

            foreach (var item in Inputs)
            {
                var codeGenerator = item.GetMetadata("CodeGenerator");
                if (string.IsNullOrEmpty(codeGenerator))
                {
                    // This case occurs when user overrides the required metadata with an empty string.
                    var type = string.IsNullOrEmpty(item.GetMetadata("SourceProject")) ?
                               "OpenApiReference" :
                               "OpenApiProjectReference";

                    Log.LogError(
                        Resources.FormatInvalidEmptyMetadataValue("CodeGenerator", "OpenApiReference", item.ItemSpec));
                    continue;
                }

                var newItem = new TaskItem(item);
                outputs.Add(newItem);

                if (codeGenerators.Add(codeGenerator))
                {
                    newItem.SetMetadata("FirstForGenerator", "true");
                }
                else
                {
                    newItem.SetMetadata("FirstForGenerator", "false");
                }

                var outputPath = item.GetMetadata("OutputPath");
                if (string.IsNullOrEmpty(outputPath))
                {
                    // No need to further sanitize this path because the file must exist.
                    var filename     = item.GetMetadata("Filename");
                    var isTypeScript = codeGenerator.EndsWith(
                        TypeScriptLanguageName,
                        StringComparison.OrdinalIgnoreCase);

                    outputPath = $"{filename}Client{(isTypeScript ? ".ts" : Extension)}";
                }

                // Place output file in correct directory (relative to project directory).
                if (!Path.IsPathRooted(outputPath) && !string.IsNullOrEmpty(OutputDirectory))
                {
                    outputPath = Path.Combine(OutputDirectory, outputPath);
                }

                if (!destinations.Add(outputPath))
                {
                    // This case may occur when user is experimenting e.g. with multiple code generators or options.
                    // May also occur when user accidentally duplicates OutputPath metadata.
                    Log.LogError(Resources.FormatDuplicateFileOutputPaths(outputPath));
                    continue;
                }

                MetadataSerializer.SetMetadata(newItem, "OutputPath", outputPath);

                var className = item.GetMetadata("ClassName");
                if (string.IsNullOrEmpty(className))
                {
                    var outputFilename = Path.GetFileNameWithoutExtension(outputPath);

                    className = CSharpIdentifier.SanitizeIdentifier(outputFilename);
                    MetadataSerializer.SetMetadata(newItem, "ClassName", className);
                }

                var @namespace = item.GetMetadata("Namespace");
                if (string.IsNullOrEmpty(@namespace))
                {
                    MetadataSerializer.SetMetadata(newItem, "Namespace", Namespace);
                }

                // Add metadata which may be used as a property and passed to an inner build.
                newItem.RemoveMetadata("SerializedMetadata");
                newItem.SetMetadata("SerializedMetadata", MetadataSerializer.SerializeMetadata(newItem));
            }

            Outputs = outputs.ToArray();

            return(!Log.HasLoggedErrors);
        }
Example #5
0
        /// <inheritdoc />
        public override bool Execute()
        {
            Outputs = new[] { MetadataSerializer.DeserializeMetadata(Input) };

            return(true);
        }