InstallAngularBuildAdapter installs the angular build adaptor in the given layer if it is not already cached.
(ctx *gcp.Context, al *libcnb.Layer)
| 30 | |
| 31 | // InstallAngularBuildAdapter installs the angular build adaptor in the given layer if it is not already cached. |
| 32 | func InstallAngularBuildAdapter(ctx *gcp.Context, al *libcnb.Layer) error { |
| 33 | var version string |
| 34 | var err error |
| 35 | if adapterVersion, adapterVersionPresent := os.LookupEnv("ANGULAR_ADAPTER_VERSION"); adapterVersionPresent { |
| 36 | ctx.Logf("Using ANGULAR_ADAPTER_VERSION: %q", adapterVersion) |
| 37 | version = adapterVersion |
| 38 | } else { |
| 39 | ctx.Logf("Using latest angular adapter version") |
| 40 | version, err = angularAdapterVersion(ctx) |
| 41 | if err != nil { |
| 42 | return gcp.InternalErrorf("failed to resolve latest angular adapter version: %w", err) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Check the metadata in the cache layer to determine if version is already installed. |
| 47 | metaVersion := ctx.GetMetadata(al, AngularVersionKey) |
| 48 | if version == metaVersion { |
| 49 | ctx.CacheHit(al.Name) |
| 50 | ctx.Logf("angular adaptor cache hit: %q, %q, skipping installation.", version, metaVersion) |
| 51 | return nil |
| 52 | } |
| 53 | |
| 54 | ctx.CacheMiss(al.Name) |
| 55 | if err := ctx.ClearLayer(al); err != nil { |
| 56 | return fmt.Errorf("clearing layer %q: %w", al.Name, err) |
| 57 | } |
| 58 | ctx.Logf("Installing angular adaptor %s", version) |
| 59 | // TODO(b/323280044) account for different versions |
| 60 | if _, err := ctx.Exec([]string{"npm", "install", "--prefix", al.Path, "@apphosting/adapter-angular@" + version}); err != nil { |
| 61 | return err |
| 62 | } |
| 63 | // Store layer flags and metadata. |
| 64 | ctx.SetMetadata(al, AngularVersionKey, version) |
| 65 | return nil |
| 66 | } |
| 67 | |
| 68 | // angularAdapterVersion determines the latest version of the Angular adapter. |
| 69 | func angularAdapterVersion(ctx *gcp.Context) (string, error) { |