swift

package
v0.30.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2024 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package swift provides utility functions for generating Bazel declarations for Swift source files.

Index

Constants

View Source
const (
	UnknownProductTypeStr    = "unknown"
	LibraryProductTypeStr    = "library"
	ExecutableProductTypeStr = "executable"
	PluginProductTypeStr     = "plugin"
)
View Source
const (
	LibraryRuleKind      = "swift_library"
	ProtoLibraryRuleKind = "swift_proto_library"
	BinaryRuleKind       = "swift_binary"
	TestRuleKind         = "swift_test"

	// Repository Rule
	SwiftPkgRuleKind      = "swift_package"
	LocalSwiftPkgRuleKind = "local_swift_package"
	HTTPArchiveRuleKind   = "http_archive"

	AliasRuleKind = "alias"
)
View Source
const HTTPArchivePkgIdentity = "__http_archive__"
View Source
const (
	ModuleNameAttrName = "module_name"
)
View Source
const (
	// SwiftProtoPackageKey - Private attribute key used to augment generated swift_proto_library
	// rules with a SwiftProtoPackage.
	// This is used to create the import specs for swift_proto_library -> swift_proto_library dependencies.
	SwiftProtoPackageKey = "_swift_proto_package"
)

Variables

This section is empty.

Functions

func BazelLabelFromTarget

func BazelLabelFromTarget(repoName string, target *swiftpkg.Target) *label.Label

BazelLabelFromTarget creates a Bazel label from a Swift target. The logic in this function must stay in sync with pkginfo_targets.bazel_label_name_from_parts() in the Starlark code.

func BzlmodStanzas

func BzlmodStanzas(di *DependencyIndex, moduleDir string, diPath string) (string, error)

func CodeDirForLocalPackage

func CodeDirForLocalPackage(pkgDir string, localPkgPath string) string

CodeDirForLocalPackage returns the path to the dependency's code. For local packages, it is the path to the local package.

func CodeDirForRemotePackage

func CodeDirForRemotePackage(buildDir string, url string) string

CodeDirForRemotePackage returns the path to the dependency's code. For source control dependencies, it is the checkout directory.

func FilterFiles

func FilterFiles(paths []string) []string

FilterFiles returns a list of Swift source files excluding `Package.swift`.

func Imports

func Imports(rules []*rule.Rule) []any

Imports returns the imports for Gazelle indexing.

func IsBuiltInFramework added in v0.22.0

func IsBuiltInFramework(name string) bool

IsBuiltInFramework determines if the module is built into the Swift standard library.

func IsBuiltInSwiftModule added in v0.22.0

func IsBuiltInSwiftModule(name string) bool

IsBuiltInSwiftModule determines if the module is built into the Swift standard library.

func IsSwiftRuleKind

func IsSwiftRuleKind(ruleKind string) bool

IsSwiftRuleKind determines whether to provided rule kind is a Swift rule.

func ModuleDir

func ModuleDir(configModPaths []string, path string) string

ModuleDir returns the module root directory. The configModPaths is a list of relative paths to module directories defined by swift_default_module_name directives.

func ModuleName

func ModuleName(r *rule.Rule) string

ModuleName returns the module name from a Swift rule declaration.

func ModulemapBazelLabelFromTargetLabel added in v0.6.0

func ModulemapBazelLabelFromTargetLabel(lbl *label.Label) *label.Label

ModulemapBazelLabelFromTargetLabel creates a Bazel label for a modulemap target from the corresponding target label.

func NewLabel

func NewLabel(lblStr LabelStr) (*label.Label, error)

NewLabel creates a Bazel label from a label string.

func NewPatchesFromYAML added in v0.5.0

func NewPatchesFromYAML(b []byte) (map[string]*Patch, error)

NewPatchesFromYAML reads the provided YAML returning a map of patches organized by Swift package identity.

func RepoNameFromIdentity

func RepoNameFromIdentity(id string) string

RepoNameFromIdentity returns a Bazel repository name from a Swift package identity/name. The value produced by this function will not have the `@` character appended. This is handled by label.Label.

func RepoRuleFromBazelRepo

func RepoRuleFromBazelRepo(
	bzlRepo *BazelRepo,
	diRel string,
	pkgDir string,
	repoDir string,
	patch *Patch,
) (*rule.Rule, error)

RepoRuleFromBazelRepo returns a repository rule declaration for a Swift Bazel repository. The pkgDir is the path to the Swift package that is referencing this Bazel repository.

func RulesFromProtos added in v0.8.0

