protected virtual void GenerateCode(CodeCompileUnit unit, CodeDomProvider provider,
                                            CodeGenerationResults content, string itemPath)
        {
            CodeCompileUnit cloneUnit = CloneUnit(unit);

            foreach (CodeNamespace ns in unit.Namespaces)
            {
                cloneUnit.Namespaces.Clear();
                cloneUnit.Namespaces.Add(CloneNamespace(ns));
                foreach (CodeTypeDeclaration codeType in ns.Types)
                {
                    string file = Path.Combine(Path.GetDirectoryName(itemPath),
                                               Path.ChangeExtension(codeType.Name, Path.GetExtension(itemPath)));
                    if (!content.ContainsFile(file))
                    {
                        cloneUnit.Namespaces[0].Types.Clear();
                        cloneUnit.Namespaces[0].Types.Add(codeType);
                        using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
                        {
                            provider.GenerateCodeFromCompileUnit(cloneUnit, stringWriter, new CodeGeneratorOptions());
                            content.Add(file, stringWriter.ToString());
                        }
                    }
                }
            }
        }
        public virtual CodeGenerationResults Generate(IArtifactLink link)
        {
            Guard.ArgumentNotNull(link, "link");

            CodeGenerationResults content = new CodeGenerationResults();

            try
            {
                XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(UseXmlSerializer(link), GetNamespace(link));
                string xsdMoniker      = Utility.GetData <string>(link, ElementDataKey);
                string xmlSchemaSource = XmlSchemaUtility.GetXmlSchemaSource(xsdMoniker, link);
                if (!string.IsNullOrEmpty(xmlSchemaSource))
                {
                    CodeCompileUnit unit    = generator.GenerateCodeCompileUnit(xmlSchemaSource);
                    string          element = new XmlSchemaElementMoniker(xsdMoniker).ElementName;
                    UpdateUnit(unit, element, link);
                    ThrowOnNoTypes(unit.Namespaces, element);
                    this.assemblyReferences = GetAssemblyReferences(link, unit.ReferencedAssemblies);
                    CodeDomProvider provider = GetCodeDomProvider(link);
                    GenerateCode(unit, provider, content, link.ItemPath);
                }
            }
            catch (Exception exception)
            {
                LogErrors(exception);
            }

            return(content);
        }
		public virtual CodeGenerationResults Generate(IArtifactLink link)
		{
			Guard.ArgumentNotNull(link, "link");

			CodeGenerationResults content = new CodeGenerationResults();

			try
			{
				XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(UseXmlSerializer(link), GetNamespace(link));
				string xsdMoniker = Utility.GetData<string>(link, ElementDataKey);
				string xmlSchemaSource = XmlSchemaUtility.GetXmlSchemaSource(xsdMoniker, link);
				if (!string.IsNullOrEmpty(xmlSchemaSource))
				{
					CodeCompileUnit unit = generator.GenerateCodeCompileUnit(xmlSchemaSource);
					string element = new XmlSchemaElementMoniker(xsdMoniker).ElementName;
					UpdateUnit(unit, element, link);
					ThrowOnNoTypes(unit.Namespaces, element);
					this.assemblyReferences = GetAssemblyReferences(link, unit.ReferencedAssemblies);
					CodeDomProvider provider = GetCodeDomProvider(link);
					GenerateCode(unit, provider, content, link.ItemPath);
				}
			}
			catch (Exception exception)
			{
				LogErrors(exception);
			}

			return content;
		}
Exemplo n.º 4
0
            public virtual CodeGenerationResults Generate(IArtifactLink link)
            {
                CodeGenerationResults result = new CodeGenerationResults();

                result.Add(link.ItemPath, HelloWorld);
                return(result);
            }
Exemplo n.º 5
0
        public void ShouldNotGenerateAndGetErrorWithSchemaNotFound()
        {
            XmlSchemaCodeGenerationStrategy strategy = new XmlSchemaCodeGenerationStrategy();
            CodeGenerationResults           content  = strategy.Generate(new MyArtifactLink("foo.xsd", "SomeElement"));

            Assert.AreEqual <int>(0, content.Count);
            Assert.AreEqual <int>(1, strategy.Errors.Count);
        }
