lingua-franca 0.10.1
Lingua Franca code generator
Loading...
Searching...
No Matches
org.lflang.util.FileUtil Class Reference

Utilities for file operations. More...

Static Public Member Functions

static void arduinoDeleteHelper (Path srcGenPath, boolean threadingOn) throws IOException
 Delete unused Files from Arduino-CLI based compilation.
static void copyDirectory (final Path srcDir, final Path dstDir, final boolean skipIfUnchanged) throws IOException
 Copy the given source directory into the given destination directory.
static void copyDirectoryContents (final Path srcDir, final Path dstDir) throws IOException
 Recursively copy the contents of the given source directory into the given destination directory.
static void copyDirectoryContents (final Path srcDir, final Path dstDir, final boolean skipIfUnchanged) throws IOException
 Recursively copy the contents of the given source directory into the given destination directory.
static void copyFile (Path srcFile, Path dstFile) throws IOException
 Copy a given source file to a given destination file.
static void copyFile (Path srcFile, Path dstFile, boolean skipIfUnchanged) throws IOException
 Copy a given source file to a given destination file.
static void copyFileFromClassPath (final String entry, final Path dstDir, final boolean skipIfUnchanged) throws IOException
 Look up the given entry in the classpath.
static void copyFilesOrDirectories (List< String > entries, Path dstDir, FileConfig fileConfig, MessageReporter messageReporter, boolean fileEntriesOnly)
 Given a list of files or directories, attempt to find each entry based on the given generator context and copy it to the destination directory.
static void copyFromClassPath (final String entry, final Path dstDir, final boolean skipIfUnchanged, final boolean contentsOnly) throws IOException
 Look up the given entry in the classpath.
static void copyFromFileSystem (Path entry, Path dstDir, boolean contentsOnly) throws IOException
 If the given entry is a file, then copy it into the destination.
static void createDirectoryIfDoesNotExist (File dir)
static void delete (Path fileOrDirectory) throws IOException
 Delete the given file or directory if it exists.
static void deleteDirectory (Path dir) throws IOException
 Recursively delete a directory if it exists.
static Path findAndCopyFile (String file, Path dstDir, FileConfig fileConfig)
 Find the given file in the package and return the path to the file that was found; null if it was not found.
static Path findInPackage (Path fileOrDirectory, FileConfig fileConfig)
 Return an absolute path to the given file or directory if it can be found within the package.
static IResource getIResource (java.net.URI uri)
 Get the specified uri as an Eclipse IResource or null if it is not found.
static IResource getIResource (Path path)
 Get the specified path as an Eclipse IResource or null if it is not found.
static IResource getIResource (Resource r)
 Get the iResource corresponding to the provided resource if it can be found.
static Path getRelativePath (Resource source, Resource target)
static Resource getResourceFromClassPath (ResourceSet resourceSet, final String entry) throws IOException
 Fetch a resource from the jar.
static List< Path > globFilesEndsWith (Path currentDir, String str)
 Return a list of files ending with "str".
static boolean isCFile (Path path)
 Return true if the given path points to a C file, false otherwise.
static boolean isSame (String text, Path path) throws IOException
 Check if the content of a file is equal to a given string.
static java.net.URI locateFile (String path, Resource resource)
 Parse the string as file location and return it as URI.
static String nameWithoutExtension (Path file)
 Return the name of the file excluding its file extension.
static String nameWithoutExtension (Resource r)
 Return the name of the file associated with the given resource, excluding its file extension.
static void relativeIncludeHelper (Path dir, Path includePath, MessageReporter messageReporter) throws IOException
 Convert all includes recursively inside files within a specified folder to relative links.
static IPath toIPath (URI uri)
 Return an org.eclipse.core.runtime.Path object corresponding to the given URI.
static Path toPath (Resource resource)
 Return a java.nio.Path object corresponding to the given Resource.
static Path toPath (URI uri)
 Return a java.nio.Path object corresponding to the given URI.
static String toUnixString (Path path)
 Convert a given path to a unix-style string.
static void writeToFile (CharSequence text, Path path) throws IOException
 Write text to a file.
static void writeToFile (String text, Path path) throws IOException
 Write text to a file.
static void writeToFile (String text, Path path, boolean skipIfUnchanged) throws IOException
 Write text to a file.

Detailed Description

