Added React Native Windows dotNet46 support (#684)

Added support react-native-windows dotNet
Added example for react-native-windows UWP based
Added example for react-native-windows dotNet based

Project structure:

CodePush.Shared - shared code between UWP and dotNet
CodePush - UWP specific code
CodePush.Net46 - dotNet specific code

For UWP solution it needs to be added the following projects:

CodePush.Shared
CodePush
For dotNet solution it needs to be added the following projects:

CodePush.Shared
CodePush.Net46
Examples:

Examples\CodePushDemoApp\windows\CodePushDemoApp.sln the solution contains both examples (UWP and dotNet).

Notes

Example for ARM configuration has not been tested. Since there is no changes in UWP part of implementation, there is low risk of failure.

In this implementation we tried to reuse UWP library as much as possible. The following issues are relevant for both platforms:

ZipFile.ExtractToDirectory is not reliable and throws exception if:
folder exists already
path is too long (> 250 chars)
Un-zipping is quite long operation. Does it make sense for async?
await UpdateUtils.UnzipBundleAsync(downloadFile.Path, unzippedFolder.Path);
This commit is contained in:
Alexander Bodalevsky
2017-02-14 02:23:20 +02:00
committed by Richard Hua
parent fc7c109535
commit be96f07eda
69 changed files with 2866 additions and 87 deletions

252
windows/CodePush.Net46/.gitignore vendored Normal file
View File

@@ -0,0 +1,252 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# DNX
project.lock.json
artifacts/
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
# NuGet v3's project.json files produces more ignoreable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.pfx
*.publishsettings
node_modules/
orleans.codegen.cs
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
*.mdf
*.ldf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml

View File

@@ -0,0 +1,28 @@
using System;
namespace CodePush.Net46.Adapters.Http
{
public enum HttpProgressStage
{
None = 0,
DetectingProxy = 10,
ResolvingName = 20,
ConnectingToServer = 30,
NegotiatingSsl = 40,
SendingHeaders = 50,
SendingContent = 60,
WaitingForResponse = 70,
ReceivingHeaders = 80,
ReceivingContent = 90
}
public struct HttpProgress
{
public UInt64 BytesReceived;
public UInt64 BytesSent;
public UInt32 Retries;
public HttpProgressStage Stage;
public UInt64? TotalBytesToReceive;
public UInt64? TotalBytesToSend;
}
}

View File

@@ -0,0 +1,103 @@
using CodePush.ReactNative;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PCLStorage;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace CodePush.Net46.Adapters.Storage
{
public enum ApplicationDataCreateDisposition
{
Always = 0,
Existing = 1
}
public class DictionaryWithDefault<TKey, TValue> : Dictionary<TKey, TValue>
{
TValue _default;
public TValue DefaultValue
{
get { return _default; }
set { _default = value; }
}
public DictionaryWithDefault() : base() { }
public DictionaryWithDefault(TValue defaultValue) : base()
{
_default = defaultValue;
}
public new TValue this[TKey key]
{
get
{
TValue t;
return base.TryGetValue(key, out t) ? t : _default;
}
set
{
base[key] = value;
DataChanged(this, null);
}
}
public bool Remove(TKey key)
{
var found = base.Remove(key);
if (found)
DataChanged(this, null);
return found;
}
public event EventHandler DataChanged;
}
// A naive implementation of Windows.Storage.ApplicationDataContainer
public class ApplicationDataContainer
{
public DictionaryWithDefault<string, string> Values;
private readonly SemaphoreSlim mutex = new SemaphoreSlim(1, 1);
const string STORAGE_NAME = "AppStorage.data";
IFile storageFile = null;
public ApplicationDataContainer(string name = STORAGE_NAME)
{
storageFile = FileSystem.Current.LocalStorage.CreateFileAsync(name, CreationCollisionOption.OpenIfExists).Result;
var data = CodePushUtils.GetJObjectFromFileAsync(storageFile).Result;
if (data != null)
{
Values = data.ToObject<DictionaryWithDefault<string, string>>();
}
else
{
Values = new DictionaryWithDefault<string, string>();
}
Values.DataChanged += async (s, e) => await SaveAsync();
}
~ApplicationDataContainer()
{
mutex.Dispose();
}
async Task SaveAsync()
{
await mutex.WaitAsync().ConfigureAwait(false);
var jobject = JObject.FromObject(Values);
await storageFile.WriteAllTextAsync(JsonConvert.SerializeObject(jobject)).ConfigureAwait(false);
mutex.Release();
}
public async Task DeleteAsync()
{
Values.Clear();
await storageFile.DeleteAsync();
}
}
}

View File

@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4DFE3F9F-5E15-4F17-8FD4-33FF0519348E}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CodePush.Net46</RootNamespace>
<AssemblyName>CodePush.Net46</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="PCLStorage, Version=1.0.2.0, Culture=neutral, PublicKeyToken=286fe515a2c35b64, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(SolutionDir)\packages\PCLStorage.1.0.2\lib\net45\PCLStorage.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="PCLStorage.Abstractions, Version=1.0.2.0, Culture=neutral, PublicKeyToken=286fe515a2c35b64, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(SolutionDir)\packages\PCLStorage.1.0.2\lib\net45\PCLStorage.Abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Management" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup>
<Compile Include="Adapters\Http\HttpProgress.cs" />
<Compile Include="Adapters\Storage\ApplicationDataContainer.cs" />
<Compile Include="CodePushUtils.cs" />
<Compile Include="FileUtils.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UpdateManager.cs" />
<Compile Include="UpdateUtils.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\react-native-windows\ReactWindows\ReactNative.Net46\ReactNative.Net46.csproj">
<Project>{22cbff9c-fe36-43e8-a246-266c7635e662}</Project>
<Name>ReactNative.Net46</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="..\CodePush.Shared\CodePush.Shared.projitems" Label="Shared" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,53 @@
using Newtonsoft.Json.Linq;
using PCLStorage;
using System;
using System.Management;
using System.Threading.Tasks;
namespace CodePush.ReactNative
{
internal partial class CodePushUtils
{
internal async static Task<JObject> GetJObjectFromFileAsync(IFile file)
{
string jsonString = await file.ReadAllTextAsync().ConfigureAwait(false);
if (jsonString.Length == 0)
{
return new JObject();
}
try
{
return JObject.Parse(jsonString);
}
catch (Exception)
{
return null;
}
}
static string GetDeviceIdImpl()
{
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
ManagementObjectCollection moc = mos.Get();
string mbId = String.Empty;
foreach (ManagementObject mo in moc)
{
mbId = (string)mo["SerialNumber"];
break;
}
ManagementObjectCollection mbsList = null;
ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_processor");
mbsList = mbs.Get();
string procId = string.Empty;
foreach (ManagementObject mo in mbsList)
{
procId = mo["ProcessorID"].ToString();
break;
}
return procId + "-" + mbId;
}
}
}

