Skip to content

Commit

Permalink
Finalized AppUpdater
Browse files Browse the repository at this point in the history
  • Loading branch information
muratcakir committed May 5, 2014
1 parent 90bc3b6 commit bbbe5e4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
4 changes: 2 additions & 2 deletions SmartStoreNET.Tasks.Targets
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@
<Delete Files="$(StageFolder)\Administration\packages.config; $(StageFolder)\packages.config"/>

<!-- copy \Media\uploaded\ folder to stage -->
<RemoveDir Directories="$(StageFolder)\Media\uploaded\" />
<Copy SourceFiles="@(UploadedImages)" DestinationFolder="$(StageFolder)\Media\uploaded\%(RecursiveDir)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" />
<!--<RemoveDir Directories="$(StageFolder)\Media\uploaded\" />
<Copy SourceFiles="@(UploadedImages)" DestinationFolder="$(StageFolder)\Media\uploaded\%(RecursiveDir)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" />-->

<!-- copy SQL Compact native binaries-->
<Copy SourceFiles="@(SqlCe-Native-Binaries-x86)" DestinationFolder="$(StageFolder)\bin\x86\%(RecursiveDir)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void Log(MessageLevel level, string message, params object[] args)
{
case MessageLevel.Debug:
//_logger.Debug(String.Format(message, args));
//break;
break;
case MessageLevel.Error:
_logger.Error(String.Format(message, args));
break;
Expand Down
80 changes: 52 additions & 28 deletions src/Libraries/SmartStore.Core/Packaging/Updater/AppUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,46 @@ public bool TryUpdate()

using (_rwLock.GetUpgradeableReadLock())
{
var package = FindPackage();

if (package == null)
return false;

if (!ValidatePackage(package))
return false;

if (!CheckEnvironment())
return false;

using (_rwLock.GetWriteLock())
try
{
var info = ExecuteUpdate(package);

FinalizeUpdate();

return true;
string packagePath = null;
var package = FindPackage(out packagePath);

if (package == null)
return false;

if (!ValidatePackage(package))
return false;

if (!CheckEnvironment())
return false;

using (_rwLock.GetWriteLock())
{
Backup();

var info = ExecuteUpdate(package);

if (info != null)
{
var newPath = packagePath + ".applied";
if (File.Exists(newPath))
{
File.Delete(packagePath);
}
else
{
File.Move(packagePath, newPath);
}
}

return info != null;
}
}
catch (Exception ex)
{
_logger.Error("An error occured while updating the application: {0}".FormatCurrent(ex.Message), ex);
return false;
}
}
}
Expand All @@ -59,8 +81,9 @@ private TraceLogger CreateLogger(IPackage package)
return new TraceLogger(logFile);
}

private IPackage FindPackage()
private IPackage FindPackage(out string path)
{
path = null;
var dir = CommonHelper.MapPath(UpdatePackagePath, false);
var files = Directory.GetFiles(dir, "SmartStore.*.nupkg", SearchOption.TopDirectoryOnly);

Expand All @@ -72,6 +95,7 @@ private IPackage FindPackage()

try
{
path = files[0];
package = new ZipPackage(files[0]);
_logger = CreateLogger(package);
_logger.Information("Found update package '{0}'".FormatInvariant(package.GetFullName()));
Expand Down Expand Up @@ -101,19 +125,19 @@ private void Backup()
{
var source = new DirectoryInfo(CommonHelper.MapPath("~/"));

var tempPath = "~/App_Data/_Backup/App/SmartStore";
var tempPath = CommonHelper.MapPath("~/App_Data/_Backup/App/SmartStore");
string localTempPath = null;
for (int i = 0; i < 50; i++)
{
localTempPath = CommonHelper.MapPath(tempPath) + (i == 0 ? "" : "." + i.ToString());
localTempPath = tempPath + (i == 0 ? "" : "." + i.ToString());
if (!Directory.Exists(localTempPath))
{
Directory.CreateDirectory(localTempPath);
break;
}
localTempPath = null;
}

if (localTempPath == null)
{
var exception = new SmartException("Too many backups in '{0}'.".FormatInvariant(tempPath));
Expand All @@ -124,12 +148,13 @@ private void Backup()
var backupFolder = new DirectoryInfo(localTempPath);
var folderUpdater = new FolderUpdater(_logger);
folderUpdater.Backup(source, backupFolder, "App_Data", "Media");

_logger.Information("Backup successfully created in folder '{0}'.".FormatInvariant(localTempPath));
}

private PackageInfo ExecuteUpdate(IPackage package)
{
//var appPath = CommonHelper.MapPath("~/");
var appPath = "D:\\_temp\\AppUpdater\\Restore";
var appPath = CommonHelper.MapPath("~/");

var logger = new NugetLogger(_logger);

Expand All @@ -147,20 +172,19 @@ private PackageInfo ExecuteUpdate(IPackage package)
// Perform the update
projectManager.AddPackageReference(package, true, false);

return new PackageInfo
var info = new PackageInfo
{
Id = package.Id,
Name = package.Title ?? package.Id,
Version = package.Version.ToString(),
Type = "App",
Path = appPath
};
}

private void FinalizeUpdate()
{
}
_logger.Information("Update '{0}' successfully executed.".FormatInvariant(info.Name));

return info;
}

protected override void OnDispose(bool disposing)
{
Expand Down

0 comments on commit bbbe5e4

Please sign in to comment.