|
| 1 | +package nuget |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/xml" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "regexp" |
| 9 | + "sort" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/bufbuild/buf/private/bufpkg/bufremoteplugin/bufremotepluginconfig" |
| 13 | +) |
| 14 | + |
| 15 | +var selfClosingTagPattern = regexp.MustCompile(`(<\w+[^>]*?)></\w+>`) |
| 16 | + |
| 17 | +// RegenerateNugetDeps processes a NuGet plugin version directory by |
| 18 | +// collecting all transitive NuGet dependencies from the plugin's |
| 19 | +// buf.plugin.yaml and regenerating the build.csproj file. |
| 20 | +func RegenerateNugetDeps(pluginVersionDir, pluginsDir string) error { |
| 21 | + yamlPath := filepath.Join(pluginVersionDir, "buf.plugin.yaml") |
| 22 | + pluginConfig, err := bufremotepluginconfig.ParseConfig(yamlPath) |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + if pluginConfig.Registry == nil || pluginConfig.Registry.Nuget == nil { |
| 27 | + return nil |
| 28 | + } |
| 29 | + dependencies, err := collectAllNugetDeps(pluginConfig, pluginsDir) |
| 30 | + if err != nil { |
| 31 | + return fmt.Errorf("collecting nuget deps: %w", err) |
| 32 | + } |
| 33 | + csproj, err := renderCsproj(pluginConfig.Registry.Nuget.TargetFrameworks, dependencies) |
| 34 | + if err != nil { |
| 35 | + return fmt.Errorf("rendering csproj: %w", err) |
| 36 | + } |
| 37 | + csprojPath := filepath.Join(pluginVersionDir, "build.csproj") |
| 38 | + if err := os.WriteFile(csprojPath, []byte(csproj), 0644); err != nil { //nolint:gosec // file permissions are intentional |
| 39 | + return fmt.Errorf("writing build.csproj: %w", err) |
| 40 | + } |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +// nugetDep represents a single NuGet package dependency. |
| 45 | +type nugetDep struct { |
| 46 | + name string |
| 47 | + version string |
| 48 | +} |
| 49 | + |
| 50 | +// collectAllNugetDeps walks the plugin's dependency tree and collects |
| 51 | +// all NuGet dependencies, including transitive ones from plugin deps. |
| 52 | +// Dependencies from deeper in the tree are collected first, matching |
| 53 | +// the order used by the test's populateNugetDeps function. |
| 54 | +func collectAllNugetDeps( |
| 55 | + pluginConfig *bufremotepluginconfig.Config, |
| 56 | + pluginsDir string, |
| 57 | +) ([]nugetDep, error) { |
| 58 | + dependencies := make(map[string]string) |
| 59 | + visited := make(map[string]bool) |
| 60 | + if err := collectNugetDepsRecursive(pluginConfig, pluginsDir, visited, dependencies); err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + result := make([]nugetDep, 0, len(dependencies)) |
| 64 | + for name, version := range dependencies { |
| 65 | + result = append(result, nugetDep{name: name, version: version}) |
| 66 | + } |
| 67 | + sort.Slice(result, func(i, j int) bool { |
| 68 | + return result[i].name < result[j].name |
| 69 | + }) |
| 70 | + return result, nil |
| 71 | +} |
| 72 | + |
| 73 | +func collectNugetDepsRecursive( |
| 74 | + pluginConfig *bufremotepluginconfig.Config, |
| 75 | + pluginsDir string, |
| 76 | + visited map[string]bool, |
| 77 | + dependencies map[string]string, |
| 78 | +) error { |
| 79 | + // First recurse into plugin dependencies. |
| 80 | + for _, dep := range pluginConfig.Dependencies { |
| 81 | + depKey := dep.IdentityString() + ":" + dep.Version() |
| 82 | + if visited[depKey] { |
| 83 | + continue |
| 84 | + } |
| 85 | + visited[depKey] = true |
| 86 | + depPath := filepath.Join( |
| 87 | + pluginsDir, dep.Owner(), dep.Plugin(), |
| 88 | + dep.Version(), "buf.plugin.yaml", |
| 89 | + ) |
| 90 | + depConfig, err := bufremotepluginconfig.ParseConfig(depPath) |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("loading dep config %s from %s: %w", depKey, depPath, err) |
| 93 | + } |
| 94 | + if err := collectNugetDepsRecursive(depConfig, pluginsDir, visited, dependencies); err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + } |
| 98 | + // Then collect this plugin's own NuGet deps. |
| 99 | + if pluginConfig.Registry != nil && pluginConfig.Registry.Nuget != nil { |
| 100 | + for _, dep := range pluginConfig.Registry.Nuget.Deps { |
| 101 | + dependencies[dep.Name] = dep.Version |
| 102 | + } |
| 103 | + } |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +// packageReference represents a PackageReference element in a csproj file. |
| 108 | +type packageReference struct { |
| 109 | + XMLName xml.Name `xml:"PackageReference"` |
| 110 | + Include string `xml:"Include,attr"` |
| 111 | + Version string `xml:"Version,attr"` |
| 112 | +} |
| 113 | + |
| 114 | +// propertyGroup represents a PropertyGroup element in a csproj file. |
| 115 | +type propertyGroup struct { |
| 116 | + XMLName xml.Name `xml:"PropertyGroup"` |
| 117 | + TargetFramework string `xml:"TargetFramework,omitempty"` |
| 118 | + TargetFrameworks string `xml:"TargetFrameworks,omitempty"` |
| 119 | +} |
| 120 | + |
| 121 | +// itemGroup represents an ItemGroup element in a csproj file. |
| 122 | +type itemGroup struct { |
| 123 | + XMLName xml.Name `xml:"ItemGroup"` |
| 124 | + PackageReferences []packageReference `xml:"PackageReference"` |
| 125 | +} |
| 126 | + |
| 127 | +// csharpProject represents a .csproj XML file. |
| 128 | +type csharpProject struct { |
| 129 | + XMLName xml.Name `xml:"Project"` |
| 130 | + SDK string `xml:"Sdk,attr"` |
| 131 | + PropertyGroup propertyGroup `xml:"PropertyGroup"` |
| 132 | + ItemGroup itemGroup `xml:"ItemGroup"` |
| 133 | +} |
| 134 | + |
| 135 | +// renderCsproj generates a build.csproj file from target frameworks and dependencies. |
| 136 | +func renderCsproj(targetFrameworks []string, dependencies []nugetDep) (string, error) { |
| 137 | + project := csharpProject{ |
| 138 | + SDK: "Microsoft.NET.Sdk", |
| 139 | + } |
| 140 | + if len(targetFrameworks) == 1 { |
| 141 | + project.PropertyGroup.TargetFramework = targetFrameworks[0] |
| 142 | + } else { |
| 143 | + project.PropertyGroup.TargetFrameworks = strings.Join(targetFrameworks, ";") |
| 144 | + } |
| 145 | + for _, dep := range dependencies { |
| 146 | + project.ItemGroup.PackageReferences = append(project.ItemGroup.PackageReferences, packageReference{ |
| 147 | + Include: dep.name, |
| 148 | + Version: dep.version, |
| 149 | + }) |
| 150 | + } |
| 151 | + output, err := xml.MarshalIndent(project, "", " ") |
| 152 | + if err != nil { |
| 153 | + return "", fmt.Errorf("marshaling csproj: %w", err) |
| 154 | + } |
| 155 | + // Convert empty XML elements to self-closing tags to match .csproj conventions. |
| 156 | + result := selfClosingTagPattern.ReplaceAllString(string(output), "$1 />") |
| 157 | + return result + "\n", nil |
| 158 | +} |
0 commit comments