Skip to content

Commit

Permalink
Allow specifying extension on file mappings sources
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Nov 4, 2024
1 parent ba9f0a4 commit 31322ee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import net.fabricmc.mappingio.tree.VisitableMappingTree;
import org.jspecify.annotations.Nullable;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down Expand Up @@ -82,7 +83,7 @@ static MappingsSourceImpl of(MappingsSource source, WorkItem workItem, Context c
yield new ChainedFiles(counter.getAndIncrement(), new TaskInput.RecursiveFileListInput("chainedFiles" + counter.getAndIncrement(), filesParts));
}
case MappingsSource.File file ->
new FileSource(counter.getAndIncrement(), TaskInput.file("fileSource" + counter.getAndIncrement(), file.input, workItem, context, PathSensitivity.NONE));
new FileSource(counter.getAndIncrement(), TaskInput.file("fileSource" + counter.getAndIncrement(), file.input, workItem, context, PathSensitivity.NONE), file.extension == null ? null : TaskInput.value("fileSource"+counter.get()+"extension", file.extension, workItem));
case MappingsSource.Merged merged ->
new MergedSource(counter.getAndIncrement(), merged.sources.stream().map(s -> of(s, workItem, context, counter)).toList());
case MappingsSource.MergedFiles mergedFiles -> {
Expand Down Expand Up @@ -261,16 +262,30 @@ public List<TaskInput> inputs() {
final class FileSource implements MappingsSourceImpl {
private final TaskInput.ValueInput label;
private final TaskInput.HasFileInput input;
private final TaskInput.@Nullable ValueInput extension;

public FileSource(int andIncrement, TaskInput.HasFileInput input) {
public FileSource(int andIncrement, TaskInput.HasFileInput input, TaskInput.@Nullable ValueInput extension) {
this.label = new TaskInput.ValueInput("fileLabel" + andIncrement, new Value.StringValue("file" + andIncrement));
this.input = input;
this.extension = extension;
}

@Override
public MappingTree makeMappings(Context context) {
try {
var path = input.path(context);
if (extension != null) {
var extensionObj = extension.value().value();
if (extensionObj instanceof String extensionString) {
var tempFile = Files.createTempFile("crochet-extracted", "." + extensionString);
try (var input = Files.newInputStream(path)) {
Files.copy(input, tempFile, StandardCopyOption.REPLACE_EXISTING);
}
path = tempFile;
} else {
throw new IllegalArgumentException("Extension value is not a string");
}
}
return loadMappings(path);
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand All @@ -279,7 +294,11 @@ public MappingTree makeMappings(Context context) {

@Override
public List<TaskInput> inputs() {
return List.of(label, input);
if (extension == null) {
return List.of(label, input);
} else {
return List.of(label, input, extension);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import org.jspecify.annotations.Nullable;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -73,6 +74,7 @@ public MappingsSource read(JsonReader in) {
@JsonAdapter(Adapter.class)
final class File implements MappingsSource {
public Input input;
public @Nullable InputValue extension;

public File(Input input) {
this.input = input;
Expand All @@ -87,7 +89,12 @@ private static final class Specialized extends FieldAdapter<File> {
@Override
public Function<Values, File> build(Builder<File> builder) {
var input = builder.field("input", source -> source.input, Input.class);
return values -> new File(values.get(input));
var extension = builder.field("extension", source -> source.extension, InputValue.class);
return values -> {
var source = new File(values.get(input));
source.extension = values.get(extension);
return source;
};
}
}
}
Expand Down

0 comments on commit 31322ee

Please sign in to comment.