func RulesFromProtos(
	args language.GenerateArgs,
	swiftProtoGenerationMode string,
	generateSwiftProtoLibraries bool,
	generateSwiftProtoLibraryGRPCFlavors []string,
	swiftProtoCompilers map[string]string,
	generatePascalCaseModuleNames bool,
	omitProtoSuffixFromModuleNames bool,
) []*rule.Rule

RulesFromProtos returns the Bazel build rule declarations for the provided source files.

func RulesFromSrcs

func RulesFromSrcs(
	args language.GenerateArgs,
	srcs []string,
	defaultName string,
	defaultModuleName string,
	swiftLibraryTags []string,
) []*rule.Rule

RulesFromSrcs returns the Bazel build rule declarations for the provided source files.

func UseRepoNames

func UseRepoNames(di *DependencyIndex) (string, error)

Types

type BazelRepo

type BazelRepo struct {
	Name     string
	Identity string
	PkgInfo  *swiftpkg.PackageInfo
	Pin      *spreso.Pin
}

A BazelRepo represents a Swift package as a Bazel repository for an external dependency.

func NewBazelRepo

func NewBazelRepo(
	identity string,
	pkgInfo *swiftpkg.PackageInfo,
	pin *spreso.Pin,
) (*BazelRepo, error)

NewBazelRepo returns a Bazel repo for a Swift package.

type DependencyIndex

type DependencyIndex struct {
	// contains filtered or unexported fields
}

A DependencyIndex encapsulates the indexes used to lookup Swift modules and products.

func NewDependencyIndex

func NewDependencyIndex() *DependencyIndex

NewDependencyIndex creates an empty dependency index.

func NewDependencyIndexFromJSON

func NewDependencyIndexFromJSON(data []byte) (*DependencyIndex, error)

NewDependencyIndexFromJSON creates a dependency index from JSON.

func (DependencyIndex) AddDirectDependency

func (di DependencyIndex) AddDirectDependency(identities ...string)

func (*DependencyIndex) AddModule

func (di *DependencyIndex) AddModule(modules ...*Module)

AddModule adds one or more modules to the underlying indexes.

func (*DependencyIndex) AddPackage

func (di *DependencyIndex) AddPackage(packages ...*Package)

func (*DependencyIndex) AddProduct

func (di *DependencyIndex) AddProduct(products ...*Product)

AddProduct adds one or more products to the underlying indexes.

func (DependencyIndex) DirectDepIdentities

func (di DependencyIndex) DirectDepIdentities() []string

func (*DependencyIndex) DirectDepPackages

func (di *DependencyIndex) DirectDepPackages() []*Package

func (*DependencyIndex) FindModules

func (di *DependencyIndex) FindModules(moduleName string, pkgIdentities []string) Modules

func (*DependencyIndex) GetPackage

func (di *DependencyIndex) GetPackage(identity string) *Package

func (*DependencyIndex) IndexBazelRepo

func (di *DependencyIndex) IndexBazelRepo(bzlRepo *BazelRepo) error

IndexBazelRepo indexes the modules and products for a Bazel repository.

func (*DependencyIndex) IndexRepoRule

func (di *DependencyIndex) IndexRepoRule(r *rule.Rule, repoRoot string) error

IndexRepoRule indexes any repository rules that are not already included in the module index generated by update-repos. In other words, the swift_package repo rules are not indexed here because their contents are already included in the module index JSON file.

func (*DependencyIndex) JSON

func (di *DependencyIndex) JSON() ([]byte, error)

JSON returns the pretty, JSON representation for the dependency index that is written to disk.

func (*DependencyIndex) MarshalJSON

func (di *DependencyIndex) MarshalJSON() ([]byte, error)

func (*DependencyIndex) Packages added in v0.14.0

func (di *DependencyIndex) Packages() []*Package

func (*DependencyIndex) ResolveModulesToProducts

func (di *DependencyIndex) ResolveModulesToProducts(
	moduleNames []string,
	pkgIdentities []string,
) *ModuleResolutionResult

ResolveModulesToProducts finds the best set of products to satisfy the list of modules restricted to a set of packages.

func (*DependencyIndex) UnmarshalJSON

func (di *DependencyIndex) UnmarshalJSON(b []byte) error

type HTTPArchive

type HTTPArchive struct {
	Name    string
	Modules []*Module
}

A HTTPArchive represents Swift module information found in a Bazel `http_archive` declaration.

func NewHTTPArchive

func NewHTTPArchive(name string, modules []*Module) *HTTPArchive

