Write new task for downloading dependencies

This commit is contained in:
Ted John 2016-12-16 22:41:11 +00:00
parent 066a9b12e2
commit 6abe5cf73a
3 changed files with 212 additions and 29 deletions

1
.gitignore vendored
View File

@ -14,6 +14,7 @@ distribution/windows/*.exe
# Build artifacts
artifacts
.cache
.dependencies
#lib
lib

View File

@ -67,6 +67,8 @@
<LibsVersionPath>$(LibsPath)libversion</LibsVersionPath>
<GtestVersion>1.8.0</GtestVersion>
<GtestUrl>https://github.com/google/googletest/archive/release-$(GtestVersion).zip</GtestUrl>
<TitleSequencesUrl>https://github.com/OpenRCT2/title-sequences/releases/download/v0.0.5/title-sequence-v0.0.5.zip</TitleSequencesUrl>
<TitleSequencesSha1>79ffb2585d12abcbfce205d7696e3472a504b005</TitleSequencesSha1>
</PropertyGroup>
<ItemGroup>
@ -221,37 +223,13 @@
StandardOutputImportance="low" />
</Target>
<PropertyGroup>
<SequencesUrl>https://github.com/OpenRCT2/title-sequences/releases/download/v0.0.5/title-sequence-v0.0.5.zip</SequencesUrl>
<SequencesPath>$(TargetDir)data\title</SequencesPath>
<SequencesZip>$(TargetDir)data\title\seqs.zip</SequencesZip>
<SequencesVersionPath>$(TargetDir)sequencesversion</SequencesVersionPath>
</PropertyGroup>
<Target Name="CheckTitleSequences" AfterTargets="Build">
<PropertyGroup>
<CurrentSequencesVersion Condition="Exists($(SequencesVersionPath))">$([System.IO.File]::ReadAllText($(SequencesVersionPath)).Trim())</CurrentSequencesVersion>
<UpdateSequences Condition="'$(CurrentSequencesVersion)'!='$(SequencesUrl)'">true</UpdateSequences>
</PropertyGroup>
<Message Condition="'$(UpdateSequences)'!='true'" Text="Sequences up to date" Importance="high" />
<Message Condition="'$(UpdateSequences)'=='true'" Text="Sequences out of date, updating..." Importance="high" />
<CallTarget Condition="'$(UpdateSequences)'=='true'" Targets="DownloadTitleSequences" />
</Target>
<!-- Target to download the title sequences -->
<Target Name="DownloadTitleSequences">
<!-- Clean sequences directory -->
<RemoveDir Directories="$(SequencesPath)" />
<MakeDir Directories="$(SequencesPath)" />
<Message Text="Downloading title sequences..." Importance="normal" />
<Exec Command="powershell -ExecutionPolicy Unrestricted -Command &quot;Invoke-WebRequest '$(SequencesUrl)' -OutFile '$(SequencesZip)'&quot;" StandardOutputImportance="low" />
<Unzip Input="$(SequencesZip)" OutputDirectory="$(SequencesPath)" />
<Delete Files="$(SequencesZip)" ContinueOnError="true" />
<!-- Update version file -->
<WriteLinesToFile Lines="$(SequencesUrl)" File="$(SequencesVersionPath)" Overwrite="true" />
<DownloadDependency Name="TitleSequences"
Url="$(TitleSequencesUrl)"
Sha1="$(TitleSequencesSha1)"
CheckFile="$(RootDir).dependencies"
OutputDirectory="$(TargetDir)data\title" />
</Target>
<!-- Target to sign OpenRCT2

View File

@ -106,6 +106,7 @@
</Task>
</UsingTask>
<!-- Unzip task -->
<UsingTask TaskName="Unzip"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
@ -123,4 +124,207 @@
</Code>
</Task>
</UsingTask>
<!-- DownloadDependency task -->
<UsingTask TaskName="DownloadDependency"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Name Required="true" ParameterType="System.String" />
<Url Required="true" ParameterType="System.String" />
<Sha1 Required="false" ParameterType="System.String" />
<CheckFile Required="false" ParameterType="System.String" />
<OutputDirectory Required="true" ParameterType="System.String" />
</ParameterGroup>
<Task>
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.IO.Compression"/>
<Using Namespace="System.Net"/>
<Using Namespace="System.Text"/>
<Using Namespace="Microsoft.Build.Framework"/>
<Using Namespace="Microsoft.Build.Utilities"/>
<Code Type="Method" Language="cs">
<![CDATA[
public override bool Execute()
{
if (!String.IsNullOrEmpty(CheckFile))
{
string checkSha1 = GetSha1FromCheckFile(CheckFile, Name);
if (String.Equals(checkSha1, Sha1, StringComparison.OrdinalIgnoreCase))
{
Log.LogMessage(MessageImportance.Normal, String.Format("{0} up to date", Name));
return true;
}
}
string tempFile = Path.GetTempFileName();
try
{
// Download the file
Log.LogMessage(MessageImportance.Normal, String.Format("Downloading '{0}'.", Url));
var client = new WebClient();
client.DownloadFile(Url, tempFile);
// Check the file matches
string actualSha1;
if (!CheckFileSha1(tempFile, Sha1, out actualSha1))
{
Log.LogError("Download file did not match expected SHA1\n expected: {0}\n actual: {1}", Sha1, actualSha1);
return false;
}
// Extract contents
Log.LogMessage(MessageImportance.Normal, String.Format("Extracting to '{0}'.", OutputDirectory));
if (!Directory.Exists(OutputDirectory))
{
Directory.CreateDirectory(OutputDirectory);
}
ExtractZip(tempFile, OutputDirectory, overwrite: true);
SetSha1InCheckFile(CheckFile, Name, Sha1);
}
catch (Exception ex)
{
Log.LogErrorFromException(ex, showStackTrace: false);
}
finally
{
try
{
File.Delete(tempFile);
}
catch
{
}
}
return true;
}
private string GetSha1FromCheckFile(string checkFile, string name)
{
string result = null;
try
{
if (File.Exists(checkFile))
{
string[] lines = File.ReadAllLines(checkFile);
string sha1;
GetCheckFileLineIndexSha1(lines, name, out sha1);
return sha1;
}
}
catch (Exception ex)
{
Log.LogWarningFromException(ex, showStackTrace: false);
}
return result;
}
private void SetSha1InCheckFile(string checkFile, string name, string sha1)
{
try
{
string newLine = String.Format("{0} = {1}", name, sha1.ToLower());
string[] lines = new string[0];
int lineIndex = -1;
if (File.Exists(checkFile))
{
lines = File.ReadAllLines(checkFile);
string oldsha1;
lineIndex = GetCheckFileLineIndexSha1(lines, name, out oldsha1);
}
if (lineIndex == -1)
{
if (lines.Length == 0 || lines[lines.Length - 1].Trim().Length > 0)
{
Array.Resize(ref lines, lines.Length + 1);
}
lineIndex = lines.Length - 1;
// End with new line
Array.Resize(ref lines, lines.Length + 1);
}
lines[lineIndex] = newLine;
File.WriteAllLines(checkFile, lines);
}
catch (Exception ex)
{
Log.LogWarningFromException(ex, showStackTrace: false);
}
}
private int GetCheckFileLineIndexSha1(string[] lines, string name, out string sha1)
{
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
string[] lineParts = line.Split('=');
if (lineParts.Length == 2)
{
string lineTag = lineParts[0].Trim();
string lineSha1 = lineParts[1].Trim();
if (lineTag == name)
{
sha1 = lineSha1;
return i;
}
}
}
sha1 = null;
return -1;
}
private bool CheckFileSha1(string file, string expectedSha1, out string actualSha1)
{
using (var fs = new FileStream(file, FileMode.Open))
{
var hasher = System.Security.Cryptography.SHA1.Create();
byte[] hash = hasher.ComputeHash(fs);
actualSha1 = BytesToHexString(hash);
if (String.Equals(actualSha1, expectedSha1, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
private string BytesToHexString(byte[] data)
{
var sb = new StringBuilder();
foreach (byte b in data)
{
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
private static void ExtractZip(string zipPath, string destinationDirectory, bool overwrite)
{
var archive = ZipFile.OpenRead(zipPath);
if (!overwrite)
{
archive.ExtractToDirectory(destinationDirectory);
return;
}
foreach (ZipArchiveEntry file in archive.Entries)
{
string fileName = Path.Combine(destinationDirectory, file.FullName);
if (file.Name == String.Empty)
{
string directory = Path.GetDirectoryName(fileName);
Directory.CreateDirectory(directory);
continue;
}
file.ExtractToFile(fileName, true);
}
}
]]>
</Code>
</Task>
</UsingTask>
</Project>