github.com/samlitowitz/goimportcycle@v1.0.9/internal/dot/file_resolution.go (about)

     1  package dot
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/samlitowitz/goimportcycle/internal"
     8  	"github.com/samlitowitz/goimportcycle/internal/config"
     9  )
    10  
    11  func writeNodeDefsForFileResolution(buf *bytes.Buffer, cfg *config.Config, pkgs []*internal.Package) {
    12  	clusterDefHeader := `
    13  	subgraph "cluster_%s" {
    14  		label="%s";
    15  		style="filled";
    16  		fontcolor="%s";
    17  		fillcolor="%s";
    18  `
    19  	clusterDefFooter := `
    20  	};
    21  `
    22  	nodeDef := `
    23  		"%s" [label="%s", style="filled", fontcolor="%s", fillcolor="%s"];`
    24  
    25  	for _, pkg := range pkgs {
    26  		if pkg.IsStub {
    27  			continue
    28  		}
    29  		if len(pkg.Files) == 0 {
    30  			continue
    31  		}
    32  		pkgText := cfg.Palette.Base.PackageName
    33  		pkgBackground := cfg.Palette.Base.PackageBackground
    34  		if pkg.InImportCycle {
    35  			pkgText = cfg.Palette.Cycle.PackageName
    36  			pkgBackground = cfg.Palette.Cycle.PackageBackground
    37  		}
    38  
    39  		buf.WriteString(
    40  			fmt.Sprintf(
    41  				clusterDefHeader,
    42  				pkgNodeName(pkg),
    43  				pkg.ModuleRelativePath(),
    44  				pkgText.Hex(),
    45  				pkgBackground.Hex(),
    46  			),
    47  		)
    48  		for _, file := range pkg.Files {
    49  			if file.IsStub {
    50  				continue
    51  			}
    52  			if len(file.Decls) == 0 {
    53  				continue
    54  			}
    55  			fileText := cfg.Palette.Base.FileName
    56  			fileBackground := cfg.Palette.Base.FileBackground
    57  			if file.InImportCycle {
    58  				fileText = cfg.Palette.Cycle.FileName
    59  				fileBackground = cfg.Palette.Cycle.FileBackground
    60  			}
    61  			buf.WriteString(
    62  				fmt.Sprintf(
    63  					nodeDef,
    64  					fileNodeName(file),
    65  					file.FileName,
    66  					fileText.Hex(),
    67  					fileBackground.Hex(),
    68  				),
    69  			)
    70  		}
    71  		buf.WriteString(clusterDefFooter)
    72  	}
    73  }
    74  
    75  func writeRelationshipsForFileResolution(buf *bytes.Buffer, cfg *config.Config, pkgs []*internal.Package) {
    76  	edgeDef := `
    77  	"%s" -> "%s" [color="%s"];`
    78  
    79  	for _, pkg := range pkgs {
    80  		if pkg.IsStub {
    81  			continue
    82  		}
    83  		for _, file := range pkg.Files {
    84  			if file.IsStub {
    85  				continue
    86  			}
    87  			for _, imp := range file.Imports {
    88  				if imp.Package == nil {
    89  					continue
    90  				}
    91  				if imp.Package.IsStub {
    92  					continue
    93  				}
    94  				for _, refTyp := range imp.ReferencedTypes {
    95  					arrowColor := cfg.Palette.Base.ImportArrow
    96  					if _, ok := imp.ReferencedFilesInCycle[refTyp.File.UID()]; ok {
    97  						arrowColor = cfg.Palette.Cycle.ImportArrow
    98  					}
    99  					buf.WriteString(
   100  						fmt.Sprintf(
   101  							edgeDef,
   102  							fileNodeName(file),
   103  							fileNodeName(refTyp.File),
   104  							arrowColor.Hex(),
   105  						),
   106  					)
   107  				}
   108  			}
   109  		}
   110  	}
   111  }