Exemplo n.º 6
0
        public void ShouldNotGenerateAndGetErrorWithNoSchema()
        {
            XmlSchemaCodeGenerationStrategy strategy = new XmlSchemaCodeGenerationStrategy();
            CodeGenerationResults           content  = strategy.Generate(new MyArtifactLink(null, null));

            Assert.AreEqual <int>(0, content.Count);
            Assert.AreEqual <int>(1, strategy.Errors.Count);
        }
Exemplo n.º 7
0
        public void ShouldNotGenerateAndGetErrorWithTopElementsWithoutType()
        {
            XmlSchemaCodeGenerationStrategy strategy = new XmlSchemaCodeGenerationStrategy();
            CodeGenerationResults           content  = strategy.Generate(new MyArtifactLink("SampleData\\TopElementNoType.xsd", "otherElement"));

            Assert.AreEqual <int>(0, content.Count);
            Assert.AreEqual <int>(1, strategy.Errors.Count);
        }
Exemplo n.º 8
0
        public void ShouldGenerateWithErrorsOnInvalidSchema()
        {
            MyArtifactLink link = new MyArtifactLink("SampleData\\InvalidSchema.xsd", "SomeElement");
            XmlSchemaCodeGenerationStrategy strategy = new XmlSchemaCodeGenerationStrategy();
            CodeGenerationResults           content  = strategy.Generate(link);

            Assert.AreEqual <int>(0, content.Count);
            Assert.AreEqual <int>(1, strategy.Errors.Count);
        }
Exemplo n.º 9
0
        public void ShouldGenerateWithXmlSerializerImporterFromData()
        {
            MyArtifactLinkWithXmlImporterFromData link     = new MyArtifactLinkWithXmlImporterFromData("SampleData\\SimpleSchema1.xsd", "element1");
            XmlSchemaCodeGenerationStrategy       strategy = new XmlSchemaCodeGenerationStrategy();
            CodeGenerationResults content = strategy.Generate(link);

            Assert.AreEqual <int>(3, content.Count);
            Assert.AreEqual <int>(0, strategy.Errors.Count);
            CompilerResults results = DynamicCompilation.CompileAssemblyFromSource(JoinContent(content), ((List <string>)strategy.AssemblyReferences).ToArray());

            TypeAsserter.AssertAttribute <XmlRootAttribute>(results.CompiledAssembly.GetType("element1"));
        }
Exemplo n.º 10
0
        public void ShouldGenerateOneXmlRootAttribute()
        {
            MyArtifactLink link = new MyArtifactLink("SampleData\\OneXmlRootAttribute.xsd", "ImplementsAbstractCT");

            Utility.SetData(link, true, XmlSchemaCodeGenerationStrategy.UseXmlSerializerDataKey);
            XmlSchemaCodeGenerationStrategy strategy = new XmlSchemaCodeGenerationStrategy();
            CodeGenerationResults           content  = strategy.Generate(link);

            CompilerResults results = DynamicCompilation.CompileAssemblyFromSource(JoinContent(content), ((List <string>)strategy.AssemblyReferences).ToArray());

            TypeAsserter.AssertAttribute <XmlRootAttribute>(results.CompiledAssembly.GetType("ImplementsAbstractCT"));
            Type type = results.CompiledAssembly.GetType("MyAbstractCT");

            Assert.AreEqual <int>(0, type.GetCustomAttributes(typeof(XmlRootAttribute), true).Length);
        }
Exemplo n.º 11
0
        public void ShouldGenerateTypeWithBaseTypes()
        {
            XmlSchemaCodeGenerationStrategy strategy = new XmlSchemaCodeGenerationStrategy();
            CodeGenerationResults           content  = strategy.Generate(new MyArtifactLink("SampleData\\BaseTypes.xsd", "GetLandmarkPointsByRectResponse"));

            Assert.AreEqual <int>(15, content.Count);
            Assert.AreEqual <int>(0, strategy.Errors.Count);
            CompilerResults results = DynamicCompilation.CompileAssemblyFromSource(JoinContent(content), ((List <string>)strategy.AssemblyReferences).ToArray());

            Assert.IsNotNull(results.CompiledAssembly.GetType("GetLandmarkPointsByRectResponse", false, false));
            Assert.IsNotNull(results.CompiledAssembly.GetType("LandmarkPoint", false, false));
            Assert.IsNotNull(results.CompiledAssembly.GetType("LandmarkBase", false, false));
            Assert.IsNotNull(results.CompiledAssembly.GetType("ShapeType", false, false));
            Assert.IsNotNull(results.CompiledAssembly.GetType("LonLatPt", false, false));
        }
