Skip to content

Commit

Permalink
Hack a way around some serialization related issues
Browse files Browse the repository at this point in the history
The abstract class only wraps some common functionality on top of the IUpdateTask interface, a consumer can either derive from it or implement the interface himself.

Binary serialization saves us a lot of work, but makes implementing a task more of an hassle than it should be, so we might move to a more transient way in the future
  • Loading branch information
synhershko committed Jul 3, 2012
1 parent ed2b72c commit 9c499e8
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 61 deletions.
1 change: 1 addition & 0 deletions src/NAppUpdate.Framework/NAppUpdate.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="Tasks\IUpdateTask.cs" />
<Compile Include="Tasks\RegistryTask.cs" />
<Compile Include="Tasks\UpdateTaskAliasAttribute.cs" />
<Compile Include="Tasks\UpdateTaskBase.cs" />
<Compile Include="UpdateManager.cs" />
<Compile Include="Utils\FileChecksum.cs" />
<Compile Include="Utils\FileDownloader.cs" />
Expand Down
37 changes: 6 additions & 31 deletions src/NAppUpdate.Framework/Tasks/FileUpdateTask.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
using System;
using System.IO;
using NAppUpdate.Framework.Common;
using NAppUpdate.Framework.Conditions;

namespace NAppUpdate.Framework.Tasks
{
[Serializable]
[UpdateTaskAlias("fileUpdate")]
public class FileUpdateTask : IUpdateTask
public class FileUpdateTask : UpdateTaskBase
{
public FileUpdateTask()
{
ExecutionStatus = TaskExecutionStatus.Pending;
}

[NauField("localPath", "The local path of the file to update", true)]
public string LocalPath { get; set; }

Expand All @@ -30,26 +24,9 @@ public FileUpdateTask()
, false)]
public bool CanHotSwap { get; set; }

internal string tempFile;
private string destinationFile, backupFile;
private string destinationFile, backupFile, tempFile;

#region IUpdateTask Members

public string Description { get; set; }
public TaskExecutionStatus ExecutionStatus { get; set; }

[NonSerialized]
private BooleanCondition _updateConditions;
public BooleanCondition UpdateConditions
{
get { return _updateConditions ?? (_updateConditions = new BooleanCondition()); }
set { _updateConditions = value; }
}

[field:NonSerialized]
public event ReportProgressDelegate OnProgress;

public bool Prepare(Sources.IUpdateSource source)
public override bool Prepare(Sources.IUpdateSource source)
{
if (string.IsNullOrEmpty(LocalPath))
return true; // Errorneous case, but there's nothing to prepare to...
Expand All @@ -66,7 +43,7 @@ public bool Prepare(Sources.IUpdateSource source)
{
string tempFileLocal = Path.Combine(UpdateManager.Instance.Config.TempFolder, Guid.NewGuid().ToString());
if (!source.GetData(fileName, UpdateManager.Instance.BaseUrl,
p => OnProgress(p),
OnProgress,
ref tempFileLocal))
return false;

Expand All @@ -87,7 +64,7 @@ public bool Prepare(Sources.IUpdateSource source)
return tempFile != null;
}

public TaskExecutionStatus Execute(bool coldRun)
public override TaskExecutionStatus Execute(bool coldRun)
{
if (string.IsNullOrEmpty(LocalPath))
return TaskExecutionStatus.Successful;
Expand Down Expand Up @@ -141,7 +118,7 @@ public TaskExecutionStatus Execute(bool coldRun)
return TaskExecutionStatus.RequiresAppRestart;
}

public bool Rollback()
public override bool Rollback()
{
if (string.IsNullOrEmpty(destinationFile))
return true;
Expand All @@ -153,7 +130,5 @@ public bool Rollback()

return true;
}

#endregion
}
}
2 changes: 1 addition & 1 deletion src/NAppUpdate.Framework/Tasks/IUpdateTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IUpdateTask : INauFieldsHolder
string Description { get; set; }
BooleanCondition UpdateConditions { get; set; }
TaskExecutionStatus ExecutionStatus { get; set; }
event ReportProgressDelegate OnProgress;
event ReportProgressDelegate ProgressDelegate;

/// <summary>
/// Do all work, especially if it is lengthy, required to prepare the update task, except from
Expand Down
31 changes: 4 additions & 27 deletions src/NAppUpdate.Framework/Tasks/RegistryTask.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
using System;
using NAppUpdate.Framework.Common;
using Microsoft.Win32;
using NAppUpdate.Framework.Conditions;

