Skip to content

Commit

Permalink
Fix migration maps not being readable by IntelliJ IDEA (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
NebelNidas authored Aug 31, 2024
1 parent 1f40679 commit d66f1a5
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -61,13 +75,46 @@ public Set<MappingFlag> 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<String> 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
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -66,9 +67,13 @@ public void visitNamespaces(String srcNamespace, List<String> 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();
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/net/fabricmc/mappingio/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions src/test/resources/read/repeated-elements/migration-map.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<migrationMap>
<entry oldName="class_1" newName="class1Ns0Rename0" type="class" />
<entry oldName="class_1" newName="class1Ns0Rename" type="class" />
<entry oldName="class_1$class_2" newName="class1Ns0Rename$class2Ns0Rename0" type="class" />
<entry oldName="class_1$class_2" newName="class1Ns0Rename$class2Ns0Rename" type="class" />
<entry oldName="class_3" newName="class3Ns0Rename0" type="class" />
<entry oldName="class_3" newName="class3Ns0Rename" type="class" />
<name value="repeated-elements"/>
<order value="0"/>
<entry oldName="class_1" newName="class1Ns0Rename0" type="class"/>
<entry oldName="class_1" newName="class1Ns0Rename" type="class"/>
<entry oldName="class_1$class_2" newName="class1Ns0Rename$class2Ns0Rename0" type="class"/>
<entry oldName="class_1$class_2" newName="class1Ns0Rename$class2Ns0Rename" type="class"/>
<entry oldName="class_3" newName="class3Ns0Rename0" type="class"/>
<entry oldName="class_3" newName="class3Ns0Rename" type="class"/>
</migrationMap>

1 change: 1 addition & 0 deletions src/test/resources/read/repeated-elements/tinyV2.tiny
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 8 additions & 6 deletions src/test/resources/read/valid-with-holes/migration-map.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<migrationMap>
<entry oldName="class_2" newName="class2Ns0Rename" type="class" />
<entry oldName="class_5" newName="class5Ns0Rename" type="class" />
<entry oldName="class_7$class_8" newName="class_7$class8Ns0Rename" type="class" />
<entry oldName="class_13$class_14" newName="class_13$class14Ns0Rename" type="class" />
<entry oldName="class_17$class_18$class_19" newName="class_17$class_18$class19Ns0Rename" type="class" />
<entry oldName="class_26$class_27$class_28" newName="class_26$class_27$class28Ns0Rename" type="class" />
<entry oldName="class_2" newName="class2Ns0Rename" type="class"/>
<entry oldName="class_5" newName="class5Ns0Rename" type="class"/>
<entry oldName="class_7$class_8" newName="class_7$class8Ns0Rename" type="class"/>
<entry oldName="class_13$class_14" newName="class_13$class14Ns0Rename" type="class"/>
<entry oldName="class_17$class_18$class_19" newName="class_17$class_18$class19Ns0Rename" type="class"/>
<entry oldName="class_26$class_27$class_28" newName="class_26$class_27$class28Ns0Rename" type="class"/>
<name value="Unnamed migration map"/>
<order value="0"/>
</migrationMap>
9 changes: 5 additions & 4 deletions src/test/resources/read/valid/migration-map.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<migrationMap>
<entry oldName="class_1" newName="class1Ns0Rename" type="class" />
<entry oldName="class_1$class_2" newName="class1Ns0Rename$class2Ns0Rename" type="class" />
<entry oldName="class_3" newName="class3Ns0Rename" type="class" />
<name value="valid"/>
<order value="0"/>
<entry oldName="class_1" newName="class1Ns0Rename" type="class"/>
<entry oldName="class_1$class_2" newName="class1Ns0Rename$class2Ns0Rename" type="class"/>
<entry oldName="class_3" newName="class3Ns0Rename" type="class"/>
</migrationMap>

1 change: 1 addition & 0 deletions src/test/resources/read/valid/tinyV2.tiny
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit d66f1a5

Please sign in to comment.