Skip to content

🏗️ 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 🏗️

MethodPurposeReturn Value
CreateProjectCreate a new project from a skeleton packageerror
CreateProjectWithOptionsCreate a project with additional optionserror
InitProjectInteractively initialize composer.jsonerror
InitProjectWithOptionsNon-interactively initialize with name/description/authorerror
RunScriptRun a script defined in composer.json, with arguments(string, error)
ExecuteScriptRun a custom script via composer run(string, error)
ArchiveProjectPackage the current project into a zip/tar archiveerror
GetProjectInfoParse basic project info (name, description, type, dependencies)(*ComposerJsonInfo, error)
ListScriptsList 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

go
func (c *Composer) CreateProject(packageName string, directory string, version string) error

Parameters

ParameterTypeDescription
packageNamestringPackage name, e.g., laravel/laravel
directorystringProject destination directory
versionstringVersion constraint, empty string means latest version

Return Values

  • error: Error that occurred during creation.

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("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

go
func (c *Composer) CreateProjectWithOptions(packageName string, directory string, version string, options map[string]string) error

Parameters

ParameterTypeDescription
packageNamestringPackage name
directorystringProject destination directory
versionstringVersion constraint, empty string means latest version
optionsmap[string]stringAdditional 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

go
// 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

go
func (c *Composer) InitProject() error

Return Values

  • error: Error that occurred during initialization.

Example

go
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

go
func (c *Composer) InitProjectWithOptions(name string, description string, author string, options map[string]string) error

Parameters

ParameterTypeDescription
namestringProject name, format vendor/name
descriptionstringProject description
authorstringAuthor info, format Name <email>
optionsmap[string]stringAdditional options, e.g., type, license, no-interaction

Return Values

  • error: Error that occurred during initialization.

Example

go
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

go
func (c *Composer) RunScript(scriptName string, args ...string) (string, error)

Parameters

ParameterTypeDescription
scriptNamestringName of the script to run
args...stringExtra arguments passed through to the script

Return Values

  • string: Command execution output.
  • error: Error that occurred during execution.

Example

go
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

go
func (c *Composer) ExecuteScript(scriptName string) (string, error)

Parameters

ParameterTypeDescription
scriptNamestringName of the script to run

Return Values

  • string: Command execution output.
  • error: Error that occurred during execution.

Example

go
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

go
func (c *Composer) ArchiveProject(directory string, format string) error

Parameters

ParameterTypeDescription
directorystringArchive output directory; empty string uses the default directory
formatstringArchive format, e.g., zip or tar; empty string uses the default format

Return Values

  • error: Error that occurred during archive creation.

Example

go
// 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.

go
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"`
}
FieldTypeDescription
NamestringProject name
DescriptionstringProject description
TypestringProject type
Requiremap[string]stringProduction dependencies
RequireDevmap[string]stringDev 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

go
func (c *Composer) GetProjectInfo() (*ComposerJsonInfo, error)

Return Values

  • *ComposerJsonInfo: Struct pointer containing basic project info.
  • error: Error that occurred during retrieval or parsing.

Example

go
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

go
func (c *Composer) ListScripts() (string, error)

Return Values

  • string: Output containing the list of all scripts.
  • error: Error that occurred while listing scripts.

Example

go
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{}.

Released under the MIT License