Hello,
I’m creating a GeneratorNewProjectWizardBuilderAdapter to create project templates in the new Project menu.
Below is my createProject override.
override fun createProject(name: String?, path: String?): Project? {
return super.createProject(name, path)?.let { project ->
val rootFile: VirtualFile? = project.guessProjectDir()
val baseFile = rootFile!!.toIoFile()
ApplicationManager.getApplication().invokeLater {
WriteAction.run<Throwable> {
unzipTemplateFile(
getTemplateZipFilePath(TemplateType.Starter),
baseFile,
wizard.group,
wizard.artifact)
}
openActivityFileAndSync(project, wizard.group, wizard.artifact)
}
project
}
}
private fun openActivityFileAndSync(project: Project, group: String, artifact: String) {
val projectRootPath = project.projectFilePath ?: return
linkAndRefreshGradleProject(projectRootPath, project)
}
This is how I am generating files from zip:
private fun unzipTemplateFile(
resourcePath: String,
rootFile: File,
group: String,
artifact: String
) {
PluginLogger.instance.info(Tag, "Trying Path Resource: $resourcePath")
val url =
SpatialSDKProjectBuilderAdapter::class.java.classLoader?.getResourceAsStream(resourcePath)
ZipInputStream(url).use { zipInputStream ->
generateSequence { zipInputStream.nextEntry }
.filterNot { it.isDirectory }
.forEach {
val fileName = processTemplateFileName(it.name, group, artifact)
val file = File(rootFile, fileName)
file.parentFile?.mkdirs()
if (fileName.endsWith(".xml") ||
fileName.endsWith(".kt") ||
fileName.endsWith(".kts")) {
val fileContent =
InputStreamReader(zipInputStream, StandardCharsets.UTF_8)
.readText()
.replace(TEMPLATE_GROUP_NAME, group)
.replace(TEMPLATE_ARTIFACT_NAME, artifact)
file.writeText(fileContent)
} else {
val byteArray = zipInputStream.readAllBytes()
FileOutputStream(file).use { outputStream -> outputStream.write(byteArray) }
}
}
}
}
1-2 seconds after creating the project with template, I see the link helper.
Clicking this will open the Gradle ToolWindow, and then clicking DownloadSources will initiate the gradle processing of the project.
I had hoped that “linkAndRefreshGradleProject” would be the thing I could use here, but I get the error “Gradle script file ‘~/StudioProjects/untitled39/.idea/misc.xml’ not found”.
How can I link gradle and initiate a sync after pulling files from zip in the invokeLater block?