Class ProjectFileManager
-
Constructor Summary
ConstructorsConstructorDescriptionProjectFileManager(com.vaadin.flow.server.startup.ApplicationConfiguration applicationConfiguration) Public only for tests. -
Method Summary
Modifier and TypeMethodDescriptionvoiddeleteFile(File file) findModule(File file) Finds the module where the given resource is located.findResource(String resource) Finds the given resource by name, in any resource folder.findResourceFolder(File resourceFile) Finds the resource folder where the given resource is located.findSourceFolder(File sourceFile) Finds the source folder where the given file is located.static ProjectFileManagerget()getAbsolutePath(String filename) com.vaadin.flow.server.startup.ApplicationConfigurationgetFileForClass(Class<?> cls) Returns the Java file for the given class.getFileForClass(String cls) Returns the Java file for the given class.getFlowNewViewFolder(com.vaadin.flow.server.VaadinSession vaadinSession) Finds the folder where a new Flow view should be created.getFlowSourceFolder(com.vaadin.flow.server.VaadinSession vaadinSession) Returns the source root that new Flow views should be created under.getFlowSourceLanguage(com.vaadin.flow.server.VaadinSession vaadinSession) Returns the language new Flow views should be written in.Returns the frontend folder.Finds the folder where Hilla views should be created for the file system router to pick them up.getJavaPackage(File sourceFile) Gets the java package for the given source fileReturns the java resource folder.Returns the java source folder.Gets the modules in the project.getProjectRelativeName(File projectFile) Returns the name of the file, relative to the project root.Returns the project base folder.Returns the resource folders for the project.getSourceFile(com.vaadin.flow.component.internal.ComponentTracker.Location location) Returns the Java file for the given component location.Returns the source folders for the project.Returns the styles.css file.Returns the styles folder (META-INF/resources).Gets current theme folder if present, fallbacks to frontend folder if none.Gets current theme namestatic ProjectFileManagerinitialize(com.vaadin.flow.server.startup.ApplicationConfiguration applicationConfiguration) makeAbsolute(String projectRelativeFilename) Deprecated.This rarely ever works as expected because the file you want to write is in one of the Maven modules, not necessarily in the root project.makeRelative(String filename) voidsanitizeFilename(String name) Makes a string safe to use as a file namevoidWrites the given content to the given file inside the project.voidWrites the given content to the given file inside the project.voidwriteFileBase64(String filename, String undoLabel, String base64Content, boolean renameIfExists)
-
Constructor Details
-
ProjectFileManager
public ProjectFileManager(com.vaadin.flow.server.startup.ApplicationConfiguration applicationConfiguration) Public only for tests. Always access throughget()instead.- Parameters:
applicationConfiguration- the application configuration
-
-
Method Details
-
initialize
public static ProjectFileManager initialize(com.vaadin.flow.server.startup.ApplicationConfiguration applicationConfiguration) -
get
-
readFile
- Throws:
IOException
-
readFile
- Throws:
IOException
-
readFile
- Throws:
IOException
-
readLines
- Throws:
IOException
-
writeFile
Writes the given content to the given file inside the project.If the filename is absolute, it is used as is. Otherwise, it is resolved relative to the project root.
If the file is outside the project, an exception is thrown
- Parameters:
filename- the filename to write to, absolute or relative to the project rootundoLabel- the undo label for the changecontent- the content to write- Throws:
IOException- if the file cannot be written
-
writeFile
Writes the given content to the given file inside the project.If the filename is absolute, it is used as is. Otherwise, it is resolved relative to the project root.
If the file is outside the project, an exception is thrown
- Parameters:
file- the filename to write to, absolute or relative to the project rootundoLabel- the undo label for the changecontent- the content to write- Throws:
IOException- if the file cannot be written
-
writeFile
- Throws:
IOException
-
writeFileBase64
public File writeFileBase64(String filename, String undoLabel, String base64Content, boolean renameIfExists) throws IOException - Throws:
IOException
-
makeAbsolute
Deprecated.This rarely ever works as expected because the file you want to write is in one of the Maven modules, not necessarily in the root project.Changes the relative file name into an absolute file name and validates it is inside the project.- Throws:
IOException
-
makeRelative
- Throws:
IOException
-
getProjectRelativeName
Returns the name of the file, relative to the project root.- Parameters:
projectFile- the file- Returns:
- the relative name of the file
-
getFileForClass
Returns the Java file for the given class.- Parameters:
cls- the class- Returns:
- the file for the class
-
getFileForClass
Returns the Java file for the given class.- Parameters:
cls- the class- Returns:
- the file for the class
-
getSourceFolders
Returns the source folders for the project.- Returns:
- the source folders
-
getResourceFolders
Returns the resource folders for the project.- Returns:
- the resource folders
-
getSourceFile
Returns the Java file for the given component location.- Parameters:
location- the component location- Returns:
- the file for the class where the component is used
-
getProjectRoot
Returns the project base folder.The project base folder is the common folder containing all module roots.
- Returns:
- the project root folder
-
getFrontendFolder
Returns the frontend folder.- Returns:
- the frontend folder
-
getStylesFolder
Returns the styles folder (META-INF/resources).- Returns:
- the styles folder
-
getStylesCss
Returns the styles.css file.- Returns:
- the styles.css file
-
getJavaResourceFolder
Returns the java resource folder.- Returns:
- the java resource folder.
-
getJavaSourceFolder
Returns the java source folder.- Returns:
- the java source folder.
-
getFlowSourceFolder
Returns the source root that new Flow views should be created under.This is not always
getJavaSourceFolder(): a Kotlin project's views live insrc/main/kotlin. The folder the existing views are in (getFlowNewViewFolder(com.vaadin.flow.server.VaadinSession)) decides, which keeps the answer correct in a mixed project, and the root is then looked up withfindSourceFolder(File)— the same lookupgetJavaPackage(java.io.File)uses, so the root and the package computed relative to it cannot disagree.Deliberately not derived by walking up to the first folder named after a language: a package segment can carry that name too, so views in
src/main/kotlin/com/example/java/viewswould have reported.../com/example/javaas the source root and generated every new view in the wrong package. This repository had exactly that shape insrc/test/kotlin/.../flowkotlin/kotlin.- Parameters:
vaadinSession- the session, used to find the existing views- Returns:
- the source root, falling back to the Java source folder
-
getFlowSourceLanguage
Returns the language new Flow views should be written in.Single source of truth for both the file that gets created and the template list offered for it: deciding those separately let a Kotlin project be shown templates that only exist in Java.
Only the source root's own name is examined, not the whole path. A checkout living under a directory called
kotlinwould otherwise make a Java project report Kotlin.- Parameters:
vaadinSession- the session, used to find the existing views- Returns:
- the language, defaulting to Java when the root is not named after one
-
getThemeName
Gets current theme name- Returns:
- optional theme name
-
getThemeFolder
Gets current theme folder if present, fallbacks to frontend folder if none.- Returns:
- theme folder if present, resources styles folder otherwise
-
getAbsolutePath
-
sanitizeFilename
Makes a string safe to use as a file name- Parameters:
name- the string to process- Returns:
- the sanitized string
-
getHillaViewsFolder
Finds the folder where Hilla views should be created for the file system router to pick them up.- Returns:
- the folder where Hilla views should be created
-
getFlowNewViewFolder
Finds the folder where a new Flow view should be created.If all views are in the same package/folder, that folder is returned. If views are in different packages, the folder for a common prefix is returned. If no common prefix is found, the folder for the main package is returned.
- Returns:
- a suitable folder to place a new Flow view in
-
findResource
Finds the given resource by name, in any resource folder.- Parameters:
resource- the name of the resource to look for
-
findResourceFolder
Finds the resource folder where the given resource is located.- Parameters:
resourceFile- the resource to find the resource folder for
-
findModule
Finds the module where the given resource is located.- Parameters:
file- the file to look up
-
findSourceFolder
Finds the source folder where the given file is located.- Parameters:
sourceFile- the source file to find the source folder for
-
getJavaPackage
Gets the java package for the given source file- Parameters:
sourceFile- the source file- Returns:
- the java package for the given source file, or null if the source file is not inside the project
- Throws:
IOException- if something goes wrong
-
getModules
Gets the modules in the project.- Returns:
- a list of modules in the project
-
getApplicationConfiguration
public com.vaadin.flow.server.startup.ApplicationConfiguration getApplicationConfiguration() -
deleteFile
- Throws:
IOException
-
moveFile
- Throws:
IOException
-