17
17
package org .scalasteward .core .buildtool .sbt
18
18
19
19
import better .files .File
20
- import cats .data .OptionT
20
+ import cats .data .{ NonEmptyList , OptionT }
21
21
import cats .effect .{Concurrent , Resource }
22
22
import cats .syntax .all ._
23
23
import org .scalasteward .core .application .Config
@@ -107,22 +107,17 @@ final class SbtAlg[F[_]](config: Config)(implicit
107
107
}
108
108
109
109
private def runSourcesMigration (buildRoot : BuildRoot , migration : ScalafixMigration ): F [Unit ] =
110
- OptionT (latestSbtScalafixVersion).foreachF { pluginVersion =>
111
- workspaceAlg.buildRootDir(buildRoot).flatMap { buildRootDir =>
112
- val plugin = scalaStewardSbtScalafix(pluginVersion)
113
- fileAlg.createTemporarily(buildRootDir / project, plugin).surround {
114
- val withScalacOptions = migration.scalacOptions.fold(Resource .unit[F ]) { opts =>
115
- val options = scalaStewardScalafixOptions(opts.toList)
116
- fileAlg.createTemporarily(buildRootDir, options)
117
- }
118
- withScalacOptions.surround {
119
- val scalafixCmds = migration.rewriteRules.map(rule => s " $scalafixAll $rule" ).toList
120
- val slurpOptions = SlurpOptions .ignoreBufferOverflow
121
- sbt(Nel (scalafixEnable, scalafixCmds), buildRootDir, slurpOptions).void
122
- }
123
- }
124
- }
125
- }
110
+ for {
111
+ buildRootDir <- workspaceAlg.buildRootDir(buildRoot)
112
+ _ <- runSbtScalafix(buildRootDir, migration, metaBuilds = 0 , startDepth = 0 )
113
+ } yield ()
114
+
115
+ private def runBuildMigration (buildRoot : BuildRoot , migration : ScalafixMigration ): F [Unit ] =
116
+ for {
117
+ buildRootDir <- workspaceAlg.buildRootDir(buildRoot)
118
+ metaBuilds <- metaBuildsCount(buildRootDir)
119
+ _ <- runSbtScalafix(buildRootDir, migration, metaBuilds, startDepth = 1 )
120
+ } yield ()
126
121
127
122
private def latestSbtScalafixVersion : F [Option [Version ]] =
128
123
versionsCache
@@ -132,48 +127,60 @@ final class SbtAlg[F[_]](config: Config)(implicit
132
127
private def addScalafixPluginTemporarily (
133
128
buildRootDir : File ,
134
129
pluginVersion : Version ,
135
- metaBuilds : Int
130
+ metaBuilds : Int ,
131
+ startDepth : Int
136
132
): Resource [F , Unit ] = {
133
+ val buildsDepth = metaBuilds + startDepth
137
134
val plugin = scalaStewardSbtScalafix(pluginVersion)
138
135
List
139
- .iterate(buildRootDir / project / project, metaBuilds + 1 )(_ / project)
136
+ .iterate(buildRootDir / project, buildsDepth + 1 )(_ / project)
137
+ .drop(startDepth)
140
138
.collectFold(fileAlg.createTemporarily(_, plugin))
141
139
}
142
140
143
141
private def addScalacOptionsTemporarily (
144
142
buildRootDir : File ,
145
143
scalacOptions : Option [Nel [String ]],
146
- metaBuilds : Int
144
+ metaBuilds : Int ,
145
+ startDepth : Int
147
146
): Resource [F , Unit ] =
148
147
scalacOptions.fold(Resource .unit[F ]) { opts =>
148
+ val buildsDepth = metaBuilds + startDepth
149
149
val options = scalaStewardScalafixOptions(opts.toList)
150
150
List
151
- .iterate(buildRootDir / project, metaBuilds + 1 )(_ / project)
151
+ .iterate(buildRootDir, buildsDepth + 1 )(_ / project)
152
+ .drop(startDepth)
152
153
.collectFold(fileAlg.createTemporarily(_, options))
153
154
}
154
155
155
- private def runBuildMigration (buildRoot : BuildRoot , migration : ScalafixMigration ): F [Unit ] =
156
+ private def runSbtScalafix (
157
+ buildRootDir : File ,
158
+ migration : ScalafixMigration ,
159
+ metaBuilds : Int ,
160
+ startDepth : Int
161
+ ): F [Unit ] =
156
162
OptionT (latestSbtScalafixVersion).foreachF { pluginVersion =>
157
- for {
158
- buildRootDir <- workspaceAlg.buildRootDir(buildRoot)
159
- metaBuilds <- metaBuildsCount(buildRootDir)
160
- _ <- addScalafixPluginTemporarily(buildRootDir, pluginVersion, metaBuilds).surround {
161
- addScalacOptionsTemporarily(buildRootDir, migration.scalacOptions, metaBuilds).surround {
162
- val scalafixCmds = migration.rewriteRules.map(rule => s " $scalafixAll $rule" ).toList
163
- val slurpOptions = SlurpOptions .ignoreBufferOverflow
164
- val commands = Nel .fromList(
165
- List .fill(metaBuilds + 1 )(List (reloadPlugins, scalafixEnable) ++ scalafixCmds).flatten
166
- )
167
- commands.fold(F .unit) { cmds =>
168
- sbt(
169
- cmds,
170
- buildRootDir,
171
- slurpOptions
172
- ).void
163
+ addScalafixPluginTemporarily(buildRootDir, pluginVersion, metaBuilds, startDepth)
164
+ .surround {
165
+ addScalacOptionsTemporarily(buildRootDir, migration.scalacOptions, metaBuilds, startDepth)
166
+ .surround {
167
+ val scalafixCmds = migration.rewriteRules.map(rule => s " $scalafixAll $rule" ).toList
168
+ val slurpOptions = SlurpOptions .ignoreBufferOverflow
169
+ val buildsDepth = metaBuilds + startDepth
170
+ val scalafixCommands = scalafixEnable :: scalafixCmds
171
+ val commandLists =
172
+ (scalafixCommands :: List .fill(buildsDepth)(reloadPlugins :: scalafixCommands))
173
+ .drop(startDepth)
174
+ val commands = NonEmptyList .fromList(commandLists.flatten)
175
+ commands.fold(F .unit) { cmds =>
176
+ sbt(
177
+ cmds,
178
+ buildRootDir,
179
+ slurpOptions
180
+ ).void
181
+ }
173
182
}
174
- }
175
183
}
176
- } yield ()
177
184
}
178
185
179
186
private def sbt (
0 commit comments