Skip to content

Commit 85d4839

Browse files
matthijskooijmanfacchinm
authored andcommitted
Merge Sketch.renameFileTo() into SketchFile.renameTo()
Now that SketchFile keeps a reference to its Sketch, `SketchFile.renameTo()` can call `Sketch.checkNewFilename()`, so there is no need for the renaming itself to go through Sketch. This changes the parameter for `SketchFile.renameTo()` from File to String, to enforce that only the filename is changed, not the directory name.
1 parent 74e5228 commit 85d4839

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

app/src/processing/app/SketchController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ protected void nameCode(String newName) {
186186
} else {
187187
// Non-primary file, rename just that file
188188
try {
189-
sketch.renameFileTo(current, newName);
189+
current.renameTo(newName);
190190
} catch (IOException e) {
191191
// This does not pass on e, to prevent showing a backtrace for
192192
// "normal" errors.
@@ -348,7 +348,7 @@ public boolean save() throws IOException {
348348
// Do rename of all .pde files to new .ino extension
349349
for (SketchFile file : oldFiles) {
350350
File newName = FileUtils.replaceExtension(file.getFile(), Sketch.DEFAULT_SKETCH_EXTENSION);
351-
file.renameTo(newName);
351+
file.renameTo(newName.getName());
352352
}
353353
}
354354
}

arduino-core/src/processing/app/Sketch.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -277,27 +277,9 @@ public void renameTo(File newFolder) throws IOException {
277277
file.renamedTo(new File(newFolder, file.getFileName()));
278278

279279
// And finally, rename the primary file
280-
if (!getPrimaryFile().renameTo(newPrimary))
281-
throw new IOException(tr("Failed to rename primary sketch file"));
280+
getPrimaryFile().renameTo(newPrimary.getName());
282281
}
283282

284-
/**
285-
* Rename the given file to get the given name.
286-
*
287-
* @param sketchfile
288-
* The SketchFile to be renamed.
289-
* @param newName
290-
* The new name, including extension, excluding directory
291-
* name.
292-
* @throws IOException
293-
* When a problem occurs, or is expected to occur. The error
294-
* message should be already translated.
295-
*/
296-
public void renameFileTo(SketchFile sketchfile, String newName) throws IOException {
297-
File newFile = new File(folder, newName);
298-
checkNewFilename(newFile);
299-
sketchfile.renameTo(newFile);
300-
}
301283

302284
public SketchFile addFile(String newName) throws IOException {
303285
// Check the name will not cause any conflicts

arduino-core/src/processing/app/SketchFile.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,25 @@ private boolean deleteCompiledFilesFrom(Path tempBuildFolder) throws IOException
160160
return true;
161161
}
162162

163-
protected boolean renameTo(File what) {
164-
boolean success = file.renameTo(what);
165-
if (success)
166-
renamedTo(what);
167-
return success;
163+
/**
164+
* Rename the given file to get the given name.
165+
*
166+
* @param newName
167+
* The new name, including extension, excluding directory
168+
* name.
169+
* @throws IOException
170+
* When a problem occurs, or is expected to occur. The error
171+
* message should be already translated.
172+
*/
173+
public void renameTo(String newName) throws IOException {
174+
File newFile = new File(file.getParentFile(), newName);
175+
sketch.checkNewFilename(newFile);
176+
if (file.renameTo(newFile)) {
177+
renamedTo(newFile);
178+
} else {
179+
String msg = I18n.format(tr("Failed to rename \"{0}\" to \"{1}\""), file.getName(), newName);
180+
throw new IOException(msg);
181+
}
168182
}
169183

170184
/**

0 commit comments

Comments
 (0)