Skip to content

Commit

Permalink
Unified the File renaming
Browse files Browse the repository at this point in the history
- all renaming of files is done  by using the same method.
- allow filenames to contain the placeholder _PROJECTNAME_
  • Loading branch information
keinhaar committed Oct 27, 2024
1 parent e946628 commit d741ee5
Showing 1 changed file with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,29 +213,15 @@ private void replacePackageNames(IProject iProject, File dir) throws IOException
File file = files.pop();
if(file.getName().contains("_PACKAGENAME_"))
{
String name = file.getName();
name = name.replace("_PACKAGENAME_", packageBaseName.replace('.', File.separatorChar));
File newFile = new File(file.getParentFile(), name);
Files.createDirectories(newFile.getParentFile().toPath());
boolean ret = file.renameTo(newFile);
if(ret == false)
{
throw new IOException("Could not rename");
}
file = newFile;
file = replace(file, "_PACKAGENAME_", packageBaseName.replace('.', File.separatorChar));
}
if(file.getName().contains("_PROJECTNAME_"))
{
file = replace(file, "_PROJECTNAME_", projectBaseName.replace('.', File.separatorChar));
}
if(file.getName().contains("_.._"))
{
String name = file.getName();
name = name.replace("_.._", "../");
File newFile = new File(file.getParentFile(), name);
Files.createDirectories(newFile.getParentFile().toPath());
boolean ret = file.renameTo(newFile);
if(ret == false)
{
throw new IOException("Could not rename");
}
file = newFile;
file = replace(file, "_.._", "../");
}
if(file.isDirectory())
{
Expand All @@ -250,6 +236,20 @@ private void replacePackageNames(IProject iProject, File dir) throws IOException
}
}

private File replace(File file, String toReplace, String replacement) throws IOException
{
String name = file.getName();
name = name.replace(toReplace, replacement);
File newFile = new File(file.getParentFile(), name);
Files.createDirectories(newFile.getParentFile().toPath());
boolean ret = file.renameTo(newFile);
if(ret == false)
{
throw new IOException("Could not rename");
}
return newFile;
}

/**
* Replaces a pattern in a File. The File is not allowed to be larger then 1.000.000 bytes.
* @param file the file, where the replacement is made.
Expand Down

0 comments on commit d741ee5

Please sign in to comment.