/** * Recursively deletes a directory or file. * * @param directory the directory, that must exist and be a valid directory * @throws IOException failed to delete the file / directory */ public static void deleteDirectoryContents(@NonNull final File directory) throws IOException { Preconditions.checkArgument(directory.isDirectory(), "!directory.isDirectory"); File[] files = directory.listFiles(); Preconditions.checkNotNull(files); for (File file : files) { deletePath(file); } }
/** * Deletes the cache directory and its contents. * * <p>If the cache is being used by another thread of any process in the case of {@code * INTER_PROCESS} locking scope, or by another thread of the current process in the case of * {@code SINGLE_PROCESS} locking scope, then this method will block until that operation * completes. */ public void delete() throws IOException { try { doLocked( mCacheDirectory, LockingType.EXCLUSIVE, () -> { FileUtils.deletePath(mCacheDirectory); return null; }); } catch (ExecutionException exception) { // The deletion action above does not throw any checked exceptions other than // IOException. if (exception.getCause() instanceof IOException) { throw new IOException(exception); } else { throw new RuntimeException(exception); } } }
/** * Makes sure {@code path} is an empty directory. If {@code path} is a directory, its contents * are removed recursively, leaving an empty directory. If {@code path} is not a directory, * it is removed and a directory created with the given path. If {@code path} does not * exist, a directory is created with the given path. * * @param path the path, that may exist or not and may be a file or directory * @throws IOException failed to delete directory contents, failed to delete {@code path} or * failed to create a directory at {@code path} */ public static void cleanOutputDir(@NonNull File path) throws IOException { if (!path.isDirectory()) { if (path.exists()) { deletePath(path); } if (!path.mkdirs()) { throw new IOException(String.format("Could not create empty folder %s", path)); } return; } deleteDirectoryContents(path); }
/** * Incremental task will only execute when any of the generated files (in particular R.java) * has changed. * * @param inputs the incremental changes as provided by Gradle. * @throws IOException if something went wrong cleaning up the javac compilation output. */ @TaskAction protected void execute(IncrementalTaskInputs inputs) throws IOException { getLogger().debug("Removing old bits to force javac non incremental mode."); File outputFile = new File(generatedOutputDir, "tag.txt"); Files.createParentDirs(outputFile); Files.write("incremental task execution", outputFile, Charsets.UTF_8); // since we execute, force the recompilation of all the user's classes. try { FileUtils.deletePath(javaOutputDir); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Copies a file or a directory's contents to another file or directory, which can have a * different name. The target file/directory is replaced if it already exists. * * <p>The source file/directory must exist and must not reside in, contain, or be identical to * the target file/directory. */ private static void copyFileOrDirectory(@NonNull File from, @NonNull File to) throws IOException { Preconditions.checkArgument( from.exists(), "Source path " + from.getAbsolutePath() + " does not exists."); Preconditions.checkArgument(!FileUtils.isFileInDirectory(from, to)); Preconditions.checkArgument(!FileUtils.isFileInDirectory(to, from)); Preconditions.checkArgument(!from.getCanonicalFile().equals(to.getCanonicalFile())); if (from.isFile()) { Files.createParentDirs(to); FileUtils.copyFile(from, to); } else if (from.isDirectory()) { FileUtils.deletePath(to); FileUtils.copyDirectory(from, to); } }
FileUtils.deletePath(to); Files.createParentDirs(to); if (multiDex) {
FileUtils.deletePath(outputFile); Files.createParentDirs(outputFile); try { FileUtils.deletePath(outputFile); Files.createParentDirs(outputFile);
FileUtils.deletePath(perStreamDexFolder); FileUtils.deletePath(file); return null; });
diagnostic("removing stale contents from '%s'", expectedJson.getParentFile()); FileUtils.deletePath(expectedJson.getParentFile());
FileUtils.deletePath(cacheEntryDir);
FileUtils.deletePath(scope.getSourceFoldersJavaResDestinationDir()); } catch (IOException e) { throw new RuntimeException("Cannot delete merged source resource folder", e);