🏗️ Project Management
The Composer SDK's project management module covers the full lifecycle of a project — from creation, initialization, script execution, to packaging and archiving. All methods are attached to the core type Composer and defined in pkg/composer/project.go.
Package path: github.com/scagogogo/composer-skills/pkg/composer
Capability Overview 🏗️
| Method | Purpose | Return Value |
|---|---|---|
CreateProject | Create a new project from a skeleton package | error |
CreateProjectWithOptions | Create a project with additional options | error |
InitProject | Interactively initialize composer.json | error |
InitProjectWithOptions | Non-interactively initialize with name/description/author | error |
RunScript | Run a script defined in composer.json, with arguments | (string, error) |
ExecuteScript | Run a custom script via composer run | (string, error) |
ArchiveProject | Package the current project into a zip/tar archive | error |
GetProjectInfo | Parse basic project info (name, description, type, dependencies) | (*ComposerJsonInfo, error) |
ListScripts | List all scripts defined in composer.json | (string, error) |
Key Type
ComposerJsonInfo is the structured return of GetProjectInfo, defined in this file, containing only the most commonly read fields of a project.
🏗️ CreateProject
Create a new project from the specified package name, equivalent to composer create-project package/name directory version.
When to Use
Use when you need to scaffold a brand-new project from a skeleton package (e.g., laravel/laravel, symfony/website-skeleton).
Signature
func (c *Composer) CreateProject(packageName string, directory string, version string) errorParameters
| Parameter | Type | Description |
|---|---|---|
packageName | string | Package name, e.g., laravel/laravel |
directory | string | Project destination directory |
version | string | Version constraint, empty string means latest version |
Return Values
error: Error that occurred during creation.
Example
package main
import (
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatalf("Failed to initialize Composer: %v", err)
}
// Create a latest version Laravel project
if err := comp.CreateProject("laravel/laravel", "my-project", ""); err != nil {
log.Fatalf("Failed to create project: %v", err)
}
// Create a specified version Symfony project
if err := comp.CreateProject("symfony/website-skeleton", "symfony-project", "^5.0"); err != nil {
log.Fatalf("Failed to create project: %v", err)
}
}Advanced
Use CreateProjectWithOptions when additional flags like --no-dev, --prefer-dist, custom repositories, etc. are needed.
🏗️ CreateProjectWithOptions
Create a project with additional options, equivalent to composer create-project [options] package/name[:version] directory.
When to Use
Use when the default CreateProject is not enough and you need to control the installation method (no dev dependencies, mirror source, stability, etc.).
Signature
func (c *Composer) CreateProjectWithOptions(packageName string, directory string, version string, options map[string]string) errorParameters
| Parameter | Type | Description |
|---|---|---|
packageName | string | Package name |
directory | string | Project destination directory |
version | string | Version constraint, empty string means latest version |
options | map[string]string | Additional options; keys are option names, values are option values (empty string means a flag option) |
Option Construction Rule
options is processed by internal buildOptionsArgs: keys are sorted alphabetically, then values that are empty generate --key, otherwise --key=value, ensuring deterministic command construction.
Return Values
error: Error that occurred during creation.
Example
// Create a Laravel project without dev dependencies
options := map[string]string{
"no-dev": "",
"prefer-dist": "",
}
if err := comp.CreateProjectWithOptions("laravel/laravel", "my-project", "", options); err != nil {
log.Fatalf("Failed to create project: %v", err)
}
// Create a Symfony project with specified stability from a private repository
options = map[string]string{
"stability": "dev",
"repository": "https://example.org/private-repo",
}
if err := comp.CreateProjectWithOptions("symfony/website-skeleton", "symfony-project", "^5.0", options); err != nil {
log.Fatalf("Failed to create project: %v", err)
}🏗️ InitProject
Interactively initialize a new project, equivalent to composer init.
When to Use
Use to hand-craft a composer.json in an empty directory. The command interactively asks for project information.
Signature
func (c *Composer) InitProject() errorReturn Values
error: Error that occurred during initialization.
Example
if err := comp.InitProject(); err != nil {
log.Fatalf("Failed to initialize project: %v", err)
}Advanced
In automated scripts/CI where interaction is not possible, use InitProjectWithOptions to pass project metadata in one call.
🏗️ InitProjectWithOptions
Non-interactively initialize a project, equivalent to composer init --name=... --description=... --author=... [options].
When to Use
Use when generating composer.json in TTY-less environments such as CI, scaffolding, containers.
Signature
func (c *Composer) InitProjectWithOptions(name string, description string, author string, options map[string]string) errorParameters
| Parameter | Type | Description |
|---|---|---|
name | string | Project name, format vendor/name |
description | string | Project description |
author | string | Author info, format Name <email> |
options | map[string]string | Additional options, e.g., type, license, no-interaction |
Return Values
error: Error that occurred during initialization.
Example
options := map[string]string{
"type": "library",
"license": "MIT",
"no-interaction": "",
}
if err := comp.InitProjectWithOptions(
"myvendor/awesome-lib",
"An awesome PHP library",
"John Doe <john@example.com>",
options,
); err != nil {
log.Fatalf("Failed to initialize project: %v", err)
}🏗️ RunScript
Run a script defined in composer.json with arguments, equivalent to composer run-script script-name -- arg1 arg2.
When to Use
Use to trigger already-defined scripts like test, build, deploy from your program, when you need to pass extra arguments through to the script.
Signature
func (c *Composer) RunScript(scriptName string, args ...string) (string, error)Parameters
| Parameter | Type | Description |
|---|---|---|
scriptName | string | Name of the script to run |
args | ...string | Extra arguments passed through to the script |
Return Values
string: Command execution output.error: Error that occurred during execution.
Example
output, err := comp.RunScript("test", "--filter=UserTest")
if err != nil {
log.Fatalf("Failed to run script: %v", err)
}
fmt.Println(output)🏗️ ExecuteScript
Run a custom script defined in composer.json via composer run, equivalent to composer run script-name.
When to Use
Use to run custom scripts (non-built-in Composer script events). The difference from RunScript is that it uses the composer run command and does not accept pass-through arguments.
Signature
func (c *Composer) ExecuteScript(scriptName string) (string, error)Parameters
| Parameter | Type | Description |
|---|---|---|
scriptName | string | Name of the script to run |
Return Values
string: Command execution output.error: Error that occurred during execution.
Example
output, err := comp.ExecuteScript("deploy")
if err != nil {
log.Fatalf("Failed to run script: %v", err)
}
fmt.Println(output)🏗️ ArchiveProject
Package the current project into an archive file, equivalent to composer archive --dir=directory --format=format.
When to Use
Use when you need to distribute the project to offline environments or back it up as an archive.
Signature
func (c *Composer) ArchiveProject(directory string, format string) errorParameters
| Parameter | Type | Description |
|---|---|---|
directory | string | Archive output directory; empty string uses the default directory |
format | string | Archive format, e.g., zip or tar; empty string uses the default format |
Return Values
error: Error that occurred during archive creation.
Example
// Create a ZIP format archive
if err := comp.ArchiveProject("./dist", "zip"); err != nil {
log.Fatalf("Failed to create archive: %v", err)
}Advanced
archive.go also has more granular archiving methods: Archive, ArchiveWithFormat, ArchiveWithOptions, ArchivePackage, ArchivePackageWithOptions, which can archive any package/version individually.
🏗️ ComposerJsonInfo Type
The struct returned by GetProjectInfo, representing partial information from composer.json.
type ComposerJsonInfo struct {
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
Require map[string]string `json:"require"`
RequireDev map[string]string `json:"require-dev"`
}| Field | Type | Description |
|---|---|---|
Name | string | Project name |
Description | string | Project description |
Type | string | Project type |
Require | map[string]string | Production dependencies |
RequireDev | map[string]string | Dev dependencies |
Field Scope
ComposerJsonInfo only covers the most commonly read fields. When you need the full composer.json structure, use ReadComposerJSON from the Config module (returns the complete ComposerJSON struct).
🏗️ GetProjectInfo
Get basic info about the current project, equivalent to running composer config --list --json and parsing the result.
When to Use
Use when you need to read the project name, description, type, and dependency manifest in your program.
Signature
func (c *Composer) GetProjectInfo() (*ComposerJsonInfo, error)Return Values
*ComposerJsonInfo: Struct pointer containing basic project info.error: Error that occurred during retrieval or parsing.
Example
info, err := comp.GetProjectInfo()
if err != nil {
log.Fatalf("Failed to get project info: %v", err)
}
fmt.Printf("Project name: %s\n", info.Name)
fmt.Printf("Project description: %s\n", info.Description)
fmt.Printf("Dependency count: %d\n", len(info.Require))🏗️ ListScripts
List all scripts defined in composer.json, equivalent to composer run-script --list.
When to Use
Use to enumerate the list of available scripts in the current project (common in scaffolding, CLI prompts).
Signature
func (c *Composer) ListScripts() (string, error)Return Values
string: Output containing the list of all scripts.error: Error that occurred while listing scripts.
Example
output, err := comp.ListScripts()
if err != nil {
log.Fatalf("Failed to list scripts: %v", err)
}
fmt.Println("Available scripts:")
fmt.Println(output)Advanced
To read scripts in a structured way, use GetScripts from the Convenience Methods module, which returns map[string]interface{}.