Package com.diffplug.gradle
Class FileMisc
- java.lang.Object
-
- com.diffplug.gradle.FileMisc
-
public class FileMisc extends Object
Miscellaneous utilties for copying files around.
-
-
Constructor Summary
Constructors Constructor Description FileMisc()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
assertMacApp(File file)
Ensures that the given file ends with ".app" on macOS, does nothing on all others.static String
asUrl(File file)
Prefixes `file://` to the file's absolute path.static void
cleanDir(File dirToRemove)
Deletes the given file or directory if it exists, then creates a fresh directory in its place.static void
concat(Iterable<File> toMerge, File dst)
Concats the first files and writes them to the last file.static boolean
containsExecutablePermission(Set<PosixFilePermission> permissions)
Returns true if any of the bits contain the executable permission.static void
copyFile(File srcFile, File dstFile, String... toReplace)
Copies from src to dst and performs a simple copy-replace templating operation along the way.static void
deleteEmptyFolders(File d)
Deletes all empty folders (recursively).static boolean
dirExists(File dir)
Returns true if the given directory exists, and waits up to 500ms for the directory to exist.static void
download(String url, File dst)
Downloads the url to the destination file (with support for redirects).static void
flatten(File dirToRemove)
Flattens a single directory (moves its children to be its peers, then deletes the given directory.static void
forceDelete(File f)
CallsFileUtils.forceDelete(File)
and throws an exception if it fails.static boolean
hasToken(File dir, String name)
Returns true iff the given directory has a file with the given name.static boolean
hasToken(File dir, String name, String content)
Returns true iff the given directory has a file with the given name containing the given content.static boolean
hasTokenFile(File tokenFile)
Returns true iff the given directory has a file with the given name.static boolean
hasTokenFile(File tokenFile, String content)
Returns true iff the given directory has a file with the given name containing the given content.static List<File>
list(File d)
Lists the children of the given file in a safe way (File.listFiles()
can return null).static String
macApp()
Returns ".app" on macOS, and empty string on all others.static String
macContentsEclipse()
Returns "Contents/Eclipse/" on macOS, and empty string on all others.static void
mkdirs(File d)
CallsFile.mkdirs()
and throws an exception if it fails.static void
modifyFile(File file, Function<String,String> modifier)
Modifies the given file in place.static String
noQuote(String input)
Throws an exception if the given input property contains whitespace.static List<File>
parseListFile(Project project, List<Object> inputs)
static String
quote(File input)
Quotes the absolute path of the given file iff it contains whitespace.static String
quote(String input)
Quotes the given input string iff it contains whitespace.static Optional<String>
readToken(File dir, String name)
Returns the contents of a file with the given name, if it exists.static String
toOctalFileMode(Set<PosixFilePermission> permissions)
Converts a set ofPosixFilePermission
to chmod-style octal file mode.static int
toOctalFileModeInt(Set<PosixFilePermission> permissions)
Converts a set ofPosixFilePermission
to chmod-style octal file mode.static String
toUnixNewline(String input)
Enforces unix newlines on the given string.static void
writeToken(File dir, String name)
Writes an empty file with the given name in the given directory.static void
writeToken(File dir, String name, String value)
Writes a file with the given name, to the given directory, containing the given value.static void
writeTokenFile(File tokenFile, String value)
Writes a token file containing the given value.
-
-
-
Field Detail
-
PROTOCOL
public static final String PROTOCOL
The `file://` protocol.
-
-
Method Detail
-
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)
CallsFile.mkdirs()
and throws an exception if it fails.
-
forceDelete
public static void forceDelete(File f)
CallsFileUtils.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
-
toOctalFileModeInt
public static int toOctalFileModeInt(Set<PosixFilePermission> permissions)
Converts a set ofPosixFilePermission
to chmod-style octal file mode.
-
toOctalFileMode
public static String toOctalFileMode(Set<PosixFilePermission> permissions)
Converts a set ofPosixFilePermission
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.
-
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.
-
-