/// <summary> /// Initializes a new instance of the <see cref="DataModelProperty"/> class. /// </summary> /// <param name="xmlSchemaDocument">The column schema.</param> public DataModelProperty(XmlSchemaDocument xmlSchemaDocument) { // Initialize the object. this.xmlSchemaDocument = xmlSchemaDocument; this.Name = this.xmlSchemaDocument.Name; // /// <summary> // /// Gets the DataModel. // /// </summary> // public DataModel DataModel { get; private set; } this.Syntax = SyntaxFactory.PropertyDeclaration( SyntaxFactory.IdentifierName(this.xmlSchemaDocument.Name), SyntaxFactory.Identifier(this.Name)) .WithAccessorList(DataModelProperty.AccessorList) .WithModifiers(DataModelProperty.Modifiers) .WithLeadingTrivia(DataModelProperty.DocumentationComment); }
/// <summary> /// Initializes a new instance of the <see cref="Class"/> class. /// </summary> /// <param name="xmlSchemaDocument">A description of a unique constraint.</param> public Class(XmlSchemaDocument xmlSchemaDocument) { // Initialize the object. this.xmlSchemaDocument = xmlSchemaDocument; this.Name = xmlSchemaDocument.Name; // /// <summary> // /// A thread-safe DataSet able to handle transactions. // /// </summary> // public partial class DataModel : IDisposable // { // <Members> // } this.Syntax = SyntaxFactory.ClassDeclaration(this.Name) .WithModifiers(Class.Modifiers) .WithMembers(this.Members) .WithLeadingTrivia(Class.DocumentationComment); }
/// <summary> /// Initializes a new instance of the <see cref="Class"/> class. /// </summary> /// <param name="xmlSchemaDocument">The document describing the data model.</param> public Class(XmlSchemaDocument xmlSchemaDocument) { // Initialize the object. this.xmlSchemaDocument = xmlSchemaDocument; this.Name = "ServiceCollectionExtension"; // /// <summary> // /// Used to add the services found in this library. // /// </summary> // public static class ServiceCollectionExtensions // { // <Members> // } this.Syntax = SyntaxFactory.ClassDeclaration(this.Name) .WithModifiers(Class.Modifiers) .WithMembers(this.Members) .WithLeadingTrivia(this.DocumentationComment); }
/// <summary> /// Initializes a new instance of the <see cref="InitializeMethod"/> class. /// </summary> /// <param name="xmlSchemaDocument">The table schema.</param> public InitializeMethod(XmlSchemaDocument xmlSchemaDocument) { // Initialize the object. this.xmlSchemaDocument = xmlSchemaDocument ?? throw new ArgumentNullException(nameof(xmlSchemaDocument)); this.Name = "Initialize"; // /// <inheritdoc/> // public async Task StartAsync(CancellationToken cancellationToken) // { // <Body> // } this.Syntax = SyntaxFactory.MethodDeclaration( SyntaxFactory.IdentifierName("Task"), SyntaxFactory.Identifier(this.Name)) .WithModifiers(InitializeMethod.Modifiers) .WithBody(this.Body) .WithLeadingTrivia(InitializeMethod.DocumentationComment); }
/// <summary> /// Initializes a new instance of the <see cref="Class"/> class. /// </summary> /// <param name="xmlSchemaDocument">A description of a unique constraint.</param> public Class(XmlSchemaDocument xmlSchemaDocument) { // Initialize the object. this.xmlSchemaDocument = xmlSchemaDocument; this.Name = $"{xmlSchemaDocument.Name}Context"; // /// <summary> // /// The DbContext for the DataModel dataModel. // /// </summary> // public class DataModelContext : DbContext // { // <Members> // } this.Syntax = SyntaxFactory.ClassDeclaration(this.Name) .WithModifiers(Class.Modifiers) .WithBaseList(Class.BaseList) .WithMembers(this.Members) .WithLeadingTrivia(this.DocumentationComment); }
/// <summary> /// Initializes a new instance of the <see cref="Class"/> class. /// </summary> /// <param name="xmlSchemaDocument">A description of a unique constraint.</param> public Class(XmlSchemaDocument xmlSchemaDocument) { // Initialize the object. this.xmlSchemaDocument = xmlSchemaDocument ?? throw new ArgumentNullException(nameof(xmlSchemaDocument)); this.Name = $"{xmlSchemaDocument.Name}Context"; // /// <summary> // /// The DbContext for the Domain domain. // /// </summary> // public class DomainContext : DbContext // { // <Members> // } this.Syntax = SyntaxFactory.ClassDeclaration(this.Name) .WithModifiers(Class.Modifiers) .WithBaseList(Class.BaseList) .WithMembers(this.Members) .WithLeadingTrivia(this.DocumentationComment); }
/// <summary> /// Initializes a new instance of the <see cref="Constructor"/> class. /// </summary> /// <param name="xmlSchemaDocument">The table schema.</param> public Constructor(XmlSchemaDocument xmlSchemaDocument) { // Initialize the object. this.xmlSchemaDocument = xmlSchemaDocument ?? throw new ArgumentNullException(nameof(xmlSchemaDocument)); this.Name = this.xmlSchemaDocument.Name; // /// <summary> // /// Initializes a new instance of the <see cref="DataModel"/> class. // /// </summary> // /// <param name="domainContext">The domain dabase context.</param> // public Domain() // { // <Body> // } this.Syntax = SyntaxFactory.ConstructorDeclaration( SyntaxFactory.Identifier(this.Name)) .WithModifiers(Constructor.Modifiers) .WithBody(this.Body) .WithLeadingTrivia(this.DocumentationComment); }
/// <summary> /// Initializes a new instance of the <see cref="OnModelCreatingMethod"/> class. /// </summary> /// <param name="xmlSchemaDocument">The XML schema document.</param> public OnModelCreatingMethod(XmlSchemaDocument xmlSchemaDocument) { // Initialize the object. this.xmlSchemaDocument = xmlSchemaDocument; this.Name = "OnModelCreating"; // /// <inheritdoc/> // protected override void OnModelCreating(ModelBuilder modelBuilder) // { // <Body> // } this.Syntax = SyntaxFactory.MethodDeclaration( SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.VoidKeyword)), SyntaxFactory.Identifier(this.Name)) .WithModifiers(OnModelCreatingMethod.Modifiers) .WithParameterList(OnModelCreatingMethod.Parameters) .WithBody(this.Body) .WithLeadingTrivia(OnModelCreatingMethod.DocumentationComment); }
/// <summary> /// Initializes a new instance of the <see cref="Constructor"/> class. /// </summary> /// <param name="xmlSchemaDocument">The table schema.</param> public Constructor(XmlSchemaDocument xmlSchemaDocument) { // Initialize the object. this.xmlSchemaDocument = xmlSchemaDocument; this.Name = this.xmlSchemaDocument.Name; // /// <summary> // /// Initializes a new instance of the <see cref="DataModel"/> class. // /// </summary> // public DataModel() // { // <Body> // } this.Syntax = SyntaxFactory.ConstructorDeclaration( SyntaxFactory.Identifier(this.Name)) .WithModifiers(Constructor.Modifiers) .WithParameterList(this.Parameters) .WithBody(this.Body) .WithLeadingTrivia(this.DocumentationComment); }
/// <summary> /// Initializes a new instance of the <see cref="DomainField"/> class. /// </summary> /// <param name="xmlSchemaDocument">The XML Schema document.</param> public DomainField(XmlSchemaDocument xmlSchemaDocument) { // Validate the argument. if (xmlSchemaDocument == null) { throw new ArgumentNullException(nameof(xmlSchemaDocument)); } // Initialize the object. this.Name = xmlSchemaDocument.Domain.ToVariableName(); // private Domain domain; this.Syntax = SyntaxFactory.FieldDeclaration( SyntaxFactory.VariableDeclaration( SyntaxFactory.IdentifierName(xmlSchemaDocument.Domain)) .WithVariables( SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>( SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier(this.Name))))) .WithModifiers(DomainField.Modifiers); }
/// <summary> /// Initializes a new instance of the <see cref="Class"/> class. /// </summary> /// <param name="tableElement">A description of a unique constraint.</param> public Class(TableElement tableElement) { // Initialize the object. this.tableElement = tableElement ?? throw new ArgumentNullException(nameof(tableElement)); this.xmlSchemaDocument = this.tableElement.XmlSchemaDocument; this.Name = tableElement.Name + "Collection"; this.rowType = this.tableElement.Name; // /// <summary> // /// The Configuration table. // /// </summary> // public partial class ConfigurationTable : IEnumerable<ConfigurationRow> // { // <Members> // } this.Syntax = SyntaxFactory.ClassDeclaration(this.Name) .WithModifiers(Class.Modifiers) .WithBaseList(this.BaseList) .WithMembers(this.Members) .WithLeadingTrivia(this.DocumentationComment); }
/// <summary> /// Initializes a new instance of the <see cref="DbContextField"/> class. /// </summary> /// <param name="xmlSchemaDocument">The XML Schema document.</param> public DbContextField(XmlSchemaDocument xmlSchemaDocument) { // Validate the parameter if (xmlSchemaDocument == null) { throw new ArgumentNullException(nameof(xmlSchemaDocument)); } // Initialize the object. this.Name = $"{xmlSchemaDocument.Domain.ToCamelCase()}Context"; // private DomainContext domainContext; this.Syntax = SyntaxFactory.FieldDeclaration( SyntaxFactory.VariableDeclaration( SyntaxFactory.IdentifierName($"{xmlSchemaDocument.Domain}Context")) .WithVariables( SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>( SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier(this.Name))))) .WithModifiers(DbContextField.Modifiers); }
/// <summary> /// Initializes a new instance of the <see cref="MergeMethod"/> class. /// </summary> /// <param name="xmlSchemaDocument">The XML document schema.</param> public MergeMethod(XmlSchemaDocument xmlSchemaDocument) { // Initialize the object. this.xmlSchemaDocument = xmlSchemaDocument; this.Name = "Merge"; // /// <summary> // /// Merge the results of an incremental update. // /// </summary> // /// <param name="jObject">The JSON object containg the incremental data.</param> // public void Merge(JObject jObject) // { this.Syntax = MethodDeclaration( PredefinedType( Token(SyntaxKind.VoidKeyword)), Identifier(this.Name)) .WithModifiers(MergeMethod.Modifiers) .WithParameterList(MergeMethod.Parameters) .WithBody(this.Body) .WithLeadingTrivia(MergeMethod.DocumentationComment); }
/// <summary> /// Initializes a new instance of the <see cref="ConstructorDbContext"/> class. /// </summary> /// <param name="xmlSchemaDocument">The table schema.</param> public ConstructorDbContext(XmlSchemaDocument xmlSchemaDocument) { // Initialize the object. this.xmlSchemaDocument = xmlSchemaDocument; this.Name = this.xmlSchemaDocument.Name; // /// <summary> // /// Initializes a new instance of the <see cref="DataModel"/> class. // /// </summary> // /// <param name="dataModelContext">The dataModel dabase context.</param> // /// <param name="logger">The log device.</param> // public DataModel(DataModelContext dataModelContext, ILogger<DataModel> logger) // { // <Body> // } this.Syntax = SyntaxFactory.ConstructorDeclaration( SyntaxFactory.Identifier(this.Name)) .WithModifiers(ConstructorDbContext.Modifiers) .WithParameterList(this.Parameters) .WithBody(this.Body) .WithLeadingTrivia(this.DocumentationComment); }
/// <summary> /// The method that does the actual work of generating code given the input file. /// </summary> /// <param name="inputFileContent">File contents as a string.</param> /// <returns>The generated code file as a byte-array.</returns> protected override byte[] GenerateCode(string inputFileContent) { byte[] buffer = Array.Empty <byte>(); try { // Extract the name of the target class from the input file name. string className = Path.GetFileNameWithoutExtension(this.InputFilePath); // This creates the compilation unit from the schema. // This schema describes the data model that is to be generated. XmlSchemaDocument xmlSchemaDocument = new XmlSchemaDocument(inputFileContent); // This creates the compilation unit from the schema. CompilationUnit compilationUnit = new CompilationUnit(xmlSchemaDocument, this.CustomToolNamespace); // A workspace is needed in order to turn the compilation unit into code. AdhocWorkspace adhocWorkspace = new AdhocWorkspace(); // Format the compilation unit. SyntaxNode syntaxNode = Formatter.Format(compilationUnit.Syntax, adhocWorkspace); // This performs the work of outputting the formatted code to a file. using (StringWriter stringWriter = new StringWriter(new StringBuilder())) { syntaxNode.WriteTo(stringWriter); buffer = Encoding.UTF8.GetBytes(stringWriter.ToString()); } } catch (Exception exception) { // This will catch any generic errors and dump them to the console. this.ErrorMessage(exception.Message); } // If successful, buffer holds the text of the output file. return(buffer); }
/// <summary> /// Initializes a new instance of the <see cref="DbContextField"/> class. /// </summary> /// <param name="xmlSchemaDocument">The XML Schema document.</param> public DbContextField(XmlSchemaDocument xmlSchemaDocument) { // Initialize the object. this.xmlSchemaDocument = xmlSchemaDocument; this.Name = $"{this.xmlSchemaDocument.Name.ToVariableName()}Context"; // /// <summary> // /// The data context for the persistent store. // /// </summary> // private DataModelContext dataModelContext; this.Syntax = SyntaxFactory.FieldDeclaration( SyntaxFactory.VariableDeclaration( SyntaxFactory.IdentifierName($"{this.xmlSchemaDocument.Name}Context")) .WithVariables( SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>( SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier(this.Name))))) .WithModifiers( SyntaxFactory.TokenList( SyntaxFactory.Token(SyntaxKind.PrivateKeyword))) .WithModifiers(DbContextField.Modifiers) .WithLeadingTrivia(DbContextField.DocumentationComment); }
/// <summary> /// Initializes a new instance of the <see cref="UseDataModelMethod"/> class. /// </summary> /// <param name="xmlSchemaDocument">The document describing the data model.</param> public UseDataModelMethod(XmlSchemaDocument xmlSchemaDocument) { // Initialize the object. this.xmlSchemaDocument = xmlSchemaDocument; this.Name = $"Use{this.xmlSchemaDocument.Name}"; // /// <summary> // /// Creates a copy of the record. // /// </summary> // /// <param name="recordVersion">The version to be produced.</param> // /// <returns>A copy of the record from the selected version.</returns> // internal Buyer Clone(RecordVersion recordVersion = RecordVersion.Current) // { // <Body> // } this.Syntax = SyntaxFactory.MethodDeclaration( SyntaxFactory.IdentifierName("IServiceCollection"), SyntaxFactory.Identifier(this.Name)) .WithModifiers(UseDataModelMethod.Modifiers) .WithParameterList(UseDataModelMethod.Parameters) .WithBody(this.Body) .WithLeadingTrivia(UseDataModelMethod.DocumentationComment); }
/// <summary> /// Initializes a new instance of the <see cref="ConstructorDbContextOptions"/> class. /// </summary> /// <param name="xmlSchemaDocument">The table schema.</param> public ConstructorDbContextOptions(XmlSchemaDocument xmlSchemaDocument) { // Initialize the object. this.xmlSchemaDocument = xmlSchemaDocument ?? throw new ArgumentNullException(nameof(xmlSchemaDocument)); this.Name = this.xmlSchemaDocument.Name; // /// <summary> // /// Initializes a new instance of the <see cref="DomainContext"/> class. // /// </summary> // /// <param name="contextOptions">The options for bulding the DbContext.</param> // public DomainContext(DbContextOptions<DomainContext> contextOptions) // : base(contextOptions) // { // <Body> // } this.Syntax = SyntaxFactory.ConstructorDeclaration( SyntaxFactory.Identifier($"{this.xmlSchemaDocument.Name}Context")) .WithModifiers(ConstructorDbContextOptions.Modifiers) .WithParameterList(this.Parameters) .WithInitializer(ConstructorDbContextOptions.Initializer) .WithBody(ConstructorDbContextOptions.Body) .WithLeadingTrivia(this.DocumentationComment); }
/// <summary> /// Orders and beautifies an XML document. /// </summary> /// <param name="source">The source XML document to order and beautify.</param> /// <param name="extension">The extension. Used to determine tab spacing.</param> /// <returns>The text of the scrubbed and beautified document.</returns> private static string ScrubDocument(string source, string extension) { // Used to write the final document in the form of a long string. using (Utf8StringWriter stringWriter = new Utf8StringWriter()) { try { // Read the source document. using (StringReader stringReader = new StringReader(source)) { XmlSchemaDocument xmlSchemaDocument = new XmlSchemaDocument(stringReader.ReadToEnd()); // Regurgitate the schema as an XDocument XElement schemaElement = new XElement( ScrubXsdCommand.xs + "schema", new XAttribute("id", xmlSchemaDocument.Name), new XAttribute("targetNamespace", xmlSchemaDocument.TargetNamespace)); XDocument targetDocument = new XDocument(schemaElement); // This will populate the namespace manager from the namespaces in the root element. foreach (XAttribute xAttribute in xmlSchemaDocument.Root.Attributes()) { if (xAttribute.IsNamespaceDeclaration) { // Place the remaining namespace attributes in the root element of the schema description. schemaElement.Add(new XAttribute(xAttribute.Name, xAttribute.Value)); } } // <xs:element name="Domain"> XElement dataModelElement = new XElement(ScrubXsdCommand.xs + "element", new XAttribute("name", xmlSchemaDocument.Name)); // This specifies the data domain used by the REST generated code. if (xmlSchemaDocument.Domain != null) { dataModelElement.SetAttributeValue(XmlSchemaDocument.DomainName, xmlSchemaDocument.Domain); } // This flag indicates that the API uses tokens for authentication. if (xmlSchemaDocument.IsSecure.HasValue) { dataModelElement.SetAttributeValue(XmlSchemaDocument.IsSecureName, xmlSchemaDocument.IsSecure.Value); } // This flag indicates that the interface is not committed to a peristent store. if (xmlSchemaDocument.IsVolatile.HasValue) { dataModelElement.SetAttributeValue(XmlSchemaDocument.IsVolatileName, xmlSchemaDocument.IsVolatile.Value); } // <xs:complexType> XElement dataModelComlexTypeElement = new XElement(ScrubXsdCommand.xs + "complexType"); // <xs:choice maxOccurs="unbounded"> XElement dataModelChoices = new XElement(ScrubXsdCommand.xs + "choice", new XAttribute("maxOccurs", "unbounded")); // This will scrub and add each of the tables to the schema in alphabetical order. foreach (TableElement tableElement in xmlSchemaDocument.Tables.OrderBy(t => t.Name)) { dataModelChoices.Add(ScrubXsdCommand.CreateTable(tableElement)); } // The complex types that define the tables. dataModelComlexTypeElement.Add(dataModelChoices); dataModelElement.Add(dataModelComlexTypeElement); // This will order the primary keys. List <UniqueKeyElement> primaryKeyList = new List <UniqueKeyElement>(); foreach (TableElement tableElement in xmlSchemaDocument.Tables) { primaryKeyList.AddRange(tableElement.UniqueKeys); } foreach (UniqueKeyElement uniqueConstraintSchema in primaryKeyList.OrderBy(pke => pke.Name)) { dataModelElement.Add(CreateUniqueKey(uniqueConstraintSchema)); } // This will order the foreign primary keys. List <ForeignKeyElement> foreignKeyList = new List <ForeignKeyElement>(); foreach (TableElement tableElement in xmlSchemaDocument.Tables) { foreignKeyList.AddRange(tableElement.ForeignKeys); } foreach (ForeignKeyElement foreignConstraintSchema in foreignKeyList.OrderBy(fke => fke.Name)) { dataModelElement.Add(CreateForeignKey(foreignConstraintSchema)); } // Add the data model element to the document. schemaElement.Add(dataModelElement); // Save the regurgitated output. targetDocument.Save(stringWriter); } } catch (Exception exception) { // Show a message box to prove we were here VsShellUtilities.ShowMessageBox( ScrubXsdCommand.serviceProvider, exception.Message, Resources.EditorExtensionsTitle, OLEMSGICON.OLEMSGICON_CRITICAL, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); } // The result of beautifying a long string of XML is another long string that is scrubbed and beautified. This string will be used // to replace the original content of the module. return(stringWriter.ToString()); } }