private CodeTypeMember ConvertToSourcesMethod(TranslateAttribute attr)
        {
            var from   = attr.TargetType.Param("from", typeof(IEnumerable <>));
            var method = TranslatorClassGenerator.DeclareToSourcesMethod(attr, from.ExtensionVar());

            log.DebugFormat("ConvertToSourcesMethod: {0}", attr.ToFormatString());
            return(TranslatorClassGenerator.ConvertToSourcesMethod(attr, method, from));
        }
Esempio n. 2
0
        public void Run()
        {
            try
            {
                Console.WriteLine("Generating translators...");
                LogManager.LogFactory = new ConsoleLogFactory();

                var parser = new Parser(System.Environment.CommandLine, this);
                parser.Parse();

                var modelAssembly = Assembly.LoadFile(ModelAssemblyPath);

                var modelAssemblyTypes = modelAssembly.GetTypes();
                log.DebugFormat("Found {0} types in Assembly", modelAssemblyTypes.Count());
                foreach (var type in modelAssemblyTypes)
                {
                    var attrs          = type.GetCustomAttributes(typeof(TranslateAttribute), false).ToList();
                    var extensionAttrs = type.GetCustomAttributes(typeof(TranslateExtensionAttribute), false).ToList();

                    if (attrs.Count == 0 && extensionAttrs.Count == 0)
                    {
                        continue;
                    }

                    var outPath = Path.Combine(this.OutputDir, type.Name + ".generated.cs");

                    if (!DoGenerateTranslator(outPath))
                    {
                        continue;
                    }

                    if (attrs.Count > 0)
                    {
                        var attr      = (TranslateAttribute)attrs[0];
                        var generator = new TranslatorClassGenerator(CodeLang.CSharp);
                        generator.Write(attr, outPath);
                    }
                    if (extensionAttrs.Count > 0)
                    {
                        var generator = new ExtensionTranslatorClassGenerator(CodeLang.CSharp);
                        generator.Write(type, this.OutputDir);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Error.Write(ex.GetType() + ": " + ex.Message + "\n" + ex.StackTrace);
                throw;
            }
        }
Esempio n. 3
0
		public void Run()
		{
			try
			{
				Console.WriteLine("Generating translators...");
				LogManager.LogFactory = new ConsoleLogFactory();

				var parser = new Parser(System.Environment.CommandLine, this);
				parser.Parse();

				var modelAssembly = Assembly.LoadFile(ModelAssemblyPath);

				var modelAssemblyTypes = modelAssembly.GetTypes();
				log.DebugFormat("Found {0} types in Assembly", modelAssemblyTypes.Count());
				foreach (var type in modelAssemblyTypes)
				{
					var attrs = type.GetCustomAttributes(typeof(TranslateAttribute), false).ToList();
					var extensionAttrs = type.GetCustomAttributes(typeof(TranslateExtensionAttribute), false).ToList();

					if (attrs.Count == 0 && extensionAttrs.Count == 0) continue;

					var outPath = Path.Combine(this.OutputDir, type.Name + ".generated.cs");

					if (!DoGenerateTranslator(outPath)) continue;

					if (attrs.Count > 0)
					{
						var attr = (TranslateAttribute)attrs[0];
						var generator = new TranslatorClassGenerator(CodeLang.CSharp);
						generator.Write(attr, outPath);
					}
					if (extensionAttrs.Count > 0)
					{
						var generator = new ExtensionTranslatorClassGenerator(CodeLang.CSharp);
						generator.Write(type, this.OutputDir);
					}
				}

			}
			catch (Exception ex)
			{
				Console.Error.Write(ex.GetType() + ": " + ex.Message + "\n" + ex.StackTrace);
				throw;
			}
		}
        private CodeTypeMember UpdateTargetMethod(TranslateAttribute attr, string translatorTypeName)
        {
            var methodName = attr.GetUpdateTargetMethodName();
            var fromSource = attr.SourceType.Param("fromParam");
            var toTarget   = attr.TargetType.Param("to");
            var method     = methodName.DeclareMethod(attr.TargetType, MemberAttributes.Public | MemberAttributes.Static, fromSource.ExtensionVar(), toTarget);

            var typeNames     = attr.TargetType.GetProperties().ToList().Select(x => x.Name);
            var fromSourceVar = attr.SourceType.DeclareVar("from", fromSource.RefArg());

            method.Statements.Add(fromSourceVar);
            foreach (var fromSourceProperty in attr.SourceType.GetProperties())
            {
                if (!typeNames.Contains(fromSourceProperty.Name))
                {
                    continue;
                }

                method.Statements.Add(TranslatorClassGenerator.CreateToTargetAssignmentMethod(attr, toTarget, fromSourceProperty, fromSourceVar.RefVar(), GetTypesTranslateAttributeFn));
            }

            method.Statements.Add(toTarget.Return());
            return(method);
        }
        public static CodeMemberMethod ConvertToTargetsMethod(TranslateAttribute attr)
        {
            var from = attr.SourceType.Param("from", typeof(IEnumerable <>));

            return(TranslatorClassGenerator.ConvertToTargetsMethod(attr, from.ExtensionVar()));
        }