void ToTypeScript(Document doc, string tneTxt) { try { ThreadHelper.ThrowIfNotOnUIThread(); string theNamespace = null; List <string> theImportList = new List <string>(); var theBase = "Tnelab.NativeObject"; var tdoc = doc.Object("TextDocument") as TextDocument; var sp = tdoc.StartPoint; var ep = sp.CreateEditPoint(); var lineNum = 2; string theType = null; while (true) { try { var txt = ep.GetLines(lineNum, lineNum + 1).Trim(); if (txt.StartsWith("//namespace:", StringComparison.OrdinalIgnoreCase)) { theNamespace = txt.Split(':')[1].Trim(); } else if (txt.StartsWith("//base:", StringComparison.OrdinalIgnoreCase)) { theBase = txt.Split(':')[1].Trim(); } else if (txt.StartsWith("//import:", StringComparison.OrdinalIgnoreCase)) { theImportList.Add(txt.Split(':')[1].Trim()); } else if (txt.StartsWith("//type:", StringComparison.OrdinalIgnoreCase)) { theType = txt.Split(':')[1].Trim(); } else { break; } lineNum++; } catch { break; } } if (theImportList.Count == 0) { theImportList.Add("TneApp.d.ts"); } CodeModel codeModel; if (theType != null) { codeModel = new CodeModel(theType); } else { codeModel = new CodeModel(doc.ProjectItem.FileCodeModel); } if (theNamespace == null) { theNamespace = codeModel.NamespaceName; } var code = JsNativeMapBuilder.Build(codeModel, theNamespace, theBase, theImportList); var filePath = $"{doc.FullName}.ts"; ProjectItem tsItem = null; bool canSave = true; if (!File.Exists(filePath)) { canSave = false; } File.WriteAllText(filePath, ""); tsItem = doc.ProjectItem.ProjectItems.AddFromFile(filePath); tsItem.Properties.Item("ItemType").Value = "TypeScriptCompile"; if (!tsItem.IsOpen) { var w = tsItem.Open(EnvDTE.Constants.vsViewKindCode); if (!canSave) { w.Activate(); } } var tdoc2 = tsItem.Document.Object("TextDocument") as TextDocument; var tsEditPoint = tdoc2.StartPoint.CreateEditPoint(); tsEditPoint.Delete(100000); tsEditPoint.Insert(code); if (canSave) { tsItem.Document.Save(); } } catch (Exception ex) { File.WriteAllText($"{doc.FullName}.ts", ex.ToString()); } }
public static string Build(CodeModel codeModel, string theNamespace, string theBase, List <string> theImportList) { Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread(); //var project = DTEHelper.GetProject(theDTE, this.ProjectName); //var pro = project.Properties.Item("AssemblyName"); //var assemblyName = pro.Value.ToString(); StringBuilder strBuilder = new StringBuilder(); strBuilder.AppendLine("//此代码由机器生成,请不要手动修改"); foreach (var import in theImportList) { strBuilder.AppendLine($"///<reference path=\"{import}\"/>"); } strBuilder.Append("namespace ").Append(theNamespace).AppendLine("{"); var constructorInfoList = new List <string>(); codeModel.ProcessConstructor((funcInfo) => { if (funcInfo.ParamList.Count != 0) { constructorInfoList.Insert(0, $"@Tnelab.ConstructorInfo({string.Join(",", funcInfo.ParamList.Select(it => "\"" + it.TypeName + "\"").ToArray())})"); } else { constructorInfoList.Insert(0, $"@Tnelab.ConstructorInfo()"); } }); strBuilder.AppendLine(String.Join("\r\n", constructorInfoList.Select(it => $"\t{it}").ToArray())); strBuilder.AppendLine($"\[email protected](\"{theNamespace}.{codeModel.ClassName}\",\"{codeModel.NamespaceName}.{codeModel.ClassName}\")"); strBuilder.AppendLine($"\texport class {codeModel.ClassName} extends {theBase} {{"); codeModel.ProcessEvent((eventInfo) => { strBuilder.AppendLine($"\t\[email protected](\"{eventInfo.TypeName}\")"); var staticFlag = eventInfo.IsStatic ? "static" : ""; strBuilder.AppendLine($"\t\tpublic {staticFlag} get {eventInfo.Name}():Tnelab.TneEvent{{ return undefined; }}"); }); codeModel.ProcessProperty((propInfo) => { if (propInfo.HasSet) { if (propInfo.Name != "this") { strBuilder.AppendLine($"\t\[email protected](undefined,\"{propInfo.TypeName}\")"); var staticFlag = propInfo.IsStatic ? "static" : ""; strBuilder.AppendLine($"\t\tpublic {staticFlag} set {propInfo.Name}(value:{GetJsTypeNameByTypeName(propInfo.TypeName)}) {{ }}"); } else { strBuilder.AppendLine($"\t\t[index:{GetJsTypeNameByTypeName(propInfo.ParamList[0].TypeName)}]:{GetJsTypeNameByTypeName(propInfo.TypeName)};"); } } if (propInfo.HasGet) { if (propInfo.Name != "this") { var staticFlag = propInfo.IsStatic ? "static" : ""; strBuilder.AppendLine($"\t\tpublic {staticFlag} get {propInfo.Name}():{GetJsTypeNameByTypeName(propInfo.TypeName)} {{ return undefined; }}"); } } }); codeModel.ProcessFunction((funcInfoList) => { if (funcInfoList.Count > 1) { var invokeInfoList = new List <string>(); bool isStatic = false; foreach (var funcInfo in funcInfoList) { if (funcInfo.ParamList.Count != 0) { invokeInfoList.Insert(0, $"@Tnelab.InvokeInfo(\"{funcInfo.Name}\", {string.Join(",", funcInfo.ParamList.Select(it => "\"" + it.TypeName + "\"").ToArray())})"); } else { invokeInfoList.Insert(0, $"@Tnelab.InvokeInfo(\"{funcInfo.Name}\")"); } funcInfo.ParamList.Insert(0, new CodeParamInfo() { Name = "tneMapId", TypeName = "System.Int32" }); if (funcInfo.GenericTypeArguments.Count > 0) { funcInfo.ParamList.Insert(1, new CodeParamInfo() { Name = "tneMapGenericTypeInfo", TypeName = "System.String" }); } var staticFlag = funcInfo.IsStatic ? "static" : ""; if (!isStatic) { isStatic = funcInfo.IsStatic; } strBuilder.AppendLine($"\t\tpublic {staticFlag} {funcInfo.Name}({string.Join(",", funcInfo.ParamList.Select(it => $"_{it.Name}:{GetJsTypeNameByTypeName(it.TypeName, codeModel.GenericTypeArguments, funcInfo.GenericTypeArguments)}"))}):{GetJsTypeNameByTypeName(funcInfo.ReturnTypeName, codeModel.GenericTypeArguments, funcInfo.GenericTypeArguments)};"); } strBuilder.AppendLine(String.Join("\r\n", invokeInfoList.Select(it => $"\t\t{it}").ToArray())); var staticFlag2 = isStatic ? "static" : ""; strBuilder.AppendLine($"\t\tpublic {staticFlag2} {funcInfoList[0].ShortName}(tneMapId:number):any{{}}"); } else { var funcInfo = funcInfoList[0]; if (funcInfo.ParamList.Count != 0) { strBuilder.AppendLine($"\t\[email protected](\"{funcInfo.Name}\", {string.Join(",", funcInfo.ParamList.Select(it => "\"" + it.TypeName + "\"").ToArray())})"); } else { strBuilder.AppendLine($"\t\[email protected](\"{funcInfo.Name}\")"); } if (funcInfo.GenericTypeArguments.Count > 0) { funcInfo.ParamList.Insert(0, new CodeParamInfo() { Name = "tneMapGenericTypeInfo", TypeName = "System.String" }); } var staticFlag = funcInfo.IsStatic ? "static" : ""; strBuilder.AppendLine($"\t\tpublic {staticFlag} {funcInfo.Name}({string.Join(",", funcInfo.ParamList.Select(it => $"_{it.Name}:{GetJsTypeNameByTypeName(it.TypeName, codeModel.GenericTypeArguments, funcInfo.GenericTypeArguments)}"))}):{GetJsTypeNameByTypeName(funcInfo.ReturnTypeName, codeModel.GenericTypeArguments, funcInfo.GenericTypeArguments)} {(funcInfo.ReturnTypeName == "System.Void" ? "{}" : "{return undefined;}")}"); } });