forked from pgpkg/pgpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.go
More file actions
66 lines (55 loc) · 1.73 KB
/
bundle.go
File metadata and controls
66 lines (55 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package pgpkg
import (
"io/fs"
"path/filepath"
)
// Bundle represents functional unit of a package, consisting of many Units.
// Bundles are, effectively, a collection of files.
// There are three types of bundles: MOB, schema and test. These are implemented
// by embedding Bundle.
//
// Different bundles have distinct behaviours; schema
// bundles perform upgrades, MOB bundles replace
// existing code, and test bundles are executed after
// everything else is complete.
type Bundle struct {
Package *Package // canonical name of the package.
Path string // Path of this bundle, relative to the Package
Index map[string]*Unit // Index of location of each unit.
Units []*Unit // Ordered list of build units within the bundle
}
func (b *Bundle) PrintInfo(w InfoWriter) {
w.Print("Bundle Path", b.Path)
for _, u := range b.Units {
w.Print(" Unit", u.Source+":"+u.Path)
}
}
// HasUnits indicates if any build units were found for this bundle.
func (b *Bundle) HasUnits() bool {
return b.Units != nil && len(b.Units) > 0
}
// Open an arbitrary file from the bundle.
func (b *Bundle) Open(path string) (fs.File, error) {
return b.Package.Source.Open(filepath.Join(b.Path, path))
}
func (b *Bundle) getUnit(path string) (*Unit, bool) {
u, ok := b.Index[path]
return u, ok
}
// addUnit adds a new unit to the package. Note that it doesn't read or parse the unit
// until requested.
func (b *Bundle) addUnit(path string) error {
if Options.Verbose {
Verbose.Printf("%s: add unit: %s", b.Package.Name, path)
}
unit := &Unit{
Bundle: b,
Path: path,
}
b.Units = append(b.Units, unit)
b.Index[path] = unit
return nil
}
func (b *Bundle) Location() string {
return filepath.Join(b.Package.Location, b.Path)
}