Skip to content

Commit

Permalink
Fixed the mess I made in ModuleInstaller
Browse files Browse the repository at this point in the history
  • Loading branch information
leila committed Nov 8, 2014
1 parent 2513936 commit b593f79
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
23 changes: 21 additions & 2 deletions CKAN/CKAN/KSPPathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,34 @@ public static string SteamPath()
return null;
}

/// <summary>
/// Normalizes the path by replace any \ with / and removing any trailing slash.
/// </summary>
/// <returns>The normalized path.</returns>
/// <param name="path">The path to normalize.</param>
public static string NormalizePath(string path)
{
return path.Replace('\\', '/').TrimEnd('/');
}

/// <summary>
/// Gets the last path element. Ex: /a/b/c returns c
/// </summary>
/// <returns>The last path element.</returns>
/// <param name="path">The path to process.</param>
public static string GetLastPathElement(string path)
{
// TODO should we really use ignore case?
return Regex.Replace(NormalizePath(path), @"^.*/", "", RegexOptions.IgnoreCase);
return Regex.Replace(NormalizePath(path), @"^.*/", "");
}

/// <summary>
/// Gets the leading path elements. Ex: /a/b/c returns /a/b
/// </summary>
/// <returns>The leading path elements.</returns>
/// <param name="path">The path to process.</param>
public static string GetLeadingPathElements(string path)
{
return Regex.Replace(NormalizePath(path), @"(^.*)/.+", "$1");
}
}
}
Expand Down
37 changes: 28 additions & 9 deletions CKAN/CKAN/ModuleInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -599,16 +599,8 @@ internal static List<InstallableFile> FindInstallableFiles(ModuleInstallDescript
// Get the full name of the file.
string outputName = entry.Name;

// Strip off everything up to the last path element
outputName = KSPPathUtils.GetLastPathElement(outputName);

string full_path = Path.Combine(installDir, outputName);

// Make the path pretty, and of course the prettiest paths use Unix separators. ;)
full_path = KSPPathUtils.NormalizePath(full_path);

// Update our file info with the install location
file_info.destination = full_path;
file_info.destination = TransformOutputName(stanza.file, outputName, installDir);
}

files.Add(file_info);
Expand All @@ -624,6 +616,33 @@ internal static List<InstallableFile> FindInstallableFiles(ModuleInstallDescript
return files;
}

/// <summary>
/// Transforms the name of the output. This will strip the leading directories from the stanza file from
/// output name and then combine it with the installDir.
/// EX: "kOS-1.1/GameData/kOS", "kOS-1.1/GameData/kOS/Plugins/kOS.dll", "GameData" will be transformed
/// to "GameData/kOS/Plugins/kOS.dll"
/// </summary>
/// <returns>The output name.</returns>
/// <param name="file">The file directive of the stanza.</param>
/// <param name="outputName">The name of the file to transform.</param>
/// <param name="installDir">The installation dir where the file should end up with.</param>
internal static string TransformOutputName(string file, string outputName, string installDir)
{
string leadingPathToRemove = KSPPathUtils.GetLeadingPathElements(file);
string leadingRegEx = "^" + Regex.Escape(leadingPathToRemove) + "/";
if (!Regex.IsMatch(outputName, leadingRegEx))
{
throw new BadMetadataKraken(null, "output file name not matching leading path of stanza.file");
}
// Strip off leading path name
outputName = Regex.Replace(outputName, leadingRegEx, "");
string full_path = Path.Combine(installDir, outputName);
// Make the path pretty, and of course the prettiest paths use Unix separators. ;)
full_path = KSPPathUtils.NormalizePath(full_path);

return full_path;
}

/// <summary>
/// Given a module and an open zipfile, return all the files that would be installed
/// for this module.
Expand Down
12 changes: 12 additions & 0 deletions CKAN/Tests/CKAN/KSPPathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ public void GetLastPathElement()
Assert.AreEqual("c", CKAN.KSPPathUtils.GetLastPathElement("\\a/b\\c"), "With mixed slashes");
Assert.AreEqual("c", CKAN.KSPPathUtils.GetLastPathElement("a/b\\c"), "No starting slash");
Assert.AreEqual("c", CKAN.KSPPathUtils.GetLastPathElement("\\a/b\\c\\"), "Trailing slash");

Assert.AreEqual("kOS", CKAN.KSPPathUtils.GetLastPathElement("GameData/kOS"), "Real world test");
}

[Test()]
public void GetLeadingPathElements()
{
Assert.AreEqual("/a/b", CKAN.KSPPathUtils.GetLeadingPathElements("/a/b/c"), "Simple case");
Assert.AreEqual("/a/b", CKAN.KSPPathUtils.GetLeadingPathElements("\\a\\b\\c"), "With other slashes");
Assert.AreEqual("/a/b", CKAN.KSPPathUtils.GetLeadingPathElements("\\a/b\\c"), "With mixed slashes");
Assert.AreEqual("a/b", CKAN.KSPPathUtils.GetLeadingPathElements("a/b\\c"), "No starting slash");
Assert.AreEqual("/a/b", CKAN.KSPPathUtils.GetLeadingPathElements("\\a/b\\c\\"), "Trailing slash");
}

}
Expand Down
7 changes: 7 additions & 0 deletions CKAN/Tests/CKAN/ModuleInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ public void CorruptZip_242()
}
}

[Test()]
public void TransformOutputName()
{
Assert.AreEqual("GameData/kOS/Plugins/kOS.dll", CKAN.ModuleInstaller.TransformOutputName("GameData/kOS", "GameData/kOS/Plugins/kOS.dll", "GameData"));
Assert.AreEqual("GameData/kOS/Plugins/kOS.dll", CKAN.ModuleInstaller.TransformOutputName("kOS-1.1/GameData/kOS", "kOS-1.1/GameData/kOS/Plugins/kOS.dll", "GameData"));
}

private static string CopyDogeFromZip()
{
string dogezip = Tests.TestData.DogeCoinFlagZip();
Expand Down

0 comments on commit b593f79

Please sign in to comment.