public static void Run() { var specs = new [] { "comments", "delimiters", "interpolation", "inverted", "partials", "sections" }; foreach (var spec in specs) { var specDefinition = new MustacheSpec(spec); if (specDefinition.StronglyTypedExamples.Children().Any()) { var generator = new Xamasoft.JsonClassGenerator.JsonClassGenerator(); generator.SingleFile = true; generator.TargetFolder = "../../Mustache_Spec/examples/"; generator.Namespace = FullTestClassNamespace(spec); generator.MainClass = spec; generator.Example = specDefinition.StronglyTypedExamples.ToString(); generator.ExplicitDeserialization = true; generator.GenerateClasses(); } } }
public static void Run() { var specs = new [] { "comments", "delimiters", "interpolation", "inverted", "partials", "sections" }; foreach (var spec in specs) { var specDefinition = new MustacheSpec(spec); if (specDefinition.StronglyTypedExamples.Children().Any()) { var generator = new Xamasoft.JsonClassGenerator.JsonClassGenerator(); generator.SingleFile = true; generator.TargetFolder = "../../Mustache_Spec/examples/"; generator.Namespace = FullTestClassNamespace(spec); generator.MainClass = spec; generator.Example = specDefinition.StronglyTypedExamples.ToString(); generator.ExplicitDeserialization = true; generator.GenerateClasses(); } } }
private void DocumentEvents_DocumentSaved(EnvDTE.Document Document) { EnvDTE.Project project; Microsoft.CodeAnalysis.Project proj; //string fileName = null; if (Document.Name.EndsWith("Controller.cs", StringComparison.OrdinalIgnoreCase)) { project = Document.ProjectItem.ContainingProject; string parentName = Document.ProjectItem.Name; string parentFileName = Document.ProjectItem.FileNames[0]; string parentPath = Path.GetDirectoryName(parentFileName); workspace = componentModel.GetService <VisualStudioWorkspace>(); proj = workspace.CurrentSolution.Projects.Where(doc => doc.FilePath == project.FileName).FirstOrDefault(); CSharpCompilation cSharpCompilation = null; Compilation compilation = null; if (cSharpCompilation == null) { if (proj.TryGetCompilation(out compilation)) { cSharpCompilation = (CSharpCompilation)compilation; } else { cSharpCompilation = (CSharpCompilation)proj.GetCompilationAsync().Result; } } string structFileName = Path.Combine(parentPath, Path.GetFileNameWithoutExtension(parentName) + ".model.cs"); ProjectItem projectItem = Helper.ProjectHelpers.FindInProject(structFileName); projectItem?.Document?.Close(vsSaveChanges.vsSaveChangesNo); //projectItem?.Remove(); using (StreamWriter sw = new StreamWriter(structFileName, false, System.Text.Encoding.UTF8)) { var document = proj.Documents.Single(doc => { return(doc.FilePath == parentFileName); }); Controller.StructModel model = new Controller.StructModel(); var tree = document.GetSyntaxTreeAsync().Result; SyntaxNode root = tree.GetRoot(); SemanticModel semanticModel = cSharpCompilation.GetSemanticModel(tree); model.WriteDocument(sw, tree, semanticModel); sw.Close(); OutPutString("生成控制器实体类成功:\"" + structFileName + "\""); } if (File.Exists(structFileName)) { Helper.ProjectHelpers.AddNestedFile(Document.ProjectItem, structFileName, "Compile", false); } } else if (Document.Name.EndsWith(".cshtml")) { project = Document.ProjectItem.ContainingProject; string parentName = Document.ProjectItem.Name; string parentFileName = Document.ProjectItem.FileNames[0]; string parentPath = Path.GetDirectoryName(parentFileName); workspace = componentModel.GetService <VisualStudioWorkspace>(); proj = workspace.CurrentSolution.Projects.Where(doc => doc.FilePath == project.FileName).FirstOrDefault(); string fileName = Path.Combine(parentPath, Path.GetFileNameWithoutExtension(parentName) + ".template.cs"); ProjectItem projectItem = Helper.ProjectHelpers.FindInProject(fileName); projectItem?.Document?.Close(vsSaveChanges.vsSaveChangesNo); projectItem?.Remove(); Document.ProjectItem.ContainingProject.Save(); using (StreamWriter sw = new StreamWriter(fileName, false, System.Text.Encoding.UTF8)) { string nameSpace = GetNameSpace(Document); string className = Path.GetFileNameWithoutExtension(parentName); Razor.RazorWriter writer = new Razor.RazorWriter(parentFileName); writer.WriteTemplate(sw, nameSpace, className); sw.Close(); OutPutString("生成模板类成功:\"" + fileName + "\""); } if (File.Exists(fileName)) { projectItem = Helper.ProjectHelpers.AddNestedFile(Document.ProjectItem, fileName, "Compile", false); //添加<Content Include="Views\Index.cshtml">BrowseToURL workspace.TryApplyChanges(proj.Solution); //代码结构 projectItem?.Document?.NewWindow(); workspace = componentModel.GetService <VisualStudioWorkspace>(); proj = workspace.CurrentSolution.Projects.Where(x => x.FilePath == project.FileName).FirstOrDefault(); Microsoft.CodeAnalysis.Document codeDocument = proj.Documents.FirstOrDefault((doc => { if (doc.FilePath == fileName) { return(true); } else { return(false); } })); //格式化 Microsoft.CodeAnalysis.Document formatDocument = Microsoft.CodeAnalysis.Formatting.Formatter.FormatAsync(codeDocument).Result; //更改 workspace.TryApplyChanges(formatDocument.Project.Solution); } } else if (Document.Name.EndsWith(".nf.json")) { project = Document.ProjectItem.ContainingProject; string parentName = Document.ProjectItem.Name; string parentFileName = Document.ProjectItem.FileNames[0]; string inputFileContent = null; using (StreamReader sr = new StreamReader(parentFileName, System.Text.Encoding.UTF8)) { inputFileContent = sr.ReadToEnd(); } string nameSpace = GetNameSpace(Document); string className = parentName.TrimSuffix(".nf.json").Replace('-', '_'); string outPutFileName = parentFileName.TrimSuffix(".nf.json") + ".cs"; ProjectItem projectItem = Helper.ProjectHelpers.FindInProject(outPutFileName); projectItem?.Document?.Close(vsSaveChanges.vsSaveChangesNo); if (inputFileContent != null) { using (StreamWriter sw = new StreamWriter(outPutFileName, false, System.Text.Encoding.UTF8)) { Xamasoft.JsonClassGenerator.JsonClassGenerator gen = new Xamasoft.JsonClassGenerator.JsonClassGenerator(); gen.Example = inputFileContent; gen.InternalVisibility = false; gen.CodeWriter = new Xamasoft.JsonClassGenerator.CodeWriters.CSharpCodeWriter(); gen.ExplicitDeserialization = false; gen.Namespace = nameSpace; gen.NoHelperClass = true; gen.SecondaryNamespace = nameSpace; gen.TargetFolder = null; gen.UseProperties = false; gen.MainClass = className; gen.UsePascalCase = false; gen.UseNestedClasses = false; gen.ApplyObfuscationAttributes = false; gen.SingleFile = true; gen.ExamplesInDocumentation = true; gen.OutputStream = sw; gen.GenerateClasses(); OutPutString("生成JSON对应实体类成功:\"" + outPutFileName + "\""); } } if (File.Exists(outPutFileName)) { Helper.ProjectHelpers.AddNestedFile(Document.ProjectItem, outPutFileName, "Compile", false); } } else if (Document.Name.EndsWith(".nf.sql")) { string parentName = Document.ProjectItem.Name; string parentFileName = Document.ProjectItem.FileNames[0]; string nameSpace = GetNameSpace(Document); string fileContent = null; using (StreamReader sr = new StreamReader(parentFileName, System.Text.Encoding.UTF8)) { fileContent = sr.ReadToEnd(); } if (fileContent != null) { Sql.SqlDocument sqlDocument = new Sql.SqlDocument(parentFileName, nameSpace, fileContent); foreach (var model in sqlDocument.modelFileDataList) { ProjectItem projectItem = Helper.ProjectHelpers.FindInProject(model.fileName); projectItem?.Document?.Close(vsSaveChanges.vsSaveChangesNo); using (StreamWriter sw = new StreamWriter(model.fileName, false, System.Text.Encoding.UTF8)) { sw.Write(model.content); sw.Close(); OutPutString("生成sql对应实体类成功:\"" + model.fileName + "\""); } if (File.Exists(model.fileName)) { Helper.ProjectHelpers.AddNestedFile(Document.ProjectItem, model.fileName, "Compile", false); } } } } }