Exemple #1
0
 /// <summary>
 /// 启动代码生成
 /// </summary>
 public void Start()
 {
     if (string.IsNullOrEmpty(ProjectPath) || !Directory.Exists(ProjectPath))
     {
         Messages.Add("项目路径不存在 : " + ProjectPath);
     }
     else
     {
         try
         {
             if (IsAutoCSerCodeGenerator || IsCustomCodeGenerator)
             {
                 run(new CSharper());
             }
             else
             {
                 KeyValue <Type, GeneratorAttribute>[] generators = (CustomConfig.Default.IsAutoCSer ? CurrentAssembly.GetTypes() : NullValue <Type> .Array)
                                                                    .concat(CustomConfig.Assembly == null ? null : CustomConfig.Assembly.GetTypes())
                                                                    .getFind(type => !type.IsInterface && !type.IsAbstract && typeof(IGenerator).IsAssignableFrom(type))
                                                                    .GetArray(type => new KeyValue <Type, GeneratorAttribute>(type, type.customAttribute <GeneratorAttribute>()))
                                                                    .getFind(value => value.Value != null && value.Value.IsAuto).ToArray();
                 if (generators.Length != 0)
                 {
                     generators = generators.sort((left, right) => string.CompareOrdinal(left.Key.FullName, right.Key.FullName));
                     HashSet <Type>          types   = generators.getHash(value => value.Key);
                     KeyValue <Type, Type>[] depends = generators
                                                       .getFind(value => value.Value.DependType != null && types.Contains(value.Value.DependType))
                                                       .GetArray(value => new KeyValue <Type, Type>(value.Key, value.Value.DependType));
                     foreach (Type type in AutoCSer.Algorithm.TopologySort.Sort(depends, types, true))
                     {
                         run(type.Assembly.CreateInstance(type.FullName) as IGenerator);
                     }
                 }
                 if (CustomConfig.Default.IsAutoCSer)
                 {
                     foreach (Type type in Types)
                     {
                         if (!type.IsGenericType && !type.IsInterface && !type.IsEnum)
                         {
                             foreach (Metadata.MethodIndex methodInfo in Metadata.MethodIndex.GetMethods <TestMethodAttribute>(type, MemberFilters.Static, false, true, false))
                             {
                                 MethodInfo method = methodInfo.Method;
                                 if (method.IsGenericMethod)
                                 {
                                     //isTest = false;
                                     Messages.Message("测试用例不能是泛型函数 " + method.fullName());
                                 }
                                 else
                                 {
                                     Type returnType = method.ReturnType;
                                     if ((returnType == typeof(bool) || returnType == typeof(void)) && method.GetParameters().Length == 0)
                                     {
                                         try
                                         {
                                             object returnValue = method.Invoke(null, null);
                                             if (method.ReturnType == typeof(bool) && !(bool)returnValue)
                                             {
                                                 //isTest = false;
                                                 Messages.Message("测试用例调用失败 " + method.fullName());
                                             }
                                         }
                                         catch (Exception error)
                                         {
                                             Messages.Message(error.ToString());
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         catch (Exception error)
         {
             if (CustomConfig.Assembly == null)
             {
                 Messages.Add(error);
             }
             else
             {
                 Messages.Add(CustomConfig.Assembly.FullName + "\r\n" + error.ToString());
             }
         }
         finally { Coder.Output(this); }
     }
 }