protected override void GenerateTransformCode(string directiveName, global::System.Text.StringBuilder codeBuffer, global::System.CodeDom.Compiler.CodeDomProvider languageProvider, global::System.Collections.Generic.IDictionary <string, string> requiresArguments, global::System.Collections.Generic.IDictionary <string, string> providesArguments)
        {
            if ((global::System.StringComparer.OrdinalIgnoreCase.Compare(directiveName, supportedDirectiveName) == 0))
            {
                if (languageProvider != null)
                {
                    // Create a field to store the model once it is loaded
                    global::System.CodeDom.CodeMemberField rootElement = new global::System.CodeDom.CodeMemberField();
                    rootElement.Name       = providesArguments[providesModelParameter].ToLower(global::System.Globalization.CultureInfo.InvariantCulture) + "Value";
                    rootElement.Type       = new global::System.CodeDom.CodeTypeReference(typeof(global::CompanyName.LanguageSm.ExampleModel));
                    rootElement.Attributes = global::System.CodeDom.MemberAttributes.Private;

                    // Create a property for the Model that delay-loads the model
                    global::System.CodeDom.CodeMemberProperty rootElementProperty = new global::System.CodeDom.CodeMemberProperty();
                    rootElementProperty.Name       = providesArguments[providesModelParameter];
                    rootElementProperty.Type       = new global::System.CodeDom.CodeTypeReference(typeof(global::CompanyName.LanguageSm.ExampleModel));
                    rootElementProperty.Attributes = global::System.CodeDom.MemberAttributes.Private;
                    rootElementProperty.HasSet     = false;
                    rootElementProperty.HasGet     = true;
                    rootElementProperty.GetStatements.Add(new global::System.CodeDom.CodeMethodReturnStatement(new global::System.CodeDom.CodeFieldReferenceExpression(new global::System.CodeDom.CodeThisReferenceExpression(), rootElement.Name)));

                    // Create a helper method for resolving relative paths
                    global::System.CodeDom.CodeMemberMethod resolver = null;
                    if (!this.oneTimeCodeGenerated && global::System.IO.File.Exists(this.Host.TemplateFile))
                    {
                        resolver            = new global::System.CodeDom.CodeMemberMethod();
                        resolver.Name       = "Convert" + "ExampleModel" + "RelativePathToTemplateRelativePath";
                        resolver.ReturnType = new global::System.CodeDom.CodeTypeReference(typeof(string));
                        resolver.Parameters.Add(new global::System.CodeDom.CodeParameterDeclarationExpression(typeof(string), "path"));
                        resolver.Attributes = global::System.CodeDom.MemberAttributes.Public;
                        global::System.CodeDom.CodeVariableDeclarationStatement declaration = new global::System.CodeDom.CodeVariableDeclarationStatement(typeof(string), "modelPath", new global::System.CodeDom.CodePrimitiveExpression((string)(requiresArguments[requiresFileParameter])));
                        resolver.Statements.Add(declaration);
                        declaration = new global::System.CodeDom.CodeVariableDeclarationStatement(typeof(string), "templatePath", new global::System.CodeDom.CodePrimitiveExpression((string)(this.Host.TemplateFile)));
                        resolver.Statements.Add(declaration);
                        global::System.CodeDom.CodeMethodReturnStatement returnStatement = new global::System.CodeDom.CodeMethodReturnStatement(
                            new global::System.CodeDom.CodeMethodInvokeExpression(
                                new global::System.CodeDom.CodeMethodReferenceExpression(new global::System.CodeDom.CodeTypeReferenceExpression("Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation"), "ConvertModelRelativePathToTemplateRelativePath"),
                                new global::System.CodeDom.CodeVariableReferenceExpression("modelPath"),
                                new global::System.CodeDom.CodeVariableReferenceExpression("templatePath"),
                                new global::System.CodeDom.CodeVariableReferenceExpression("path")));
                        resolver.Statements.Add(returnStatement);
                    }

                    // Generate the actual code using the CodeDomProvider
                    global::System.CodeDom.Compiler.CodeGeneratorOptions options = new global::System.CodeDom.Compiler.CodeGeneratorOptions();
                    options.BlankLinesBetweenMembers = true;
                    options.IndentString             = "    ";
                    options.VerbatimOrder            = true;
                    options.BracingStyle             = "C";
                    using (global::System.IO.StringWriter writer = new global::System.IO.StringWriter(codeBuffer, global::System.Globalization.CultureInfo.InvariantCulture))
                    {
                        languageProvider.GenerateCodeFromMember(rootElement, writer, options);
                        languageProvider.GenerateCodeFromMember(rootElementProperty, writer, options);
                        if (resolver != null)
                        {
                            languageProvider.GenerateCodeFromMember(resolver, writer, options);
                        }
                    }
                    this.oneTimeCodeGenerated = true;
                }
            }
        }
