Skip to content

Commit 909ecb8

Browse files
authored
Merge pull request #3462 from pyrevitlabs/cursor/fix-clone-already-exists-99fc
fix(cli): allow clone registration after failed install rollback
2 parents fc580ef + f547cf0 commit 909ecb8

2 files changed

Lines changed: 39 additions & 10 deletions

File tree

dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitClones.cs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,20 @@ public static void RegisterClone(string cloneName, string repoPath, bool forceUp
4646

4747
var registeredClones = GetRegisteredClones();
4848

49-
if (forceUpdate && registeredClones.Contains(clone))
50-
registeredClones.Remove(clone);
51-
52-
if (!registeredClones.Contains(clone))
49+
if (forceUpdate)
5350
{
54-
registeredClones.Add(clone);
55-
SaveRegisteredClones(registeredClones);
51+
registeredClones.RemoveAll(
52+
registeredClone => registeredClone.Matches(cloneName) || registeredClone.Equals(clone));
5653
}
57-
else
54+
else if (registeredClones.Contains(clone))
55+
{
5856
throw new PyRevitException(
5957
string.Format("Clone with repo path \"{0}\" already exists.", clone.ClonePath)
6058
);
59+
}
60+
61+
registeredClones.Add(clone);
62+
SaveRegisteredClones(registeredClones);
6163
}
6264

6365
// renames a clone in a configs
@@ -241,6 +243,9 @@ public static void DeployFromRepo(string cloneName,
241243
// make sure destPath exists
242244
CommonUtils.EnsurePath(destPath);
243245

246+
// Drop stale registrations before cloning so a retried install can register cleanly.
247+
PruneStaleCloneRegistrations();
248+
244249
// check existing destination path
245250
if (CommonUtils.VerifyPath(destPath))
246251
{
@@ -273,7 +278,7 @@ public static void DeployFromRepo(string cloneName,
273278
PyRevitClone.VerifyCloneValidity(clonedPath);
274279
InstallBinariesForRepoClone(clonedPath, repoSourcePath, BinArtifactInstallMode.Clone);
275280
logger.Debug("Clone successful \"{0}\"", clonedPath);
276-
RegisterClone(cloneName, clonedPath);
281+
RegisterClone(cloneName, clonedPath, forceUpdate: true);
277282
}
278283
catch (pyRevitBinArtifactNotFoundException ex)
279284
{
@@ -287,6 +292,7 @@ public static void DeployFromRepo(string cloneName,
287292
{
288293
logger.Debug(string.Format("Exception occured after clone complete. Deleting clone \"{0}\" | {1}",
289294
clonedPath, ex.Message));
295+
UnregisterCloneAtPath(clonedPath);
290296
try
291297
{
292298
CommonUtils.DeleteDirectory(clonedPath);
@@ -346,6 +352,9 @@ public static void DeployFromImage(string cloneName,
346352

347353
logger.Debug("Destination path determined as \"{0}\"", destPath);
348354

355+
// Drop stale registrations before deploying so a retried install can register cleanly.
356+
PruneStaleCloneRegistrations();
357+
349358
// process source
350359
// decide to download if source is a url
351360
if (imageSource.IsValidHttpUrl())
@@ -463,6 +472,7 @@ public static void DeployFromImage(string cloneName,
463472
{
464473
logger.Debug(string.Format("Exception occured after clone from image complete. " +
465474
"Deleting clone \"{0}\" | {1}", destPath, ex.Message));
475+
UnregisterCloneAtPath(destPath);
466476
try
467477
{
468478
CommonUtils.DeleteDirectory(destPath);
@@ -506,12 +516,13 @@ private static void VerifyAndRegisterClone(string cloneName, string clonePath)
506516
{
507517
PyRevitClone.VerifyCloneValidity(clonePath);
508518
logger.Debug("Clone successful \"{0}\"", clonePath);
509-
RegisterClone(cloneName, clonePath);
519+
RegisterClone(cloneName, clonePath, forceUpdate: true);
510520
}
511521
catch (Exception ex)
512522
{
513523
logger.Debug(string.Format("Exception occured after clone complete. Deleting clone \"{0}\" | {1}",
514524
clonePath, ex.Message));
525+
UnregisterCloneAtPath(clonePath);
515526
try
516527
{
517528
CommonUtils.DeleteDirectory(clonePath);
@@ -731,6 +742,24 @@ private static void InstallBinariesForImageClone(
731742
}
732743
}
733744

745+
private static void PruneStaleCloneRegistrations()
746+
{
747+
GetRegisteredClones();
748+
}
749+
750+
private static void UnregisterCloneAtPath(string clonePath)
751+
{
752+
if (string.IsNullOrWhiteSpace(clonePath))
753+
return;
754+
755+
var normalizedPath = clonePath.NormalizeAsPath();
756+
foreach (var clone in GetRegisteredClones().ToList())
757+
{
758+
if (clone.ClonePath.Equals(normalizedPath, StringComparison.OrdinalIgnoreCase))
759+
UnregisterClone(clone);
760+
}
761+
}
762+
734763
private static void WarnIfBinEnginesMissing(string clonePath) {
735764
var netfxEngines = Path.Combine(clonePath, "bin", "netfx", "engines");
736765
var netcoreEngines = Path.Combine(clonePath, "bin", "netcore", "engines");

dev/pyRevitLoader/pyRevitExtensionParserTester/ExtensionAuthUsersTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void ResetParserConfig()
1919
ClearAllCaches();
2020
}
2121

22-
private static void UseTestPyRevitConfig(string iniContent)
22+
private void UseTestPyRevitConfig(string iniContent)
2323
{
2424
var configPath = Path.Combine(TestTempDir, "pyRevit_config.ini");
2525
File.WriteAllText(configPath, iniContent);

0 commit comments

Comments
 (0)