forked from QuiltMC/quilt-mappings
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add generateDiff task (not yet used in generate-diff.yml)
- Loading branch information
1 parent
8d6fff4
commit 732d4d0
Showing
2 changed files
with
143 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
buildSrc/src/main/java/quilt/internal/tasks/diff/DiffDirectoriesTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package quilt.internal.tasks.diff; | ||
|
||
import org.gradle.api.GradleException; | ||
import org.gradle.api.file.Directory; | ||
import org.gradle.api.file.DirectoryProperty; | ||
import org.gradle.api.file.RegularFileProperty; | ||
import org.gradle.api.provider.ListProperty; | ||
import org.gradle.api.specs.Specs; | ||
import org.gradle.api.tasks.Exec; | ||
import org.gradle.api.tasks.Input; | ||
import org.gradle.api.tasks.InputDirectory; | ||
import org.gradle.api.tasks.Optional; | ||
import org.gradle.api.tasks.OutputFile; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.gradle.api.tasks.options.Option; | ||
import quilt.internal.Constants.Groups; | ||
import quilt.internal.tasks.MappingsTask; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static quilt.internal.util.ProviderUtil.toOptional; | ||
|
||
public abstract class DiffDirectoriesTask extends Exec implements MappingsTask { | ||
public static final String GENERATE_DIFF_TASK_NAME = "generateDiff"; | ||
|
||
public static final String DIFF_COMMAND = "diff"; | ||
|
||
private static final String EXIT_VALUE_1_ERROR = | ||
"Process 'command '" + DIFF_COMMAND + "'' finished with non-zero exit value 1"; | ||
|
||
@Option( | ||
option = "args", | ||
description = "Additional args passed to the " + DIFF_COMMAND + " command." | ||
) | ||
@Optional | ||
@Input | ||
public abstract ListProperty<String> getAdditionalArgs(); | ||
|
||
@Option( | ||
option = "first", | ||
description = "The first file passed to the " + DIFF_COMMAND + " command." | ||
) | ||
@InputDirectory | ||
public abstract DirectoryProperty getFirst(); | ||
|
||
@Option( | ||
option = "second", | ||
description = "The second file passed to the " + DIFF_COMMAND + " command." | ||
) | ||
@InputDirectory | ||
public abstract DirectoryProperty getSecond(); | ||
|
||
@Option( | ||
option = "dest", | ||
description = "The location to save the " + DIFF_COMMAND + " command output to." | ||
) | ||
@OutputFile | ||
public abstract RegularFileProperty getDest(); | ||
|
||
public DiffDirectoriesTask() { | ||
this.setGroup(Groups.DIFF); | ||
|
||
this.setExecutable(DIFF_COMMAND); | ||
|
||
this.getOutputs().cacheIf( | ||
"Re-enable caching because Exec has @DisableCachingByDefault", | ||
Specs.satisfyAll() | ||
); | ||
|
||
this.getArgumentProviders().add(() -> { | ||
// require neither directory is empty so the diff isn't just the full contents of one of them | ||
final Directory first = this.getFirst().get(); | ||
if (first.getAsFileTree().isEmpty()) { | ||
throw new GradleException("first directory is empty"); | ||
} | ||
|
||
final Directory second = this.getSecond().get(); | ||
if (second.getAsFileTree().isEmpty()) { | ||
throw new GradleException("second directory is empty"); | ||
} | ||
|
||
final List<String> args = new ArrayList<>(); | ||
|
||
toOptional(this.getAdditionalArgs()).ifPresent(args::addAll); | ||
|
||
args.add(first.getAsFile().getAbsolutePath()); | ||
args.add(second.getAsFile().getAbsolutePath()); | ||
|
||
return args; | ||
}); | ||
} | ||
|
||
@Override | ||
@TaskAction | ||
public void exec() { | ||
try { | ||
final File dest = this.getDest().get().getAsFile(); | ||
|
||
dest.getParentFile().mkdirs(); | ||
|
||
dest.createNewFile(); | ||
|
||
this.setStandardOutput(new FileOutputStream(dest.getAbsolutePath())); | ||
} catch (IOException e) { | ||
throw new GradleException("Failed to access destination file", e); | ||
} | ||
|
||
try { | ||
super.exec(); | ||
} catch (GradleException e) { | ||
// ignore exit value 1 which just means there was a difference between the inputs | ||
if (!e.getMessage().equals(EXIT_VALUE_1_ERROR)) { | ||
throw new GradleException("Error executing " + DIFF_COMMAND, e); | ||
} | ||
} | ||
} | ||
} |
732d4d0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No difference between head and target.