Skip to content

Commit

Permalink
Better to do the deletion prior to the project open
Browse files Browse the repository at this point in the history
This seems to work much better -- otherwise we delete new .idea
files along with our old ones.

Note that this is exactly the same behavior that has been required
already when directing users to `New Project from Existing Sources...`,
in that they have to agree to overwrite the old contents of `.idea`
when building the new project.
  • Loading branch information
ab5tract committed Sep 19, 2024
1 parent 8b6654e commit 6c73c88
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 50 deletions.
46 changes: 0 additions & 46 deletions src/main/java/org/raku/comma/project/RakuProjectBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,48 +73,6 @@ public boolean isSuitableSdkType(SdkTypeId sdkType) {
return sdkType instanceof RakuSdkType;
}

// XXX: This feels like quite a messy hack... but it works.
private void removeOldCommaProjectFile(Project project) {
String basePath = project.getBasePath();

if (basePath == null) return;

String imlPath = Paths.get(basePath + "/" + project.getName() + ".iml").toString();
File imlFile = new File(imlPath);

if (! imlFile.exists()) return;

Scanner scanner;
try {
scanner = new Scanner(imlFile);
} catch (Exception ignored) {
return;
}

boolean isOldComma = false;
while (scanner.hasNextLine()) {
String lineFromFile = scanner.nextLine();
if (lineFromFile.contains("PERL6_MODULE_TYPE")) {
isOldComma = true;
break;
}
}


if (isOldComma) {
Notifications.Bus.notify(new Notification("raku.messages",
"Old-style Comma project found. Converting.",
NotificationType.INFORMATION));

String oldModulesXmlPath = Paths.get(basePath, ".idea", "misc.xml").toString();
boolean success = FileUtil.delete(imlFile) && FileUtil.delete(new File(oldModulesXmlPath));
String message = success
? "Conversion from old-style Comma project successful."
: "Conversion from old-style Comma project failed. The project may fail to load or otherwise act strangely.";
Notifications.Bus.notify(new Notification("raku.messages", message, NotificationType.INFORMATION));
}
}

@Nullable
@Override
public List<Module> commit(@NotNull Project project,
Expand All @@ -133,8 +91,6 @@ public List<Module> commit(@NotNull Project project,
VirtualFile contentRoot = lfs.findFileByPath(path.substring(5));
if (contentRoot == null) return;

removeOldCommaProjectFile(project);

String projectFilePath = project.getProjectFilePath();
if (projectFilePath == null) return;

Expand Down Expand Up @@ -186,8 +142,6 @@ public List<Module> commit(@NotNull Project project,
manager.setPM(list.getFirst().toPM());
}
}

project.scheduleSave();
});
} catch (Exception e) {
LOG.info(e);
Expand Down
61 changes: 57 additions & 4 deletions src/main/java/org/raku/comma/project/RakuProjectOpenProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ package org.raku.comma.project

import com.intellij.ide.impl.OpenProjectTask
import com.intellij.ide.impl.ProjectUtilCore
import com.intellij.notification.Notification
import com.intellij.notification.NotificationType
import com.intellij.notification.Notifications
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ex.ProjectManagerEx
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.projectImport.ProjectOpenProcessor
import kotlinx.coroutines.*
import com.intellij.util.containers.stream
import kotlinx.coroutines.runBlocking
import org.jetbrains.annotations.Nls
import kotlin.coroutines.CoroutineContext
import java.io.File
import java.nio.file.Paths
import java.util.*

class RakuProjectOpenProcessor : ProjectOpenProcessor() {
override val name: @Nls String
Expand All @@ -35,15 +42,17 @@ class RakuProjectOpenProcessor : ProjectOpenProcessor() {
val projectDirectory = if (virtualFile.isDirectory) virtualFile else virtualFile.parent
val nioPath = projectDirectory.toNioPath()

val isValidIdeaProject = ProjectUtilCore.isValidProjectPath(projectDirectory.toNioPath())
removeOldCommaProjectFiles(projectDirectory)

val isValidIdeaProject = ProjectUtilCore.isValidProjectPath(nioPath)
val options = OpenProjectTask(
true, projectToClose, !isValidIdeaProject, isValidIdeaProject
)
val project = ProjectManagerEx.getInstanceEx().openProject(nioPath, options)
if (project != null) {

var iniialized = false
while (! iniialized) {
while (!iniialized) {
iniialized = project.isInitialized
}

Expand All @@ -58,12 +67,56 @@ class RakuProjectOpenProcessor : ProjectOpenProcessor() {
val projectBuilder = RakuProjectBuilder(null)
projectBuilder.fileToImport = file.toString()
projectBuilder.commit(project, null, null)
project.save()
}

override fun canImportProjectAfterwards(): Boolean {
return true
}

// XXX: This feels like quite a messy hack... but it works.
private fun removeOldCommaProjectFiles(basePath: VirtualFile) {
val oldImls = basePath.toNioPath().toFile().listFiles().stream()
.filter { file -> file.extension == "iml" }.toList()

if (oldImls.isEmpty() || oldImls.size > 1) return

val imlFile = oldImls.first();
val scanner: Scanner
try {
scanner = Scanner(imlFile)
} catch (ignored: Exception) {
return
}

var isOldComma = false
while (scanner.hasNextLine()) {
val lineFromFile = scanner.nextLine()
if (lineFromFile.contains("PERL6_MODULE_TYPE")) {
isOldComma = true
break
}
}

if (isOldComma) {
Notifications.Bus.notify(
Notification(
"raku.messages",
"Old-style Comma project found. Converting.",
NotificationType.INFORMATION
)
)

FileUtil.delete(imlFile)

val oldIdeaPath = Paths.get(basePath.toNioPath().toString(), ".idea").toString()
val oldIdeaDirectory = File(oldIdeaPath)
if (oldIdeaDirectory.exists() && oldIdeaDirectory.isDirectory) {
FileUtil.delete(oldIdeaDirectory)
}
}
}

override val isStrongProjectInfoHolder: Boolean
get() = true
}

0 comments on commit 6c73c88

Please sign in to comment.