public static async Task GenerateEditableInterface(DocumentWithCodeAnalysis document) { string filePath = document.FullFilePath; string destinationPath = Path.Combine( Path.GetDirectoryName(filePath).Replace(@"CSharpDom\Common", @"CSharpDom\Common\Editable"), GetNewName(Path.GetFileName(filePath))); if (File.Exists(destinationPath)) { return; } LoadedDocumentWithCodeAnalysis loadedDocument = await document.LoadAsync(); NamespaceWithCodeAnalysis @namespace = loadedDocument.Namespaces.First(); if (@namespace.Interfaces.Count == 0) { return; } Console.WriteLine($"Writing: {Path.GetFileName(destinationPath)}"); string namespaceName = @namespace.Name; using (CodeAnalysisSettings.AllowEdits(loadedDocument)) { EditUsingDirectives(loadedDocument, namespaceName); @namespace.Name = Regex.Replace(namespaceName, "^CSharpDom.Common", "CSharpDom.Common.Editable"); InterfaceWithCodeAnalysis @interface = @namespace.Interfaces.First(); string interfaceName = @interface.Name; @interface.Name = GetNewName(interfaceName); List <ITypeReferenceWithCodeAnalysis> genericParameters = EditInterfaceGenericParameters(@interface); EditInterfaceBaseInterfaces(@interface, interfaceName, genericParameters); EditInterfaceProperties(@interface); } Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)); const int maximumLineLength = 120; string sourceCode = loadedDocument.ToSourceCode( new IndentBaseTypeListIfTooLongRule(maximumLineLength), new IndentGenericParamterDefinitionsIfTooLongRule(maximumLineLength), new IndentMethodParametersIfTooLongRule(maximumLineLength)); File.WriteAllText(destinationPath, sourceCode); }
internal static PartialClassCollectionWithCodeAnalysis Create(NamespaceWithCodeAnalysis @namespace) { var members = @namespace.Members; var abstractClasses = new NamespaceMemberListWrapper <AbstractPartialClassWithCodeAnalysis, ClassDeclarationSyntax>( @namespace.Node, () => new AbstractPartialClassWithCodeAnalysis(@namespace.Document), ClassDeclarationSyntaxExtensions.IsAbstractPartialClass); var classes = new NamespaceMemberListWrapper <PartialClassWithCodeAnalysis, ClassDeclarationSyntax>( @namespace.Node, () => new PartialClassWithCodeAnalysis(@namespace.Document), ClassDeclarationSyntaxExtensions.IsPartialClass); var sealedClasses = new NamespaceMemberListWrapper <SealedPartialClassWithCodeAnalysis, ClassDeclarationSyntax>( @namespace.Node, () => new SealedPartialClassWithCodeAnalysis(@namespace.Document), ClassDeclarationSyntaxExtensions.IsSealedPartialClass); var staticClasses = new NamespaceMemberListWrapper <StaticPartialClassWithCodeAnalysis, ClassDeclarationSyntax>( @namespace.Node, () => new StaticPartialClassWithCodeAnalysis(@namespace.Document), ClassDeclarationSyntaxExtensions.IsStaticPartialClass); return(new PartialClassCollectionWithCodeAnalysis(members, abstractClasses, classes, sealedClasses, staticClasses)); }
private static async Task GenerateEditableWrappers() { string baseDirectory = Path.GetFullPath( Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), @"..\..\..\..")); ProjectWithCodeAnalysis project = await ProjectWithCodeAnalysis.OpenAsync( Path.Combine(baseDirectory, @"CSharpDom\CSharpDom.csproj")); project.Lock(); foreach (DocumentWithCodeAnalysis document in project.Documents.OrderBy(document => document.FullFilePath)) { document.IsLocked = true; string filePath = document.FullFilePath; if (!filePath.Contains(@"CSharpDom\BaseClasses\Wrappers")) { continue; } string newDirectory = Path.GetDirectoryName(filePath) .Replace(@"CSharpDom\BaseClasses\Wrappers", @"CSharpDom\BaseClasses\Editable\Wrappers"); string destinationPath = Path.Combine(newDirectory, GetNewName(Path.GetFileName(filePath))); if (File.Exists(destinationPath)) { continue; } LoadedDocumentWithCodeAnalysis loadedDocument = await document.LoadAsync(); NamespaceWithCodeAnalysis @namespace = loadedDocument.Namespaces.First(); Console.WriteLine($"Writing: {Path.GetFileName(destinationPath)}"); string namespaceName = @namespace.Name; using (CodeAnalysisSettings.AllowEdits(loadedDocument)) { foreach (UsingDirectiveWithCodeAnalysis directive in loadedDocument.UsingDirectives) { directive.Name = directive.Name.Replace("CSharpDom.Common", "CSharpDom.Common.Editable"); } loadedDocument.UsingDirectives.Add(new UsingDirectiveWithCodeAnalysis("CSharpDom.Common")); loadedDocument.UsingDirectives = loadedDocument.UsingDirectives .OrderBy(directive => directive.Name) .ToArray(); @namespace.Name = "CSharpDom.BaseClasses.Editable.Wrappers"; SealedClassWithCodeAnalysis @class = @namespace.Classes.SealedClasses.First(); @class.Name = "Editable" + @class.Name; @class.BaseClass.Name = Regex.Replace(@class.BaseClass.Name, "^Abstract", "Editable"); ITypeReferenceWithCodeAnalysis interfaceReference = @class.ImplementedInterfaces.First().GenericParameters[0]; interfaceReference.Name = Regex.Replace(interfaceReference.Name, "^I", "IEditable"); foreach (GenericParameterDeclarationWithCodeAnalysis genericParameter in @class.GenericParameters) { InterfaceReferenceWithCodeAnalysis constraint = genericParameter.InterfaceConstraints.First(); constraint.Name = Regex.Replace(constraint.Name, "^I", "IEditable"); } ITypeReferenceWithCodeAnalysis constructorParameterType = @class.Constructors.First().Parameters[0].ParameterType; constructorParameterType.Name = Regex.Replace(constructorParameterType.Name, "^I", "IEditable"); foreach (SealedClassAutoPropertyWithCodeAnalysis property in @class.Properties.AutoProperties) { if (property.Name == "WrappedObject") { //CodeAnalysisLogger.StartLoggingDebugMessages(); property.PropertyType.Name = Regex.Replace(property.PropertyType.Name, "^I", "IEditable"); //string[] logMessages = CodeAnalysisLogger.GetDebugLogMessages(); //CodeAnalysisLogger.StopLoggingDebugMessages(); break; } } foreach (SealedClassPropertyWithCodeAnalysis property in @class.Properties) { string propertyName = property.Name; if (propertyName == "WrappedObject") { continue; } string propertyTypeName = property.PropertyType.Name; if (propertyTypeName.Contains("ReadOnly")) { property.PropertyType.Name = propertyTypeName.Replace("ReadOnly", string.Empty); } IExpressionWithCodeAnalysis expression = ExpressionFactory.Binary( ExpressionFactory.Member(ExpressionFactory.Identifier("WrappedObject"), propertyName), BinaryOperatorExpressionType.Assign, ExpressionFactory.Identifier("value")); property.SetAccessor = new ClassAccessorWithCodeAnalysis( AccessorType.Set, new MethodBodyWithCodeAnalysis(StatementFactory.Expression(expression))); } Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)); const int maximumLineLength = 120; string sourceCode = loadedDocument.ToSourceCode( new IndentBaseTypeListIfTooLongRule(maximumLineLength), new IndentGenericParamterDefinitionsIfTooLongRule(maximumLineLength), new IndentMethodParametersIfTooLongRule(maximumLineLength)); File.WriteAllText(destinationPath, sourceCode); } } }
public static async Task GenerateBaseClass(DocumentWithCodeAnalysis document) { string filePath = document.FullFilePath; string destinationPath = Path.Combine( Path.GetDirectoryName(filePath).Replace(@"CSharpDom\Common", @"CSharpDom\BaseClasses"), GetNewName(Path.GetFileName(filePath))); if (File.Exists(destinationPath)) { return; } LoadedDocumentWithCodeAnalysis loadedDocument = await document.LoadAsync(); NamespaceWithCodeAnalysis @namespace = loadedDocument.Namespaces.First(); if (@namespace.Interfaces.Count == 0) { return; } InterfaceWithCodeAnalysis @interface = @namespace.Interfaces.First(); if (@interface.GenericParameters.Count == 0) { return; } Console.WriteLine($"Writing: {Path.GetFileName(destinationPath)}"); string namespaceName = @namespace.Name; using (CodeAnalysisSettings.AllowEdits(loadedDocument)) { LoadedDocumentWithCodeAnalysis newDocument = new LoadedDocumentWithCodeAnalysis(); newDocument.UsingDirectives.Add(new UsingDirectiveWithCodeAnalysis(namespaceName)); DocumentWithCodeAnalysis baseClassDocument = document.Project.AddDocument( destinationPath, newDocument); NamespaceWithCodeAnalysis newNamespace = new NamespaceWithCodeAnalysis( baseClassDocument, namespaceName.Replace("Common", "BaseClasses")); AbstractClassWithCodeAnalysis baseClass = new AbstractClassWithCodeAnalysis( baseClassDocument, TypeVisibilityModifier.Public, GetNewName(@interface.Name)) { GenericParameters = @interface.GenericParameters, BaseClass = new ClassReferenceWithCodeAnalysis("AbstractExpression") }; ITypeReferenceWithCodeAnalysis[] implementedInterfaceGenericParameters = @interface.GenericParameters .Select(parameter => new GenericParameterReferenceWithCodeAnalysis(parameter.Name)) .ToArray(); baseClass.ImplementedInterfaces.Add( new InterfaceReferenceWithCodeAnalysis(@interface.Name, implementedInterfaceGenericParameters)); newNamespace.Classes.AbstractClasses.Add(baseClass); newDocument.Namespaces.Add(newNamespace); await baseClass.ImplementInterfaceAsync(@class => @class.ImplementedInterfaces.First(), baseClassDocument); foreach (AbstractClassPropertyWithCodeAnalysis property in baseClass.Properties) { } } Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)); const int maximumLineLength = 120; string sourceCode = loadedDocument.ToSourceCode( new IndentBaseTypeListIfTooLongRule(maximumLineLength), new IndentGenericParamterDefinitionsIfTooLongRule(maximumLineLength), new IndentMethodParametersIfTooLongRule(maximumLineLength)); File.WriteAllText(destinationPath, sourceCode); }