Exemplo n.º 12
0
        public void ShouldGenerateTypesRefsFromAttributes()
        {
            MyArtifactLink link = new MyArtifactLink("SampleData\\TypeRefsInAttributes.wsdl", "AttachmentAddRq");

            Utility.SetData(link, true, XmlSchemaCodeGenerationStrategy.UseXmlSerializerDataKey);
            XmlSchemaCodeGenerationStrategy strategy = new XmlSchemaCodeGenerationStrategy();
            CodeGenerationResults           content  = strategy.Generate(link);

            Assert.AreEqual <int>(69, content.Count);
            Assert.AreEqual <int>(0, strategy.Errors.Count);

            CompilerResults results = DynamicCompilation.CompileAssemblyFromSource(JoinContent(content), ((List <string>)strategy.AssemblyReferences).ToArray());

            Assert.AreEqual <int>(69, results.CompiledAssembly.GetTypes().Length);
            Assert.IsNotNull(results.CompiledAssembly.GetType("AddressType", false, false));
        }
        public CodeGenerationResults Generate(IArtifactLink link)
        {
            CodeGenerationResults result = new CodeGenerationResults();

            if (link is IModelReference)
            {
                this.serviceProvider = Utility.GetData <IServiceProvider>(link);
                ProjectNode project = Utility.GetData <ProjectNode>(link);

                ModelElement modelElement = ((IModelReference)link).ModelElement;
                TextTemplateArtifactLinkWrapper textTemplateArtifactLink = new TextTemplateArtifactLinkWrapper(link);
                textTemplateArtifactLink.ResourceResolver = ResourceResolver;
                string template = GetTemplateBasedOnProject(textTemplateArtifactLink, project);

                if (modelElement != null && !string.IsNullOrEmpty(template))
                {
                    Engine           engine = new Engine();
                    DomainModel      model  = (DomainModel)modelElement.Store.GetDomainModel(modelElement.GetDomainClass().DomainModel.Id);
                    TextTemplateHost host   = new TextTemplateHost(model, modelElement, modelElement, GetService <ICodeGenerationService>());
                    host.ResourceResolver = textTemplateArtifactLink.ResourceResolver;
                    string content = engine.ProcessTemplate(template, host);

                    if (host.GenerateOutput)
                    {
                        this.projectReferences  = new List <Guid>(host.ProjectReferences);
                        this.assemblyReferences = GetAssemblyReferences(link);
                        if (host.CompilerErrors.Count > 0)
                        {
                            foreach (CompilerError error in host.CompilerErrors)
                            {
                                LogError(error);
                            }
                        }
                        // Will create a file with the 'ErrorGeneratingOutput' text in the generated file.
                        result.Add(link.ItemPath, content);
                    }
                }
            }

            return(result);
        }
		protected virtual void GenerateCode(CodeCompileUnit unit, CodeDomProvider provider, 
			CodeGenerationResults content, string itemPath)
		{
			CodeCompileUnit cloneUnit = CloneUnit(unit);

			foreach (CodeNamespace ns in unit.Namespaces)
			{
				cloneUnit.Namespaces.Clear();
				cloneUnit.Namespaces.Add(CloneNamespace(ns));
				foreach (CodeTypeDeclaration codeType in ns.Types)
				{
					string file = Path.Combine(Path.GetDirectoryName(itemPath),
						Path.ChangeExtension(codeType.Name, Path.GetExtension(itemPath)));
					if (!content.ContainsFile(file))
					{
						cloneUnit.Namespaces[0].Types.Clear();
						cloneUnit.Namespaces[0].Types.Add(codeType);
						using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
						{
							provider.GenerateCodeFromCompileUnit(cloneUnit, stringWriter, new CodeGeneratorOptions());
							content.Add(file, stringWriter.ToString());
						}
					}
				}
			}
		}
			public virtual CodeGenerationResults Generate(IArtifactLink link)
			{
				CodeGenerationResults result = new CodeGenerationResults();
				result.Add(link.ItemPath, HelloWorld);
				return result;
			}
