#48: Remove base URL from filename and instead keep it as a BaseUrl in ReleaseEntry

This commit is contained in:
Christian Rondeau
2014-09-27 16:39:27 -04:00
parent b9a28954db
commit 1c98657bdb
3 changed files with 43 additions and 17 deletions

View File

@@ -30,23 +30,32 @@ namespace Squirrel
public class ReleaseEntry : IEnableLogger, IReleaseEntry
{
[DataMember] public string SHA1 { get; protected set; }
[DataMember] public string BaseUrl { get; protected set; }
[DataMember] public string Filename { get; protected set; }
[DataMember] public long Filesize { get; protected set; }
[DataMember] public bool IsDelta { get; protected set; }
protected ReleaseEntry(string sha1, string filename, long filesize, bool isDelta)
protected ReleaseEntry(string sha1, string filename, long filesize, bool isDelta, string baseUrl = null)
{
Contract.Requires(sha1 != null && sha1.Length == 40);
Contract.Requires(filename != null);
Contract.Requires(filename.Contains(Path.DirectorySeparatorChar) == false);
Contract.Requires(filesize > 0);
SHA1 = sha1; Filename = filename; Filesize = filesize; IsDelta = isDelta;
if(baseUrl != null)
{
if (Uri.IsWellFormedUriString(baseUrl, UriKind.Absolute) && !baseUrl.EndsWith("/"))
baseUrl += "/";
else if (!baseUrl.EndsWith(Path.DirectorySeparatorChar.ToString()))
baseUrl += Path.DirectorySeparatorChar;
}
SHA1 = sha1; BaseUrl = baseUrl; Filename = filename; Filesize = filesize; IsDelta = isDelta;
}
[IgnoreDataMember]
public string EntryAsString {
get { return String.Format("{0} {1} {2}", SHA1, Filename, Filesize); }
get { return String.Format("{0} {1}{2} {3}", SHA1, BaseUrl, Filename, Filesize); }
}
[IgnoreDataMember]
@@ -89,9 +98,25 @@ namespace Squirrel
throw new Exception("Invalid release entry: " + entry);
}
string filename = m.Groups[2].Value;
// Extract the filename if a path or a uri is provided
string baseUrl = null;
if(Uri.IsWellFormedUriString(filename, UriKind.Absolute))
{
var indexOfLastPathSeparator = filename.LastIndexOf("/");
baseUrl = filename.Substring(0, indexOfLastPathSeparator);
filename = filename.Substring(indexOfLastPathSeparator + 1);
}
else if (filename.IndexOf(Path.DirectorySeparatorChar) > -1)
{
baseUrl = Path.GetDirectoryName(filename);
filename = Path.GetFileName(filename);
}
long size = Int64.Parse(m.Groups[3].Value);
bool isDelta = filenameIsDeltaFile(m.Groups[2].Value);
return new ReleaseEntry(m.Groups[1].Value, m.Groups[2].Value, size, isDelta);
bool isDelta = filenameIsDeltaFile(filename);
return new ReleaseEntry(m.Groups[1].Value, filename, size, isDelta, baseUrl);
}
public static IEnumerable<ReleaseEntry> ParseReleaseFile(string fileContents)
@@ -134,19 +159,19 @@ namespace Squirrel
}
}
public static ReleaseEntry GenerateFromFile(Stream file, string filename)
public static ReleaseEntry GenerateFromFile(Stream file, string filename, string baseUrl = null)
{
Contract.Requires(file != null && file.CanRead);
Contract.Requires(!String.IsNullOrEmpty(filename));
var hash = Utility.CalculateStreamSHA1(file);
return new ReleaseEntry(hash, filename, file.Length, filenameIsDeltaFile(filename));
return new ReleaseEntry(hash, filename, file.Length, filenameIsDeltaFile(filename), baseUrl);
}
public static ReleaseEntry GenerateFromFile(string path)
public static ReleaseEntry GenerateFromFile(string path, string baseUrl = null)
{
using (var inf = File.OpenRead(path)) {
return GenerateFromFile(inf, Path.GetFileName(path));
return GenerateFromFile(inf, Path.GetFileName(path), baseUrl);
}
}