Utilities for file operations.

Member Function Documentation

◆ arduinoDeleteHelper()

void org.lflang.util.FileUtil.arduinoDeleteHelper ( Path srcGenPath,
boolean threadingOn ) throws IOException
static

Delete unused Files from Arduino-CLI based compilation.

Arduino-CLI (the build system) uses lazy compilation (i.e. compiles every file recursively from a source directory). This does the work of CMake by explicitly deleting files that shouldn't get compiled by the CLI. Generally, we delete all CMake artifacts and multithreaded support files (including semaphores and thread folders)

Parameters
srcGenPathThe folder to search for folders and files to delete.
threadingOnWhether threading is enabled.
Exceptions
IOExceptionIf the given folder and unneeded files cannot be deleted.

◆ copyDirectory()

void org.lflang.util.FileUtil.copyDirectory ( final Path srcDir,
final Path dstDir,
final boolean skipIfUnchanged ) throws IOException
static

Copy the given source directory into the given destination directory.

For example, if the source directory is foo/bar and the destination is baz, then copies of the contents of foo/bar will be located in baz/bar.

Parameters
srcDirThe source directory path.
dstDirThe destination directory path.
skipIfUnchangedIf true, don't overwrite anything in the destination if its content would not be changed.
Exceptions
IOExceptionIf the operation fails.

◆ copyDirectoryContents() [1/2]

void org.lflang.util.FileUtil.copyDirectoryContents ( final Path srcDir,
final Path dstDir ) throws IOException
static

Recursively copy the contents of the given source directory into the given destination directory.

Existing files of the destination may be overwritten.

Parameters
srcDirThe directory to copy files from.
dstDirThe directory to copy files to.
Exceptions
IOExceptionif copy fails.

◆ copyDirectoryContents() [2/2]

void org.lflang.util.FileUtil.copyDirectoryContents ( final Path srcDir,
final Path dstDir,
final boolean skipIfUnchanged ) throws IOException
static

Recursively copy the contents of the given source directory into the given destination directory.

Existing files of the destination may be overwritten.

Parameters
srcDirThe source directory path.
dstDirThe destination directory path.
skipIfUnchangedIf true, don't overwrite anything in the destination if its content would not be changed.
Exceptions
IOExceptionIf the operation fails.

◆ copyFile() [1/2]

void org.lflang.util.FileUtil.copyFile ( Path srcFile,
Path dstFile ) throws IOException
static

Copy a given source file to a given destination file.

This also creates new directories for any directories on the path to dstFile that do not yet exist.

Parameters
srcFileThe source file path.
dstFileThe destination file path.
Exceptions
IOExceptionif copy fails.

◆ copyFile() [2/2]

void org.lflang.util.FileUtil.copyFile ( Path srcFile,
Path dstFile,
boolean skipIfUnchanged ) throws IOException
static

Copy a given source file to a given destination file.

This also creates new directories on the path to dstFile that do not yet exist.

Parameters
srcFileThe source file path.
dstFileThe destination file path.
skipIfUnchangedIf true, don't overwrite the destination file if its content would not be changed.
Exceptions
IOExceptionIf the operation fails.

◆ copyFileFromClassPath()

void org.lflang.util.FileUtil.copyFileFromClassPath ( final String entry,
final Path dstDir,
final boolean skipIfUnchanged ) throws IOException
static

Look up the given entry in the classpath.

If it is found and is a file, copy it into the destination directory. If the entry is not found or not a file, throw an exception.

Parameters
entryA file copy to the destination directory.
dstDirA directory to copy the entry to.
skipIfUnchangedIf true, don't overwrite the destination file if its content would not be changed.
Exceptions
IOExceptionIf the operation failed.

◆ copyFilesOrDirectories()

void org.lflang.util.FileUtil.copyFilesOrDirectories ( List< String > entries,
Path dstDir,
FileConfig fileConfig,
MessageReporter messageReporter,
boolean fileEntriesOnly )
static

Given a list of files or directories, attempt to find each entry based on the given generator context and copy it to the destination directory.

Entries are searched for in the file system first, relative to the source file and relative to the package root. Entries that cannot be found in the file system are looked for on the class path.

If contentsOnly is true, then for each entry that is a directory, only its contents are copied, not the directory itself. For example, if the entry is a directory foo/bar and the destination is baz, then copies of the contents of foo/bar will be located directly in baz. If contentsOnly is false, then copies of the contents of foo/bar will be located in baz/bar.

