Class ProjectFileManager

java.lang.Object
com.vaadin.copilot.ProjectFileManager

public class ProjectFileManager extends Object
  • Constructor Details

    • ProjectFileManager

      public ProjectFileManager(com.vaadin.flow.server.startup.ApplicationConfiguration applicationConfiguration)
      Public only for tests. Always access through get() instead.
      Parameters:
      applicationConfiguration - the application configuration
  • Method Details

    • initialize

      public static ProjectFileManager initialize(com.vaadin.flow.server.startup.ApplicationConfiguration applicationConfiguration)
    • get

      public static ProjectFileManager get()
    • readFile

      public String readFile(String filename) throws IOException
      Throws:
      IOException
    • readFile

      public String readFile(Path filename) throws IOException
      Throws:
      IOException
    • readFile

      public String readFile(File file) throws IOException
      Throws:
      IOException
    • readLines

      public List<String> readLines(File file) throws IOException
      Throws:
      IOException
    • writeFile

      public void writeFile(String filename, String undoLabel, String content) throws IOException
      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 root
      undoLabel - the undo label for the change
      content - the content to write
      Throws:
      IOException - if the file cannot be written
    • writeFile

      public void writeFile(File file, String undoLabel, String content) throws IOException
      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 root
      undoLabel - the undo label for the change
      content - the content to write
      Throws:
      IOException - if the file cannot be written
    • writeFile

      public void writeFile(Path file, String undoLabel, String content) throws IOException
      Throws:
      IOException
    • writeFileBase64

      public File writeFileBase64(String filename, String undoLabel, String base64Content, boolean renameIfExists) throws IOException
      Throws:
      IOException
    • makeAbsolute

      @Deprecated public String makeAbsolute(String projectRelativeFilename) throws IOException
      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

      public String makeRelative(String filename) throws IOException
      Throws:
      IOException
    • getProjectRelativeName

      public String getProjectRelativeName(File projectFile)
      Returns the name of the file, relative to the project root.
      Parameters:
      projectFile - the file
      Returns:
      the relative name of the file
    • getFileForClass

      public File getFileForClass(Class<?> cls)
      Returns the Java file for the given class.
      Parameters:
      cls - the class
      Returns:
      the file for the class
    • getFileForClass

      public File getFileForClass(String cls)
      Returns the Java file for the given class.
      Parameters:
      cls - the class
      Returns:
      the file for the class
    • getSourceFolders

      public List<Path> getSourceFolders()
      Returns the source folders for the project.
      Returns:
      the source folders
    • getResourceFolders

      public List<Path> getResourceFolders()
      Returns the resource folders for the project.
      Returns:
      the resource folders
    • getSourceFile

      public File getSourceFile(com.vaadin.flow.component.internal.ComponentTracker.Location location)
      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

      public File getProjectRoot()
      Returns the project base folder.

      The project base folder is the common folder containing all module roots.

      Returns:
      the project root folder
    • getFrontendFolder

      public File getFrontendFolder()
      Returns the frontend folder.
      Returns:
      the frontend folder
    • getStylesCss

      public File getStylesCss()
      Returns the styles.css file.
      Returns:
      the styles.css file
    • getJavaResourceFolder

      public File getJavaResourceFolder()
      Returns the java resource folder.
      Returns:
      the java resource folder.
    • getJavaSourceFolder

      public File getJavaSourceFolder()
      Returns the java source folder.
      Returns:
      the java source folder.
    • getThemeName

      public Optional<String> getThemeName()
      Gets current theme name
      Returns:
      optional theme name
    • getThemeFolder

      public File getThemeFolder()
      Gets current theme folder if present, fallbacks to frontend folder if none.
      Returns:
      theme folder if present, frontend folder otherwise
    • getAbsolutePath

      public File getAbsolutePath(String filename)
    • sanitizeFilename

      public String sanitizeFilename(String name)
      Makes a string safe to use as a file name
      Parameters:
      name - the string to process
      Returns:
      the sanitized string
    • getHillaViewsFolder

      public File 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

      public File getFlowNewViewFolder(com.vaadin.flow.server.VaadinSession vaadinSession)
      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

      public Optional<Path> findResource(String resource)
      Finds the given resource by name, in any resource folder.
      Parameters:
      resource - the name of the resource to look for
    • findResourceFolder

      public Optional<Path> findResourceFolder(File resourceFile)
      Finds the resource folder where the given resource is located.
      Parameters:
      resourceFile - the resource to find the resource folder for
    • findModule

      public Optional<JavaSourcePathDetector.ModuleInfo> findModule(File file)
      Finds the module where the given resource is located.
      Parameters:
      file - the file to look up
    • findSourceFolder

      public Optional<Path> findSourceFolder(File sourceFile)
      Finds the source folder where the given file is located.
      Parameters:
      sourceFile - the source file to find the source folder for
    • getJavaPackage

      public String getJavaPackage(File sourceFile) throws IOException
      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

      public void deleteFile(File file) throws IOException
      Throws:
      IOException