Skip to content

Commit

Permalink
Split out new super-pretty ConflictsKraken
Browse files Browse the repository at this point in the history
  • Loading branch information
politas committed Jan 5, 2018
1 parent aa88b50 commit 1bf5b7f
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 11 deletions.
7 changes: 7 additions & 0 deletions Cmdline/Action/Install.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ public int RunCommand(CKAN.KSP ksp, object raw_options)
user.RaiseMessage("Install canceled. Your files have been returned to their initial state.");
return Exit.ERROR;
}
catch (ConflictsKraken ex)
{
// The prettiest Kraken formats itself for us.
user.RaiseMessage(ex.ConflictsPretty);
user.RaiseMessage("Install canceled. Your files have been returned to their initial state.");
return Exit.ERROR;
}
catch (CancelledActionKraken)
{
user.RaiseMessage("Installation canceled at user request.");
Expand Down
17 changes: 17 additions & 0 deletions Cmdline/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,23 @@ private static int Scan(CKAN.KSP ksp_instance, IUser user, string next_command =
user.RaiseMessage("Proceeding with {0} in case it fixes it.\r\n", next_command);
}

return Exit.ERROR;
}
catch (ConflictsKraken kraken)
{

if (next_command == null)
{
user.RaiseError(kraken.ConflictsPretty);
user.RaiseError("The repo has not been saved.");
}
else
{
user.RaiseMessage("Preliminary scanning shows that the install is in a inconsistent state.");
user.RaiseMessage("Use ckan.exe scan for more details");
user.RaiseMessage("Proceeding with {0} in case it fixes it.\r\n", next_command);
}

return Exit.ERROR;
}
}
Expand Down
6 changes: 6 additions & 0 deletions Core/Net/Repo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ You should reinstall them in order to preserve consistency with the repository.
log.WarnFormat("Skipping installation of {0} due to relationship error.", changedIdentifier);
user.RaiseMessage("Skipping installation of {0} due to relationship error.", changedIdentifier);
}
// Thrown when a conflicts relationship is violated
catch (ConflictsKraken)
{
log.WarnFormat("Skipping installation of {0} due to relationship error.", changedIdentifier);
user.RaiseMessage("Skipping installation of {0} due to relationship error.", changedIdentifier);
}
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions Core/Relationships/RelationshipResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public static RelationshipResolverOptions DefaultOpts()
/// <param name="modules">Modules to attempt to install</param>
public void AddModulesToInstall(IEnumerable<CkanModule> modules)
{
var inconsistencies = new List<string>();
var newConflicts = new List<KeyValuePair<CkanModule, CkanModule>>();
//Count may need to do a full enumeration. Might as well convert to array
var ckan_modules = modules as CkanModule[] ?? modules.ToArray();
log.DebugFormat("Processing relationships for {0} modules", ckan_modules.Count());
Expand All @@ -219,13 +219,13 @@ public void AddModulesToInstall(IEnumerable<CkanModule> modules)
}
else
{
inconsistencies.Add(string.Format("{0} conflicts with {1}, can't install both.", module, listed_mod));
newConflicts.Add(new KeyValuePair<CkanModule, CkanModule>(module, listed_mod));
}
}
user_requested_mods.Add(module);
Add(module, new SelectionReason.UserRequested());
}
if (inconsistencies.Count() > 0) throw new InconsistentKraken(inconsistencies);
if (newConflicts.Count() > 0) throw new ConflictsKraken(newConflicts);
// Now that we've already pre-populated the modlist, we can resolve
// the rest of our dependencies.

Expand Down Expand Up @@ -430,8 +430,7 @@ private void ResolveStanza(IEnumerable<RelationshipDescriptor> stanza, Selection
}
else
{
throw new InconsistentKraken(string.Format("{0} conflicts with {1}, can't install both.", conflicting_mod,
candidate));
throw new ConflictsKraken(new KeyValuePair<CkanModule, CkanModule>(candidate, conflicting_mod));
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions Core/Types/Kraken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,45 @@ public override string ToString()
}
}