func NewHTTPArchiveFromRule

func NewHTTPArchiveFromRule(r *rule.Rule, repoRoot string) (*HTTPArchive, error)

NewHTTPArchiveFromRule returns an HTTPArchive from a repository rule. If the repository does not provide any Swift modules, it returns nil.

type LabelStr

type LabelStr string

A LabelStr represents a Bazel label serialized as a string.

func NewLabelStr

func NewLabelStr(l *label.Label) LabelStr

NewLabelStr creates a label string.

type LabelStrs

type LabelStrs []LabelStr

A LabelStrs represents a slice of label strings.

type LocalPackage

type LocalPackage struct {
	Path string `json:"path"`
}

type Module

type Module struct {
	Name               string
	C99name            string
	SrcType            swiftpkg.SourceType
	Label              *label.Label
	ModulemapLabel     *label.Label
	PkgIdentity        string
	ProductMemberships []string
}

Module represents a Swift module mapped to a Bazel target.

func NewModule

func NewModule(
	name, c99name string,
	srcType swiftpkg.SourceType,
	bzlLabel *label.Label,
	modulemapLabel *label.Label,
	pkgIdentity string,
	pms []string,
) *Module

NewModule creates a new module.

func NewModuleFromLabelStruct

func NewModuleFromLabelStruct(
	name, c99name string,
	srcType swiftpkg.SourceType,
	bzlLabel label.Label,
	pkgIdentity string,
	pms []string,
) *Module

NewModuleFromLabelStruct is a convenience function because label.New returns a struct, not a pointer.

func NewModuleFromTarget

func NewModuleFromTarget(repoName, pkgIdentity string, t *swiftpkg.Target) (*Module, error)

NewModuleFromTarget returns a module from the specified Swift target.

func (*Module) LabelStr

func (m *Module) LabelStr() LabelStr

LabelStr returns the label string for module.

func (*Module) MarshalJSON

func (m *Module) MarshalJSON() ([]byte, error)

MarshalJSON customizes the marshalling of a module to JSON.

func (*Module) UnmarshalJSON

func (m *Module) UnmarshalJSON(b []byte) error

UnmarshalJSON customizes the unmarshalling of a module from JSON.

type ModuleIndex

type ModuleIndex map[string]Modules

A ModuleIndex represents an index organized by module name.

func NewModuleIndex

func NewModuleIndex(modules ...*Module) ModuleIndex

NewModuleIndex creates a new module index populated with the provided modules.

func (ModuleIndex) Add

func (mi ModuleIndex) Add(modules ...*Module)

Add indexes the provided modules.

func (ModuleIndex) Modules

func (mi ModuleIndex) Modules() Modules

Modules returns a unique list of modules.

func (ModuleIndex) Resolve

func (mi ModuleIndex) Resolve(repoName, moduleName string) *Module

Resolve finds the module given the Bazel repo name and the Swift module name.

type ModuleResolutionResult

type ModuleResolutionResult struct {
	Products   Products
	Unresolved []string
}

type ModuleToProductsIndex

type ModuleToProductsIndex map[string][]ProductIndexKey

A ModuleToProductsIndex maps a module name to the products to which it belongs (i.e., has membership).

func (ModuleToProductsIndex) Add

func (mpi ModuleToProductsIndex) Add(moduleName string, newKeys ...ProductIndexKey)

func (ModuleToProductsIndex) IndexModule

func (mpi ModuleToProductsIndex) IndexModule(m *Module)

type ModuleType

type ModuleType int

A ModuleType is an enum for the type of Swift module.

const (
	UnknownModuleType ModuleType = iota
	LibraryModuleType
	BinaryModuleType
	TestModuleType
)

type Modules

type Modules []*Module

A Modules represents a slice of Swift modules.

func (Modules) LabelStrs

func (modules Modules) LabelStrs() LabelStrs

LabelStrs returns the label strings for the modules.

type Package

type Package struct {
	Name                string         `json:"name"`
	Identity            string         `json:"identity"`
	Local               *LocalPackage  `json:"local,omitempty"`
	Remote              *RemotePackage `json:"remote,omitempty"`
	CLanguageStandard   string         `json:"cLanguageStandard,omitempty"`
	CxxLanguageStandard string         `json:"cxxLanguageStandard,omitempty"`
}

func NewPackageFromBazelRepo

func NewPackageFromBazelRepo(
	bzlRepo *BazelRepo,
	diRel string,
	pkgDir string,
	repoRoot string,
	patch *Patch,
) (*Package, error)