Exemplo n.º 2
0
        protected override void GeneratePostInitializationCode(string directiveName, global::System.Text.StringBuilder codeBuffer, global::System.CodeDom.Compiler.CodeDomProvider languageProvider, global::System.Collections.Generic.IDictionary <string, string> requiresArguments, global::System.Collections.Generic.IDictionary <string, string> providesArguments)
        {
            if ((global::System.StringComparer.OrdinalIgnoreCase.Compare(directiveName, supportedDirectiveName) == 0))
            {
                // We check the encoding of the model file, and ask the host to use that
                // encoding to write the output. This is to make sure we use the correct encoding
                // if the model file contains international characters that could be used in
                // the template output.
                string fileName = requiresArguments[requiresFileParameter];
                if (!string.IsNullOrEmpty(fileName) && global::System.IO.File.Exists(fileName))
                {
                    Host.SetOutputEncoding(VSTextTemplating::EncodingHelper.GetEncoding(fileName), false);
                }

                if (languageProvider != null)
                {
                    global::System.CodeDom.CodeMethodInvokeExpression invokeLoad;
                    global::System.String transactionName = providesArguments[providesModelParameter].ToLower(global::System.Globalization.CultureInfo.InvariantCulture) + "Transaction";
                    global::System.CodeDom.CodeVariableDeclarationStatement transactionDeclaration = new global::System.CodeDom.CodeVariableDeclarationStatement(new global::System.CodeDom.CodeTypeReference(typeof(DslModeling::Transaction)), transactionName, new global::System.CodeDom.CodePrimitiveExpression(null));
                    global::System.CodeDom.CodeStatement finallyStatement = new global::System.CodeDom.CodeConditionStatement(new global::System.CodeDom.CodeBinaryOperatorExpression(new global::System.CodeDom.CodeVariableReferenceExpression(transactionName), global::System.CodeDom.CodeBinaryOperatorType.IdentityInequality, new global::System.CodeDom.CodePrimitiveExpression(null)), new global::System.CodeDom.CodeExpressionStatement(new global::System.CodeDom.CodeMethodInvokeExpression(new global::System.CodeDom.CodeVariableReferenceExpression(transactionName), "Dispose")));
                    global::System.Collections.Generic.List <global::System.CodeDom.CodeStatement> txTryStatements = new global::System.Collections.Generic.List <global::System.CodeDom.CodeStatement>();
                    global::System.CodeDom.CodeVariableDeclarationStatement serializationResultDeclaration         = new global::System.CodeDom.CodeVariableDeclarationStatement(new global::System.CodeDom.CodeTypeReference(typeof(DslModeling::SerializationResult)), "serializationResult", new global::System.CodeDom.CodeObjectCreateExpression(new global::System.CodeDom.CodeTypeReference(typeof(DslModeling::SerializationResult))));

                    global::System.CodeDom.CodeMethodInvokeExpression       invokeEnableDiagramRules = null;
                    global::System.CodeDom.CodeVariableDeclarationStatement diagramFileDeclaration   = null;
                    global::System.CodeDom.CodeAssignStatement diagramFileAssign = null;
                    if (LoadDiagramData)
                    {
                        // generate code to enable diagram fixup rules and load the diagram if required.
                        invokeEnableDiagramRules = new global::System.CodeDom.CodeMethodInvokeExpression(
                            new global::System.CodeDom.CodeMethodReferenceExpression(new global::System.CodeDom.CodeTypeReferenceExpression("GEB_NGN_PCR_Practica1DomainModel"), "EnableDiagramRules"), new global::System.CodeDom.CodePropertyReferenceExpression(new global::System.CodeDom.CodeThisReferenceExpression(), "Store"));
                        diagramFileDeclaration = new global::System.CodeDom.CodeVariableDeclarationStatement(new global::System.CodeDom.CodeTypeReference("System.String"), "diagramFileName");
                        diagramFileAssign      = new global::System.CodeDom.CodeAssignStatement(new global::System.CodeDom.CodeVariableReferenceExpression("diagramFileName"), new global::System.CodeDom.CodeBinaryOperatorExpression(new global::System.CodeDom.CodePrimitiveExpression(requiresArguments[requiresFileParameter]), global::System.CodeDom.CodeBinaryOperatorType.Add, new global::System.CodeDom.CodePrimitiveExpression(".diagram")));
                        invokeLoad             = new global::System.CodeDom.CodeMethodInvokeExpression(
                            new global::System.CodeDom.CodeMethodReferenceExpression(new global::System.CodeDom.CodeFieldReferenceExpression(new global::System.CodeDom.CodeTypeReferenceExpression(typeof(GEB_NGN_PCR_Practica1SerializationHelper)), "Instance"), "LoadModelAndDiagram"), new global::System.CodeDom.CodeVariableReferenceExpression("serializationResult"), new global::System.CodeDom.CodePropertyReferenceExpression(new global::System.CodeDom.CodeThisReferenceExpression(), "Store"), new global::System.CodeDom.CodePrimitiveExpression(requiresArguments[requiresFileParameter]), new global::System.CodeDom.CodeVariableReferenceExpression("diagramFileName"), new global::System.CodeDom.CodePrimitiveExpression(null), new global::System.CodeDom.CodePrimitiveExpression(null), new global::System.CodeDom.CodePrimitiveExpression(null));
                    }
                    else
                    {
                        invokeLoad = new global::System.CodeDom.CodeMethodInvokeExpression(
                            new global::System.CodeDom.CodeMethodReferenceExpression(new global::System.CodeDom.CodeFieldReferenceExpression(new global::System.CodeDom.CodeTypeReferenceExpression(typeof(GEB_NGN_PCR_Practica1SerializationHelper)), "Instance"), "LoadModel"), new global::System.CodeDom.CodeVariableReferenceExpression("serializationResult"), new global::System.CodeDom.CodePropertyReferenceExpression(new global::System.CodeDom.CodeThisReferenceExpression(), "Store"), new global::System.CodeDom.CodePrimitiveExpression(requiresArguments[requiresFileParameter]), new global::System.CodeDom.CodePrimitiveExpression(null), new global::System.CodeDom.CodePrimitiveExpression(null), new global::System.CodeDom.CodePrimitiveExpression(null));
                    }

                    global::System.CodeDom.CodeAssignStatement loadAssign = new global::System.CodeDom.CodeAssignStatement(new global::System.CodeDom.CodeFieldReferenceExpression(new global::System.CodeDom.CodeThisReferenceExpression(), providesArguments[providesModelParameter].ToLower(global::System.Globalization.CultureInfo.InvariantCulture) + "Value"), invokeLoad);
                    txTryStatements.Add(serializationResultDeclaration);
                    txTryStatements.Add(new global::System.CodeDom.CodeAssignStatement(new global::System.CodeDom.CodeVariableReferenceExpression(transactionName), new global::System.CodeDom.CodeMethodInvokeExpression(new global::System.CodeDom.CodePropertyReferenceExpression(new global::System.CodeDom.CodePropertyReferenceExpression(new global::System.CodeDom.CodeThisReferenceExpression(), "Store"), "TransactionManager"), "BeginTransaction", new global::System.CodeDom.CodePrimitiveExpression("Load"), new global::System.CodeDom.CodePrimitiveExpression(true))));
                    txTryStatements.Add(loadAssign);
                    global::System.CodeDom.CodeConditionStatement serializationResultCheck = new global::System.CodeDom.CodeConditionStatement(
                        new global::System.CodeDom.CodePropertyReferenceExpression(new global::System.CodeDom.CodeVariableReferenceExpression("serializationResult"), "Failed"),
                        new global::System.CodeDom.CodeThrowExceptionStatement(new global::System.CodeDom.CodeObjectCreateExpression(new global::System.CodeDom.CodeTypeReference(typeof(DslModeling::SerializationException)), new global::System.CodeDom.CodeVariableReferenceExpression("serializationResult")))
                        );
                    txTryStatements.Add(serializationResultCheck);
                    txTryStatements.Add(new global::System.CodeDom.CodeExpressionStatement(new global::System.CodeDom.CodeMethodInvokeExpression(new global::System.CodeDom.CodeVariableReferenceExpression(transactionName), "Commit")));
                    global::System.CodeDom.CodeTryCatchFinallyStatement txTryStatement = new global::System.CodeDom.CodeTryCatchFinallyStatement(txTryStatements.ToArray(), new global::System.CodeDom.CodeCatchClause[] { }, new global::System.CodeDom.CodeStatement[] { finallyStatement });


                    // We check if the user has requested validation to be performed. If so, we call
                    // ValidateStore() on the base ModelingTextTransformation class.
                    string validationCategories = requiresArguments[requiresValidationParameter];
                    global::System.CodeDom.CodeMethodInvokeExpression validationInvoke = null;
                    if (!string.IsNullOrEmpty(validationCategories))
                    {
                        validationInvoke = new global::System.CodeDom.CodeMethodInvokeExpression(new global::System.CodeDom.CodeThisReferenceExpression(), "ValidateStore", new global::System.CodeDom.CodePrimitiveExpression(validationCategories), new global::System.CodeDom.CodePropertyReferenceExpression(new global::System.CodeDom.CodeThisReferenceExpression(), "Errors"));
                    }

                    global::System.CodeDom.Compiler.CodeGeneratorOptions options = new global::System.CodeDom.Compiler.CodeGeneratorOptions();
                    options.BlankLinesBetweenMembers = true;
                    options.IndentString             = "    ";
                    options.VerbatimOrder            = true;
                    options.BracingStyle             = "C";
                    using (global::System.IO.StringWriter writer = new global::System.IO.StringWriter(codeBuffer, global::System.Globalization.CultureInfo.InvariantCulture))
                    {
                        if (invokeEnableDiagramRules != null)
                        {
                            languageProvider.GenerateCodeFromStatement(new global::System.CodeDom.CodeExpressionStatement(invokeEnableDiagramRules), writer, options);
                        }

                        if (diagramFileDeclaration != null)
                        {
                            languageProvider.GenerateCodeFromStatement(diagramFileDeclaration, writer, options);
                            languageProvider.GenerateCodeFromStatement(diagramFileAssign, writer, options);
                        }

                        languageProvider.GenerateCodeFromStatement(transactionDeclaration, writer, options);
                        languageProvider.GenerateCodeFromStatement(txTryStatement, writer, options);

                        if (validationInvoke != null)
                        {
                            languageProvider.GenerateCodeFromStatement(new global::System.CodeDom.CodeExpressionStatement(validationInvoke), writer, options);
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Override StartProcessingRun to reset the helpers flag.
 /// </summary>
 /// <param name="languageProvider"></param>
 /// <param name="templateContents"></param>
 /// <param name="errors"></param>
 public override void StartProcessingRun(global::System.CodeDom.Compiler.CodeDomProvider languageProvider, string templateContents, global::System.CodeDom.Compiler.CompilerErrorCollection errors)
 {
     this.oneTimeCodeGenerated = false;
     base.StartProcessingRun(languageProvider, templateContents, errors);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Contribute additively to initialization code for the TextTransformation generated class.
        /// </summary>
        /// <remarks>
        /// This code will be added before the call to the base class.
        /// </remarks>
        /// <param name="directiveName"></param>
        /// <param name="codeBuffer"></param>
        /// <param name="languageProvider"></param>
        /// <param name="requiresArguments"></param>
        /// <param name="providesArguments"></param>
        protected override void GeneratePreInitializationCode(string directiveName, global::System.Text.StringBuilder codeBuffer, global::System.CodeDom.Compiler.CodeDomProvider languageProvider, global::System.Collections.Generic.IDictionary <string, string> requiresArguments, global::System.Collections.Generic.IDictionary <string, string> providesArguments)
        {
            if ((global::System.StringComparer.OrdinalIgnoreCase.Compare(directiveName, supportedDirectiveName) == 0))
            {
                if (languageProvider != null)
                {
                    string[] domainModels =
                    {
                        "ce.GEB_NGN_PCR_Practica1.GEB_NGN_PCR_Practica1DomainModel",
                    };

                    global::System.CodeDom.Compiler.CodeGeneratorOptions options = new global::System.CodeDom.Compiler.CodeGeneratorOptions();
                    options.BlankLinesBetweenMembers = true;
                    options.IndentString             = "    ";
                    options.VerbatimOrder            = true;
                    options.BracingStyle             = "C";
                    using (global::System.IO.StringWriter writer = new global::System.IO.StringWriter(codeBuffer, global::System.Globalization.CultureInfo.InvariantCulture))
                    {
                        foreach (string domainModel in domainModels)
                        {
                            global::System.CodeDom.CodeExpressionStatement addModel = new global::System.CodeDom.CodeExpressionStatement(new global::System.CodeDom.CodeMethodInvokeExpression(new global::System.CodeDom.CodeMethodReferenceExpression(new global::System.CodeDom.CodeThisReferenceExpression(), "AddDomainModel"), new global::System.CodeDom.CodeTypeOfExpression(domainModel)));
                            languageProvider.GenerateCodeFromStatement(addModel, writer, options);
                        }
                    }
                }
            }
        }