public void Build(string projPath) { foreach (string fileFullName in Directory.GetFiles(Path.Combine(projPath, "Component", "Item", "State")).Where(s => s.EndsWith("State.cs"))) { var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileFullName); string file = File.ReadAllText(fileFullName); var tree = CSharpSyntaxTree.ParseText(file); var root = tree.GetRoot() as CompilationUnitSyntax; var cls = root.Members.OfType <NamespaceDeclarationSyntax>().First() .Members.OfType <ClassDeclarationSyntax>().First(); var props = cls.Members.OfType <PropertyDeclarationSyntax>(); List <string> res = new List <string>(); foreach (var prop in props) { res.Add(string.Empty); res.Add($"Task<{prop.Type}> Get{prop.Identifier}();"); res.Add(string.Empty); res.Add($"Task Set{prop.Identifier}({prop.Type} value);"); } res.Add(string.Empty); string target = Path.Combine(projPath, "Component", "Item", "I" + fileNameWithoutExtension.Substring(0, fileNameWithoutExtension.Length - "State".Length) + ".cs"); var targetFile = new FileModifier(target); List <string> lines = targetFile.Read(); try { TryInsertLinesToRegion(lines, res, "StateProperty"); } catch (Exception e) { Console.WriteLine($"In interface of {fileNameWithoutExtension}, {e.Message}"); } if (targetFile.Write(lines)) { Console.WriteLine($"Updated interface of {fileNameWithoutExtension}."); } else { Console.WriteLine($"Skipped update interface of {fileNameWithoutExtension}, file has no change."); } } }
public void Build(string projPath) { foreach (string fileFullName in Directory.GetFiles(Path.Combine(BuilderDispatcher.ProjectDirs["SimCivil.Orleans.Interfaces"], "Component", "Item", "State")).Where(s => s.EndsWith("State.cs"))) { var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileFullName); string file = File.ReadAllText(fileFullName); var tree = CSharpSyntaxTree.ParseText(file); var root = tree.GetRoot() as CompilationUnitSyntax; var cls = root.Members.OfType <NamespaceDeclarationSyntax>().First() .Members.OfType <ClassDeclarationSyntax>().First(); var props = cls.Members.OfType <PropertyDeclarationSyntax>(); List <string> res = new List <string>(); foreach (var prop in props) { res.Add(string.Empty); res.Add($"public Task<{prop.Type}> Get{prop.Identifier}()"); res.Add("{"); res.Add($" return Task.FromResult(State.{prop.Identifier});"); res.Add("}"); res.Add(string.Empty); res.Add($"public Task Set{prop.Identifier}({prop.Type} value)"); res.Add("{"); res.Add($" State.{prop.Identifier} = value;"); res.Add($" return WriteStateAsync();"); res.Add("}"); } res.Add(string.Empty); string target = Path.Combine(projPath, "Component", "Item", fileNameWithoutExtension.Substring(0, fileNameWithoutExtension.Length - "State".Length) + "Grain.cs"); FileModifier targetFile; try { targetFile = new FileModifier(target, false); } catch { Console.WriteLine($"No Grain file of {fileNameWithoutExtension} found, skipped."); continue; } List <string> lines = targetFile.Read(); try { TryInsertLinesToRegion(lines, res, "StateProperty"); } catch (Exception e) { Console.WriteLine($"In {fileNameWithoutExtension}Grain, {e.Message}"); } if (targetFile.Write(lines)) { Console.WriteLine($"Updated grain of {fileNameWithoutExtension}."); } else { Console.WriteLine($"Skipped update grain of {fileNameWithoutExtension}, file has no change."); } } }