type PackageIndex

type PackageIndex map[string]*Package

func NewPackageIndex

func NewPackageIndex(packages ...*Package) PackageIndex

func (PackageIndex) Add

func (pi PackageIndex) Add(packages ...*Package)

func (PackageIndex) Packages

func (pi PackageIndex) Packages() []*Package

type Patch added in v0.5.0

type Patch struct {
	Files   []string `json:"files" yaml:"files"`
	Args    []string `json:"args,omitempty" yaml:"args,omitempty"`
	Cmds    []string `json:"cmds,omitempty" yaml:"cmds,omitempty"`
	WinCmds []string `json:"win_cmds,omitempty" yaml:"win_cmds,omitempty"`
	Tool    string   `json:"tool,omitempty" yaml:"tool,omitempty"`
}

Patch represents the parameters needed to patch a Swift package.

type Product

type Product struct {
	Identity string
	Name     string
	Type     ProductType
	Label    *label.Label
}

A Product represents a Swift package product.

func NewProduct

func NewProduct(identity, name string, ptype ProductType, label *label.Label) *Product

func NewProductFromPkgInfoProduct

func NewProductFromPkgInfoProduct(
	bzlRepo *BazelRepo,
	prd *swiftpkg.Product,
) (*Product, error)

NewProductFromPkgInfoProduct returns a Swift product based upon a Swift manifest product.

func (*Product) IndexKey

func (p *Product) IndexKey() ProductIndexKey

IndexKey returns a unique key for the product.

func (*Product) MarshalJSON

func (p *Product) MarshalJSON() ([]byte, error)

func (*Product) UnmarshalJSON

func (p *Product) UnmarshalJSON(b []byte) error

type ProductIndex

type ProductIndex map[ProductIndexKey]*Product

A ProductIndex represents products organized by a unique key.

func NewProductIndex

func NewProductIndex(products ...*Product) ProductIndex

NewProductIndex creates a product index populated with the provided products.

func (ProductIndex) Add

func (pi ProductIndex) Add(products ...*Product)

Add indexes the provided products.

func (ProductIndex) Products

func (pi ProductIndex) Products() []*Product

Products returns the products in the index.

func (ProductIndex) Resolve

func (pi ProductIndex) Resolve(identity, name string) *Product

Resolve finds the product based upon identity and product name.

type ProductIndexKey

type ProductIndexKey string

A ProductIndexKey represents the key used to index products.

func NewProductIndexKey

func NewProductIndexKey(identity, name string) ProductIndexKey

NewProductIndexKey creates a product index key from a package identity and product name.

func (ProductIndexKey) Identity

func (pik ProductIndexKey) Identity() string

type ProductIndexKeys

type ProductIndexKeys []ProductIndexKey

func (ProductIndexKeys) Len

func (piks ProductIndexKeys) Len() int

func (ProductIndexKeys) Less

func (piks ProductIndexKeys) Less(i, j int) bool

func (ProductIndexKeys) Swap

func (piks ProductIndexKeys) Swap(i, j int)

type ProductMembershipsIndex

type ProductMembershipsIndex map[ProductIndexKey]mapset.Set[string]

func (ProductMembershipsIndex) Add

func (pmi ProductMembershipsIndex) Add(prdIndexKey ProductIndexKey, moduleNames ...string)

func (ProductMembershipsIndex) IndexModule

func (pmi ProductMembershipsIndex) IndexModule(m *Module)

type ProductType

type ProductType int

A ProductType is an enum for a Swift product type.

const (
	UnknownProductType ProductType = iota
	LibraryProductType
	ExecutableProductType
	PluginProductType
)

type Products

type Products []*Product

func (Products) Labels

func (prds Products) Labels() mapset.Set[*label.Label]

type RemotePackage

type RemotePackage struct {
	Commit  string `json:"commit"`
	Remote  string `json:"remote"`
	Version string `json:"version,omitempty"`
	Branch  string `json:"branch,omitempty"`
	Patch   *Patch `json:"patch,omitempty"`
}

type SwiftProtoPackage added in v0.30.0

type SwiftProtoPackage struct {

	// The canonical absolute path to the directory containing both the proto_library and swift_proto_library.
	Dir string

	// The workspace-relative absolute path to the directory containing both the proto_library and swift_proto_library.
	Rel string

	// The proto package from the proto_library from which the swift_proto_library was generated.
	ProtoPackage proto.Package
}

SwiftProtoPackage - Holds information about the proto package from which a swift_proto_library was generated.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL