Search in sources :

Example 1 with PatchFileDestination

use of com.google.errorprone.apply.PatchFileDestination in project error-prone by google.

the class RefactoringCollection method refactor.

static RefactoringCollection refactor(PatchingOptions patchingOptions) {
    Path rootPath = buildRootPath();
    FileDestination fileDestination;
    Function<URI, RefactoringResult> postProcess;
    if (patchingOptions.inPlace()) {
        fileDestination = new FsFileDestination(rootPath);
        postProcess = uri -> RefactoringResult.create(String.format("Refactoring changes were successfully applied to %s," + " please check the refactored code and recompile.", uri), RefactoringResultType.CHANGED);
    } else {
        Path baseDir = rootPath.resolve(patchingOptions.baseDirectory());
        Path patchFilePath = baseDir.resolve("error-prone.patch");
        PatchFileDestination patchFileDestination = new PatchFileDestination(baseDir, rootPath);
        postProcess = new Function<URI, RefactoringResult>() {

            private final AtomicBoolean first = new AtomicBoolean(true);

            @Override
            public RefactoringResult apply(URI uri) {
                try {
                    writePatchFile(first, uri, patchFileDestination, patchFilePath);
                    return RefactoringResult.create("Changes were written to " + patchFilePath + ". Please inspect the file and apply with: " + "patch -p0 -u -i error-prone.patch", RefactoringResultType.CHANGED);
                } catch (IOException e) {
                    throw new RuntimeException("Failed to emit patch file!", e);
                }
            }
        };
        fileDestination = patchFileDestination;
    }
    ImportOrganizer importOrganizer = patchingOptions.importOrganizer();
    return new RefactoringCollection(rootPath, fileDestination, postProcess, importOrganizer);
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) URI(java.net.URI) ImportOrganizer(com.google.errorprone.apply.ImportOrganizer) FsFileDestination(com.google.errorprone.apply.FsFileDestination) FileDestination(com.google.errorprone.apply.FileDestination) PatchFileDestination(com.google.errorprone.apply.PatchFileDestination) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FsFileDestination(com.google.errorprone.apply.FsFileDestination) PatchFileDestination(com.google.errorprone.apply.PatchFileDestination)

Example 2 with PatchFileDestination

use of com.google.errorprone.apply.PatchFileDestination in project error-prone by google.

the class RefactoringCollection method refactorToPatchFile.

static RefactoringCollection refactorToPatchFile(String baseDirectory) {
    Path rootPath = buildRootPath();
    Path baseDir = rootPath.resolve(baseDirectory);
    Path patchFilePath = baseDir.resolve("error-prone.patch");
    PatchFileDestination patchFileDestination = new PatchFileDestination(baseDir, rootPath);
    Callable<RefactoringResult> postProcess = () -> {
        try {
            writePatchFile(patchFileDestination, patchFilePath);
            return RefactoringResult.create("Changes were written to " + patchFilePath + ". Please inspect the file and apply with: patch -p0 -u -i error-prone.patch", RefactoringResultType.CHANGED);
        } catch (IOException e) {
            throw new RuntimeException("Failed to emit patch file!", e);
        }
    };
    return new RefactoringCollection(rootPath, patchFileDestination, postProcess);
}
Also used : Path(java.nio.file.Path) PatchFileDestination(com.google.errorprone.apply.PatchFileDestination) IOException(java.io.IOException)

Aggregations

PatchFileDestination (com.google.errorprone.apply.PatchFileDestination)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 FileDestination (com.google.errorprone.apply.FileDestination)1 FsFileDestination (com.google.errorprone.apply.FsFileDestination)1 ImportOrganizer (com.google.errorprone.apply.ImportOrganizer)1 URI (java.net.URI)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1