private static async Task GenerateCode() { ContractDefinition contract = InterfaceContractDefinitionBuilder.BuildContract(typeof(IMyContract)); string transmitterImplementationName = $"GeneratedTransmitter_{contract.Name}"; TransmitterGenerator transmitterGenerator = new(); string generatedTransmitterFile = transmitterGenerator.Run( contract, transmitterImplementationName, "RoRamu.Decoupler.DotNet.Generator.Test" ); string receiverImplementationName = $"GeneratedReceiver_{contract.Name}"; ReceiverGenerator receiverGenerator = new(); string generatedReceiverFile = receiverGenerator.Run( contract, receiverImplementationName, "RoRamu.Decoupler.DotNet.Generator.Test" ); string directory = Path.GetFullPath("generated"); Directory.CreateDirectory(directory); Console.WriteLine(generatedTransmitterFile); await File.WriteAllTextAsync(Path.Combine(directory, $"{transmitterImplementationName}.cs"), generatedTransmitterFile); Console.WriteLine(generatedReceiverFile); await File.WriteAllTextAsync(Path.Combine(directory, $"{receiverImplementationName}.cs"), generatedReceiverFile); }
public static void Run(Options options) { string assemblyFile = options.AssemblyFilePath; Component component = options.Component; string outputDirectory = options.OutputDirectory; string @namespace = options.Namespace; CSharpAccessModifier accessModifier = options.AccessModifier; if (!File.Exists(assemblyFile)) { throw new ArgumentException($"Assembly file '{assemblyFile}' does not exist.", nameof(assemblyFile)); } if (string.IsNullOrWhiteSpace(@namespace)) { throw new ArgumentException("The provided namespace was empty.", nameof(@namespace)); } // Load the assembly which contains the interfaces to generate from Assembly assembly = Assembly.LoadFrom(assemblyFile); // Create the generator Generator generator = component switch { Component.Transmitter => new TransmitterGenerator(), Component.Receiver => new ReceiverGenerator(), _ => throw new ArgumentException("Unknown Decoupler component type.", nameof(Options.Component)) }; // Create the output directory if it doesn't exist if (!Directory.Exists(outputDirectory)) { Directory.CreateDirectory(outputDirectory); } // Iterate over each interface, generating the output foreach (Type @interface in GetInterfaces(assembly)) { // Build the contract definition from the interface ContractDefinition contract = InterfaceContractDefinitionBuilder.BuildContract(@interface); // Create the names for the transmitter and receiver classes string className = $"{component}_{@interface.GetCSharpName(identifierOnly: true, includeNamespace: false)}"; // Generate the transmitter code string code = generator.Run(contract, className, @namespace, accessModifier); string outputFilePath = Path.Combine(outputDirectory, $"{className}.generated.cs"); File.WriteAllText(outputFilePath, code); } }