Create New Module Programitically

Answered

I am working on a plugin and want to create a Kotlin module programmatically as we have in New Module Action.

override fun actionPerformed(event: AnActionEvent) {
val project = event.getData(PlatformDataKeys.PROJECT) ?: return

val dataContext = event.dataContext
val module = LangDataKeys.MODULE.getData(dataContext) ?: return

runWriteAction {

val moduleManager = ModuleManager.getInstance(project)

val newModule = ModuleManager.getInstance(project).newModule(
project.basePath + "/" + module.name + ".abdul" + ModuleFileType.DOT_DEFAULT_EXTENSION,
ModuleType.EMPTY.id
)

}
}

So this creates a new Module that gets shown in settings but unable to see in the package. Also after that, I have to create different folders like src, main etc and then I have to add files.
Any idea how I can do that. 

2 comments
Comment actions Permalink

Hi Abdul,

Please see:

0
Comment actions Permalink

Thanks, Karol Lewandowski got it. This is how I am doing it, Would love to hear your thoughts on this,

var f: VirtualFile = createProjectSubFile(
ModuleRootManager.getInstance(module).contentRoots[0].path,
"$moduleName/$moduleName.iml"
)
val module = ModuleManager.getInstance(project).newModule(f.path, ModuleTypeId.JAVA_MODULE)


val model = ModuleRootManager.getInstance(module).modifiableModel
val contentEntry: ContentEntry = model.addContentEntry(f.parent)
contentEntry.addSourceFolder(contentEntry.file?.url!!, false)
model.commit()

0

Please sign in to leave a comment.