View File

@@ -260,13 +260,13 @@ namespace Squirrel.Update
var dp = deltaBuilder.CreateDeltaPackage(prev, rp,
Path.Combine(di.FullName, rp.SuggestedReleaseFileName.Replace("full", "delta")));
processed.Add(dp.InputPackageFile);
processed.Insert(0, dp.InputPackageFile);
}
}
foreach (var file in toProcess) { File.Delete(file.FullName); }
var releaseEntries = previousReleases.Concat(processed.Select(packageFilename => ReleaseEntry.GenerateFromFile(packageFilename)));
var releaseEntries = previousReleases.Concat(processed.Select(packageFilename => ReleaseEntry.GenerateFromFile(packageFilename, baseUrl)));
ReleaseEntry.WriteReleaseFile(releaseEntries, releaseFilePath);
var targetSetupExe = Path.Combine(di.FullName, "Setup.exe");

View File

@@ -10,16 +10,17 @@ namespace Squirrel.Tests.Core
public class ReleaseEntryTests
{
[Theory]
[InlineData(@"94689fede03fed7ab59c24337673a27837f0c3ec MyCoolApp-1.0.nupkg 1004502", "MyCoolApp-1.0.nupkg", 1004502)]
[InlineData(@"3a2eadd15dd984e4559f2b4d790ec8badaeb6a39 MyCoolApp-1.1.nupkg 1040561", "MyCoolApp-1.1.nupkg", 1040561)]
[InlineData(@"14db31d2647c6d2284882a2e101924a9c409ee67 MyCoolApp-1.1.nupkg.delta 80396", "MyCoolApp-1.1.nupkg.delta", 80396)]
[InlineData(@"0000000000000000000000000000000000000000 http://test.org/Folder/MyCoolApp-1.2.nupkg 1231953", "http://test.org/Folder/MyCoolApp-1.2.nupkg", 1231953)]
[InlineData(@"0000000000000000000000000000000000000000 \\Somewhere\NetworkShare\MyCoolApp-1.3.nupkg.delta 0", @"\\Somewhere\NetworkShare\MyCoolApp-1.3.nupkg.delta", 0)]
public void ParseValidReleaseEntryLines(string releaseEntry, string fileName, long fileSize)
[InlineData(@"94689fede03fed7ab59c24337673a27837f0c3ec MyCoolApp-1.0.nupkg 1004502", "MyCoolApp-1.0.nupkg", 1004502, null)]
[InlineData(@"3a2eadd15dd984e4559f2b4d790ec8badaeb6a39 MyCoolApp-1.1.nupkg 1040561", "MyCoolApp-1.1.nupkg", 1040561, null)]
[InlineData(@"14db31d2647c6d2284882a2e101924a9c409ee67 MyCoolApp-1.1.nupkg.delta 80396", "MyCoolApp-1.1.nupkg.delta", 80396, null)]
[InlineData(@"0000000000000000000000000000000000000000 http://test.org/Folder/MyCoolApp-1.2.nupkg 1231953", "MyCoolApp-1.2.nupkg", 1231953, "http://test.org/Folder/")]
[InlineData(@"0000000000000000000000000000000000000000 \\Somewhere\NetworkShare\MyCoolApp-1.3.nupkg.delta 0", @"MyCoolApp-1.3.nupkg.delta", 0, @"\\Somewhere\NetworkShare\")]
public void ParseValidReleaseEntryLines(string releaseEntry, string fileName, long fileSize, string baseUrl)
{
var fixture = ReleaseEntry.ParseReleaseEntry(releaseEntry);
Assert.Equal(fileName, fixture.Filename);
Assert.Equal(fileSize, fixture.Filesize);
Assert.Equal(baseUrl, fixture.BaseUrl);
}
[Theory]