Documentation
¶
Overview ¶
Package project provides abstractions for Directory.Packages.props files used in Central Package Management.
Package project provides abstractions for loading, parsing, and saving .NET project files (.csproj, .fsproj, .vbproj).
Index ¶
- func FindProjectFile(dir string) (string, error)
- type AssemblyReference
- type DirectoryPackagesProps
- func (dp *DirectoryPackagesProps) AddOrUpdatePackageVersion(packageID, version string) (bool, error)
- func (dp *DirectoryPackagesProps) GetPackageVersion(packageID string) string
- func (dp *DirectoryPackagesProps) RemovePackageVersion(packageID string) bool
- func (dp *DirectoryPackagesProps) Save() error
- type DirectoryPackagesRootElement
- type ItemGroup
- type PackageReference
- type PackageVersion
- type PackageVersionGroup
- type Project
- func (p *Project) AddOrUpdatePackageReference(id, version string, frameworks []string) (bool, error)
- func (p *Project) GetDirectoryPackagesPropsPath() string
- func (p *Project) GetPackageReferences() []PackageReference
- func (p *Project) GetTargetFrameworks() []string
- func (p *Project) IsCentralPackageManagementEnabled() bool
- func (p *Project) IsMultiTargeting() bool
- func (p *Project) IsSDKStyle() bool
- func (p *Project) RemovePackageReference(id string) bool
- func (p *Project) Save() error
- type PropertyGroup
- type Reference
- type RootElement
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FindProjectFile ¶
FindProjectFile finds a single .csproj, .fsproj, or .vbproj file in the directory. Returns error if 0 or >1 project files are found.
Types ¶
type AssemblyReference ¶
type AssemblyReference struct {
Include string `xml:"Include,attr"`
}
AssemblyReference represents a <Reference> element (legacy .NET Framework assembly references). Named AssemblyReference to distinguish from Reference (project references) and avoid conflicts.
type DirectoryPackagesProps ¶
type DirectoryPackagesProps struct {
Path string
Root *DirectoryPackagesRootElement
// contains filtered or unexported fields
}
DirectoryPackagesProps represents a Directory.Packages.props file.
func LoadDirectoryPackagesProps ¶
func LoadDirectoryPackagesProps(path string) (*DirectoryPackagesProps, error)
LoadDirectoryPackagesProps loads a Directory.Packages.props file from disk.
func (*DirectoryPackagesProps) AddOrUpdatePackageVersion ¶
func (dp *DirectoryPackagesProps) AddOrUpdatePackageVersion(packageID, version string) (bool, error)
AddOrUpdatePackageVersion adds a new PackageVersion or updates an existing one. Returns true if an existing PackageVersion was updated, false if a new one was added.
func (*DirectoryPackagesProps) GetPackageVersion ¶
func (dp *DirectoryPackagesProps) GetPackageVersion(packageID string) string
GetPackageVersion returns the version for a package ID, or empty string if not found.
func (*DirectoryPackagesProps) RemovePackageVersion ¶
func (dp *DirectoryPackagesProps) RemovePackageVersion(packageID string) bool
RemovePackageVersion removes a PackageVersion by package ID. Returns true if a PackageVersion was removed, false if not found.
func (*DirectoryPackagesProps) Save ¶
func (dp *DirectoryPackagesProps) Save() error
Save saves the Directory.Packages.props file to disk.
type DirectoryPackagesRootElement ¶
type DirectoryPackagesRootElement struct {
XMLName xml.Name `xml:"Project"`
Properties []PropertyGroup `xml:"PropertyGroup"`
ItemGroups []PackageVersionGroup `xml:"ItemGroup"`
}
DirectoryPackagesRootElement represents the root <Project> element.
type ItemGroup ¶
type ItemGroup struct {
Condition string `xml:"Condition,attr,omitempty"`
PackageReferences []PackageReference `xml:"PackageReference,omitempty"`
ProjectReferences []Reference `xml:"ProjectReference,omitempty"`
References []AssemblyReference `xml:"Reference,omitempty"`
}
ItemGroup represents an <ItemGroup> element containing package references or other items.
type PackageReference ¶
type PackageReference struct {
Include string `xml:"Include,attr"`
Version string `xml:"Version,attr,omitempty"`
// Additional attributes for advanced scenarios (M2.2)
PrivateAssets string `xml:"PrivateAssets,attr,omitempty"`
IncludeAssets string `xml:"IncludeAssets,attr,omitempty"`
ExcludeAssets string `xml:"ExcludeAssets,attr,omitempty"`
GeneratePathProperty string `xml:"GeneratePathProperty,attr,omitempty"`
}
PackageReference represents a <PackageReference> element.
type PackageVersion ¶
type PackageVersion struct {
XMLName xml.Name `xml:"PackageVersion"`
Include string `xml:"Include,attr"`
Version string `xml:"Version,attr"`
}
PackageVersion represents a <PackageVersion> element in Directory.Packages.props.
type PackageVersionGroup ¶
type PackageVersionGroup struct {
XMLName xml.Name `xml:"ItemGroup"`
PackageVersions []PackageVersion `xml:"PackageVersion"`
}
PackageVersionGroup represents an <ItemGroup> containing PackageVersion elements.
type Project ¶
type Project struct {
Path string
Root *RootElement
TargetFramework string // Single target framework (e.g., "net8.0")
TargetFrameworks []string // Multiple target frameworks (e.g., ["net6.0", "net7.0", "net8.0"])
// contains filtered or unexported fields
}
Project represents a .NET project file.
func LoadProject ¶
LoadProject loads and parses a project file from the given path.
func (*Project) AddOrUpdatePackageReference ¶
func (p *Project) AddOrUpdatePackageReference(id, version string, frameworks []string) (bool, error)
AddOrUpdatePackageReference adds a new PackageReference or updates an existing one. Parameters:
- id: Package ID
- version: Package version (can be empty for CPM)
- frameworks: Target frameworks for conditional references (pass nil/empty for unconditional)
Behavior matches dotnet CLI:
- If frameworks is empty/nil: Adds unconditional reference
- If frameworks is non-empty: Creates conditional ItemGroup(s) with '$(TargetFramework)' == 'tfm' condition
Returns true if an existing reference was updated, false if a new one was added.
func (*Project) GetDirectoryPackagesPropsPath ¶
GetDirectoryPackagesPropsPath returns the path to Directory.Packages.props. It checks the DirectoryPackagesPropsPath property first, then walks up the directory tree.
func (*Project) GetPackageReferences ¶
func (p *Project) GetPackageReferences() []PackageReference
GetPackageReferences returns all PackageReference elements in the project.
func (*Project) GetTargetFrameworks ¶
GetTargetFrameworks returns the list of target frameworks for the project. Returns single framework from TargetFramework or multiple from TargetFrameworks.
func (*Project) IsCentralPackageManagementEnabled ¶
IsCentralPackageManagementEnabled checks if Central Package Management (CPM) is enabled. Checks the ManagePackageVersionsCentrally property in the project file.
func (*Project) IsMultiTargeting ¶
IsMultiTargeting returns true if the project targets multiple frameworks.
func (*Project) IsSDKStyle ¶
IsSDKStyle returns true if this is an SDK-style project.
func (*Project) RemovePackageReference ¶
RemovePackageReference removes a PackageReference by package ID. Returns true if a reference was removed, false if not found.
type PropertyGroup ¶
type PropertyGroup struct {
Condition string `xml:"Condition,attr,omitempty"`
TargetFramework string `xml:"TargetFramework,omitempty"`
TargetFrameworks string `xml:"TargetFrameworks,omitempty"`
OutputType string `xml:"OutputType,omitempty"`
RootNamespace string `xml:"RootNamespace,omitempty"`
AssemblyName string `xml:"AssemblyName,omitempty"`
ManagePackageVersionsCentrally string `xml:"ManagePackageVersionsCentrally,omitempty"`
DirectoryPackagesPropsPath string `xml:"DirectoryPackagesPropsPath,omitempty"`
}
PropertyGroup represents a <PropertyGroup> element.
type Reference ¶
type Reference struct {
Include string `xml:"Include,attr"`
}
Reference represents a <ProjectReference> element (references to other projects). Named Reference rather than ProjectReference to avoid package name stuttering.
type RootElement ¶
type RootElement struct {
XMLName xml.Name `xml:"Project"`
Sdk string `xml:"Sdk,attr,omitempty"`
PropertyGroup []PropertyGroup `xml:"PropertyGroup"`
ItemGroups []ItemGroup `xml:"ItemGroup"`
RawXML []byte `xml:"-"` // Store original XML for formatting preservation
}
RootElement represents the root <Project> element of a .csproj file.