Skip to content

Commit

Permalink
[python] Fix bug parsing extras (#322)
Browse files Browse the repository at this point in the history
* Threading through PkgCoordinates instead of just PkgSpec into Add

* Promoting pep440VersionComponent to a regexp

* Promoting regexes to have proper "interfaces" with exposed indices

* Demote extrasSpec to not group

* Expose extrasSpec as an explicit top-level group

* Promoting pep440Join to accept PkgCoordinates

* Raise python "extras" to PkgCoordinates
  • Loading branch information
blast-hardcheese authored Dec 20, 2024
1 parent 0f86a14 commit 09a4de8
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 112 deletions.
7 changes: 4 additions & 3 deletions internal/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ func (n PkgName) HasPrefix(other PkgName) bool {
}

type PkgCoordinates struct {
Name string
Spec PkgSpec
Name string
Spec PkgSpec
Extra any
}

// PkgSpec represents a package version constraint, e.g. "^1.1" or ">=
Expand Down Expand Up @@ -263,7 +264,7 @@ type LanguageBackend struct {
// it does not exist already.
//
// This field is mandatory.
Add func(context.Context, map[PkgName]PkgSpec, string)
Add func(context.Context, map[PkgName]PkgCoordinates, string)

// Remove packages from the specfile. The map is guaranteed to
// have at least one package, and all of the packages are
Expand Down
8 changes: 4 additions & 4 deletions internal/backends/dart/dart.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func readSpecFile() dartPubspecYaml {
return specs
}

func dartAdd(ctx context.Context, pkgs map[api.PkgName]api.PkgSpec, projectName string) {
func dartAdd(ctx context.Context, pkgs map[api.PkgName]api.PkgCoordinates, projectName string) {
//nolint:ineffassign,wastedassign,staticcheck
span, ctx := tracer.StartSpanFromContext(ctx, "dartAdd")
defer span.Finish()
Expand All @@ -267,10 +267,10 @@ func dartAdd(ctx context.Context, pkgs map[api.PkgName]api.PkgSpec, projectName

specs := readSpecFile()

for name, spec := range pkgs {
for name, coords := range pkgs {
arg := string(name)
if spec != "" {
specs.Dependencies[arg] = string(spec)
if coords.Spec != "" {
specs.Dependencies[arg] = string(coords.Spec)
} else {
specs.Dependencies[arg] = nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/backends/dotnet/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var DotNetBackend = api.LanguageBackend{
Remove: func(ctx context.Context, pkgs map[api.PkgName]bool) {
removePackages(ctx, pkgs, findSpecFile(), util.RunCmd)
},
Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgSpec, projectName string) {
Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgCoordinates, projectName string) {
addPackages(ctx, pkgs, projectName, util.RunCmd)
},
Search: search,
Expand Down
8 changes: 4 additions & 4 deletions internal/backends/dotnet/dotnet_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ func removePackages(ctx context.Context, pkgs map[api.PkgName]bool, specFileName
}

// adds packages using dotnet command which automatically updates lock files
func addPackages(ctx context.Context, pkgs map[api.PkgName]api.PkgSpec, projectName string, cmdRunner func([]string)) {
func addPackages(ctx context.Context, pkgs map[api.PkgName]api.PkgCoordinates, projectName string, cmdRunner func([]string)) {
//nolint:ineffassign,wastedassign,staticcheck
span, ctx := tracer.StartSpanFromContext(ctx, "dotnet add package")
defer span.Finish()
for packageName, spec := range pkgs {
for packageName, coords := range pkgs {
command := []string{"dotnet", "add", "package", string(packageName)}
if string(spec) != "" {
command = append(command, "--version", string(spec))
if string(coords.Spec) != "" {
command = append(command, "--version", string(coords.Spec))
}
cmdRunner(command)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/backends/dotnet/dotnet_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestAddPackages(t *testing.T) {
cmds = append(cmds, strings.Join(cmd, " "))
}

addPackages(context.Background(), map[api.PkgName]api.PkgSpec{"package": "1.0"}, "", cmdRunner)
addPackages(context.Background(), map[api.PkgName]api.PkgCoordinates{"package": {Name: "package", Spec: "1.0"}}, "", cmdRunner)

if len(cmds) != 1 {
t.Errorf("Expected one command but got %q", len(cmds))
Expand All @@ -31,7 +31,7 @@ func TestAddPackagesWithoutVersion(t *testing.T) {
cmds = append(cmds, strings.Join(cmd, " "))
}

addPackages(context.Background(), map[api.PkgName]api.PkgSpec{"package": ""}, "", cmdRunner)
addPackages(context.Background(), map[api.PkgName]api.PkgCoordinates{"package": {Name: "package"}}, "", cmdRunner)

if len(cmds) != 1 {
t.Errorf("Expected one command but got %q", len(cmds))
Expand Down
8 changes: 4 additions & 4 deletions internal/backends/elisp/elisp.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var ElispBackend = api.LanguageBackend{
}
return info
},
Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgSpec, projectName string) {
Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgCoordinates, projectName string) {
//nolint:ineffassign,wastedassign,staticcheck
span, ctx := tracer.StartSpanFromContext(ctx, "elisp add")
defer span.Finish()
Expand All @@ -110,10 +110,10 @@ var ElispBackend = api.LanguageBackend{
contents += "\n"
}

for name, spec := range pkgs {
for name, coords := range pkgs {
contents += fmt.Sprintf(`(depends-on "%s"`, name)
if spec != "" {
contents += fmt.Sprintf(" %s", spec)
if coords.Spec != "" {
contents += fmt.Sprintf(" %s", coords.Spec)
}
contents += ")\n"
}
Expand Down
5 changes: 3 additions & 2 deletions internal/backends/java/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func isAvailable() bool {
return err == nil
}

func addPackages(ctx context.Context, pkgs map[api.PkgName]api.PkgSpec, projectName string) {
func addPackages(ctx context.Context, pkgs map[api.PkgName]api.PkgCoordinates, projectName string) {
//nolint:ineffassign,wastedassign,staticcheck
span, ctx := tracer.StartSpanFromContext(ctx, "Java add package")
defer span.Finish()
Expand All @@ -136,7 +136,8 @@ func addPackages(ctx context.Context, pkgs map[api.PkgName]api.PkgSpec, projectN
}

newDependencies := []Dependency{}
for pkgName, pkgSpec := range pkgs {
for pkgName, coords := range pkgs {
pkgSpec := coords.Spec
submatches := pkgNameRegexp.FindStringSubmatch(string(pkgName))
if nil == submatches {
util.DieConsistency(
Expand Down
44 changes: 24 additions & 20 deletions internal/backends/nodejs/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,24 +411,25 @@ var NodejsYarnBackend = api.LanguageBackend{
},
Search: nodejsSearch,
Info: nodejsInfo,
Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgSpec, projectName string) {
Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgCoordinates, projectName string) {
//nolint:ineffassign,wastedassign,staticcheck
span, ctx := tracer.StartSpanFromContext(ctx, "yarn (init) add")
defer span.Finish()
if !util.Exists("package.json") {
util.RunCmd([]string{"yarn", "init", "-y"})
}
cmd := []string{"yarn", "add"}
for name, spec := range pkgs {
for name, coords := range pkgs {
name := string(name)
if found, ok := moduleToYarnpkgPackageAliases[name]; ok {
delete(pkgs, api.PkgName(name))
name = found
pkgs[api.PkgName(name)] = api.PkgSpec(spec)
coords.Name = found
pkgs[api.PkgName(name)] = coords
}
arg := name
if spec != "" {
arg += "@" + string(spec)
if coords.Spec != "" {
arg += "@" + string(coords.Spec)
}
cmd = append(cmd, arg)
}
Expand Down Expand Up @@ -497,24 +498,25 @@ var NodejsPNPMBackend = api.LanguageBackend{
},
Search: nodejsSearch,
Info: nodejsInfo,
Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgSpec, projectName string) {
Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgCoordinates, projectName string) {
//nolint:ineffassign,wastedassign,staticcheck
span, ctx := tracer.StartSpanFromContext(ctx, "pnpm (init) add")
defer span.Finish()
if !util.Exists("package.json") {
util.RunCmd([]string{"pnpm", "init"})
}
cmd := []string{"pnpm", "add"}
for name, spec := range pkgs {
for name, coords := range pkgs {
name := string(name)
if found, ok := moduleToNpmjsPackageAliases[name]; ok {
delete(pkgs, api.PkgName(name))
name = found
pkgs[api.PkgName(name)] = api.PkgSpec(spec)
coords.Name = found
pkgs[api.PkgName(name)] = coords
}
arg := name
if spec != "" {
arg += "@" + string(spec)
if coords.Spec != "" {
arg += "@" + string(coords.Spec)
}
cmd = append(cmd, arg)
}
Expand Down Expand Up @@ -600,24 +602,25 @@ var NodejsNPMBackend = api.LanguageBackend{
},
Search: nodejsSearch,
Info: nodejsInfo,
Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgSpec, projectName string) {
Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgCoordinates, projectName string) {
//nolint:ineffassign,wastedassign,staticcheck
span, ctx := tracer.StartSpanFromContext(ctx, "npm (init) install")
defer span.Finish()
if !util.Exists("package.json") {
util.RunCmd([]string{"npm", "init", "-y"})
}
cmd := []string{"npm", "install"}
for name, spec := range pkgs {
for name, coords := range pkgs {
name := string(name)
if found, ok := moduleToNpmjsPackageAliases[name]; ok {
delete(pkgs, api.PkgName(name))
name = found
pkgs[api.PkgName(name)] = api.PkgSpec(spec)
coords.Name = found
pkgs[api.PkgName(name)] = coords
}
arg := name
if spec != "" {
arg += "@" + string(spec)
if coords.Spec != "" {
arg += "@" + string(coords.Spec)
}
cmd = append(cmd, arg)
}
Expand Down Expand Up @@ -692,7 +695,7 @@ var BunBackend = api.LanguageBackend{
},
Search: nodejsSearch,
Info: nodejsInfo,
Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgSpec, projectName string) {
Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgCoordinates, projectName string) {
//nolint:ineffassign,wastedassign,staticcheck
span, ctx := tracer.StartSpanFromContext(ctx, "bun (init) add")
defer span.Finish()
Expand All @@ -701,16 +704,17 @@ var BunBackend = api.LanguageBackend{
util.RunCmd([]string{"bun", "init", "-y"})
}
cmd := []string{"bun", "add"}
for name, spec := range pkgs {
for name, coords := range pkgs {
name := string(name)
if found, ok := moduleToNpmjsPackageAliases[name]; ok {
delete(pkgs, api.PkgName(name))
name = found
pkgs[api.PkgName(name)] = api.PkgSpec(spec)
coords.Name = found
pkgs[api.PkgName(name)] = coords
}
arg := name
if spec != "" {
arg += "@" + string(spec)
if coords.Spec != "" {
arg += "@" + string(coords.Spec)
}
cmd = append(cmd, arg)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/backends/php/php.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,16 @@ var PhpComposerBackend = api.LanguageBackend{
},
Search: search,
Info: info,
Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgSpec, projectVendorName string) {
Add: func(ctx context.Context, pkgs map[api.PkgName]api.PkgCoordinates, projectVendorName string) {
//nolint:ineffassign,wastedassign,staticcheck
span, ctx := tracer.StartSpanFromContext(ctx, "composer require")
defer span.Finish()
cmd := []string{"composer", "require"}

for name, spec := range pkgs {
for name, coords := range pkgs {
arg := string(name)
if spec != "" {
arg += ":" + string(spec)
if coords.Spec != "" {
arg += ":" + string(coords.Spec)
}
cmd = append(cmd, arg)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/backends/python/pypi_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func (p *PypiMap) QueryToResults(query string) ([]api.PkgInfo, error) {
return nil, err
}
packageNames = append(packageNames, api.PkgInfo{
Name: packageName,
Version: version,
Name: packageName,
Version: version,
Description: summary,
})
}
Expand Down
Loading

0 comments on commit 09a4de8

Please sign in to comment.