If a local file is being opened, now the function tries to convert
the path into a URI. This seems to be a more reliable way to open file
on Windows 10 that has a more strict permission policy on cmd.exe.
Previously the CRC error was quite annoying to recover because
the user needed to manually delete the corrupted file from the
staging folder (without knowing the exact path of the file to
remove).
Now the IDE tries autonomously to resolve the situation by
removing the file and downloading it again.
Fixes#5394#4303
This regression originates from:
8725bb1e Clean up sketch loading
before this commit the sketch name sanitization was made on the sketch
name without the extension.
After 8725bb1e instead the name sanitization is made on the filename, so
including the ".ino" extension.
This lead to a weird corner case, caused by the limit of 63 characters
on the sketch name: before 8725bb1e it would be possible to save a sketch
with a name of exactly 63 characters, but after 8725bb1e this sketch will
suddenly becomes invalid becuase the 63 chars name + extension would exceed
the 63 characters limit.
This commit fix this regression.
Fix#5431
Previously, this used a hash of the sketch filename, so the same build
path would be generated for the same sketch between multiple
compilations. Now that the build path is stored, this requirement has
disappeared, so a random filename can be generated again. While here,
this commit also changes the prefix from "build" to "arduino_build_",
which makes it a bit more clear what the directory's purpose is.
Previously, everywhere where it was needed, the path was requested from
BaseNoGui. Because the path is based on a hash of the sketch filename,
every caller would get the same path for the same sketch.
However, it makes more sense to store the path used for a given sketch
inside the Sketch object. This prevents having to pass around or
regenerate the build path everywhere, and no longer requires the build
path to be deterministic (though it still is in this commit).
This allows removing some methods and constructors of which two versions
were available - one with a build path argument and one without.
Previously, callers of `SketchFile.delete()` would also call
`Sketch.removeFile()`, but letting SketchFile handle this is more
robust.
This is possible now that SketchFile keeps a reference to Sketch and
makes updating the Sketch file list less fragile.
Eventually this might be further decoupled by letting SketchFile
broadcast a "deleted" event instead.
This isn't much code, but it makes deletion more consistent with
renaming and saving with the SketchController handling the UI part and
Sketch actually doing the delete.
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.
These methods shouldn't really be in Base (or BaseNoGui, which did the
actual work), especially since there is already a
`FileUtils.recursiveDelete()` which just does the same thing. This
commit removes the code from Base and BaseNoGui and instead uses the
method from FileUtils.
There is one difference between these methods: the Base methods did not
delete files if the "compiler.save_build_files" preference was set.
However, the Base methods were only used when deleting a sketch, or
deleting an existing folder before overwriting it on save as, so this
preference didn't actually do what it was supposed to anyway, so
dropping it shouldn't be a problem.
This commits replaces a significant part of the code handling these
features. A lot of responsibilities are moved from SketchController to
Sketch, though the code involved is rewritten mostly.
Most of the handling now happens inside Sketch, including various checks
against the new filename. Basically SketchController processes the user
input to decide what needs to be done, and Sketch checks if it can be
done and does it.
If problems occur, an IOException is thrown, using a translated error
message that is shown by SketchController as-is. This might not be the
best way to transfer error messages (regular IOExceptions might contain
less-friendly messages), so this might need further improvement later.
In addition to moving around code and responsibilities, this code also
changes behaviour in some places:
- Because Sketch and SketchFile are now in control of renames and
saves, they can update their internal state after a rename. This
removes the need for reloading the entire sketch after a rename or
save as and allows `Editor.handleOpenUnchecked()` to be removed.
- When renaming the entire sketch, all files used to be saved before
renaming, since the sketch would be re-opened after renaming. Since
the re-opening no longer happens, there is no longer a need to save
the sketch, so any unsaved changes remain unsaved in the editor after
renaming the sketch.
- When renaming or adding new files, duplicate filenames are detected.
Initially, this happened case sensitively, but it was later changed to
use case insensitive matching to prevent problems on Windows (where
filenames cannot differ in just case). To prevent complexity, this
did not distinguish between systems. In commit 5fbf9621f6 (Sketch
rename: allowig a case change rename if NOT on windows), the
intention was to only do case insensitive checking on Windows, but it
effectively disabled all checking on other systems, making the check
not catch duplicate filenames at all.
With this commit, all these checks are done using `File.equals()`
instead of comparing strings, which is already aware of the case
sensitivity of the platform and should act accordingly.
- Some error messages were changed.
- When adding a file, an empty file is not created directly, but only a
SketchFile and EditorTab is added. When the sketch is saved, the file
is created.
- When importing a file that already exists (thus overwriting it),
instead of replacing the SketchFile instance, this just lets the
EditorTab reload its contents. This was broken since the introduction
of EditorTab. The file would be replaced, but not this was not
reflected in the editor, which is now fixed. This change allows
`Sketch.replaceFile()` to be removed.
- When importing a file that does not exist yet (thus adding it), a tab
is now also added for it (in addition to a SketchFile). This was
broken since the introduction of EditorTab, and would result in the
file being added, but not shown in the editor.
This commit adds a `Sketch.renameFileTo()` method, to rename a single
file within the sketch. It would be better to integrate its contents
into `Sketch.renameTo()`, but that does not have access to the `Sketch`
instance it is contained in. This will be changed in a future commit.
This makes a few related changes:
- `FileUtils.replaceExtension()` is introduced to handle replacing the
.pde extension with .ino.
- Instead of iterating .pde files on disk, this iterates SketchFiles in
memory, saving another lookup from filename -> SketchFile later.
- `SketchController.renameCodeToInoExtension()` is removed. Now it no
longer needs to look up the SketchFile and FileUtils handles the
extension replacement, this method did not have any reason to exist
anymore.
- Instead of hardcoding the .pde extension, a new
Sketch.OLD_SKETCH_EXTENSIONS constant is introduced.
Sketch already stores the sketch folder, and the sketch name should be
identical to the folder name. In the case where the filename passed to
the sketch constructor is not the primary .ino file (named after the
sketch), this will slightly change behaviour. However, the calling code
should prevent this from happening, and with the old code, some internal
assumptions were probably violated, so this changes makes handling this
situation a bit more robust.
Since the actual filename passed to Sketch is no longer used, it is no
longer required to pass the name of the primary .ino file. At some
point, the constructor should probably be changed to accept a folder
name instead of a filename, but that would require a lot of changes
to trace this back through the code, so this is something for later.
Keeping filenames as File objects for as long as possible is generally a
good idea and this removes a dependency on `Sketch.getMainFilePath()`,
so it can be removed later.
Previously, it returned a File object, which the Sketch separately
stored from the primary SketchFile. By letting it just return the
SketchFile, and let callers query that for the filename, Sketch does not
need to store the File object itself and there is less chance of info
getting out of sync.
Instead, just the File object when requested. It is not used during
normal operation (just when adding files, or using save as), so no point
in already creating the object in the constructor.
That name more accurately reflects its purpose: It represents a single
file within a sketch. This just updates the class name and variable
names referring to these objects and some comments, so no behaviour
should change.
Previously, the index of the SketchCode instance in the list kept by
Sketch was kept, which isn't really robust.
With this change, Sketch.indexOfCode is no longer needed and is removed.
For determining if the current file was a sketch file, it previously
(indirectly) used a hardcoded "ino" comparison. Now, it uses
`SKETCH_EXTENSIONS` so it also applies to .pde files and the hardcoded
"ino" (and the methods leading up to it) can be removed.
This lets it use FileUtils.splitFilename and reference Sketch.EXTENSIONS
and the new Sketch.DEFAULT_SKETCH_EXTENSION directly, allowing to remove
a few helper functions.