コード例 #1
0
        private bool GeneratePlantUmlFromFile(Dictionary <string, string> parameters)
        {
            var inputFileName = parameters["in"];

            if (!File.Exists(inputFileName))
            {
                OnError(new Exception($"\"{inputFileName}\" does not exist."));
                return(false);
            }
            var outputFileName = "";

            if (parameters.ContainsKey("out"))
            {
                outputFileName = parameters["out"];
                try
                {
                    var outdir = Path.GetDirectoryName(outputFileName);
                    Directory.CreateDirectory(outdir);
                }
                catch (Exception e)
                {
                    OnError(e);
                    return(false);
                }
            }
            else
            {
                outputFileName = CombinePath(Path.GetDirectoryName(inputFileName),
                                             Path.GetFileNameWithoutExtension(inputFileName) + ".puml");
            }

            try
            {
                using (var stream = new FileStream(inputFileName, FileMode.Open, FileAccess.Read))
                {
                    var             tree      = CSharpSyntaxTree.ParseText(SourceText.From(stream));
                    var             root      = tree.GetRoot();
                    Accessibilities ignoreAcc = GetIgnoreAccessibilities(parameters);

                    using (var filestream = new FileStream(outputFileName, FileMode.Create, FileAccess.Write))
                        using (var writer = new StreamWriter(filestream))
                        {
                            var gen = new PlantUmlClassDiagramGenerator.Library.ClassDiagramGenerator(writer, "    ", ignoreAcc, parameters.ContainsKey("-createAssociation"));
                            gen.Generate(root);
                        }
                }
            }
            catch (Exception e)
            {
                OnError(e);
                return(false);
            }
            return(true);
        }
コード例 #2
0
        private bool GeneratePlantUmlFromDir(Dictionary <string, string> parameters)
        {
            var inputRoot = parameters["in"];

            if (!Directory.Exists(inputRoot))
            {
                OnError(new Exception($"Directory \"{inputRoot}\" does not exist."));
                return(false);
            }

            // Use GetFullPath to fully support relative paths.
            var outputRoot = Path.GetFullPath(inputRoot);

            if (parameters.ContainsKey("out"))
            {
                outputRoot = parameters["out"];
                try
                {
                    Directory.CreateDirectory(outputRoot);
                }
                catch (Exception e)
                {
                    OnError(e);
                    return(false);
                }
            }

            var excludePaths = new List <string>();
            var pumlexclude  = CombinePath(inputRoot, ".pumlexclude");

            if (File.Exists(pumlexclude))
            {
                excludePaths = File.ReadAllLines(pumlexclude).ToList();
            }
            if (parameters.ContainsKey("-excludePaths"))
            {
                excludePaths.AddRange(parameters["-excludePaths"].Split(','));
            }

            var files = Directory.EnumerateFiles(inputRoot, "*.cs", SearchOption.AllDirectories);

            var includeRefs = new StringBuilder();

            includeRefs.AppendLine("@startuml");

            var error = false;

            foreach (var inputFile in files)
            {
                if (excludePaths
                    .Select(p => CombinePath(inputRoot, p))
                    .Any(p => inputFile.StartsWith(p, StringComparison.InvariantCultureIgnoreCase)))
                {
                    OnProgress($"Skipped \"{inputFile}\"...");
                    continue;
                }

                OnProgress($"Create puml: \"{inputFile}\"...");
                try
                {
                    var outputDir = CombinePath(outputRoot, Path.GetDirectoryName(inputFile).Replace(inputRoot, ""));
                    Directory.CreateDirectory(outputDir);
                    var outputFile = CombinePath(outputDir,
                                                 Path.GetFileNameWithoutExtension(inputFile) + ".puml");

                    using (var stream = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
                    {
                        var             tree      = CSharpSyntaxTree.ParseText(SourceText.From(stream));
                        var             root      = tree.GetRoot();
                        Accessibilities ignoreAcc = GetIgnoreAccessibilities(parameters);

                        using (var filestream = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
                            using (var writer = new StreamWriter(filestream))
                            {
                                var gen = new PlantUmlClassDiagramGenerator.Library.ClassDiagramGenerator(writer, "    ", ignoreAcc, parameters.ContainsKey("-createAssociation"));
                                gen.Generate(root);
                            }
                    }

                    if (parameters.ContainsKey("-allInOne"))
                    {
                        var lines = File.ReadAllLines(outputFile);
                        foreach (string line in lines.Skip(1).SkipLast(1))
                        {
                            includeRefs.AppendLine(line);
                        }
                    }
                    else
                    {
                        var newRoot = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @".\" : @".";
                        includeRefs.AppendLine("!include " + outputFile.Replace(outputRoot, newRoot));
                    }
                }
                catch (Exception e)
                {
                    Exception ex = new Exception($"Error {inputFile}, {e.Message}", e);
                    OnError(ex);
                    error = true;
                }
            }
            includeRefs.AppendLine("@enduml");
            File.WriteAllText(CombinePath(outputRoot, "include.puml"), includeRefs.ToString());

            if (error)
            {
                OnError(new Exception("There were files that could not be processed."));
                return(false);
            }
            return(true);
        }