예제 #1
0
		public CodeMemberProperty AddProperty(IColumInfoModel info)
		{
			var propertyName = info.GetPropertyName();
			var targetType = info.ColumnInfo.TargetType.FullName;
			CodeMemberProperty codeMemberProperty;
			if (info.EnumDeclaration != null)
			{
				codeMemberProperty = AddProperty(propertyName, new CodeTypeReference(info.EnumDeclaration.Name));
				//var enumConverter = new ValueConverterAttribute(typeof(EnumMemberConverter));
				codeMemberProperty.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(ValueConverterAttribute).Name, new CodeAttributeArgument(new CodeTypeOfExpression(typeof(EnumMemberConverter)))));
			}
			else
			{
				codeMemberProperty = AddProperty(propertyName, new CodeTypeReference(targetType));

				if (info.IsRowVersion)
				{
					var forModel = new RowVersionAttribute();
					codeMemberProperty.CustomAttributes.Add(new CodeAttributeDeclaration(forModel.GetType().Name));
				}
			}

			if (!string.IsNullOrEmpty(info.NewColumnName))
			{
				var forModel = new ForModelAttribute(info.ColumnInfo.ColumnName);
				codeMemberProperty.CustomAttributes.Add(new CodeAttributeDeclaration(forModel.GetType().Name, new CodeAttributeArgument(new CodePrimitiveExpression(forModel.AlternatingName))));
			}


			return codeMemberProperty;
		}
예제 #2
0
		public virtual void Compile(IEnumerable<IColumInfoModel> columnInfos, Stream to = null)
		{
			if (to != null)
			{
				if (!to.CanSeek)
					throw new InvalidOperationException("The stream must be seekable");
				if (!to.CanWrite)
					throw new InvalidOperationException("The stream must be writeable");
			}

			this.PreCompile();
			if (string.IsNullOrEmpty(TableName))
			{
				TableName = TargetCsName;
			}

			var comments = (new[]
			{
				new CodeCommentStatement("Created by " + Environment.UserDomainName + @"\" + Environment.UserName),
				new CodeCommentStatement("Created on " + DateTime.Now.ToString("yyyy MMMM dd"))
			}).ToArray();

			//Create DOM class
			_base.Name = TargetCsName;

			if (CompileHeader)
				_base.Comments.AddRange(comments);

			//Write static members
			_base.TypeAttributes = TypeAttributes.Sealed | TypeAttributes.Public;
			_base.IsPartial = true;

			CodeNamespace importNameSpace;

			if (string.IsNullOrEmpty(Namespace))
			{
				importNameSpace = new CodeNamespace("JPB.DataAccess.EntryCreator.AutoGeneratedEntrys");
			}
			else
			{
				importNameSpace = new CodeNamespace(Namespace);
			}

			//Add Code Generated Attribute
			var generatedCodeAttribute = new GeneratedCodeAttribute(AttrbuteHeader, "1.0.0.8");
			var codeAttrDecl = new CodeAttributeDeclaration(generatedCodeAttribute.GetType().Name,
				new CodeAttributeArgument(
					new CodePrimitiveExpression(generatedCodeAttribute.Tool)),
				new CodeAttributeArgument(
					new CodePrimitiveExpression(generatedCodeAttribute.Version)));

			_base.CustomAttributes.Add(codeAttrDecl);
			//Add members

			if (GenerateConfigMethod)
			{
				GenerateConfigMehtod(columnInfos, importNameSpace);
			}
			else
			{
				if (!string.IsNullOrEmpty(TableName) && TableName != TargetCsName)
				{
					var forModel = new ForModelAttribute(TableName);
					var codeAttributeDeclaration = new CodeAttributeDeclaration(forModel.GetType().Name,
						new CodeAttributeArgument(new CodePrimitiveExpression(forModel.AlternatingName)));
					_base.CustomAttributes.Add(codeAttributeDeclaration);
				}
			}

			var compileUnit = new CodeCompileUnit();

			using (var memStream = new MemoryStream())
			{
				using (var writer = new StreamWriter(memStream, Encoding.UTF8, 128, true))
				{
					writer.NewLine = Environment.NewLine;

					var cp = new CompilerParameters();
					cp.ReferencedAssemblies.Add("System.dll");
					cp.ReferencedAssemblies.Add("System.Core.dll");
					cp.ReferencedAssemblies.Add("System.Data.dll");
					cp.ReferencedAssemblies.Add("System.Xml.dll");
					cp.ReferencedAssemblies.Add("System.Xml.Linq.dll");
					cp.ReferencedAssemblies.Add("JPB.DataAccess.dll");
					
					importNameSpace.Imports.Add(new CodeNamespaceImport("System"));
					importNameSpace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
					importNameSpace.Imports.Add(new CodeNamespaceImport("System.CodeDom.Compiler"));
					importNameSpace.Imports.Add(new CodeNamespaceImport("System.Linq"));
					importNameSpace.Imports.Add(new CodeNamespaceImport("System.Data"));
					importNameSpace.Imports.Add(new CodeNamespaceImport(typeof(ForModelAttribute).Namespace));
					importNameSpace.Types.Add(_base);
					compileUnit.Namespaces.Add(importNameSpace);

					Provider.GenerateCodeFromCompileUnit(compileUnit, writer, new CodeGeneratorOptions() {
						BlankLinesBetweenMembers = false,
						BracingStyle = "C",
						IndentString = "	",
						VerbatimOrder = true,
						ElseOnClosing = true
					});

					Console.WriteLine("Generated class" + _base.Name);
					writer.Flush();
				}

				Console.WriteLine("Compute changes");
				//check if hascodes are diverent
				var hasher = MD5.Create();
				var neuHash = hasher.ComputeHash(memStream.ToArray());
				var targetFileName = Path.Combine(TargetDir, _base.Name + ".cs");
				if (to == null)
				{
					using (var fileStream = new FileStream(targetFileName, FileMode.OpenOrCreate))
					{
						var exisitingHash = hasher.ComputeHash(fileStream);
						if (!exisitingHash.SequenceEqual(neuHash))
						{
							Console.WriteLine("Class changed. Old file will be kept and new contnt will be written");
							fileStream.SetLength(0);
							fileStream.Flush();
							fileStream.Seek(0, SeekOrigin.Begin);
							memStream.WriteTo(fileStream);
							memStream.Flush();
							fileStream.Flush();
						}
					}
					
				}
				else
				{
					var exisitingHash = hasher.ComputeHash(to);
					if (WriteAllways || !exisitingHash.SequenceEqual(neuHash))
					{
						Console.WriteLine("Class changed. Old file will be kept and new contnt will be written");
						to.SetLength(0);
						to.Flush();
						to.Seek(0, SeekOrigin.Begin);
						memStream.WriteTo(to);
						memStream.Flush();
						to.Flush();
					}
				}
			}
		}