during IDEA export user selects directory instead of iml file

This commit is contained in:
Jindra Petřík
2024-08-05 11:17:25 +02:00
parent d0b33a8cd6
commit 0e7c4a6954
4 changed files with 149 additions and 56 deletions
@@ -26,8 +26,11 @@ import com.jpexs.helpers.utf8.Utf8Helper;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.security.SecureRandom;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.UUID;
/** /**
* *
@@ -35,6 +38,20 @@ import java.util.List;
*/ */
public class SwfIntelliJIdeaExporter { public class SwfIntelliJIdeaExporter {
private static final String PROJECTID_CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final int PROJECTID_LENGTH = 27;
private static final SecureRandom RANDOM = new SecureRandom();
private static String generateProjectId() {
StringBuilder sb = new StringBuilder(PROJECTID_LENGTH);
for (int i = 0; i < PROJECTID_LENGTH; i++) {
int randomIndex = RANDOM.nextInt(PROJECTID_CHARACTERS.length());
sb.append(PROJECTID_CHARACTERS.charAt(randomIndex));
}
return sb.toString();
}
private static String doubleToString(double d) { private static String doubleToString(double d) {
String ds = "" + d; String ds = "" + d;
if (ds.endsWith(".0")) { if (ds.endsWith(".0")) {
@@ -43,22 +60,30 @@ public class SwfIntelliJIdeaExporter {
return ds; return ds;
} }
public void exportIntelliJIdeaProject(SWF swf, File outFile, AbortRetryIgnoreHandler handler) throws IOException { public void exportIntelliJIdeaProject(SWF swf, File outDir, AbortRetryIgnoreHandler handler) throws IOException {
exportIntelliJIdeaProject(swf, outFile, handler, null); exportIntelliJIdeaProject(swf, outDir, handler, null);
} }
public void exportIntelliJIdeaProject(SWF swf, File outFile, AbortRetryIgnoreHandler handler, EventListener eventListener) throws IOException { public void exportIntelliJIdeaProject(SWF swf, File outDir, AbortRetryIgnoreHandler handler, EventListener eventListener) throws IOException {
if (!swf.isAS3()) { if (!swf.isAS3()) {
throw new IllegalArgumentException("SWF must be AS3"); throw new IllegalArgumentException("SWF must be AS3");
} }
String simpleName = outFile.getName(); if (!outDir.exists()) {
if (!outDir.mkdirs()) {
throw new IOException("Cannot create directory");
}
}
if (!outDir.isDirectory()) {
throw new IOException("The selected file is not a directory");
}
String simpleName = swf.getShortFileName();
if (simpleName.contains(".")) { if (simpleName.contains(".")) {
simpleName = simpleName.substring(0, simpleName.lastIndexOf(".")); simpleName = simpleName.substring(0, simpleName.lastIndexOf("."));
} }
File baseDir = outFile.getParentFile(); File ideaDir = new File(outDir, ".idea");
File ideaDir = new File(baseDir, ".idea");
String documentClass = swf.getDocumentClass(); String documentClass = swf.getDocumentClass();
if (documentClass == null) { if (documentClass == null) {
@@ -100,17 +125,113 @@ public class SwfIntelliJIdeaExporter {
+ " <orderEntry type=\"sourceFolder\" forTests=\"false\" />\n" + " <orderEntry type=\"sourceFolder\" forTests=\"false\" />\n"
+ " </component>\n" + " </component>\n"
+ "</module>"; + "</module>";
try (FileOutputStream fos = new FileOutputStream(outFile)) { try (FileOutputStream fos = new FileOutputStream(new File(outDir, simpleName + ".iml"))) {
fos.write(Utf8Helper.getBytes(imlData)); fos.write(Utf8Helper.getBytes(imlData));
} }
ideaDir.mkdir(); ideaDir.mkdir();
long created = System.currentTimeMillis();
String workspaceXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<project version=\"4\">\n"
+ " <component name=\"ChangeListManager\">\n"
+ " <list default=\"true\" id=\"" + UUID.randomUUID() +"\" name=\"Changes\" comment=\"\" />\n"
+ " <option name=\"SHOW_DIALOG\" value=\"false\" />\n"
+ " <option name=\"HIGHLIGHT_CONFLICTS\" value=\"true\" />\n"
+ " <option name=\"HIGHLIGHT_NON_ACTIVE_CHANGELIST\" value=\"false\" />\n"
+ " <option name=\"LAST_RESOLUTION\" value=\"IGNORE\" />\n"
+ " </component>\n"
+ " <component name=\"ProjectColorInfo\"><![CDATA[{\n"
+ " \"associatedIndex\": 8\n"
+ "}]]></component>\n"
+ " <component name=\"ProjectId\" id=\"" + generateProjectId() + "\" />\n"
+ " <component name=\"ProjectViewState\">\n"
+ " <option name=\"hideEmptyMiddlePackages\" value=\"true\" />\n"
+ " <option name=\"showLibraryContents\" value=\"true\" />\n"
+ " </component>\n"
+ " <component name=\"PropertiesComponent\"><![CDATA[{\n"
+ " \"keyToString\": {\n"
+ " \"Flash App." + simpleName +".executor\": \"Run\",\n"
+ " \"RunOnceActivity.ShowReadmeOnStart\": \"true\",\n"
+ " \"kotlin-language-version-configured\": \"true\",\n"
+ " \"nodejs_package_manager_path\": \"npm\",\n"
+ " \"vue.rearranger.settings.migration\": \"true\"\n"
+ " }\n"
+ "}]]></component>\n"
+ " <component name=\"RunManager\">\n"
+ " <configuration name=\"" + simpleName+ "\" type=\"FlashRunConfigurationType\">\n"
+ " <option name=\"BCName\" value=\"" + simpleName+ "\" />\n"
+ " <option name=\"IOSSimulatorDevice\" value=\"\" />\n"
+ " <option name=\"IOSSimulatorSdkPath\" value=\"\" />\n"
+ " <option name=\"adlOptions\" value=\"\" />\n"
+ " <option name=\"airProgramParameters\" value=\"\" />\n"
+ " <option name=\"appDescriptorForEmulator\" value=\"Android\" />\n"
+ " <option name=\"clearAppDataOnEachLaunch\" value=\"true\" />\n"
+ " <option name=\"debugTransport\" value=\"USB\" />\n"
+ " <option name=\"debuggerSdkRaw\" value=\"BC SDK\" />\n"
+ " <option name=\"emulator\" value=\"NexusOne\" />\n"
+ " <option name=\"emulatorAdlOptions\" value=\"\" />\n"
+ " <option name=\"fastPackaging\" value=\"true\" />\n"
+ " <option name=\"fullScreenHeight\" value=\"0\" />\n"
+ " <option name=\"fullScreenWidth\" value=\"0\" />\n"
+ " <option name=\"launchUrl\" value=\"false\" />\n"
+ " <option name=\"launcherParameters\">\n"
+ " <LauncherParameters>\n"
+ " <option name=\"browser\" value=\"a7bb68e0-33c0-4d6f-a81a-aac1fdb870c8\" />\n"
+ " <option name=\"launcherType\" value=\"OSDefault\" />\n"
+ " <option name=\"newPlayerInstance\" value=\"false\" />\n"
+ " <option name=\"playerPath\" value=\"FlashPlayerDebugger.exe\" />\n"
+ " </LauncherParameters>\n"
+ " </option>\n"
+ " <option name=\"mobileRunTarget\" value=\"Emulator\" />\n"
+ " <option name=\"moduleName\" value=\"" + simpleName + "\" />\n"
+ " <option name=\"overriddenMainClass\" value=\"\" />\n"
+ " <option name=\"overriddenOutputFileName\" value=\"\" />\n"
+ " <option name=\"overrideMainClass\" value=\"false\" />\n"
+ " <option name=\"runTrusted\" value=\"true\" />\n"
+ " <option name=\"screenDpi\" value=\"0\" />\n"
+ " <option name=\"screenHeight\" value=\"0\" />\n"
+ " <option name=\"screenWidth\" value=\"0\" />\n"
+ " <option name=\"url\" value=\"http://\" />\n"
+ " <option name=\"usbDebugPort\" value=\"7936\" />\n"
+ " <method v=\"2\">\n"
+ " <option name=\"Make\" enabled=\"true\" />\n"
+ " </method>\n"
+ " </configuration>\n"
+ " </component>\n"
+ " <component name=\"SharedIndexes\">\n"
+ " <attachedChunks>\n"
+ " <set>\n"
+ " <option value=\"bundled-jdk-9f38398b9061-39b83d9b5494-intellij.indexing.shared.core-IU-241.18034.62\" />\n"
+ " <option value=\"bundled-js-predefined-1d06a55b98c1-0b3e54e931b4-JavaScript-IU-241.18034.62\" />\n"
+ " </set>\n"
+ " </attachedChunks>\n"
+ " </component>\n"
+ " <component name=\"SpellCheckerSettings\" RuntimeDictionaries=\"0\" Folders=\"0\" CustomDictionaries=\"0\" DefaultDictionary=\"application-level\" UseSingleDictionary=\"true\" transferred=\"true\" />\n"
+ " <component name=\"TaskManager\">\n"
+ " <task active=\"true\" id=\"Default\" summary=\"Default task\">\n"
+ " <changelist id=\"" + UUID.randomUUID() +"\" name=\"Changes\" comment=\"\" />\n"
+ " <created>" + created + "</created>\n"
+ " <option name=\"number\" value=\"Default\" />\n"
+ " <option name=\"presentableId\" value=\"Default\" />\n"
+ " <updated>" + created + "</updated>\n"
+ " <workItem from=\"" + created + "\" duration=\"0\" />\n"
+ " </task>\n"
+ " <servers />\n"
+ " </component>\n"
+ " <component name=\"TypeScriptGeneratedFilesManager\">\n"
+ " <option name=\"version\" value=\"3\" />\n"
+ " </component>\n"
+ "</project>";
try (FileOutputStream fos = new FileOutputStream(new File(ideaDir, "workspace.xml"))) {
fos.write(Utf8Helper.getBytes(workspaceXml));
}
String modulesXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" String modulesXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<project version=\"4\">\n" + "<project version=\"4\">\n"
+ " <component name=\"ProjectModuleManager\">\n" + " <component name=\"ProjectModuleManager\">\n"
+ " <modules>\n" + " <modules>\n"
+ " <module fileurl=\"file://$PROJECT_DIR$/" + outFile.getName() + "\" filepath=\"$PROJECT_DIR$/" + outFile.getName() + "\" />\n" + " <module fileurl=\"file://$PROJECT_DIR$/" + simpleName + ".iml" + "\" filepath=\"$PROJECT_DIR$/" + simpleName + ".iml" + "\" />\n"
+ " </modules>\n" + " </modules>\n"
+ " </component>\n" + " </component>\n"
+ "</project>"; + "</project>";
@@ -130,7 +251,7 @@ public class SwfIntelliJIdeaExporter {
String miscXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" String miscXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<project version=\"4\">\n" + "<project version=\"4\">\n"
+ " <component name=\"ProjectRootManager\" version=\"2\" languageLevel=\"JDK_22\" project-jdk-name=\"flex\" project-jdk-type=\"Flex SDK Type (new)\">\n" + " <component name=\"ProjectRootManager\">\n"
+ " <output url=\"file://$PROJECT_DIR$/out\" />\n" + " <output url=\"file://$PROJECT_DIR$/out\" />\n"
+ " </component>\n" + " </component>\n"
+ "</project>"; + "</project>";
@@ -139,7 +260,7 @@ public class SwfIntelliJIdeaExporter {
} }
try (FileOutputStream fos = new FileOutputStream(new File(ideaDir, ".name"))) { try (FileOutputStream fos = new FileOutputStream(new File(ideaDir, ".name"))) {
fos.write(Utf8Helper.getBytes(outFile.getName())); fos.write(Utf8Helper.getBytes(simpleName + ".iml"));
} }
String gitIgnore = "# Default ignored files\n" String gitIgnore = "# Default ignored files\n"
@@ -149,13 +270,13 @@ public class SwfIntelliJIdeaExporter {
+ "/httpRequests/\n" + "/httpRequests/\n"
+ "# Datasource local storage ignored files\n" + "# Datasource local storage ignored files\n"
+ "/dataSources/\n" + "/dataSources/\n"
+ "/dataSources.local.xml"; + "/dataSources.local.xml\n";
try (FileOutputStream fos = new FileOutputStream(new File(ideaDir, ".gitignore"))) { try (FileOutputStream fos = new FileOutputStream(new File(ideaDir, ".gitignore"))) {
fos.write(Utf8Helper.getBytes(gitIgnore)); fos.write(Utf8Helper.getBytes(gitIgnore));
} }
boolean parallel = Configuration.parallelSpeedUp.get(); boolean parallel = Configuration.parallelSpeedUp.get();
ScriptExportSettings scriptExportSettings = new ScriptExportSettings(ScriptExportMode.AS, false, false, true, false, false); ScriptExportSettings scriptExportSettings = new ScriptExportSettings(ScriptExportMode.AS, false, false, true, false, false);
swf.exportActionScript(handler, new File(outFile.getParentFile(), "src").getAbsolutePath(), scriptExportSettings, parallel, eventListener); swf.exportActionScript(handler, new File(outDir, "src").getAbsolutePath(), scriptExportSettings, parallel, eventListener);
} }
} }
@@ -3429,50 +3429,18 @@ public final class MainPanel extends JPanel implements TreeSelectionListener, Se
JFileChooser fc = new JFileChooser(); JFileChooser fc = new JFileChooser();
String selDir = Configuration.lastOpenDir.get(); String selDir = Configuration.lastOpenDir.get();
fc.setCurrentDirectory(new File(selDir)); fc.setCurrentDirectory(new File(selDir));
if (!selDir.endsWith(File.separator)) { JFileChooser chooser = new JFileChooser();
selDir += File.separator; chooser.setCurrentDirectory(new File(Configuration.lastExportDir.get()));
} chooser.setDialogTitle(translate("export.project.select.directory"));
String swfShortName = swf.getShortFileName(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if ("".equals(swfShortName)) { chooser.setAcceptAllFileFilterUsed(false);
swfShortName = "untitled.swf"; if (chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
}
String fileName;
if (swfShortName.contains(".")) {
fileName = swfShortName.substring(0, swfShortName.lastIndexOf(".")) + ".iml";
} else {
fileName = swfShortName + ".iml";
}
FileFilter f = new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory() || (f.getName().toLowerCase(Locale.ENGLISH).endsWith(".iml"));
}
@Override
public String getDescription() {
return translate("filter.iml");
}
};
fc.setFileFilter(f);
fc.setAcceptAllFileFilterUsed(false);
fc.setSelectedFile(new File(selDir + fileName));
if (fc.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
return; return;
} }
final String fpath = Helper.fixDialogFile(chooser.getSelectedFile()).getAbsolutePath();
Configuration.lastOpenDir.set(Helper.fixDialogFile(fc.getSelectedFile()).getParentFile().getAbsolutePath()); Configuration.lastExportDir.set(Helper.fixDialogFile(chooser.getSelectedFile()).getAbsolutePath());
File sf = Helper.fixDialogFile(fc.getSelectedFile());
SwfIntelliJIdeaExporter exporter = new SwfIntelliJIdeaExporter(); SwfIntelliJIdeaExporter exporter = new SwfIntelliJIdeaExporter();
String path = sf.getAbsolutePath();
if (path.endsWith(".iml")) {
path = path.substring(0, path.length() - ".iml".length());
}
path += ".iml";
final String fpath = path;
long timeBefore = System.currentTimeMillis(); long timeBefore = System.currentTimeMillis();
new CancellableWorker() { new CancellableWorker() {
@@ -1292,3 +1292,5 @@ work.exporting.idea = Exporting IntelliJ IDEA project
menu.file.export.idea = Export IDEA project menu.file.export.idea = Export IDEA project
contextmenu.exportFla = Export to FLA contextmenu.exportFla = Export to FLA
export.project.select.directory = Select location of the new project directory
@@ -1268,3 +1268,5 @@ work.exporting.idea = Exportov\u00e1n\u00ed IntelliJ IDEA projektu
menu.file.export.idea = Exportovat IDEA projekt menu.file.export.idea = Exportovat IDEA projekt
contextmenu.exportFla = Exportovat do FLA contextmenu.exportFla = Exportovat do FLA
export.project.select.directory = Vyberte um\u00edst\u011bn\u00ed nov\u00e9ho adres\u00e1\u0159e projektu