Fix package restore happening on every script recompilation in projects #424
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When NuGetForUnity is included in a user project, and they make changes to their scripts, Unity also reloads the editor scripts for NuGetForUnity, which will then retrigger the static NugetHelper constructor because it is marked as InitializeOnLoad. Because this constructor restores packages, a package restore is done every script reload. Usually this amounts to no actual change because the packages in question are already installed, but just checking one or two packages this way can already slow down the script recompilation process by several seconds, which quickly becomes tiresome.
This changes the restore to only happen once on project start-up, fixing the issue. There was also little benefit to the restore happening, as it didn't happen automatically when packages.config was modified, which would have been the most useful and already requires a manual reload.
Solution was proposed by @Shii2 in #407. I just formalized the change in a PR, also skipped doing I/O to check if the packages folder exists every time, and tested in a Unity 2020.3.20f1 project that it works as intended. Checks done: