@@ -16,6 +16,14 @@ import (
1616 "github.com/pkg/errors"
1717)
1818
19+ const buildIntegrationsFolder = "integrations"
20+
21+ type BuildOptions struct {
22+ PackageRoot string
23+
24+ CreateZip bool
25+ }
26+
1927// BuildDirectory function locates the target build directory. If the directory doesn't exist, it will create it.
2028func BuildDirectory () (string , error ) {
2129 buildDir , found , err := findBuildDirectory ()
@@ -55,25 +63,49 @@ func findBuildDirectory() (string, bool, error) {
5563 return "" , false , nil
5664}
5765
58- // BuildPackagesDirectory function locates the target build directory for packages.
59- // If the directories path doesn't exist, it will create it.
66+ // BuildPackagesDirectory function locates the target build directory for the package.
6067func BuildPackagesDirectory (packageRoot string ) (string , error ) {
68+ buildDir , err := buildPackagesRootDirectory ()
69+ if err != nil {
70+ return "" , errors .Wrap (err , "can't locate build packages root directory" )
71+ }
72+ m , err := packages .ReadPackageManifestFromPackageRoot (packageRoot )
73+ if err != nil {
74+ return "" , errors .Wrapf (err , "reading package manifest failed (path: %s)" , packageRoot )
75+ }
76+ return filepath .Join (buildDir , m .Name , m .Version ), nil
77+ }
78+
79+ // buildPackagesZipPath function locates the target zipped package path.
80+ func buildPackagesZipPath (packageRoot string ) (string , error ) {
81+ buildDir , err := buildPackagesRootDirectory ()
82+ if err != nil {
83+ return "" , errors .Wrap (err , "can't locate build packages root directory" )
84+ }
85+ m , err := packages .ReadPackageManifestFromPackageRoot (packageRoot )
86+ if err != nil {
87+ return "" , errors .Wrapf (err , "reading package manifest failed (path: %s)" , packageRoot )
88+ }
89+ return ZippedBuiltPackagePath (buildDir , * m ), nil
90+ }
91+
92+ // ZippedBuiltPackagePath function returns the path to zipped built package.
93+ func ZippedBuiltPackagePath (buildDir string , m packages.PackageManifest ) string {
94+ return filepath .Join (buildDir , fmt .Sprintf ("%s-%s.zip" , m .Name , m .Version ))
95+ }
96+
97+ func buildPackagesRootDirectory () (string , error ) {
6198 buildDir , found , err := FindBuildPackagesDirectory ()
6299 if err != nil {
63100 return "" , errors .Wrap (err , "can't locate build directory" )
64101 }
65102 if ! found {
66- buildDir , err = createBuildDirectory ("integrations" ) // TODO add support for other package types
103+ buildDir , err = createBuildDirectory (buildIntegrationsFolder ) // TODO add support for other package types
67104 if err != nil {
68105 return "" , errors .Wrap (err , "can't create new build directory" )
69106 }
70107 }
71-
72- m , err := packages .ReadPackageManifestFromPackageRoot (packageRoot )
73- if err != nil {
74- return "" , errors .Wrapf (err , "reading package manifest failed (path: %s)" , packageRoot )
75- }
76- return filepath .Join (buildDir , m .Name , m .Version ), nil
108+ return buildDir , nil
77109}
78110
79111// FindBuildPackagesDirectory function locates the target build directory for packages.
@@ -84,7 +116,7 @@ func FindBuildPackagesDirectory() (string, bool, error) {
84116 }
85117
86118 if found {
87- path := filepath .Join (buildDir , "integrations" ) // TODO add support for other package types
119+ path := filepath .Join (buildDir , buildIntegrationsFolder ) // TODO add support for other package types
88120 fileInfo , err := os .Stat (path )
89121 if errors .Is (err , os .ErrNotExist ) {
90122 return "" , false , nil
@@ -102,8 +134,8 @@ func FindBuildPackagesDirectory() (string, bool, error) {
102134}
103135
104136// BuildPackage function builds the package.
105- func BuildPackage (packageRoot string ) (string , error ) {
106- destinationDir , err := BuildPackagesDirectory (packageRoot )
137+ func BuildPackage (options BuildOptions ) (string , error ) {
138+ destinationDir , err := BuildPackagesDirectory (options . PackageRoot )
107139 if err != nil {
108140 return "" , errors .Wrap (err , "can't locate build directory" )
109141 }
@@ -115,8 +147,8 @@ func BuildPackage(packageRoot string) (string, error) {
115147 return "" , errors .Wrap (err , "clearing package contents failed" )
116148 }
117149
118- logger .Debugf ("Copy package content (source: %s)" , packageRoot )
119- err = files .CopyWithoutDev (packageRoot , destinationDir )
150+ logger .Debugf ("Copy package content (source: %s)" , options . PackageRoot )
151+ err = files .CopyWithoutDev (options . PackageRoot , destinationDir )
120152 if err != nil {
121153 return "" , errors .Wrap (err , "copying package contents failed" )
122154 }
@@ -128,11 +160,26 @@ func BuildPackage(packageRoot string) (string, error) {
128160 }
129161
130162 logger .Debug ("Resolve external fields" )
131- err = resolveExternalFields (packageRoot , destinationDir )
163+ err = resolveExternalFields (options . PackageRoot , destinationDir )
132164 if err != nil {
133165 return "" , errors .Wrap (err , "resolving external fields failed" )
134166 }
135- return destinationDir , nil
167+
168+ if ! options .CreateZip {
169+ return destinationDir , nil
170+ }
171+
172+ logger .Debug ("Build zipped package" )
173+ zippedPackagePath , err := buildPackagesZipPath (options .PackageRoot )
174+ if err != nil {
175+ return "" , errors .Wrap (err , "can't evaluate path for the zipped package" )
176+ }
177+
178+ err = files .Zip (destinationDir , zippedPackagePath )
179+ if err != nil {
180+ return "" , errors .Wrapf (err , "can't compress the built package (compressed file path: %s)" , zippedPackagePath )
181+ }
182+ return zippedPackagePath , nil
136183}
137184
138185func createBuildDirectory (dirs ... string ) (string , error ) {
0 commit comments