Class FileMisc

java.lang.Object
com.diffplug.gradle.FileMisc

public class FileMisc extends Object
Miscellaneous utilties for copying files around.
  • Field Details

    • PROTOCOL

      public static final String PROTOCOL
      The `file://` protocol.
  • Constructor Details

    • FileMisc

      public FileMisc()
  • Method Details

    • download

      public static void download(String url, File dst) throws IOException
      Downloads the url to the destination file (with support for redirects).
      Throws:
      IOException
    • list

      public static List<File> list(File d)
      Lists the children of the given file in a safe way (File.listFiles() can return null).
    • mkdirs

      public static void mkdirs(File d)
      Calls File.mkdirs() and throws an exception if it fails.
    • forceDelete

      public static void forceDelete(File f)
      Calls FileUtils.forceDelete(File) and throws an exception if it fails. If the file doesn't exist at all, that's fine.
    • dirExists

      public static boolean dirExists(File dir)
      Returns true if the given directory exists, and waits up to 500ms for the directory to exist.
    • toUnixNewline

      public static String toUnixNewline(String input)
      Enforces unix newlines on the given string.
    • quote

      public static String quote(String input)
      Quotes the given input string iff it contains whitespace.
    • quote

      public static String quote(File input)
      Quotes the absolute path of the given file iff it contains whitespace.
    • noQuote

      public static String noQuote(String input)
      Throws an exception if the given input property contains whitespace.
    • writeToken

      public static void writeToken(File dir, String name, String value) throws IOException
      Writes a file with the given name, to the given directory, containing the given value.
      Throws:
      IOException
    • writeTokenFile

      public static void writeTokenFile(File tokenFile, String value) throws IOException
      Writes a token file containing the given value.
      Throws:
      IOException
    • readToken

      public static Optional<String> readToken(File dir, String name) throws IOException
      Returns the contents of a file with the given name, if it exists.
      Throws:
      IOException
    • writeToken

      public static void writeToken(File dir, String name) throws IOException
      Writes an empty file with the given name in the given directory.
      Throws:
      IOException
    • hasToken

      public static boolean hasToken(File dir, String name) throws IOException
      Returns true iff the given directory has a file with the given name.
      Throws:
      IOException
    • hasToken

      public static boolean hasToken(File dir, String name, String content) throws IOException
      Returns true iff the given directory has a file with the given name containing the given content.
      Throws:
      IOException
    • hasTokenFile

      public static boolean hasTokenFile(File tokenFile) throws IOException
      Returns true iff the given directory has a file with the given name.
      Throws:
      IOException
    • hasTokenFile

      public static boolean hasTokenFile(File tokenFile, String content) throws IOException
      Returns true iff the given directory has a file with the given name containing the given content.
      Throws:
      IOException
    • copyFile

      public static void copyFile(File srcFile, File dstFile, String... toReplace) throws IOException
      Copies from src to dst and performs a simple copy-replace templating operation along the way. ```java copyFile(src, dst, "%username%", "lskywalker" "%firstname%", "Luke", "%lastname%", "Skywalker"); ```
      Throws:
      IOException
    • modifyFile

      public static void modifyFile(File file, Function<String,String> modifier) throws IOException
      Modifies the given file in place.
      Throws:
      IOException
    • cleanDir

      public static void cleanDir(File dirToRemove) throws IOException
      Deletes the given file or directory if it exists, then creates a fresh directory in its place.
      Throws:
      IOException
    • flatten

      public static void flatten(File dirToRemove) throws IOException
      Flattens a single directory (moves its children to be its peers, then deletes the given directory. ``` before: root/ toFlatten/ child1 child2 flatten("root/toFlatten") after: root/ child1 child2 ```
      Throws:
      IOException
    • concat

      public static void concat(Iterable<File> toMerge, File dst) throws IOException
      Concats the first files and writes them to the last file.
      Throws:
      IOException
    • deleteEmptyFolders

      public static void deleteEmptyFolders(File d) throws IOException
      Deletes all empty folders (recursively).
      Throws:
      IOException
    • parseListFile

      public static List<File> parseListFile(Project project, List<Object> inputs)
    • toOctalFileModeInt

      public static int toOctalFileModeInt(Set<PosixFilePermission> permissions)
      Converts a set of PosixFilePermission to chmod-style octal file mode.
    • toOctalFileMode

      public static String toOctalFileMode(Set<PosixFilePermission> permissions)
      Converts a set of PosixFilePermission to chmod-style octal file mode.
    • containsExecutablePermission

      public static boolean containsExecutablePermission(Set<PosixFilePermission> permissions)
      Returns true if any of the bits contain the executable permission.
    • asUrl

      public static String asUrl(File file)
      Prefixes `file://` to the file's absolute path.
    • macApp

      public static String macApp()
      Returns ".app" on macOS, and empty string on all others.
    • macContentsEclipse

      public static String macContentsEclipse()
      Returns "Contents/Eclipse/" on macOS, and empty string on all others.
    • assertMacApp

      public static void assertMacApp(File file)
      Ensures that the given file ends with ".app" on macOS, does nothing on all others.