Skip to content

Commit

Permalink
fix: Install/uninstall package via UPMClient instead of updating mani…
Browse files Browse the repository at this point in the history
…fest.json directly
  • Loading branch information
mob-sakai committed May 18, 2022
1 parent b642b01 commit 69c6021
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
30 changes: 6 additions & 24 deletions Editor/Coffee.UpmGitExtension/GitPackageDataBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,15 @@ internal class GitPackageDatabase : ScriptableSingleton<GitPackageDatabase>
// public static event Action OnChangedPackages;

public static void Install(string packageId)
{
_upmClient.AddByUrl(packageId);
}

public static void Uninstall(string packageId)
{
var i = packageId.IndexOf('@');
var packageName = packageId.Substring(0, i);
var url = packageId.Substring(i + 1);
UpdateJson("Packages/manifest.json", jsonDic =>
{
// Add to dependencies.
var dependencies = jsonDic["dependencies"] as Dictionary<string, object>;
dependencies[packageName] = url;
});
_upmClient.RemoveByName(packageName);
}

public static IEnumerable<UpmPackage> GetUpmPackages()
Expand Down Expand Up @@ -193,23 +192,6 @@ public static IEnumerable<UpmPackageVersionEx> GetAvailablePackageVersions(strin
private static bool _enablePreReleasePackages => _settings.enablePreviewPackages;
#endif

private static void UpdateJson(string path, Action<Dictionary<string, object>> action)
{
if (!File.Exists(path)) return;

try
{
var jsonDic = Json.Deserialize(File.ReadAllText(path)) as Dictionary<string, object>;
action?.Invoke(jsonDic);
File.WriteAllText(path, Json.Serialize(jsonDic, true));
EditorApplication.delayCall += () => UnityEditor.PackageManager.Client.Resolve();
}
catch (Exception e)
{
UnityEngine.Debug.LogException(e);
}
}

private static void RequestUpdateGitPackageVersions()
{
EditorApplication.delayCall -= UpdateGitPackageVersions;
Expand Down
14 changes: 14 additions & 0 deletions Editor/Coffee.UpmGitExtension/UI/GitPackageInstallationWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,24 @@ public GitPackageInstallationWindow()
root.Add(new SearchResultListView(_repoUrlText, () => GitPackageDatabase.GetCachedRepositoryUrls()));

OnClick_Close();

//
GitPackageDatabase._upmClient.onAddOperation += op => {
_loadingSpinner.Start();
_rootContainer.SetEnabled(false);

op.onOperationFinalized += _ =>
{
_loadingSpinner.Stop();
_rootContainer.SetEnabled(true);
OnClick_Close();
};
};
}

public void Open()
{
_loadingSpinner.Stop();
UIUtils.SetElementDisplay(this, true);
SetState(State.None);

Expand Down

0 comments on commit 69c6021

Please sign in to comment.