Skip to content

Commit

Permalink
fix: make param mappings properly optional (#28)
Browse files Browse the repository at this point in the history
also makes the report cli param optional
  • Loading branch information
MiniDigger authored Mar 22, 2024
1 parent 29c9add commit 175ea4c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static final class ReportOptions {
hidden = true)
private @Nullable Path reportsDir;

@CommandLine.ArgGroup(multiplicity = "1")
@CommandLine.ArgGroup(multiplicity = "1", exclusive = false)
private SelectedReports selectedReports;

static final class SelectedReports {
Expand Down
62 changes: 36 additions & 26 deletions src/main/java/io/papermc/codebook/pages/InspectJarPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.cadixdev.lorenz.MappingSet;
import org.cadixdev.lorenz.model.ClassMapping;
import org.cadixdev.lorenz.model.MethodMapping;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.parchmentmc.feather.io.gson.MDCGsonAdapterFactory;
import org.parchmentmc.feather.io.gson.SimpleVersionAdapter;
import org.parchmentmc.feather.mapping.MappingDataContainer;
Expand All @@ -63,36 +64,21 @@ public final class InspectJarPage extends CodeBookPage {

private final Path inputJar;
private final List<Path> classpathJars;
private final Path paramMappings;
private final @Nullable Path paramMappings;

@Inject
public InspectJarPage(
@InputJar final Path inputJar,
@ClasspathJars final List<Path> classpathJars,
@ParamMappings final Path paramMappings) {
@ParamMappings @Nullable final Path paramMappings) {
this.inputJar = inputJar;
this.classpathJars = classpathJars;
this.paramMappings = paramMappings;
}

@Override
public void exec() {
final Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new MDCGsonAdapterFactory())
.registerTypeAdapter(SimpleVersion.class, new SimpleVersionAdapter())
.create();

final MappingDataContainer mappings;
try (final FileSystem fs = FileSystems.newFileSystem(this.paramMappings)) {
final Path jsonFile = fs.getPath("/parchment.json");
try (final BufferedReader reader = Files.newBufferedReader(jsonFile)) {
mappings = gson.fromJson(reader, VersionedMappingDataContainer.class);
}
} catch (final IOException e) {
throw new UnexpectedException("Failed to read param mappings file", e);
}

final MappingSet lorenzMappings = this.toLorenz(mappings);
final MappingSet lorenzMappings = this.loadMappings();

final HypoContext ctx;

Expand All @@ -119,15 +105,39 @@ public void exec() {
throw new UnexpectedException("Failed to hydrate data model", e);
}

// Fill in any missing mapping information
final MappingSet completedMappings = ChangeChain.create()
.addLink(
CopyMappingsDown.createWithoutOverwrite(),
CopyLambdaParametersDown.createWithoutOverwrite(),
CopyRecordParameters.create())
.applyChain(lorenzMappings, MappingsCompletionManager.create(ctx));
if (this.paramMappings != null) {
// Fill in any missing mapping information
final MappingSet completedMappings = ChangeChain.create()
.addLink(
CopyMappingsDown.createWithoutOverwrite(),
CopyLambdaParametersDown.createWithoutOverwrite(),
CopyRecordParameters.create())
.applyChain(lorenzMappings, MappingsCompletionManager.create(ctx));

this.bind(ParamMappings.KEY).to(completedMappings);
} else {
this.bind(ParamMappings.KEY).to(null);
}
}

this.bind(ParamMappings.KEY).to(completedMappings);
private MappingSet loadMappings() {
if (this.paramMappings == null) {
return MappingSet.create();
}

final Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new MDCGsonAdapterFactory())
.registerTypeAdapter(SimpleVersion.class, new SimpleVersionAdapter())
.create();

try (final FileSystem fs = FileSystems.newFileSystem(this.paramMappings)) {
final Path jsonFile = fs.getPath("/parchment.json");
try (final BufferedReader reader = Files.newBufferedReader(jsonFile)) {
return this.toLorenz(gson.fromJson(reader, VersionedMappingDataContainer.class));
}
} catch (final IOException e) {
throw new UnexpectedException("Failed to read param mappings file", e);
}
}

private MappingSet toLorenz(final MappingDataContainer container) {
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/io/papermc/codebook/pages/RemapLvtPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.cadixdev.lorenz.MappingSet;
import org.checkerframework.checker.nullness.qual.Nullable;

public final class RemapLvtPage extends CodeBookPage {

private final HypoContext context;
private final MappingSet paramMappings;
private final @Nullable MappingSet paramMappings;
private final Reports reports;

@Inject
public RemapLvtPage(
@Hypo final HypoContext hypoContext,
@ParamMappings final MappingSet paramMappings,
@ParamMappings @Nullable final MappingSet paramMappings,
@Report final Reports reports) {
this.context = hypoContext;
this.paramMappings = paramMappings;
Expand All @@ -54,6 +55,10 @@ public RemapLvtPage(

@Override
public void exec() {
if (this.paramMappings == null) {
return;
}

final LvtNamer lvtNamer;
try {
lvtNamer = new LvtNamer(this.context, this.paramMappings, this.reports);
Expand Down

0 comments on commit 175ea4c

Please sign in to comment.