1/*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import org.gradle.api.GradleException
18import org.gradle.api.initialization.Settings
19
20class SkikoSetup {
21    /**
22     * Declares the skiko entry in the version catalog of the given settings instance.
23     *
24     * @param settings The settings instance for the current root project
25     */
26    static void defineSkikoInVersionCatalog(Settings settings) {
27        settings.dependencyResolutionManagement {
28            versionCatalogs {
29                libs {
30                    def skikoOverride = System.getenv("SKIKO_VERSION")
31                    if (skikoOverride != null) {
32                        logger.warn("Using custom version ${skikoOverride} of SKIKO due to " +
33                                "SKIKO_VERSION being set.")
34                        version('skiko', skikoOverride)
35                    }
36                    String os = System.getProperty("os.name").toLowerCase(Locale.US)
37                    String currentOsArtifact
38                    if (os.contains("mac os x") || os.contains("darwin") || os.contains("osx")) {
39                        def arch = System.getProperty("os.arch")
40                        if (arch == "aarch64") {
41                            currentOsArtifact = "skiko-awt-runtime-macos-arm64"
42                        } else {
43                            currentOsArtifact = "skiko-awt-runtime-macos-x64"
44                        }
45                    } else if (os.startsWith("win")) {
46                        currentOsArtifact = "skiko-awt-runtime-windows-x64"
47                    } else if (os.startsWith("linux")) {
48                        def arch = System.getProperty("os.arch")
49                        if (arch == "aarch64") {
50                            currentOsArtifact = "skiko-awt-runtime-linux-arm64"
51                        } else {
52                            currentOsArtifact = "skiko-awt-runtime-linux-x64"
53                        }
54                    } else {
55                        throw new GradleException("Unsupported operating system $os")
56                    }
57                    library("skikoCurrentOs", "org.jetbrains.skiko",
58                            currentOsArtifact).versionRef("skiko")
59                }
60            }
61        }
62    }
63}
64
65ext.skikoSetup = new SkikoSetup()