Exemplo n.º 16
0
 private string[] JoinContent(CodeGenerationResults content)
 {
     string[] result = new string[content.Values.Count];
     content.Values.CopyTo(result, 0);
     return(result);
 }
		private string[] JoinContent(CodeGenerationResults content)
		{
			string[] result = new string[content.Values.Count];
			content.Values.CopyTo(result, 0);
			return result;
		}
Exemplo n.º 18
0
        public CodeGenerationResults Generate(IArtifactLink link)
        {
            CodeGenerationResults result           = new CodeGenerationResults();
            string       serviceImplementationName = string.Empty;
            string       serviceContractName       = string.Empty;
            string       serviceNamespace          = string.Empty;
            const string behavior = "_Behavior";

            if (link is IModelReference)
            {
                this.serviceProvider = Utility.GetData <IServiceProvider>(link);
                ProjectNode project = Utility.GetData <ProjectNode>(link);

                ServiceDescription serviceDescription = ((IModelReference)link).ModelElement as ServiceDescription;
                Configuration      configuration      = GetConfiguration(link, project);

                // abort if we got errors in config file
                if (configuration == null)
                {
                    return(result);
                }

                try
                {
                    ServiceReference serviceReference = (ServiceReference)serviceDescription;
                    SCModel.Service  service          = GetMelReference <SCModel.Service>(serviceReference.ServiceImplementationType);
                    serviceImplementationName = ResolveTypeReference(service);
                    serviceContractName       = GetServiceContractName(service.ServiceContract);
                    serviceNamespace          = service.ServiceContract.Namespace;

                    ServiceModelConfigurationManager manager = new ServiceModelConfigurationManager(configuration);

                    ServiceElement serviceElement = new ServiceElement();
                    serviceElement.Name = serviceImplementationName;
                    serviceElement.BehaviorConfiguration = string.Concat(serviceImplementationName, behavior);

                    foreach (Endpoint endpoint in serviceDescription.Endpoints)
                    {
                        ServiceEndpointElement endpointElement = new ServiceEndpointElement();
                        endpointElement.Name             = endpoint.Name;
                        endpointElement.Contract         = serviceContractName;
                        endpointElement.Binding          = ((WcfEndpoint)endpoint.ObjectExtender).BindingType.ToString();
                        endpointElement.Address          = new Uri(endpoint.Address ?? string.Empty, UriKind.RelativeOrAbsolute);
                        endpointElement.BindingNamespace = serviceNamespace;
                        serviceElement.Endpoints.Add(endpointElement);
                    }

                    manager.UpdateService(serviceElement);

                    ServiceBehaviorElement behaviorElement = new ServiceBehaviorElement();
                    behaviorElement.Name = string.Concat(serviceImplementationName, behavior);
                    ServiceDebugElement debugElement = new ServiceDebugElement();
                    debugElement.IncludeExceptionDetailInFaults = false;
                    behaviorElement.Add(debugElement);

                    if (((WcfServiceDescription)serviceDescription.ObjectExtender).EnableMetadataPublishing)
                    {
                        ServiceMetadataPublishingElement metadataPublishingElement = new ServiceMetadataPublishingElement();
                        metadataPublishingElement.HttpGetEnabled = true;
                        behaviorElement.Add(metadataPublishingElement);
                        ServiceEndpointElement mexEndpointElement = ServiceModelConfigurationManager.GetMetadataExchangeEndpoint();
                        serviceElement.Endpoints.Add(mexEndpointElement);
                    }

                    manager.UpdateBehavior(behaviorElement);
                    manager.Save();

                    result.Add(link.ItemPath, File.ReadAllText(configuration.FilePath));
                }
                finally
                {
                    if (configuration != null && File.Exists(configuration.FilePath))
                    {
                        File.Delete(configuration.FilePath);
                    }
                }
            }

            return(result);
        }