/
GenerateSmithyBuild.scala
136 lines (118 loc) · 3.81 KB
/
GenerateSmithyBuild.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Copyright 2021-2024 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package smithy4s.codegen
import sbt.Keys._
import sbt._
import Smithy4sCodegenPlugin.autoImport._
private final case class SmithyBuildData(
imports: Seq[String],
deps: Seq[String],
repos: Seq[String]
) {
def addAll(
imports: Seq[String],
deps: Seq[String],
repos: Seq[String]
): SmithyBuildData = {
SmithyBuildData(
this.imports ++ imports,
this.deps ++ deps,
this.repos ++ repos
)
}
}
private[codegen] object GenerateSmithyBuild {
lazy val command = Command.command(
"smithy4sUpdateLSPConfig",
briefHelp =
"Write a smithy-build.json file from the modules' configuration.",
detail =
"""|Export the configuration from all modules where Smithy4sCodegenPlugin is enabled into a smithy-build.sjon
|file. If a file already exists, the content will be merged with the existing file. If not, a new file is written.""".stripMargin
) { s =>
val extracted = Project.extract(s)
val rootDir = new File(extracted.structure.root)
val SmithyBuildData(imports, deps, repos) = extractInfo(extracted, rootDir)
val json = SmithyBuildJson.toJson(imports, deps, repos)
val target = rootDir / "smithy-build.json"
val content = if (target.exists()) {
val content = IO.readLines(target).mkString("\n")
SmithyBuildJson.merge(content, json)
} else json
IO.write(target, content)
extracted.appendWithoutSession(Seq.empty, s)
}
private def extractInfo(
extracted: Extracted,
rootDir: File
): SmithyBuildData =
extracted.structure.allProjectRefs
.foldLeft(SmithyBuildData(Seq.empty, Seq.empty, Seq.empty)) {
case (gsb, pr) =>
gsb.addAll(
extractImports(pr, extracted.structure.data, rootDir),
extractDeps(pr, extracted.structure.data),
extractRepos(pr, extracted.structure.data)
)
}
private def extractDeps(
pr: ProjectRef,
settings: Settings[Scope]
): Seq[String] = {
val scalaBin = (pr / scalaBinaryVersion).get(settings)
(pr / libraryDependencies)
.get(settings)
.toList
.flatten
.filter(_.configurations.exists(_.contains(Smithy4s.name)))
.flatMap(Smithy4sCodegenPlugin.moduleIdEncode(_, scalaBin))
.distinct
}
private def extractRepos(
pr: ProjectRef,
settings: Settings[Scope]
): Seq[String] =
(pr / resolvers)
.get(settings)
.toList
.flatten
.collect(prepareResolvers)
.distinct
private def extractImports(
pr: ProjectRef,
settings: Settings[Scope],
rootDir: File
): Seq[String] =
(pr / Compile / smithy4sInputDirs)
.get(settings)
.toList
.flatten
.collect(prepareInputDirs(rootDir))
.distinct
private val prepareResolvers: PartialFunction[Resolver, String] = {
case mr: MavenRepository if !mr.root.contains("repo1.maven.org") => mr.root
}
private def prepareInputDirs(
base: File
): PartialFunction[File, String] = {
// exclude files that are under sourceManaged
case file =>
file.relativeTo(base) match {
case None => file.getAbsolutePath()
case Some(value) => value.toString()
}
}
}