forked from pgpkg/pgpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip.go
More file actions
131 lines (103 loc) · 2.91 KB
/
zip.go
File metadata and controls
131 lines (103 loc) · 2.91 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package pgpkg
import (
"archive/zip"
"fmt"
"path"
"path/filepath"
)
// Creates a ZIP archive of a project(s).
//
// The emitted ZIP file consists of a directory for each named package.
//
// Write the contents of the migration file.
func writeMigration(zw *zip.Writer, pkgPath string, schema *Schema) error {
if len(schema.migrationIndex) == 0 {
return nil // no migration scripts; nothing to import.
}
filename := filepath.Join(pkgPath, schema.migrationDir, "/@migration.pgpkg")
mw, err := zw.Create(filename)
if err != nil {
return fmt.Errorf("unable to create migration file %s: %w", filename, err)
}
for _, p := range schema.migrationIndex {
if _, err := mw.Write([]byte(p + "\n")); err != nil {
return fmt.Errorf("unable to add path to migration file %s: %w", filename, err)
}
}
return nil
}
func zipWriteConfig(zw *zip.Writer, pkgPath string, p *Package) error {
filename := filepath.Join(pkgPath, "pgpkg.toml")
tw, err := zw.Create(filename)
if err != nil {
return fmt.Errorf("unable to create toml file %s: %w", filename, err)
}
return p.config.writeConfig(tw)
}
func zipWriteUnits(zw *zip.Writer, pkgPath string, bundle *Bundle) error {
for _, unit := range bundle.Units {
unitpath := filepath.Join(pkgPath, unit.Path)
if err := unit.Parse(); err != nil {
return fmt.Errorf("unable to parse %s: %w", unitpath, err)
}
uw, err := zw.Create(unitpath)
if err != nil {
return fmt.Errorf("unable to create unit file %s: %w", unitpath, err)
}
if _, err := uw.Write([]byte(unit.Source)); err != nil {
return fmt.Errorf("unable to write unit file %s: %w", unitpath, err)
}
}
return nil
}
func writePackage(zw *zip.Writer, pkg *Package) error {
var pkgPath string
if pkg.IsDependency {
pkgPath = path.Join(".pgpkg", pkg.config.Package)
} else {
pkgPath = "."
}
if err := zipWriteConfig(zw, pkgPath, pkg); err != nil {
return err
}
if err := writeMigration(zw, pkgPath, pkg.Schema); err != nil {
return err // FIXME: add context
}
if err := zipWriteUnits(zw, pkgPath, pkg.Schema.Bundle); err != nil {
return err
}
if err := zipWriteUnits(zw, pkgPath, pkg.MOB.Bundle); err != nil {
return err
}
if err := zipWriteUnits(zw, pkgPath, pkg.Tests.Bundle); err != nil {
return err
}
return nil
}
func WriteProject(z *zip.Writer, p *Project) error {
// load any dependencies for the project. This should come from the project's cache.
if err := p.resolveDependencies(); err != nil {
return err
}
// Load the package contents.
if err := p.parseSchemas(); err != nil {
return err
}
mainPackageFound := false
for _, pkg := range p.pkgs {
// don't export pgpkg itself.
if pkg.Name == "github.com/pgpkg/pgpkg" {
continue
}
if !pkg.IsDependency {
if mainPackageFound {
return fmt.Errorf("found multiple non-dependency packages")
}
mainPackageFound = true
}
if err := writePackage(z, pkg); err != nil {
return err
}
}
return nil
}