Parameters
entriesThe list of file or directory entries to copy.
dstDirThe destination directory.
fileConfigThe file configuration that specifies where to find entries.
messageReporterUsed to report errors and warnings.
fileEntriesOnlyWhether to copy only file entries.

◆ copyFromClassPath()

void org.lflang.util.FileUtil.copyFromClassPath ( final String entry,
final Path dstDir,
final boolean skipIfUnchanged,
final boolean contentsOnly ) throws IOException
static

Look up the given entry in the classpath.

If it is a file, copy it into the destination directory. If the entry is a directory and contentsOnly is true, then copy its contents to the destination directory. If the entry is a directory and contentsOnly is true, then copy it including its contents to the destination directory.

This also creates new directories for any directories on the destination path that do not yet exist.

Parameters
entryThe entry to be found on the class path and copied to the given destination.
dstDirThe file system path that found files are to be copied to.
skipIfUnchangedIf true, don't overwrite the file or directory if its content would not be changed
contentsOnlyIf true and the entry is a directory, then copy its contents but not the directory itself.
Exceptions
IOExceptionIf the operation failed.

◆ copyFromFileSystem()

void org.lflang.util.FileUtil.copyFromFileSystem ( Path entry,
Path dstDir,
boolean contentsOnly ) throws IOException
static

If the given entry is a file, then copy it into the destination.

If the entry is a directory and contentsOnly is true, then copy its contents to the destination directory. If the entry is a directory and contentsOnly is true, then copy it including its contents to the destination directory.

Parameters
entryA file or directory to copy to the destination directory.
dstDirA directory to copy the entry or its contents to.
contentsOnlyIf true and entry is a directory, then copy its contents but not the directory itself.
Exceptions
IOExceptionIf the operation fails.

◆ createDirectoryIfDoesNotExist()

void org.lflang.util.FileUtil.createDirectoryIfDoesNotExist ( File dir)
static

◆ delete()

void org.lflang.util.FileUtil.delete ( Path fileOrDirectory) throws IOException
static

Delete the given file or directory if it exists.

If fileOrDirectory is a directory, deletion is recursive.

Parameters
fileOrDirectoryThe file or directory to delete.
Exceptions
IOExceptionIf the operation failed.

◆ deleteDirectory()

void org.lflang.util.FileUtil.deleteDirectory ( Path dir) throws IOException
static

Recursively delete a directory if it exists.

Exceptions
IOExceptionIf an I/O error occurs.

◆ findAndCopyFile()

Path org.lflang.util.FileUtil.findAndCopyFile ( String file,
Path dstDir,
FileConfig fileConfig )
static

Find the given file in the package and return the path to the file that was found; null if it was not found.

Parameters
fileThe file to look for.
dstDirThe directory to copy it to.
fileConfigThe file configuration that specifies where look for the file.
Returns
The path to the file that was found, or null if it was not found.

◆ findInPackage()

Path org.lflang.util.FileUtil.findInPackage ( Path fileOrDirectory,
FileConfig fileConfig )
static

Return an absolute path to the given file or directory if it can be found within the package.

Otherwise, return null.

NOTE: If the given file or directory is given as an absolute path but cannot be found, it is interpreted as a relative path with respect to the project root.

Parameters
fileOrDirectoryThe file or directory to look for.
fileConfigA file configuration that determines where the package is located.
Returns
An absolute path of the file or directory was found; null otherwise.

◆ getIResource() [1/3]

IResource org.lflang.util.FileUtil.getIResource ( java.net.URI uri)
static

Get the specified uri as an Eclipse IResource or null if it is not found.

Also returns null if this is not called from within a running Eclipse instance.

Parameters
uriA java.net.uri of the form "file://path".

◆ getIResource() [2/3]

IResource org.lflang.util.FileUtil.getIResource ( Path path)
static

Get the specified path as an Eclipse IResource or null if it is not found.

◆ getIResource() [3/3]

IResource org.lflang.util.FileUtil.getIResource ( Resource r)
static

Get the iResource corresponding to the provided resource if it can be found.

◆ getRelativePath()

Path org.lflang.util.FileUtil.getRelativePath ( Resource source,
Resource target )
static

