|
| 1 | +// Copyright (c) 2023 Spotify AB. |
| 2 | +// |
| 3 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +// or more contributor license agreements. See the NOTICE file |
| 5 | +// distributed with this work for additional information |
| 6 | +// regarding copyright ownership. The ASF licenses this file |
| 7 | +// to you under the Apache License, Version 2.0 (the |
| 8 | +// "License"); you may not use this file except in compliance |
| 9 | +// with the License. You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, |
| 14 | +// software distributed under the License is distributed on an |
| 15 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +// KIND, either express or implied. See the License for the |
| 17 | +// specific language governing permissions and limitations |
| 18 | +// under the License. |
| 19 | + |
| 20 | +import Foundation |
| 21 | + |
| 22 | +enum ArtifactMetaUpdaterError: Error { |
| 23 | + /// The prebuild plugin execution was called but the local |
| 24 | + /// path to the artifact directory is still unknown |
| 25 | + /// Might happen that the artifact processor didn't invoke the updater's |
| 26 | + /// .process() after downloading/activating an artifact |
| 27 | + case artifactLocationIsUnknown |
| 28 | +} |
| 29 | + |
| 30 | +/// Updates the meta file in an unzipped artifact directory, by placing an up-to-date |
| 31 | +/// and remapped meta file. Updating the meta in the artifact allows reusing existing |
| 32 | +/// artifacts it a new meta.json schema has been released to the meta format, while |
| 33 | +/// artifacts are still backward-compatible |
| 34 | +class ArtifactMetaUpdater: ArtifactProcessor { |
| 35 | + private var artifactLocation: URL? |
| 36 | + private let metaWriter: MetaWriter |
| 37 | + private let fileRemapper: FileDependenciesRemapper |
| 38 | + |
| 39 | + init( |
| 40 | + fileRemapper: FileDependenciesRemapper, |
| 41 | + metaWriter: MetaWriter |
| 42 | + ) { |
| 43 | + self.metaWriter = metaWriter |
| 44 | + self.fileRemapper = fileRemapper |
| 45 | + } |
| 46 | + |
| 47 | + /// Remembers the artifact location, used later in the plugin |
| 48 | + /// - Parameter url: artifact's root directory |
| 49 | + func process(rawArtifact url: URL) throws { |
| 50 | + // Storing the location of the just downloaded/activated artifact |
| 51 | + // Note, the `url` location already includes a meta (generated by producer |
| 52 | + // while compiling and building an artifact) |
| 53 | + artifactLocation = url |
| 54 | + } |
| 55 | + |
| 56 | + func process(localArtifact url: URL) throws { |
| 57 | + // No need to do anything in the postbuild |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +extension ArtifactMetaUpdater: ArtifactConsumerPrebuildPlugin { |
| 62 | + |
| 63 | + /// Updates the meta json file in a local, unzipped, artifact location. It also remaps |
| 64 | + /// all paths so other steps (like actool or postbuild) don't have to do it again |
| 65 | + func run(meta: MainArtifactMeta) throws { |
| 66 | + guard let artifactLocation = artifactLocation else { |
| 67 | + throw ArtifactMetaUpdaterError.artifactLocationIsUnknown |
| 68 | + } |
| 69 | + let metaURL = try metaWriter.write(meta, locationDir: artifactLocation) |
| 70 | + try fileRemapper.remap(fromGeneric: metaURL) |
| 71 | + } |
| 72 | +} |
0 commit comments