|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package website.gradle.tasks |
| 20 | + |
| 21 | +import groovy.transform.CompileStatic |
| 22 | +import org.gradle.api.GradleException |
| 23 | +import org.gradle.api.Project |
| 24 | +import org.gradle.api.file.RegularFileProperty |
| 25 | +import org.gradle.api.tasks.CacheableTask |
| 26 | +import org.gradle.api.tasks.InputFile |
| 27 | +import org.gradle.api.tasks.Internal |
| 28 | +import org.gradle.api.tasks.OutputFile |
| 29 | +import org.gradle.api.tasks.PathSensitive |
| 30 | +import org.gradle.api.tasks.PathSensitivity |
| 31 | +import org.gradle.api.tasks.TaskAction |
| 32 | +import org.gradle.api.tasks.TaskProvider |
| 33 | +import org.yaml.snakeyaml.Yaml |
| 34 | +import website.gradle.GrailsWebsiteExtension |
| 35 | + |
| 36 | +@CompileStatic |
| 37 | +@CacheableTask |
| 38 | +abstract class BskyAtProtoDidTask extends GrailsWebsiteTask { |
| 39 | + |
| 40 | + @Internal |
| 41 | + final String description = 'Generates the .well-known/atproto-did file for Bsky' |
| 42 | + |
| 43 | + public static final String NAME = 'genBskyAtProtoDid' |
| 44 | + |
| 45 | + @InputFile |
| 46 | + @PathSensitive(PathSensitivity.RELATIVE) |
| 47 | + abstract RegularFileProperty getSocial() |
| 48 | + |
| 49 | + @OutputFile |
| 50 | + abstract RegularFileProperty getBskyAtProtoDid() |
| 51 | + |
| 52 | + static TaskProvider<BskyAtProtoDidTask> register( |
| 53 | + Project project, |
| 54 | + GrailsWebsiteExtension siteExt, |
| 55 | + String name = NAME |
| 56 | + ) { |
| 57 | + project.tasks.register(name, BskyAtProtoDidTask) { |
| 58 | + it.social.set(siteExt.social) |
| 59 | + it.bskyAtProtoDid.set(siteExt.bskyAtProtoDid) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @TaskAction |
| 64 | + void generateAtProtoDid() { |
| 65 | + bskyAtProtoDid.get().asFile.tap { |
| 66 | + parentFile.mkdirs() |
| 67 | + text = getBskyAtProtoDidText() |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private Map getSocialProperties() { |
| 72 | + File socialFile = social.get().asFile |
| 73 | + socialFile.withInputStream { is -> new Yaml().load(is) } as Map |
| 74 | + } |
| 75 | + |
| 76 | + private String getBskyAtProtoDidText() { |
| 77 | + try { |
| 78 | + socialProperties['bsky']['did'] |
| 79 | + } catch (NullPointerException ignore) { |
| 80 | + throw new GradleException('Missing property [bsky.did]') |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments