@@ -53,16 +53,22 @@ class SimpleGradleSetupStep(
53
53
private val project : Project ,
54
54
private val rootDirectory : Path ,
55
55
private val buildSystem : BuildSystem ,
56
- private val gradleFiles : GradleFiles <String >
56
+ private val gradleFiles : GradleFiles <String >,
57
+ private val kotlinScript : Boolean = false
57
58
) : CreatorStep {
58
59
59
60
override fun runStep (indicator : ProgressIndicator ) {
60
61
runWriteTask {
62
+ if (project.isDisposed) {
63
+ return @runWriteTask
64
+ }
65
+
61
66
buildSystem.directories =
62
67
DirectorySet .create(rootDirectory)
63
68
val (buildGradle, gradleProp, settingsGradle) = setupGradleFiles(
64
69
rootDirectory,
65
- gradleFiles
70
+ gradleFiles,
71
+ kotlinScript
66
72
)
67
73
68
74
val psiManager = PsiManager .getInstance(project)
@@ -93,13 +99,18 @@ class GradleSetupStep(
93
99
private val project : Project ,
94
100
private val rootDirectory : Path ,
95
101
private val buildSystem : BuildSystem ,
96
- private val gradleFiles : GradleFiles <String >
102
+ private val gradleFiles : GradleFiles <String >,
103
+ private val kotlinScript : Boolean = false
97
104
) : CreatorStep {
98
105
override fun runStep (indicator : ProgressIndicator ) {
99
- val (_, gradleProp, settingsGradle) = setupGradleFiles(rootDirectory, gradleFiles)
106
+ val (_, gradleProp, settingsGradle) = setupGradleFiles(rootDirectory, gradleFiles, kotlinScript )
100
107
101
108
runWriteTask {
102
- val buildGradlePsi = addBuildGradleDependencies(project, buildSystem, gradleFiles.buildGradle)
109
+ if (project.isDisposed) {
110
+ return @runWriteTask
111
+ }
112
+
113
+ val buildGradlePsi = addBuildGradleDependencies(project, buildSystem, gradleFiles.buildGradle, kotlinScript)
103
114
val psiManager = PsiManager .getInstance(project)
104
115
psiManager.findDirectory(rootDirectory.virtualFileOrError)?.let { dir ->
105
116
dir.findFile(buildGradlePsi.name)?.delete()
@@ -123,11 +134,11 @@ data class GradleFiles<out T>(
123
134
val settingsGradle : T ?
124
135
)
125
136
126
- fun setupGradleFiles (dir : Path , givenFiles : GradleFiles <String >): GradleFiles <Path > {
137
+ fun setupGradleFiles (dir : Path , givenFiles : GradleFiles <String >, kotlinScript : Boolean = false ): GradleFiles <Path > {
127
138
return GradleFiles (
128
- dir.resolve(" build.gradle" ),
139
+ dir.resolve(if (kotlinScript) " build.gradle.kts " else " build.gradle" ),
129
140
givenFiles.gradleProperties?.let { dir.resolve(" gradle.properties" ) },
130
- givenFiles.settingsGradle?.let { dir.resolve(" settings.gradle" ) }
141
+ givenFiles.settingsGradle?.let { dir.resolve(if (kotlinScript) " settings.gradle.kts " else " settings.gradle " ) },
131
142
).apply {
132
143
Files .deleteIfExists(buildGradle)
133
144
Files .createFile(buildGradle)
@@ -136,10 +147,15 @@ fun setupGradleFiles(dir: Path, givenFiles: GradleFiles<String>): GradleFiles<Pa
136
147
}
137
148
}
138
149
139
- fun addBuildGradleDependencies (project : Project , buildSystem : BuildSystem , text : String ): PsiFile {
150
+ fun addBuildGradleDependencies (
151
+ project : Project ,
152
+ buildSystem : BuildSystem ,
153
+ text : String ,
154
+ kotlinScript : Boolean = false
155
+ ): PsiFile {
140
156
val file = PsiFileFactory .getInstance(project).createFileFromText(GroovyLanguage , text)
141
157
return file.runWriteAction {
142
- val fileName = " build.gradle"
158
+ val fileName = if (kotlinScript) " build.gradle.kts " else " build.gradle"
143
159
file.name = fileName
144
160
145
161
val groovyFile = file as GroovyFile
@@ -203,7 +219,8 @@ private fun getClosableBlockByName(element: PsiElement, name: String) =
203
219
class BasicGradleFinalizerStep (
204
220
private val module : Module ,
205
221
private val rootDirectory : Path ,
206
- private val buildSystem : BuildSystem
222
+ private val buildSystem : BuildSystem ,
223
+ private vararg val additionalRunTasks : String
207
224
) : CreatorStep {
208
225
private val project
209
226
get() = module.project
@@ -212,7 +229,7 @@ class BasicGradleFinalizerStep(
212
229
// Tell IntelliJ to import this project
213
230
rootDirectory.virtualFileOrError.refresh(false , true )
214
231
215
- invokeLater {
232
+ invokeLater(module.disposed) {
216
233
val path = rootDirectory.toAbsolutePath().toString()
217
234
if (canLinkAndRefreshGradleProject(path, project, false )) {
218
235
linkAndRefreshGradleProject(path, project)
@@ -222,10 +239,17 @@ class BasicGradleFinalizerStep(
222
239
223
240
// Set up the run config
224
241
// Get the gradle external task type, this is what sets it as a gradle task
242
+ addRunTaskConfiguration(" build" )
243
+ for (tasks in additionalRunTasks) {
244
+ addRunTaskConfiguration(tasks)
245
+ }
246
+ }
247
+
248
+ private fun addRunTaskConfiguration (task : String ) {
225
249
val gradleType = GradleExternalTaskConfigurationType .getInstance()
226
250
227
251
val runManager = RunManager .getInstance(project)
228
- val runConfigName = buildSystem.artifactId + " build "
252
+ val runConfigName = buildSystem.artifactId + ' ' + task
229
253
230
254
val runConfiguration = ExternalSystemRunConfiguration (
231
255
GradleConstants .SYSTEM_ID ,
@@ -237,7 +261,7 @@ class BasicGradleFinalizerStep(
237
261
// Set relevant gradle values
238
262
runConfiguration.settings.externalProjectPath = rootDirectory.toAbsolutePath().toString()
239
263
runConfiguration.settings.executionName = runConfigName
240
- runConfiguration.settings.taskNames = listOf (" build " )
264
+ runConfiguration.settings.taskNames = listOf (task )
241
265
242
266
runConfiguration.isAllowRunningInParallel = false
243
267
0 commit comments