/// <summary> /// 安装入口 /// </summary> /// <param name="parameter">安装参数</param> /// <returns>是否安装成功</returns> public bool Run(ProjectParameter parameter) { if (parameter != null) { HashSet <string> codeFileNames = new HashSet <string>(); foreach (Type type in parameter.Types ?? EmptyArray <Type> .Array) { if (!type.IsAbstract && typeof(CombinationTemplateConfig).IsAssignableFrom(type) && type != typeof(CombinationTemplateConfig)) { CombinationTemplateConfig config = (CombinationTemplateConfig)Activator.CreateInstance(type); string codeFileName = config.GetCodeFileName(parameter.DefaultNamespace); if (!codeFileNames.Add(codeFileName)) { Messages.Error("自定义简单组合模板目标文件名称冲突 " + codeFileName); return(false); } LeftArray <string> codes = new LeftArray <string>(0); foreach (string templatePath in config.TemplatePath) { DirectoryInfo directory = new DirectoryInfo(Path.Combine(parameter.ProjectPath, templatePath)); if (directory.Exists) { foreach (FileInfo file in directory.GetFiles("*.cs")) { LeftArray <string> newCodes = getCode(file.FullName); if (newCodes.Length == 0) { return(false); } codes.Add(ref newCodes); } } else { Messages.Error("没有找到自定义模板相对项目路径" + config.TemplatePath); return(false); } } string fileName = Path.Combine(parameter.ProjectPath, @"{" + codeFileName + "}.CombinationTemplate.cs"); if (Coder.WriteFile(fileName, Coder.WarningCode + string.Concat(codes.ToArray()) + Coder.FileEndCode)) { Messages.Message(fileName + " 被修改"); } } } return(true); } return(false); }
/// <summary> /// 安装入口 /// </summary> /// <param name="parameter">安装参数</param> /// <returns>是否安装成功</returns> public bool Run(ProjectParameter parameter) { if (parameter != null) { //if (!parameter.IsAutoCSerCodeGenerator) return true; string path = parameter.ProjectPath + DefaultTemplatePath; if (Directory.Exists(path)) { LeftArray <string>[] codes = Directory.GetFiles(path, "*.cs").getArray(name => code(name)); if (!codes.any(code => code.Length == 0)) { string fileName = parameter.ProjectPath + @"..\Sort\{AutoCSer}.CombinationTemplate.cs"; if (Coder.WriteFile(fileName, Coder.WarningCode + string.Concat(codes.getArray(code => code.ToArray()).getArray()) + Coder.FileEndCode)) { Messages.Message(fileName + " 被修改"); } return(true); } } } return(false); }
/// <summary> /// 生成类定义字符串 /// </summary> /// <returns>类定义字符串</returns> public override string ToString() { typeDefinition = new TemplateGenerator.CSharpTypeDefinition(Type, true, true); (coder = new Coder(Parameter, Type, Auto.Language)).SkinEnd(coder.GetNode(Auto.GetFileName(Type))); codeBuilder = new StringArray(); if (!Auto.IsDotNet2) { codeBuilder.Append(@" #if DOTNET2 #else"); } if (!Auto.IsMono) { codeBuilder.Append(@" #if MONO #else"); } codeBuilder.Append(@" ", typeDefinition.Start, @" /// <summary> /// 生成代码 /// </summary> /// <param name=""isOut"">是否输出代码</param> protected override void create(bool _isOut_) { if (outStart(AutoCSer.CodeGenerator.CodeLanguage.", Auto.Language.ToString(), @", _isOut_)) { "); switch (Auto.Language) { case CodeLanguage.JavaScript: case CodeLanguage.TypeScript: return(javaScript()); default: return(cSharp()); } }
/// <summary> /// 安装入口 /// </summary> /// <param name="parameter">安装参数</param> /// <returns>是否安装成功</returns> public bool Run(ProjectParameter parameter) { if (parameter != null) { if (parameter.IsAutoCSerCodeGenerator || parameter.IsCustomCodeGenerator) { LeftArray <Definition> definitions = (parameter.IsCustomCodeGenerator ? parameter.Assembly : ProjectParameter.CurrentAssembly).GetTypes().getArray(type => new Definition { Type = type, Auto = type.customAttribute <GeneratorAttribute>(), Parameter = parameter }) .getFind(type => type.Auto != null && type.Auto.IsTemplate)// && type.Auto.DependType == typeof(cSharper) .Sort((left, right) => string.CompareOrdinal(left.Type.FullName, right.Type.FullName)); LeftArray <string> codes = new LeftArray <string>(definitions.Length); foreach (Definition definition in definitions) { codes.Add(definition.ToString()); if (Messages.IsError) { return(false); } } #if DotNetStandard string path = new System.IO.FileInfo(parameter.AssemblyPath).Directory.fullName(); copyDotNetCoreJson(path, "AutoCSer.CodeGenerator.deps.json"); copyDotNetCoreJson(path, "AutoCSer.CodeGenerator.runtimeconfig.dev.json"); copyDotNetCoreJson(path, "AutoCSer.CodeGenerator.runtimeconfig.json"); #endif string fileName = parameter.ProjectPath + @"{AutoCSer}.CSharper.cs"; if (Coder.WriteFile(fileName, Coder.WarningCode + string.Concat(codes.ToArray()) + Coder.FileEndCode)) { Messages.Error(fileName + " 被修改"); return(false); } } return(true); } return(false); }
/// <summary> /// 启动代码生成 /// </summary> public void Start() { if (string.IsNullOrEmpty(ProjectPath) || !Directory.Exists(ProjectPath)) { Messages.Error("项目路径不存在 : " + ProjectPath); } else { try { if (IsAutoCSerCodeGenerator || IsCustomCodeGenerator) { run(new CSharper()); } else { KeyValue <Type, GeneratorAttribute>[] generators = (CustomConfig.Default.IsAutoCSer ? CurrentAssembly.GetTypes() : EmptyArray <Type> .Array) .concat(CustomConfig.Assembly == null ? EmptyArray <Type> .Array : 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 <HashType> types = generators.getHash(value => (HashType)value.Key); KeyValue <HashType, HashType>[] depends = generators .getFind(value => value.Value.DependType != null && types.Contains(value.Value.DependType)) .GetArray(value => new KeyValue <HashType, HashType>((HashType)value.Key, (HashType)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.Exception(error); } else { Messages.Error(CustomConfig.Assembly.FullName + "\r\n" + error.ToString()); } } finally { Coder.Output(this); } } }