From 9258e3337563b901bcf4cb493bf0f7202be0a15e Mon Sep 17 00:00:00 2001 From: GCP Buildpacks Team Date: Mon, 4 Dec 2023 12:08:05 -0800 Subject: [PATCH] Move files to proper directory based on env variable PiperOrigin-RevId: 587799903 Change-Id: I2ff493ebeb319d6e818bf5d5c85f09f124f3b9a5 --- cmd/nodejs/firebasebundle/main.go | 56 +++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/cmd/nodejs/firebasebundle/main.go b/cmd/nodejs/firebasebundle/main.go index babd96186..888fe13e2 100644 --- a/cmd/nodejs/firebasebundle/main.go +++ b/cmd/nodejs/firebasebundle/main.go @@ -15,12 +15,13 @@ // Implements nodejs/firebasebundle buildpack. // The output bundle buildpack sets up the output bundle for future steps // It will do the following -// 1. Copy over static assets to the build layer +// 1. Copy over static assets to the output bundle dir // 2. Delete unnecessary files // 3. Override run script with a new one to run the optimized build package main import ( + "os" "path/filepath" "strings" @@ -30,7 +31,8 @@ import ( ) const ( - defaultPublicDir = "public" + defaultPublicDir = "public" + firebaseOutputBundleDir = "FIREBASE_OUTPUT_BUNDLE_DIR" ) func main() { @@ -50,42 +52,48 @@ func buildFn(ctx *gcp.Context) error { return err } - staticAssetLayer, err := ctx.Layer("staticAssets", gcp.BuildLayer) - if err != nil { - return err + outputBundleDir, ok := os.LookupEnv(firebaseOutputBundleDir) + + if !ok { + return gcp.InternalErrorf("looking up output bundle env %s", firebaseOutputBundleDir) } workspacePublicDir := filepath.Join(ctx.ApplicationRoot(), defaultPublicDir) - layerPublicDir := filepath.Join(staticAssetLayer.Path, defaultPublicDir) + outputPublicDir := filepath.Join(outputBundleDir, defaultPublicDir) if bundleYaml == nil { ctx.Logf("bundle.yaml does not exist, assuming default configs") // if public folder exists assume that the code there should be in cdn ctx.Logf("No static assets declared, copying public directory (if it exists) to staticAssets by default") - err := ctx.MkdirAll(layerPublicDir, 0744) + err := copyPublicDirToOutputBundleDir(outputPublicDir, workspacePublicDir, ctx) + if err != nil { + return err + } + err = generateDefaultBundleYaml(outputBundleDir, ctx) if err != nil { return err } - fileutil.MaybeCopyPathContents(layerPublicDir, workspacePublicDir, fileutil.AllPaths) return nil } ctx.Logf("Copying static assets.") - fileutil.CopyFile(filepath.Join(staticAssetLayer.Path, "bundle.yaml"), bundlePath) + err = fileutil.CopyFile(filepath.Join(outputBundleDir, "bundle.yaml"), bundlePath) + if err != nil { + return gcp.InternalErrorf("copying output bundle dir %s: %w", outputBundleDir, err) + } if bundleYaml.StaticAssets == nil { // copy public folder by default if there are no static assets declared ctx.Logf("No static assets declared, copying public directory (if it exists) to staticAssets by default") - err := ctx.MkdirAll(layerPublicDir, 0744) + err := copyPublicDirToOutputBundleDir(outputPublicDir, workspacePublicDir, ctx) if err != nil { return err } - fileutil.MaybeCopyPathContents(layerPublicDir, workspacePublicDir, fileutil.AllPaths) } else { for _, staticAsset := range bundleYaml.StaticAssets { - ctx.MkdirAll(filepath.Join(staticAssetLayer.Path, staticAsset), 0744) - err := fileutil.MaybeCopyPathContents(filepath.Join(staticAssetLayer.Path, staticAsset), filepath.Join(ctx.ApplicationRoot(), staticAsset), fileutil.AllPaths) + ctx.MkdirAll(filepath.Join(outputBundleDir, staticAsset), 0744) + err := fileutil.MaybeCopyPathContents(filepath.Join(outputBundleDir, staticAsset), filepath.Join(ctx.ApplicationRoot(), staticAsset), fileutil.AllPaths) if err != nil { ctx.Logf("%s dir not detected", staticAsset) } @@ -103,7 +111,7 @@ func buildFn(ctx *gcp.Context) error { return err } for _, file := range files { - if _, ok := neededDirMap[file.Name()]; ok { + if _, ok := neededDirMap[file.Name()]; !ok { err := ctx.RemoveAll(filepath.Join(ctx.ApplicationRoot(), file.Name())) if err != nil { return err @@ -154,3 +162,23 @@ func readBundleYaml(ctx *gcp.Context, bundlePath string) (*bundleYaml, error) { } return &bundleYaml, nil } + +func generateDefaultBundleYaml(outputBundleDir string, ctx *gcp.Context) error { + f, err := ctx.CreateFile(filepath.Join(outputBundleDir, "bundle.yaml")) + if err != nil { + return err + } + defer f.Close() + return nil +} + +func copyPublicDirToOutputBundleDir(outputPublicDir string, workspacePublicDir string, ctx *gcp.Context) error { + err := ctx.MkdirAll(outputPublicDir, 0744) + if err != nil { + return err + } + if err := fileutil.MaybeCopyPathContents(outputPublicDir, workspacePublicDir, fileutil.AllPaths); err != nil { + return err + } + return nil +}