Skip to content

Commit

Permalink
add --replace to change dependency version
Browse files Browse the repository at this point in the history
  • Loading branch information
WeidiDeng committed May 29, 2024
1 parent 12718b9 commit 2a93ed7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
7 changes: 7 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ type Dependency struct {
Version string `json:"version,omitempty"`
}

func (d Dependency) String() string {
if d.Version != "" {
return d.PackagePath + "@" + d.Version
}
return d.PackagePath
}

// ReplacementPath represents an old or new path component
// within a Go module replacement directive.
type ReplacementPath string
Expand Down
25 changes: 23 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,30 @@ func runBuild(ctx context.Context, args []string) error {
}
log.Printf("[INFO] Resolved relative replacement %s to %s", args[i], repl)
}
replacements = append(replacements, xcaddy.NewReplace(mod, repl))
replacements = append(replacements, xcaddy.NewReplace(xcaddy.Dependency{PackagePath: mod, Version: ver}.String(), repl))
}

case "--replace":
if i == len(args)-1 {
return fmt.Errorf("expected value after --replace flag")
}
i++
mod, ver, repl, err := splitWith(args[i])
if err != nil {
return err
}
mod = strings.TrimSuffix(mod, "/") // easy to accidentally leave a trailing slash if pasting from a URL, but is invalid for Go modules
if repl == "" {
return fmt.Errorf("expected value after --replace flag")
}
// adjust relative replacements in current working directory since our temporary module is in a different directory
if strings.HasPrefix(repl, ".") {
repl, err = filepath.Abs(repl)
if err != nil {
log.Fatalf("[FATAL] %v", err)
}
log.Printf("[INFO] Resolved relative replacement %s to %s", args[i], repl)
}
replacements = append(replacements, xcaddy.NewReplace(xcaddy.Dependency{PackagePath: mod, Version: ver}.String(), repl))
case "--output":
if i == len(args)-1 {
return fmt.Errorf("expected value after --output flag")
Expand Down

0 comments on commit 2a93ed7

Please sign in to comment.