/// <summary>
/// Thrown if we find ourselves with multiple modules installed which conflict with each other.
/// </summary>
public class ConflictsKraken : Kraken
{
public List<KeyValuePair<CkanModule, CkanModule>> Conflicts = new List<KeyValuePair<CkanModule, CkanModule>>();

public string ConflictsPretty
{
get
{
string message = "The following mod conflicts were found:\r\n";
foreach (KeyValuePair<CkanModule, CkanModule> conflict in Conflicts)
{
message = string.Format("{0}\r\n * {1} conflicts with {2}, can't install both",
message, conflict.Key.name, conflict.Value.name);
}
return message;
}
}

public ConflictsKraken( List<KeyValuePair<CkanModule, CkanModule>> conflicts, Exception innerException = null)
: base(null, innerException)
{
this.Conflicts = conflicts;
}

public ConflictsKraken(KeyValuePair<CkanModule, CkanModule> conflict, Exception innerException = null)
: base(null, innerException)
{
this.Conflicts.Add(conflict);
}

public override string ToString()
{
return ConflictsPretty + StackTrace;
}
}

/// <summary>
/// The terrible state when a file exists when we expect it not to be there.
/// For example, when we install a mod, and it tries to overwrite a file from another mod.
Expand Down
6 changes: 6 additions & 0 deletions GUI/MainInstall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ private void InstallMods(object sender, DoWorkEventArgs e) // this probably need
GUI.user.RaiseMessage(ex.InconsistenciesPretty);
return;
}
catch (ConflictsKraken ex)
{
// The prettiest Kraken formats itself for us.
GUI.user.RaiseMessage(ex.ConflictsPretty);
return;
}
catch (CancelledActionKraken)
{
return;
Expand Down
5 changes: 5 additions & 0 deletions GUI/MainModList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ public async Task<IEnumerable<ModChange>> ComputeChangeSetFromModList(
user.RaiseError(k.InconsistenciesPretty);
return null;
}
catch (ConflictsKraken k)
{
user.RaiseError(k.ConflictsPretty);
return null;
}
catch (ModuleNotFoundKraken k)
{
//We shouldn't need this. However the relationship provider will throw TMPs with incompatible mods.
Expand Down
12 changes: 6 additions & 6 deletions Tests/Core/Relationships/RelationshipResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void Constructor_WithConflictingModules()
list.Add(mod_b.identifier);
AddToRegistry(mod_a, mod_b);

Assert.Throws<InconsistentKraken>(() => new RelationshipResolver(
Assert.Throws<ConflictsKraken>(() => new RelationshipResolver(
list,
options,
registry,
Expand Down Expand Up @@ -85,7 +85,7 @@ public void Constructor_WithConflictingModulesVersion_Throws()
list.Add(mod_b.identifier);
AddToRegistry(mod_a, mod_b);

Assert.Throws<InconsistentKraken>(() => new RelationshipResolver(
Assert.Throws<ConflictsKraken>(() => new RelationshipResolver(
list,
options,
registry,
Expand All @@ -109,7 +109,7 @@ public void Constructor_WithConflictingModulesVersionMin_Throws(string ver, stri
list.Add(mod_b.identifier);
AddToRegistry(mod_a, mod_b);

Assert.Throws<InconsistentKraken>(() => new RelationshipResolver(
Assert.Throws<ConflictsKraken>(() => new RelationshipResolver(
list,
options,
registry,
Expand All @@ -133,7 +133,7 @@ public void Constructor_WithConflictingModulesVersionMax_Throws(string ver, stri
list.Add(mod_b.identifier);
AddToRegistry(mod_a, mod_b);

Assert.Throws<InconsistentKraken>(() => new RelationshipResolver(
Assert.Throws<ConflictsKraken>(() => new RelationshipResolver(
list,
options,
registry,
Expand All @@ -158,7 +158,7 @@ public void Constructor_WithConflictingModulesVersionMinMax_Throws(string ver, s
list.Add(mod_b.identifier);
AddToRegistry(mod_a, mod_b);

Assert.Throws<InconsistentKraken>(() => new RelationshipResolver(
Assert.Throws<ConflictsKraken>(() => new RelationshipResolver(
list,
options,
registry,
Expand Down Expand Up @@ -383,7 +383,7 @@ public void Constructor_WithConflictingModulesInDependancies_ThrowUnderDefaultSe
list.Add(conflicts_with_dependant.identifier);
AddToRegistry(depender, dependant, conflicts_with_dependant);

Assert.Throws<InconsistentKraken>(() => new RelationshipResolver(
Assert.Throws<ConflictsKraken>(() => new RelationshipResolver(
list,
options,
registry,
Expand Down

0 comments on commit 1bf5b7f

Please sign in to comment.