namespace NAppUpdate.Framework.Tasks
{
[Serializable]
[UpdateTaskAlias("registryUpdate")]
public class RegistryTask : IUpdateTask
public class RegistryTask : UpdateTaskBase
{
public RegistryTask()
{
ExecutionStatus = TaskExecutionStatus.Pending;
}

[NauField("keyName", "The full path to the registry key", true)]
public string KeyName { get; set; }

Expand Down Expand Up @@ -50,28 +44,13 @@ protected object ValueToSet
}
private object originalValue;

#region IUpdateTask Members

public string Description { get; set; }
public TaskExecutionStatus ExecutionStatus { get; set; }

[NonSerialized]
private BooleanCondition _updateConditions;
public BooleanCondition UpdateConditions
{
get { return _updateConditions ?? (_updateConditions = new BooleanCondition()); }
set { _updateConditions = value; }
}

public event ReportProgressDelegate OnProgress;

public bool Prepare(Sources.IUpdateSource source)
public override bool Prepare(Sources.IUpdateSource source)
{
// No preparation required
return true;
}

public TaskExecutionStatus Execute(bool coldRun /* unused */)
public override TaskExecutionStatus Execute(bool coldRun /* unused */)
{
if (String.IsNullOrEmpty(KeyName) || String.IsNullOrEmpty(KeyValueName))
return ExecutionStatus = TaskExecutionStatus.Successful;
Expand All @@ -93,7 +72,7 @@ public TaskExecutionStatus Execute(bool coldRun /* unused */)
return ExecutionStatus = TaskExecutionStatus.Successful;
}

public bool Rollback()
public override bool Rollback()
{
try
{
Expand All @@ -102,7 +81,5 @@ public bool Rollback()
catch { return false; }
return true;
}

#endregion
}
}
35 changes: 35 additions & 0 deletions src/NAppUpdate.Framework/Tasks/UpdateTaskBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using NAppUpdate.Framework.Common;
using NAppUpdate.Framework.Conditions;
using NAppUpdate.Framework.Sources;

namespace NAppUpdate.Framework.Tasks
{
[Serializable]
public abstract class UpdateTaskBase : IUpdateTask
{
public string Description { get; set; }
public TaskExecutionStatus ExecutionStatus { get; set; }

[NonSerialized]
private BooleanCondition _updateConditions;
public BooleanCondition UpdateConditions
{
get { return _updateConditions ?? (_updateConditions = new BooleanCondition()); }
set { _updateConditions = value; }
}

[field: NonSerialized]
public event ReportProgressDelegate ProgressDelegate;

public virtual void OnProgress(UpdateProgressInfo pi)
{
if (ProgressDelegate != null)
ProgressDelegate(pi);
}

public abstract bool Prepare(IUpdateSource source);
public abstract TaskExecutionStatus Execute(bool coldRun);
public abstract bool Rollback();
}
}
4 changes: 2 additions & 2 deletions src/NAppUpdate.Framework/UpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private bool PrepareUpdates(Action<bool> callback)
}

var t = task;
task.OnProgress += status => TaskProgressCallback(status, t);
task.ProgressDelegate += status => TaskProgressCallback(status, t);

if (!task.Prepare(UpdateSource))
{
Expand Down Expand Up @@ -416,7 +416,7 @@ public bool ApplyUpdates(bool relaunchApplication, bool updaterDoLogging, bool u
foreach (var task in UpdatesToApply)
{
IUpdateTask t = task;
task.OnProgress += status => TaskProgressCallback(status, t);
task.ProgressDelegate += status => TaskProgressCallback(status, t);

try
{
Expand Down
1 change: 1 addition & 0 deletions src/NAppUpdate.Tests/NAppUpdate.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<Compile Include="FeedReaders\NauXmlFeedReaderTests.cs" />
<Compile Include="Integration\FileDownloaderTests.cs" />
<Compile Include="Integration\SimpleWebSourceTests.cs" />
<Compile Include="Tasks\BasicTaskTests.cs" />
<Compile Include="UtilTests\PermissionsCheckTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="FeedReaders\AppcastReaderTests.cs" />
Expand Down
16 changes: 16 additions & 0 deletions src/NAppUpdate.Tests/Tasks/BasicTaskTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NAppUpdate.Framework.Tasks;

namespace NAppUpdate.Tests.Tasks
{
[TestClass]
public class BasicTaskTests
{
[TestMethod]
public void TestTaskDefaultCharacteristics()
{
var task = new FileUpdateTask(); // just a random task object
Assert.IsTrue(task.ExecutionStatus == TaskExecutionStatus.Pending);
}
}
}

0 comments on commit 9c499e8

Please sign in to comment.