private static AssemblyAttribute ExtractAttributeFromItem(ITaskItem item) { var type = item.ItemSpec; // Some attributes only allow positional constructor arguments, or the user may just prefer them. // To set those, use metadata names like "_Parameter1", "_Parameter2" etc. // If a parameter index is skipped, it's an error. var customMetadata = item.CloneCustomMetadata() ?? new Dictionary <string, string>(); var orderedParameters = new List <object?>(customMetadata.Count + 1 /* max possible slots needed */); var namedParameters = new Dictionary <string, object?>(); foreach (var customMetadataEntry in customMetadata) { if (customMetadataEntry is not DictionaryEntry entry) { continue; } var name = (string)entry.Key; var valueStr = (string?)entry.Value; if (!CodeGeneratorConfigurationUtility.TryParseConstantValue(valueStr, out var value)) { throw new BuildErrorException(Strings.LiteralAssemblyAttributes.InvalidParameterValueFmt, name); } if (name.StartsWith("_Parameter", StringComparison.OrdinalIgnoreCase)) { if (!int.TryParse(name["_Parameter".Length..], out var index))
private static Constant ExtractConstantDefinitionFromItem(ITaskItem item) { var name = item.ItemSpec.Trim(); var valueStr = item.GetMetadata("Value")?.Trim() ?? "<null>"; if (!CodeGeneratorConfigurationUtility.TryParseConstantValue(valueStr, out var value)) { throw new BuildErrorException(Strings.ThisAssemblyClass.InvalidConstantValueFmt, name, valueStr); } return(new Constant(name, value)); }
protected sealed override void Run() { if (!CodeGeneratorConfigurationUtility.TryParseLanguage(Language, out var codeGeneratorLanguage)) { throw new BuildErrorException(Strings.UnsupportedLanguageFmt, Language ?? "<null>"); } if (StringUtility.IsNullOrEmpty(OutputPath)) { throw new BuildErrorException(Strings.MissingParameterFmt, nameof(OutputPath)); } var generatedCode = new GeneratedCode(codeGeneratorLanguage, GetCodeFragments()); var code = CodeGenerator.GenerateCode(generatedCode); File.WriteAllText(OutputPath, code, Encoding.UTF8); }
public void GenerateCode_ReturnsNonEmptyString(string languageStr) { var assemblyAttribute = new AssemblyAttribute( "System.Runtime.InteropServices.ComVisible", new[] { new OrderedParameter(false) }); var thisAssemblyClass = new ThisAssemblyClass( null, "ThisAssembly", new[] { new Constant("Answer", 42) }); _ = CodeGeneratorConfigurationUtility.TryParseLanguage(languageStr, out var language); var configuration = new GeneratedCode( language, new CodeFragment[] { assemblyAttribute, thisAssemblyClass }); var code = CodeGenerator.GenerateCode(configuration); Assert.IsNotNull(code); Assert.IsNotEmpty(code); }