🔄 UpdatePackageVersion
Updates the version constraint of a package in the project. It first formats the version string according to the constraint type, then writes it back to composer.json via the require command.
When to use
Use this when you need to adjust dependency version constraints in batch (e.g. switching from ^1.2 to ~1.2 or pinning an exact version). It internally calls RequirePackage, so it triggers actual dependency resolution and installation.
Signature
go
func (c *Composer) UpdatePackageVersion(packageName string, version string, constraintType VersionConstraint) errorParameters
| Parameter | Type | Description |
|---|---|---|
packageName | string | Package name, e.g. symfony/console |
version | string | Base version value, e.g. 1.2.3 |
constraintType | VersionConstraint | Constraint type: ExactVersion, CaretVersion, TildeVersion, RangeVersion, WildcardVersion |
Return value
| Return value | Type | Description |
|---|---|---|
| Sole return value | error | Returned when formatting or the require call fails |
Example
go
package main
import (
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatalf("init failed: %v", err)
}
// Change the constraint of symfony/console to ^5.4.0
if err := comp.UpdatePackageVersion(
"symfony/console",
"5.4.0",
composer.CaretVersion,
); err != nil {
log.Fatalf("update version constraint failed: %v", err)
}
}Advanced
- The constraint string is assembled by FormatVersionConstraint, which can be called standalone to preview the result.
- To pin a package to an exact version, use LockPackageVersion directly.
- To query the list of available versions of a package, use GetPackageVersions (in
version_constraints.go).