/// <summary> /// CSharp代码生成器 /// </summary> /// <param name="parameter">安装参数</param> /// <param name="type">模板数据视图</param> /// <param name="language">语言</param> public coder(auto.parameter parameter, Type type, auto.language language) : base(type, error.Add, error.Message) { this.parameter = parameter; extensionName = "." + Enum <auto.language, auto.languageAttribute> .Array((int)(byte)language).ExtensionName; creators[command.NOTE.ToString()] = note; creators[command.LOOP.ToString()] = creators[command.FOR.ToString()] = loop; creators[command.AT.ToString()] = at; creators[command.PUSH.ToString()] = push; creators[command.IF.ToString()] = ifThen; creators[command.NOT.ToString()] = not; creators[command.NAME.ToString()] = name; creators[command.FROMNAME.ToString()] = fromName; creators[command.PART.ToString()] = part; }
/// <summary> /// 输出代码 /// </summary> public static void Output(auto.parameter parameter) { stringBuilder[] builders = new stringBuilder[codes.Length]; for (int index = codes.Length; index != 0;) { stringBuilder builder = codes[--index]; if (builder.Count != 0) { builders[index] = builder; codes[index] = new stringBuilder(); } auto.language language = (auto.language)(byte) index; switch (language) { case auto.language.JavaScript: case auto.language.TypeScript: if (builders[index] != null) { error.Add("生成了未知的 " + language + " 代码。"); } break; } } stringBuilder rememberCodeBuilder = null; if (rememberCodes.Count != 0) { rememberCodeBuilder = rememberCodes; rememberCodes = new stringBuilder(); } codeTypes.Clear(); error.ThrowError(); string message = string.Empty; for (int index = builders.Length; index != 0;) { stringBuilder builder = builders[--index]; if (builder != null) { switch (index) { case (int)auto.language.CSharp: string code = builder.ToString(), fastCSharpFileName = null, rememberFileName = null; bool isFastCSharp = false, isRemember = false; if (code.length() != 0) { string fileName = parameter.ProjectPath + (fastCSharpFileName = "{" + parameter.DefaultNamespace + "}." + pub.fastCSharp + ".cs"); if (WriteFile(fileName, WarningCode + code + FileEndCode)) { isFastCSharp = true; message = fileName + " 被修改"; } } if (rememberCodeBuilder != null && (code = rememberCodeBuilder.ToString()).length() != 0) { string fileName = parameter.ProjectPath + (rememberFileName = "{" + parameter.DefaultNamespace + "}.remember." + pub.fastCSharp + ".cs"); if (WriteFile(fileName, WarningCode + code + FileEndCode)) { isRemember = true; message += @" " + fileName + " 被修改"; } } if (parameter.IsFastCSharp && (isFastCSharp | isRemember)) { string projectFile = parameter.AssemblyPath + parameter.ProjectName + ".csproj"; if (File.Exists(projectFile)) { string projectXml = File.ReadAllText(projectFile, System.Text.Encoding.UTF8); if (isFastCSharp) { fastCSharpFileName = @"<Compile Include=""" + fastCSharpFileName + @""" />"; } if (isRemember) { rememberFileName = @"<Compile Include=""" + rememberFileName + @""" />"; } int fileIndex; if (isFastCSharp && (fileIndex = projectXml.IndexOf(fastCSharpFileName)) != -1) { if (isRemember && projectXml.IndexOf(rememberFileName) == -1) { projectXml = projectXml.Insert(fileIndex + fastCSharpFileName.Length, @" " + rememberFileName); MoveFile(projectFile, projectXml); } break; } if (isRemember && (fileIndex = projectXml.IndexOf(rememberFileName)) != -1) { if (isFastCSharp && projectXml.IndexOf(fastCSharpFileName) == -1) { projectXml = projectXml.Insert(fileIndex + rememberFileName.Length, @" " + fastCSharpFileName); MoveFile(projectFile, projectXml); } break; } string csFileName = @".cs"" /> "; if ((fileIndex = projectXml.IndexOf(csFileName)) != -1) { if (isFastCSharp) { fastCSharpFileName += @" "; if (isRemember) { fastCSharpFileName += rememberFileName + @" "; } } else { fastCSharpFileName = rememberFileName + @" "; } projectXml = projectXml.Insert(fileIndex + csFileName.Length, fastCSharpFileName); MoveFile(projectFile, projectXml); } } } break; } } } if (message.Length != 0) { fastCSharp.log.Default.ThrowReal(message, new System.Diagnostics.StackFrame(), false); } }
/// <summary> /// 添加代码 /// </summary> /// <param name="code">代码</param> /// <param name="language">代码生成语言</param> public static void Add(string code, auto.language language = auto.language.CSharp) { codes[(int)(byte)language].Add(code); }