◆ getResourceFromClassPath()

Resource org.lflang.util.FileUtil.getResourceFromClassPath ( ResourceSet resourceSet,
final String entry ) throws IOException
static

Fetch a resource from the jar.

Parameters
resourceSetThe resource set
entryThe path to the resource within the jar
Exceptions
IOExceptionIf the resource cannot be found.

◆ globFilesEndsWith()

List< Path > org.lflang.util.FileUtil.globFilesEndsWith ( Path currentDir,
String str )
static

Return a list of files ending with "str".

Parameters
currentDirThe current directory.
strThe pattern to match against.

◆ isCFile()

boolean org.lflang.util.FileUtil.isCFile ( Path path)
static

Return true if the given path points to a C file, false otherwise.

◆ isSame()

boolean org.lflang.util.FileUtil.isSame ( String text,
Path path ) throws IOException
static

Check if the content of a file is equal to a given string.

Parameters
textThe text to compare with.
pathThe file to compare with.
Returns
true, if the given text is identical to the file content.

◆ locateFile()

java.net.URI org.lflang.util.FileUtil.locateFile ( String path,
Resource resource )
static

Parse the string as file location and return it as URI.

Supports URIs, plain file paths, and paths relative to a model.

Parameters
paththe file location as string.
resourcethe model resource this file should be resolved relatively. May be null.
Returns
the (Java) URI or null if no file can be located.

◆ nameWithoutExtension() [1/2]

String org.lflang.util.FileUtil.nameWithoutExtension ( Path file)
static

Return the name of the file excluding its file extension.

Parameters
fileA Path object
Returns
The name of the file excluding its file extension.

◆ nameWithoutExtension() [2/2]

String org.lflang.util.FileUtil.nameWithoutExtension ( Resource r)
static

Return the name of the file associated with the given resource, excluding its file extension.

Parameters
rAny Resource.
Returns
The name of the file associated with the given resource, excluding its file extension.
Exceptions
IllegalArgumentExceptionIf the resource has an invalid URI.

◆ relativeIncludeHelper()

void org.lflang.util.FileUtil.relativeIncludeHelper ( Path dir,
Path includePath,
MessageReporter messageReporter ) throws IOException
static

Convert all includes recursively inside files within a specified folder to relative links.

Parameters
dirThe folder to search for includes to change.
includePathThe include path.
messageReporterError reporter
Exceptions
IOExceptionIf the given set of files cannot be relativized.

◆ toIPath()

IPath org.lflang.util.FileUtil.toIPath ( URI uri)
static

Return an org.eclipse.core.runtime.Path object corresponding to the given URI.

Exceptions
IllegalArgumentExceptionIf the given URI is invalid.

◆ toPath() [1/2]

Path org.lflang.util.FileUtil.toPath ( Resource resource)
static

Return a java.nio.Path object corresponding to the given Resource.

Exceptions
IllegalArgumentExceptionIf the given resource has an invalid URI.

◆ toPath() [2/2]

Path org.lflang.util.FileUtil.toPath ( URI uri)
static

Return a java.nio.Path object corresponding to the given URI.

Exceptions
IllegalArgumentExceptionIf the given URI is invalid.

◆ toUnixString()

String org.lflang.util.FileUtil.toUnixString ( Path path)
static

Convert a given path to a unix-style string.

This ensures that '/' is used instead of '\' as file separator.

◆ writeToFile() [1/3]

void org.lflang.util.FileUtil.writeToFile ( CharSequence text,
Path path ) throws IOException
static

Write text to a file.

Parameters
textThe text to be written.
pathThe file to write the code to.

◆ writeToFile() [2/3]

void org.lflang.util.FileUtil.writeToFile ( String text,
Path path ) throws IOException
static

Write text to a file.

Parameters
textThe text to be written.
pathThe file to write the code to.

◆ writeToFile() [3/3]

void org.lflang.util.FileUtil.writeToFile ( String text,
Path path,
boolean skipIfUnchanged ) throws IOException
static

Write text to a file.

Parameters
textThe text to be written.
pathThe file to write the code to.
skipIfUnchangedIf true, don't overwrite the destination file if its content would not be changed

The documentation for this class was generated from the following file:
  • /Users/runner/work/lingua-franca/lingua-franca/core/src/main/java/org/lflang/util/FileUtil.java