🧪 RequireDryRun Simulate adding a package dependency
🔍
RequireDryRunsimulates adding a specified dependency package to the project without actually modifyingcomposer.jsonor installing the package. It is equivalent to runningcomposer require --dry-run packageName:version, suitable for previewing the dependency resolution result and potential conflicts before performing an addition.
📋 Signature
go
func (c *Composer) RequireDryRun(packageName string, version string) (string, error)📥 Parameters
| Parameter | Type | Description |
|---|---|---|
packageName | string | The name of the package to simulate adding, e.g. symfony/console |
version | string | Version constraint, e.g. ^5.0; pass an empty string "" to use the latest version |
📤 Return value
| Return value | Type | Description |
|---|---|---|
| First return value | string | The text output of the simulated addition process |
| Second return value | error | Returned when an error occurs during the simulation; nil on success |
📝 Example
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.NewComposer()
if err != nil {
log.Fatalf("failed to create Composer instance: %v", err)
}
// Simulate adding a specific version
output, err := comp.RequireDryRun("symfony/console", "^5.0")
if err != nil {
log.Fatalf("simulate addition failed: %v", err)
}
fmt.Println("Simulated addition result:")
fmt.Println(output)
// Simulate adding the latest version
output, err = comp.RequireDryRun("monolog/monolog", "")
if err != nil {
log.Fatalf("simulate addition failed: %v", err)
}
fmt.Println(output)
}🚀 Advanced
- ✅ After the dry run passes, use
RequirePackage(packageName, version, dev)to perform the real addition; to carry custom options (e.g.--prefer-source,--no-update), useRequirePackageWithOptionsand add"dry-run": ""tooptionsfor an equivalent effect. - 🧪 Run
RequireDryRunbefore introducing a new dependency to catch version conflicts or unresolvable constraints early, avoiding pollutingcomposer.json. - 📦 To simulate adding multiple packages at once, refer to
RequireMultipleand add"dry-run": ""to the options. - 🔗 Under the hood, based on whether
versionis empty, it concatenatespackage:versionor justpackage, then callsc.Run("require", "--dry-run", ...).