From d66f1a5b6c05f759ed8120b35b9006a5538fca19 Mon Sep 17 00:00:00 2001 From: NebelNidas <48808497+NebelNidas@users.noreply.github.com> Date: Sat, 31 Aug 2024 16:19:53 +0200 Subject: [PATCH] Fix migration maps not being readable by IntelliJ IDEA (#112) --- .../intellij/MigrationMapConstants.java | 28 ++++++++ .../intellij/MigrationMapFileReader.java | 8 ++- .../intellij/MigrationMapFileWriter.java | 68 +++++++++++++++---- .../format/tiny/Tiny2FileWriter.java | 7 +- .../net/fabricmc/mappingio/TestHelper.java | 3 + .../fabricmc/mappingio/tree/MetadataTest.java | 1 + .../read/repeated-elements/migration-map.xml | 15 ++-- .../read/repeated-elements/tinyV2.tiny | 1 + .../read/valid-with-holes/migration-map.xml | 14 ++-- .../resources/read/valid/migration-map.xml | 9 +-- src/test/resources/read/valid/tinyV2.tiny | 1 + 11 files changed, 123 insertions(+), 32 deletions(-) create mode 100644 src/main/java/net/fabricmc/mappingio/format/intellij/MigrationMapConstants.java diff --git a/src/main/java/net/fabricmc/mappingio/format/intellij/MigrationMapConstants.java b/src/main/java/net/fabricmc/mappingio/format/intellij/MigrationMapConstants.java new file mode 100644 index 00000000..b9da53eb --- /dev/null +++ b/src/main/java/net/fabricmc/mappingio/format/intellij/MigrationMapConstants.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.mappingio.format.intellij; + +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public final class MigrationMapConstants { + private MigrationMapConstants() { + } + + public static final String ORDER_KEY = "migrationmap:order"; + public static final String MISSING_NAME = "Unnamed migration map"; +} diff --git a/src/main/java/net/fabricmc/mappingio/format/intellij/MigrationMapFileReader.java b/src/main/java/net/fabricmc/mappingio/format/intellij/MigrationMapFileReader.java index 4968e945..1148c177 100644 --- a/src/main/java/net/fabricmc/mappingio/format/intellij/MigrationMapFileReader.java +++ b/src/main/java/net/fabricmc/mappingio/format/intellij/MigrationMapFileReader.java @@ -108,9 +108,15 @@ private static void read0(BufferedReader reader, String sourceNs, String targetN switch (name) { case "name": + case "order": case "description": if (visitHeader) { - // TODO: visit as metadata once https://github.com/FabricMC/mapping-io/pull/29 is merged + String value = xmlReader.getAttributeValue(null, "value"); + + if (name.equals("order")) name = MigrationMapConstants.ORDER_KEY; + if (name.equals("name") && value.equals(MigrationMapConstants.MISSING_NAME)) break; + + visitor.visitMetadata(name, value); } break; diff --git a/src/main/java/net/fabricmc/mappingio/format/intellij/MigrationMapFileWriter.java b/src/main/java/net/fabricmc/mappingio/format/intellij/MigrationMapFileWriter.java index 98d91c9d..45ed47ae 100644 --- a/src/main/java/net/fabricmc/mappingio/format/intellij/MigrationMapFileWriter.java +++ b/src/main/java/net/fabricmc/mappingio/format/intellij/MigrationMapFileWriter.java @@ -46,7 +46,21 @@ public MigrationMapFileWriter(Writer writer) { public void close() throws IOException { try { if (xmlWriter != null) { + if (!wroteName) { + xmlWriter.writeCharacters("\n\t"); + xmlWriter.writeEmptyElement("name"); + xmlWriter.writeAttribute("value", MigrationMapConstants.MISSING_NAME); + } + + if (!wroteOrder) { + xmlWriter.writeCharacters("\n\t"); + xmlWriter.writeEmptyElement("order"); + xmlWriter.writeAttribute("value", "0"); + } + + xmlWriter.writeCharacters("\n"); xmlWriter.writeEndDocument(); + xmlWriter.writeCharacters("\n"); xmlWriter.close(); } } catch (XMLStreamException e) { @@ -61,13 +75,46 @@ public Set getFlags() { return flags; } + @Override + public boolean visitHeader() throws IOException { + assert xmlWriter == null; + + try { + xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer); + + xmlWriter.writeStartDocument("UTF-8", "1.0"); + xmlWriter.writeCharacters("\n"); + xmlWriter.writeStartElement("migrationMap"); + } catch (FactoryConfigurationError | XMLStreamException e) { + throw new IOException(e); + } + + return true; + } + @Override public void visitNamespaces(String srcNamespace, List dstNamespaces) throws IOException { } @Override public void visitMetadata(String key, @Nullable String value) throws IOException { - // TODO: Support once https://github.com/FabricMC/mapping-io/pull/29 is merged + try { + switch (key) { + case "name": + wroteName = true; + break; + case MigrationMapConstants.ORDER_KEY: + wroteOrder = true; + key = "order"; + break; + } + + xmlWriter.writeCharacters("\n\t"); + xmlWriter.writeEmptyElement(key); + xmlWriter.writeAttribute("value", value); + } catch (XMLStreamException e) { + throw new IOException(e); + } } @Override @@ -110,23 +157,16 @@ public boolean visitElementContent(MappedElementKind targetKind) throws IOExcept if (dstName == null) return false; try { - if (xmlWriter == null) { - xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer); - - xmlWriter.writeStartDocument("UTF-8", "1.0"); - xmlWriter.writeStartElement("migrationMap"); - } - - xmlWriter.writeStartElement("entry"); + xmlWriter.writeCharacters("\n\t"); + xmlWriter.writeEmptyElement("entry"); xmlWriter.writeAttribute("oldName", srcName.replace('/', '.')); xmlWriter.writeAttribute("newName", dstName.replace('/', '.')); xmlWriter.writeAttribute("type", "class"); - xmlWriter.writeEndElement(); - - return false; - } catch (XMLStreamException | FactoryConfigurationError e) { + } catch (XMLStreamException e) { throw new IOException(e); } + + return false; } @Override @@ -138,6 +178,8 @@ public void visitComment(MappedElementKind targetKind, String comment) throws IO private final Writer writer; private XMLStreamWriter xmlWriter; + private boolean wroteName; + private boolean wroteOrder; private String srcName; private String dstName; } diff --git a/src/main/java/net/fabricmc/mappingio/format/tiny/Tiny2FileWriter.java b/src/main/java/net/fabricmc/mappingio/format/tiny/Tiny2FileWriter.java index 6b049925..2931845c 100644 --- a/src/main/java/net/fabricmc/mappingio/format/tiny/Tiny2FileWriter.java +++ b/src/main/java/net/fabricmc/mappingio/format/tiny/Tiny2FileWriter.java @@ -29,6 +29,7 @@ import net.fabricmc.mappingio.MappingFlag; import net.fabricmc.mappingio.MappingWriter; import net.fabricmc.mappingio.format.MappingFormat; +import net.fabricmc.mappingio.format.intellij.MigrationMapConstants; /** * {@linkplain MappingFormat#TINY_2_FILE Tiny v2 file} writer. @@ -66,9 +67,13 @@ public void visitNamespaces(String srcNamespace, List dstNamespaces) thr @Override public void visitMetadata(String key, @Nullable String value) throws IOException { - if (key.equals(Tiny2Util.escapedNamesProperty)) { + switch (key) { + case Tiny2Util.escapedNamesProperty: escapeNames = true; wroteEscapedNamesProperty = true; + break; + case MigrationMapConstants.ORDER_KEY: + return; } writeTab(); diff --git a/src/test/java/net/fabricmc/mappingio/TestHelper.java b/src/test/java/net/fabricmc/mappingio/TestHelper.java index 8e6b1432..d6f1a5dd 100644 --- a/src/test/java/net/fabricmc/mappingio/TestHelper.java +++ b/src/test/java/net/fabricmc/mappingio/TestHelper.java @@ -29,6 +29,7 @@ import org.jetbrains.annotations.Nullable; import net.fabricmc.mappingio.format.MappingFormat; +import net.fabricmc.mappingio.format.intellij.MigrationMapConstants; import net.fabricmc.mappingio.tree.MappingTreeView; import net.fabricmc.mappingio.tree.MemoryMappingTree; @@ -147,6 +148,8 @@ public static Path writeToDir(MappingTreeView tree, Path dir, MappingFormat form public static MemoryMappingTree createTestTree() { MemoryMappingTree tree = new MemoryMappingTree(); tree.visitNamespaces(MappingUtil.NS_SOURCE_FALLBACK, Arrays.asList(MappingUtil.NS_TARGET_FALLBACK, MappingUtil.NS_TARGET_FALLBACK + "2")); + tree.visitMetadata("name", "valid"); + tree.visitMetadata(MigrationMapConstants.ORDER_KEY, "0"); int[] dstNs = new int[] { 0, 1 }; nameGen.reset(); diff --git a/src/test/java/net/fabricmc/mappingio/tree/MetadataTest.java b/src/test/java/net/fabricmc/mappingio/tree/MetadataTest.java index 1e29d5ce..cacab9bb 100644 --- a/src/test/java/net/fabricmc/mappingio/tree/MetadataTest.java +++ b/src/test/java/net/fabricmc/mappingio/tree/MetadataTest.java @@ -43,6 +43,7 @@ public class MetadataTest { @BeforeAll public static void setup() throws Exception { tree = TestHelper.createTestTree(); + tree.getMetadata().clear(); for (int i = 0; i < 40; i++) { String key = "key" + random.nextInt(3); diff --git a/src/test/resources/read/repeated-elements/migration-map.xml b/src/test/resources/read/repeated-elements/migration-map.xml index dc689ab6..4a49c0ce 100644 --- a/src/test/resources/read/repeated-elements/migration-map.xml +++ b/src/test/resources/read/repeated-elements/migration-map.xml @@ -1,10 +1,11 @@ - - - - - - + + + + + + + + - diff --git a/src/test/resources/read/repeated-elements/tinyV2.tiny b/src/test/resources/read/repeated-elements/tinyV2.tiny index a8636c70..184c44e7 100644 --- a/src/test/resources/read/repeated-elements/tinyV2.tiny +++ b/src/test/resources/read/repeated-elements/tinyV2.tiny @@ -1,4 +1,5 @@ tiny 2 0 source target target2 + name repeated-elements c class_1 class1Ns0Rename0 class1Ns1Rename0 c class_1 class1Ns0Rename class1Ns1Rename f I field_1 field1Ns0Rename0 field1Ns1Rename0 diff --git a/src/test/resources/read/valid-with-holes/migration-map.xml b/src/test/resources/read/valid-with-holes/migration-map.xml index dc805b2f..7beec0cf 100644 --- a/src/test/resources/read/valid-with-holes/migration-map.xml +++ b/src/test/resources/read/valid-with-holes/migration-map.xml @@ -1,9 +1,11 @@ - - - - - - + + + + + + + + diff --git a/src/test/resources/read/valid/migration-map.xml b/src/test/resources/read/valid/migration-map.xml index 161a8c9d..4266c8a6 100644 --- a/src/test/resources/read/valid/migration-map.xml +++ b/src/test/resources/read/valid/migration-map.xml @@ -1,7 +1,8 @@ - - - + + + + + - diff --git a/src/test/resources/read/valid/tinyV2.tiny b/src/test/resources/read/valid/tinyV2.tiny index c681c7d2..347970de 100644 --- a/src/test/resources/read/valid/tinyV2.tiny +++ b/src/test/resources/read/valid/tinyV2.tiny @@ -1,4 +1,5 @@ tiny 2 0 source target target2 + name valid c class_1 class1Ns0Rename class1Ns1Rename f I field_1 field1Ns0Rename field1Ns1Rename m ()I method_1 method1Ns0Rename method1Ns1Rename