View File

@@ -0,0 +1,55 @@
using PCLStorage;
using System;
using System.IO;
using System.Threading.Tasks;
namespace CodePush.ReactNative
{
internal class FileUtils
{
internal async static Task MergeFoldersAsync(IFolder source, IFolder target)
{
foreach (IFile sourceFile in await source.GetFilesAsync().ConfigureAwait(false))
{
await CopyFileAsync(sourceFile.Path, Path.Combine(target.Path, sourceFile.Name)).ConfigureAwait(false);
}
foreach (IFolder sourceDirectory in await source.GetFoldersAsync().ConfigureAwait(false))
{
var nextTargetSubDir = await target.CreateFolderAsync(sourceDirectory.Name, CreationCollisionOption.OpenIfExists).ConfigureAwait(false);
await MergeFoldersAsync(sourceDirectory, nextTargetSubDir).ConfigureAwait(false);
}
}
internal async static Task ClearReactDevBundleCacheAsync()
{
if (await FileSystem.Current.LocalStorage.CheckExistsAsync(CodePushConstants.ReactDevBundleCacheFileName).ConfigureAwait(false) != ExistenceCheckResult.FileExists)
return;
var devBundleCacheFile = await FileSystem.Current.LocalStorage.GetFileAsync(CodePushConstants.ReactDevBundleCacheFileName).ConfigureAwait(false);
await devBundleCacheFile.DeleteAsync().ConfigureAwait(false);
}
internal static Task<long> GetBinaryResourcesModifiedTimeAsync(string fileName)
{
var pathToAssembly = CodePushUtils.GetAppFolder();
var pathToAssemblyResource = Path.Combine(pathToAssembly, CodePushConstants.AssetsBundlePrefix, fileName);
var lastUpdateTime = File.GetCreationTime(pathToAssemblyResource);
return Task.FromResult(new DateTimeOffset(lastUpdateTime).ToUnixTimeMilliseconds());
}
internal async static Task CopyFileAsync(string sourcePath, string destinationPath)
{
using (var source = File.Open(sourcePath, FileMode.Open, System.IO.FileAccess.Read))
{
using (var destination = File.Create(destinationPath)) // Replace if exists
{
await source.CopyToAsync(destination);
}
}
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CodePush.Net46")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CodePush.Net46")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("4dfe3f9f-5e15-4f17-8fd4-33ff0519348e")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,313 @@
using CodePush.Net46.Adapters.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PCLStorage;
using System;
using System.IO;
using System.IO.Compression;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
[assembly: InternalsVisibleTo("CodePush.Net46.UnitTest")]
namespace CodePush.ReactNative
{
internal class UpdateManager
{
#region Internal methods
internal async Task ClearUpdatesAsync()
{
await (await UpdateUtils.GetCodePushFolderAsync().ConfigureAwait(false)).DeleteAsync().ConfigureAwait(false);
}
internal async Task DownloadPackageAsync(JObject updatePackage, string expectedBundleFileName, Progress<HttpProgress> downloadProgress)
{
// Using its hash, get the folder where the new update will be saved
var codePushFolder = await UpdateUtils.GetCodePushFolderAsync().ConfigureAwait(false);
var newUpdateHash = (string)updatePackage[CodePushConstants.PackageHashKey];
var newUpdateFolder = await GetPackageFolderAsync(newUpdateHash, false).ConfigureAwait(false);
if (newUpdateFolder != null)
{
// This removes any stale data in newUpdateFolder that could have been left
// uncleared due to a crash or error during the download or install process.
await newUpdateFolder.DeleteAsync().ConfigureAwait(false);
}
newUpdateFolder = await GetPackageFolderAsync(newUpdateHash, true).ConfigureAwait(false);
var newUpdateMetadataFile = await newUpdateFolder.CreateFileAsync(CodePushConstants.PackageFileName, CreationCollisionOption.ReplaceExisting).ConfigureAwait(false);
var downloadUrlString = (string)updatePackage[CodePushConstants.DownloadUrlKey];
var downloadFile = await GetDownloadFileAsync().ConfigureAwait(false);
await UpdateUtils.DownloadBundleAsync(downloadUrlString, downloadFile.Path, downloadProgress);
try
{
// Unzip the downloaded file and then delete the zip
var unzippedFolder = await GetUnzippedFolderAsync().ConfigureAwait(false);
/**
* TODO:
* 1) ZipFile.ExtractToDirectory is not reliable and throws exception if:
* - folder exists already
* - path is too long (> 250 chars)
*
* 2) Un-zipping is quite long operation. Does it make sense for async?
* await UpdateUtils.UnzipBundleAsync(downloadFile.Path, unzippedFolder.Path);
*
* Possible implementation
*
* internal async static Task UnzipBundleAsync(string zipFileName, string targetDir)
* {
* await Task.Run(() =>
* {
* ZipFile.ExtractToDirectory(zipFileName, targetDir)
* return Task.CompletedTask;
* });
* }
*/
ZipFile.ExtractToDirectory(downloadFile.Path, unzippedFolder.Path);
await downloadFile.DeleteAsync().ConfigureAwait(false);
// Merge contents with current update based on the manifest
IFile diffManifestFile = null;
try
{
diffManifestFile = await unzippedFolder.GetFileAsync(CodePushConstants.DiffManifestFileName).ConfigureAwait(false);
}
catch (FileNotFoundException)
{
//file may not be present in folder just skip it
}
if (diffManifestFile != null)
{
var currentPackageFolder = await GetCurrentPackageFolderAsync().ConfigureAwait(false);
if (currentPackageFolder == null)
{
throw new InvalidDataException("Received a diff update, but there is no current version to diff against.");
}
await UpdateUtils.CopyNecessaryFilesFromCurrentPackageAsync(diffManifestFile, currentPackageFolder, newUpdateFolder).ConfigureAwait(false);
await diffManifestFile.DeleteAsync().ConfigureAwait(false);
}
await FileUtils.MergeFoldersAsync(unzippedFolder, newUpdateFolder).ConfigureAwait(false);
await unzippedFolder.DeleteAsync().ConfigureAwait(false);
// For zip updates, we need to find the relative path to the jsBundle and save it in the
// metadata so that we can find and run it easily the next time.
var relativeBundlePath = await UpdateUtils.FindJSBundleInUpdateContentsAsync(newUpdateFolder, expectedBundleFileName).ConfigureAwait(false);
if (relativeBundlePath == null)
{
throw new InvalidDataException("Update is invalid - A JS bundle file named \"" + expectedBundleFileName + "\" could not be found within the downloaded contents. Please check that you are releasing your CodePush updates using the exact same JS bundle file name that was shipped with your app's binary.");
}
else
{
if (diffManifestFile != null)
{
// TODO verify hash for diff update
// CodePushUpdateUtils.verifyHashForDiffUpdate(newUpdateFolderPath, newUpdateHash);
}
updatePackage[CodePushConstants.RelativeBundlePathKey] = relativeBundlePath;
}
}
catch (InvalidDataException)
{
// Downloaded file is not a zip, assume it is a jsbundle
await downloadFile.RenameAsync(expectedBundleFileName).ConfigureAwait(false);
await downloadFile.MoveAsync(newUpdateFolder.Path, NameCollisionOption.ReplaceExisting).ConfigureAwait(false);
}
// Save metadata to the folder
await newUpdateMetadataFile.WriteAllTextAsync(JsonConvert.SerializeObject(updatePackage)).ConfigureAwait(false);
}
internal async Task<JObject> GetCurrentPackageAsync()
{
var packageHash = await GetCurrentPackageHashAsync().ConfigureAwait(false);
return packageHash == null ? null : await GetPackageAsync(packageHash).ConfigureAwait(false);
}
internal async Task<IFile> GetCurrentPackageBundleAsync(string bundleFileName)
{
var packageFolder = await GetCurrentPackageFolderAsync().ConfigureAwait(false);
if (packageFolder == null)
{
return null;
}
var currentPackage = await GetCurrentPackageAsync().ConfigureAwait(false);
var relativeBundlePath = (string)currentPackage[CodePushConstants.RelativeBundlePathKey];
return relativeBundlePath == null
? await packageFolder.GetFileAsync(bundleFileName).ConfigureAwait(false)
: await packageFolder.GetFileAsync(relativeBundlePath).ConfigureAwait(false);
}
internal async Task<string> GetCurrentPackageHashAsync()
{
var info = await GetCurrentPackageInfoAsync().ConfigureAwait(false);
var currentPackageShortHash = (string)info[CodePushConstants.CurrentPackageKey];
if (currentPackageShortHash == null)
{
return null;
}
var currentPackageMetadata = await GetPackageAsync(currentPackageShortHash).ConfigureAwait(false);
return currentPackageMetadata == null ? null : (string)currentPackageMetadata[CodePushConstants.PackageHashKey];
}
internal async Task<JObject> GetPackageAsync(string packageHash)
{
var packageFolder = await GetPackageFolderAsync(packageHash, false).ConfigureAwait(false);
if (packageFolder == null)
{
return null;
}
try
{
var packageFile = await packageFolder.GetFileAsync(CodePushConstants.PackageFileName).ConfigureAwait(false);
return await CodePushUtils.GetJObjectFromFileAsync(packageFile).ConfigureAwait(false);
}
catch (IOException)
{
return null;
}
}
internal async Task<IFolder> GetPackageFolderAsync(string packageHash, bool createIfNotExists)
{
var codePushFolder = await UpdateUtils.GetCodePushFolderAsync().ConfigureAwait(false);
try
{
packageHash = ShortenPackageHash(packageHash);
return createIfNotExists
? await codePushFolder.CreateFolderAsync(packageHash, CreationCollisionOption.OpenIfExists).ConfigureAwait(false)
: await codePushFolder.GetFolderAsync(packageHash).ConfigureAwait(false);
}
catch (FileNotFoundException)
{
return null;
}
catch (DirectoryNotFoundException)
{
return null;
}
}
internal async Task<JObject> GetPreviousPackageAsync()
{
var packageHash = await GetPreviousPackageHashAsync().ConfigureAwait(false);
return packageHash == null ? null : await GetPackageAsync(packageHash).ConfigureAwait(false);
}
internal async Task<string> GetPreviousPackageHashAsync()
{
var info = await GetCurrentPackageInfoAsync().ConfigureAwait(false);
var previousPackageShortHash = (string)info[CodePushConstants.PreviousPackageKey];
if (previousPackageShortHash == null)
{
return null;
}
var previousPackageMetadata = await GetPackageAsync(previousPackageShortHash).ConfigureAwait(false);
return previousPackageMetadata == null ? null : (string)previousPackageMetadata[CodePushConstants.PackageHashKey];
}
internal async Task InstallPackageAsync(JObject updatePackage, bool currentUpdateIsPending)
{
var packageHash = (string)updatePackage[CodePushConstants.PackageHashKey];
var info = await GetCurrentPackageInfoAsync().ConfigureAwait(false);
if (currentUpdateIsPending)
{
// Don't back up current update to the "previous" position because
// it is an unverified update which should not be rolled back to.
var currentPackageFolder = await GetCurrentPackageFolderAsync().ConfigureAwait(false);
if (currentPackageFolder != null)
{
await currentPackageFolder.DeleteAsync().ConfigureAwait(false);
}
}
else
{
var previousPackageHash = await GetPreviousPackageHashAsync().ConfigureAwait(false);
if (previousPackageHash != null && !previousPackageHash.Equals(packageHash))
{
var previousPackageFolder = await GetPackageFolderAsync(previousPackageHash, false).ConfigureAwait(false);
if (previousPackageFolder != null)
{
await previousPackageFolder.DeleteAsync().ConfigureAwait(false);
}
}
info[CodePushConstants.PreviousPackageKey] = info[CodePushConstants.CurrentPackageKey];
}
info[CodePushConstants.CurrentPackageKey] = packageHash;
await UpdateCurrentPackageInfoAsync(info).ConfigureAwait(false);
}
internal async Task RollbackPackageAsync()
{
var info = await GetCurrentPackageInfoAsync().ConfigureAwait(false);
var currentPackageFolder = await GetCurrentPackageFolderAsync().ConfigureAwait(false);
if (currentPackageFolder != null)
{
await currentPackageFolder.DeleteAsync().ConfigureAwait(false);
}
info[CodePushConstants.CurrentPackageKey] = info[CodePushConstants.PreviousPackageKey];
info[CodePushConstants.PreviousPackageKey] = null;
await UpdateCurrentPackageInfoAsync(info).ConfigureAwait(false);
}
#endregion
#region Private methods
private async Task<IFolder> GetCurrentPackageFolderAsync()
{
var info = await GetCurrentPackageInfoAsync().ConfigureAwait(false);
var packageHash = (string)info[CodePushConstants.CurrentPackageKey];
return packageHash == null ? null : await GetPackageFolderAsync(packageHash, false).ConfigureAwait(false);
}
private async Task<JObject> GetCurrentPackageInfoAsync()
{
var statusFile = await GetStatusFileAsync().ConfigureAwait(false);
return await CodePushUtils.GetJObjectFromFileAsync(statusFile).ConfigureAwait(false);
}
private async Task<IFile> GetDownloadFileAsync()
{
var codePushFolder = await UpdateUtils.GetCodePushFolderAsync().ConfigureAwait(false);
return await codePushFolder.CreateFileAsync(CodePushConstants.DownloadFileName, CreationCollisionOption.OpenIfExists).ConfigureAwait(false);
}
private async Task<IFile> GetStatusFileAsync()
{
var codePushFolder = await UpdateUtils.GetCodePushFolderAsync().ConfigureAwait(false);
return await codePushFolder.CreateFileAsync(CodePushConstants.StatusFileName, CreationCollisionOption.OpenIfExists).ConfigureAwait(false);
}
private async Task<IFolder> GetUnzippedFolderAsync()
{
var codePushFolder = await UpdateUtils.GetCodePushFolderAsync().ConfigureAwait(false);
return await codePushFolder.CreateFolderAsync(CodePushConstants.UnzippedFolderName, CreationCollisionOption.OpenIfExists).ConfigureAwait(false);
}
private string ShortenPackageHash(string longPackageHash)
{
return longPackageHash.Substring(0, 8);
}
private async Task UpdateCurrentPackageInfoAsync(JObject packageInfo)
{
var file = await GetStatusFileAsync().ConfigureAwait(false);
await file.WriteAllTextAsync(JsonConvert.SerializeObject(packageInfo)).ConfigureAwait(false);
}
#endregion
}
}

View File

@@ -0,0 +1,70 @@
using CodePush.Net46.Adapters.Http;
using Newtonsoft.Json.Linq;
using PCLStorage;
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace CodePush.ReactNative
{
internal class UpdateUtils
{
internal async static Task CopyNecessaryFilesFromCurrentPackageAsync(IFile diffManifestFile, IFolder currentPackageFolder, IFolder newPackageFolder)
{
await FileUtils.MergeFoldersAsync(currentPackageFolder, newPackageFolder).ConfigureAwait(false);
JObject diffManifest = await CodePushUtils.GetJObjectFromFileAsync(diffManifestFile).ConfigureAwait(false);
var deletedFiles = (JArray)diffManifest["deletedFiles"];
foreach (string fileNameToDelete in deletedFiles)
{
var fileToDelete = await newPackageFolder.GetFileAsync(fileNameToDelete).ConfigureAwait(false);
await fileToDelete.DeleteAsync().ConfigureAwait(false);
}
}
internal async static Task<string> FindJSBundleInUpdateContentsAsync(IFolder updateFolder, string expectedFileName)
{
foreach (IFile file in await updateFolder.GetFilesAsync().ConfigureAwait(false))
{
string fileName = file.Name;
if (fileName.Equals(expectedFileName))
{
return fileName;
}
}
foreach (IFolder folder in await updateFolder.GetFoldersAsync().ConfigureAwait(false))
{
string mainBundlePathInSubFolder = await FindJSBundleInUpdateContentsAsync(folder, expectedFileName).ConfigureAwait(false);
if (mainBundlePathInSubFolder != null)
{
return Path.Combine(folder.Name, mainBundlePathInSubFolder);
}
}
return null;
}
internal async static Task DownloadBundleAsync(string url, string fileName, IProgress<HttpProgress> downloadProgress)
{
var uri = new Uri(url);
var client = new WebClient();
client.DownloadProgressChanged += (s, e) =>
{
downloadProgress.Report(new HttpProgress
{
BytesReceived = (ulong)e.BytesReceived, //conversion long to ulong is safe
TotalBytesToReceive = (ulong)e.TotalBytesToReceive //because size can't be negative
});
};
await client.DownloadFileTaskAsync(uri, fileName);
}
internal static async Task<IFolder> GetCodePushFolderAsync()
{
var pathToCodePush = Path.Combine(CodePushUtils.GetAppFolder(), CodePushConstants.CodePushFolderPrefix);
return await FileSystem.Current.LocalStorage.CreateFolderAsync(pathToCodePush, CreationCollisionOption.OpenIfExists).ConfigureAwait(false);
}
}
}

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
<package id="PCLStorage" version="1.0.2" targetFramework="net46" />
</packages>