@@ -53,16 +53,22 @@ class SimpleGradleSetupStep(
5353 private val project : Project ,
5454 private val rootDirectory : Path ,
5555 private val buildSystem : BuildSystem ,
56- private val gradleFiles : GradleFiles <String >
56+ private val gradleFiles : GradleFiles <String >,
57+ private val kotlinScript : Boolean = false
5758) : CreatorStep {
5859
5960 override fun runStep (indicator : ProgressIndicator ) {
6061 runWriteTask {
62+ if (project.isDisposed) {
63+ return @runWriteTask
64+ }
65+
6166 buildSystem.directories =
6267 DirectorySet .create(rootDirectory)
6368 val (buildGradle, gradleProp, settingsGradle) = setupGradleFiles(
6469 rootDirectory,
65- gradleFiles
70+ gradleFiles,
71+ kotlinScript
6672 )
6773
6874 val psiManager = PsiManager .getInstance(project)
@@ -93,13 +99,18 @@ class GradleSetupStep(
9399 private val project : Project ,
94100 private val rootDirectory : Path ,
95101 private val buildSystem : BuildSystem ,
96- private val gradleFiles : GradleFiles <String >
102+ private val gradleFiles : GradleFiles <String >,
103+ private val kotlinScript : Boolean = false
97104) : CreatorStep {
98105 override fun runStep (indicator : ProgressIndicator ) {
99- val (_, gradleProp, settingsGradle) = setupGradleFiles(rootDirectory, gradleFiles)
106+ val (_, gradleProp, settingsGradle) = setupGradleFiles(rootDirectory, gradleFiles, kotlinScript )
100107
101108 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)
103114 val psiManager = PsiManager .getInstance(project)
104115 psiManager.findDirectory(rootDirectory.virtualFileOrError)?.let { dir ->
105116 dir.findFile(buildGradlePsi.name)?.delete()
@@ -123,11 +134,11 @@ data class GradleFiles<out T>(
123134 val settingsGradle : T ?
124135)
125136
126- fun setupGradleFiles (dir : Path , givenFiles : GradleFiles <String >): GradleFiles <Path > {
137+ fun setupGradleFiles (dir : Path , givenFiles : GradleFiles <String >, kotlinScript : Boolean = false ): GradleFiles <Path > {
127138 return GradleFiles (
128- dir.resolve(" build.gradle" ),
139+ dir.resolve(if (kotlinScript) " build.gradle.kts " else " build.gradle" ),
129140 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 " ) },
131142 ).apply {
132143 Files .deleteIfExists(buildGradle)
133144 Files .createFile(buildGradle)
@@ -136,10 +147,15 @@ fun setupGradleFiles(dir: Path, givenFiles: GradleFiles<String>): GradleFiles<Pa
136147 }
137148}
138149
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 {
140156 val file = PsiFileFactory .getInstance(project).createFileFromText(GroovyLanguage , text)
141157 return file.runWriteAction {
142- val fileName = " build.gradle"
158+ val fileName = if (kotlinScript) " build.gradle.kts " else " build.gradle"
143159 file.name = fileName
144160
145161 val groovyFile = file as GroovyFile
@@ -203,7 +219,8 @@ private fun getClosableBlockByName(element: PsiElement, name: String) =
203219class BasicGradleFinalizerStep (
204220 private val module : Module ,
205221 private val rootDirectory : Path ,
206- private val buildSystem : BuildSystem
222+ private val buildSystem : BuildSystem ,
223+ private vararg val additionalRunTasks : String
207224) : CreatorStep {
208225 private val project
209226 get() = module.project
@@ -212,7 +229,7 @@ class BasicGradleFinalizerStep(
212229 // Tell IntelliJ to import this project
213230 rootDirectory.virtualFileOrError.refresh(false , true )
214231
215- invokeLater {
232+ invokeLater(module.disposed) {
216233 val path = rootDirectory.toAbsolutePath().toString()
217234 if (canLinkAndRefreshGradleProject(path, project, false )) {
218235 linkAndRefreshGradleProject(path, project)
@@ -222,10 +239,17 @@ class BasicGradleFinalizerStep(
222239
223240 // Set up the run config
224241 // 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 ) {
225249 val gradleType = GradleExternalTaskConfigurationType .getInstance()
226250
227251 val runManager = RunManager .getInstance(project)
228- val runConfigName = buildSystem.artifactId + " build "
252+ val runConfigName = buildSystem.artifactId + ' ' + task
229253
230254 val runConfiguration = ExternalSystemRunConfiguration (
231255 GradleConstants .SYSTEM_ID ,
@@ -237,7 +261,7 @@ class BasicGradleFinalizerStep(
237261 // Set relevant gradle values
238262 runConfiguration.settings.externalProjectPath = rootDirectory.toAbsolutePath().toString()
239263 runConfiguration.settings.executionName = runConfigName
240- runConfiguration.settings.taskNames = listOf (" build " )
264+ runConfiguration.settings.taskNames = listOf (task )
241265
242266 runConfiguration.isAllowRunningInParallel = false
243267
0 commit comments