final String packagePath = packageName.replace('.', '/');
final String path = sourceRoot != null ? sourceRoot.getPath() : directory.getPath();
// ToDo: I shouldn't create directories, only create VirtualFile object.
final Ref<PsiDirectory> resultRef = new Ref<PsiDirectory>();
new WriteAction() {
protected void run(@NotNull Result result) throws Throwable {
final VirtualFile packageFile = VfsUtil.createDirectoryIfMissing(path + '/' + packagePath);
if (packageFile != null) {
resultRef.set(PsiDirectoryFactory.getInstance(project).createDirectory(packageFile));
}