1+ /*
2+ * UniPub: GenerateSettingsTemplateTasks.kt
3+ * Copyright (C) 2025 mtctx
4+ *
5+ * This program is free software: you can redistribute it and/or modify
6+ * it under the terms of the GNU General Public License as published by
7+ * the Free Software Foundation, either version 3 of the License, or
8+ * (at your option) any later version.
9+ *
10+ * This program is distributed in the hope that it will be useful,
11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ * GNU General Public License for more details.
14+ *
15+ * You should have received a copy of the GNU General Public License
16+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
17+ */
18+
19+ package dev.mtctx.unipub
20+
21+ import com.charleskorn.kaml.Yaml
22+ import okio.Path
23+ import okio.Path.Companion.toPath
24+ import okio.buffer
25+ import org.gradle.api.DefaultTask
26+ import org.gradle.api.tasks.Input
27+ import org.gradle.api.tasks.TaskAction
28+
29+ open class UniPubGenerateTemplateUniPubInProjectDirTask : UniPubGenerateTemplateUniPubTask () {
30+ override fun file (): Path = projectPath(project.projectDir.absolutePath)
31+
32+ @TaskAction
33+ override fun run () {
34+ super .run ()
35+ val gitignore = fs
36+ .canonicalize(project.projectDir.absolutePath.toPath())
37+ .resolve(" .gitignore" )
38+
39+ val entry = file().name
40+
41+ if (! fs.exists(gitignore)) {
42+ fs.write(gitignore) {
43+ writeUtf8(entry)
44+ writeUtf8(" \n " )
45+ }
46+ return
47+ }
48+
49+ val content = fs.read(gitignore) { readUtf8() }
50+ if (! content.contains(entry)) {
51+ fs.appendingSink(gitignore).buffer().use {
52+ it.writeUtf8(" \n " )
53+ it.writeUtf8(entry)
54+ }
55+ }
56+ }
57+
58+ }
59+
60+ open class UniPubGenerateTemplateUniPubInHomeDirTask : UniPubGenerateTemplateUniPubTask () {
61+ override fun file (): Path = globalPath
62+ }
63+
64+ abstract class UniPubGenerateTemplateUniPubTask : DefaultTask () {
65+ @Input
66+ var profileName: String = " main"
67+
68+ @Input
69+ var overwrite: Boolean = false
70+
71+ abstract fun file (): Path
72+
73+ @TaskAction
74+ open fun run () {
75+ var settingsYaml = Yaml .default.encodeToString(
76+ UniPubSettings .serializer(), UniPubSettings (
77+ profiles = listOf (
78+ UniPubSettings .Profile (
79+ " primary # Can be anything you want" ,
80+ " YOUR_USERNAME" ,
81+ " YOUR_PASSWORD"
82+ ),
83+ UniPubSettings .Profile (
84+ " secondary" ,
85+ " YOUR_USERNAME" ,
86+ " YOUR_PASSWORD"
87+ )
88+ )
89+ )
90+ )
91+
92+ settingsYaml = """
93+ # You can customize the settings file for UniPub here.
94+ # You can use ENV(VARIABLE_NAME) to refer to environment variables.
95+
96+ $settingsYaml
97+ """ .trimIndent()
98+
99+ val file = file()
100+ if (fs.exists(file) && ! overwrite) {
101+ logger.lifecycle(" Settings file already exists at $file . Use '-Poverwrite=true' to overwrite it." )
102+ return
103+ }
104+
105+ fs.write(file) {
106+ writeUtf8(settingsYaml)
107+ }
108+ logger.lifecycle(" Settings file generated at $file " )
109+ }
110+ }
0 commit comments