Skip to content

🔄 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) error

Parameters

ParameterTypeDescription
packageNamestringPackage name, e.g. symfony/console
versionstringBase version value, e.g. 1.2.3
constraintTypeVersionConstraintConstraint type: ExactVersion, CaretVersion, TildeVersion, RangeVersion, WildcardVersion

Return value

Return valueTypeDescription
Sole return valueerrorReturned 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

Released under the MIT License