Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add editor.delete_directory() #8473

Merged
merged 2 commits into from Jan 31, 2024
Merged

Conversation

astrochili
Copy link
Contributor

@astrochili astrochili commented Jan 30, 2024

Fixes #8472

Technical changes

This changeset adds a new function to editor scripts that allows the user to delete an existing directory. The function expects a resource path, i.e. a /-prefixed path from the root of the project, for example:

editor.delete_directory("/assets/generated")

@astrochili
Copy link
Contributor Author

astrochili commented Jan 30, 2024

I created it by looking at the PR #8434

Haven't figured out how to build the editor locally yet, but the code seems simple. Please correct me if something is wrong 🙈.

@astrochili astrochili changed the title Add editor.delete_directory() Add editor.delete_directory() Jan 30, 2024
@astrochili
Copy link
Contributor Author

defold/doc#383

@britzl britzl linked an issue Jan 30, 2024 that may be closed by this pull request
@britzl britzl requested a review from matgis January 30, 2024 11:23
@britzl
Copy link
Contributor

britzl commented Jan 30, 2024

Haven't figured out how to build the editor locally yet, but the code seems simple. Please correct me if something is wrong 🙈.

I think it's good if you try to set up the editor so that you can build it. Submitting PRs "in the blind" so to speak is not a good practice :-) What kind of problems did you have when trying to set everything up?

@AGulev
Copy link
Contributor

AGulev commented Jan 30, 2024

I've just tested it and it doesn't work:

ERROR:EXT: hook "on_target_terminated" in /hooks.editor_script failed:
ERROR:EXT: /hooks.editor_script:5 class sun.nio.fs.UnixPath cannot be cast to class java.io.File (sun.nio.fs.UnixPath and java.io.File are in module java.base of loader 'bootstrap')

@astrochili
Copy link
Contributor Author

astrochili commented Jan 30, 2024

Sorry for your time, yes, that was brave 🫣. Gone to build the editor...

@AGulev
Copy link
Contributor

AGulev commented Jan 30, 2024

Sorry for your time, yes, that was brave 🫣. Gone to build the editor...

We have #source-code channel in Discord. Feel free to ask your questions there. We will help you to build the engine (the first step) and the editor.

@matgis
Copy link
Contributor

matgis commented Jan 30, 2024

No need to build the engine if you're making a change that only affects the editor in isolation. The lein init archived-stable command will download the latest stable engine and Bob binaries for you so you can skip setting up the whole engine build environment. Just make sure to prefix your branch name with DEFEDIT- and target the editor-dev branch with your PR. If your PR is accepted, it will be merged into the next editor update, so no need to wait for the next stable Defold release.

More info about the lein init archived-stable command here:
https://github.com/defold/defold/blob/dev/editor/README_BUILD.md#build-using-existing-engine-and-tools

@AGulev
Copy link
Contributor

AGulev commented Jan 30, 2024

@matgis oh, I didn't know that. Nice!

@JCash
Copy link
Contributor

JCash commented Jan 30, 2024

Thanks for the PR!
I don't mean to pile on here, but perhaps @matgis could direct you in how to add a small test as well?

@matgis
Copy link
Contributor

matgis commented Jan 30, 2024

@JCash I would feel more justified in asking for a test if our code they based this on had tests. 😅 Personally I think we can add tests later.

@astrochili
Copy link
Contributor Author

astrochili commented Jan 30, 2024

More info about the lein init archived-stable command here:
https://github.com/defold/defold/blob/dev/editor/README_BUILD.md#build-using-existing-engine-and-tools

Thanks. I built the editor and fixed the code. Updated the commit, now directory deletion works 👍

But still not sure if the exceptions I added will work, or if any other checks are needed.

I have not been able to reproduce NoSuchFileException and NotDirectoryException. If there is no directory, nothing happens. If instead of a directory it is a file - the file will be deleted.

@astrochili
Copy link
Contributor Author

astrochili commented Jan 31, 2024

UPDATE:

  • Removed NoSuchFileException because it doesn't sense when deleting.
  • Fixed NotDirectoryException, now it throws an exception to avoid deleting the file. Because creating and deleting files still available with io.write and os.remove as I understand.

Now there are only issues of code reuse in editor-extensions.clj and consistency of function call chains inside fs.clj. But I'm not really good at this, first time writing in clojure.

@matgis
Copy link
Contributor

matgis commented Jan 31, 2024

@astrochili I added a commit with some additional protections against deleting anything below .git or .internal, as well as preventing you from deleting the project directory itself.

If it looks good to you, perhaps you could cherry-pick it into your PR?

f18dec0

@astrochili
Copy link
Contributor Author

astrochili commented Jan 31, 2024

@matgis Added a cherry-picked commit, looks good?

Copy link
Contributor

@matgis matgis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I'm going to merge it to editor-dev. Once CI completes, you should get it in the next editor update.

@matgis matgis added editor Issues related to the Defold editor editor scripts Issue related to editor scripts labels Jan 31, 2024
@matgis matgis merged commit 1300e83 into defold:editor-dev Jan 31, 2024
1 check passed
(if (.startsWith dir-path root-path)
(try
(fs/create-path-directories! dir-path)
(vreset! request-sync true)
nil
(catch FileAlreadyExistsException e
(throw (LuaError. (str "File already exists: " (.getMessage e)))))
(throw (LuaError. (str "Directory already exists: " (.getMessage e)))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fairly certain the error message should say "file already exists" and not "directory already exists". Directory already existing is not an error and should not throw exception. What should throw exception is if you wanted a directory at a certain path, and it turned out at that path there is a file instead, that's what the error it's about.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh really? I was thinking it's just a typo. So how to fix it back, a new PR? 🤔

matgis pushed a commit that referenced this pull request Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editor scripts Issue related to editor scripts editor Issues related to the Defold editor
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Add editor.delete_directory function
6 participants