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/.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,121 @@
<?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>{446A85D9-55EB-4C7D-8B9D-448306C833D6}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CodePush</RootNamespace>
<AssemblyName>CodePush</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion>10.0.10586.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.10240.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<PlatformTarget>x86</PlatformTarget>
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM'">
<PlatformTarget>ARM</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\ARM\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM'">
<PlatformTarget>ARM</PlatformTarget>
<OutputPath>bin\ARM\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<PlatformTarget>x64</PlatformTarget>
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .Net Framework and Windows SDK are automatically included -->
<None Include="project.json" />
</ItemGroup>
<ItemGroup>
<Compile Include="UpdateManager.cs" />
<Compile Include="UpdateUtils.cs" />
<Compile Include="CodePushUtils.cs" />
<Compile Include="FileUtils.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Properties\CodePush.rd.xml" />
</ItemGroup>
<ItemGroup>
<SDKReference Include="WindowsMobile, Version=10.0.10586.0">
<Name>Windows Mobile Extensions for the UWP</Name>
</SDKReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\react-native-windows\ReactWindows\ReactNative\ReactNative.csproj">
<Project>{c7673ad5-e3aa-468c-a5fd-fa38154e205c}</Project>
<Name>ReactNative</Name>
</ProjectReference>
</ItemGroup>
<Import Project="..\CodePush.Shared\CodePush.Shared.projitems" Label="Shared" />
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.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,42 @@
using Newtonsoft.Json.Linq;
using System;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.System.Profile;
namespace CodePush.ReactNative
{
internal partial class CodePushUtils
{
internal async static Task<JObject> GetJObjectFromFileAsync(StorageFile file)
{
string jsonString = await FileIO.ReadTextAsync(file).AsTask().ConfigureAwait(false);
if (jsonString.Length == 0)
{
return new JObject();
}
try
{
return JObject.Parse(jsonString);
}
catch (Exception)
{
return null;
}
}
static string GetDeviceIdImpl()
{
HardwareToken token = HardwareIdentification.GetPackageSpecificToken(null);
IBuffer hardwareId = token.Id;
var dataReader = DataReader.FromBuffer(hardwareId);
var bytes = new byte[hardwareId.Length];
dataReader.ReadBytes(bytes);
return BitConverter.ToString(bytes);
}
}
}

View File

@@ -0,0 +1,40 @@
using System;
using System.Threading.Tasks;
using Windows.Storage;
namespace CodePush.ReactNative
{
internal class FileUtils
{
internal async static Task MergeFoldersAsync(StorageFolder source, StorageFolder target)
{
foreach (StorageFile sourceFile in await source.GetFilesAsync().AsTask().ConfigureAwait(false))
{
await sourceFile.CopyAndReplaceAsync(await target.CreateFileAsync(sourceFile.Name, CreationCollisionOption.OpenIfExists).AsTask().ConfigureAwait(false)).AsTask().ConfigureAwait(false);
}
foreach (StorageFolder sourceDirectory in await source.GetFoldersAsync().AsTask().ConfigureAwait(false))
{
StorageFolder nextTargetSubDir = await target.CreateFolderAsync(sourceDirectory.Name, CreationCollisionOption.OpenIfExists).AsTask().ConfigureAwait(false);
await MergeFoldersAsync(sourceDirectory, nextTargetSubDir).ConfigureAwait(false);
}
}
internal async static Task ClearReactDevBundleCacheAsync()
{
var devBundleCacheFile = (StorageFile)await ApplicationData.Current.LocalFolder.TryGetItemAsync(CodePushConstants.ReactDevBundleCacheFileName).AsTask().ConfigureAwait(false);
if (devBundleCacheFile != null)
{
await devBundleCacheFile.DeleteAsync().AsTask().ConfigureAwait(false);
}
}
internal async static Task<long> GetBinaryResourcesModifiedTimeAsync(string fileName)
{
var assetJSBundleFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(CodePushConstants.AssetsBundlePrefix + fileName)).AsTask().ConfigureAwait(false);
var fileProperties = await assetJSBundleFile.GetBasicPropertiesAsync().AsTask().ConfigureAwait(false);
return fileProperties.DateModified.ToUnixTimeMilliseconds();
}
}
}

View File

@@ -0,0 +1,29 @@
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")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CodePush")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 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")]
[assembly: ComVisible(false)]

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains Runtime Directives, specifications about types your application accesses
through reflection and other dynamic code patterns. Runtime Directives are used to control the
.NET Native optimizer and ensure that it does not remove code accessed by your library. If your
library does not do any reflection, then you generally do not need to edit this file. However,
if your library reflects over types, especially types passed to it or derived from its types,
then you should write Runtime Directives.
The most common use of reflection in libraries is to discover information about types passed
to the library. Runtime Directives have three ways to express requirements on types passed to
your library.
1. Parameter, GenericParameter, TypeParameter, TypeEnumerableParameter
Use these directives to reflect over types passed as a parameter.
2. SubTypes
Use a SubTypes directive to reflect over types derived from another type.
3. AttributeImplies
Use an AttributeImplies directive to indicate that your library needs to reflect over
types or methods decorated with an attribute.
For more information on writing Runtime Directives for libraries, please visit
http://go.microsoft.com/fwlink/?LinkID=391919
-->
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Library Name="CodePush">
<!-- add directives for your library here -->
</Library>
</Directives>

View File

@@ -0,0 +1,299 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.IO.Compression;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.Web.Http;
namespace CodePush.ReactNative
{
internal class UpdateManager
{
#region Internal methods
internal async Task ClearUpdatesAsync()
{
await (await GetCodePushFolderAsync().ConfigureAwait(false)).DeleteAsync().AsTask().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
StorageFolder codePushFolder = await GetCodePushFolderAsync().ConfigureAwait(false);
var newUpdateHash = (string)updatePackage[CodePushConstants.PackageHashKey];
StorageFolder 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().AsTask().ConfigureAwait(false);
}
newUpdateFolder = await GetPackageFolderAsync(newUpdateHash, true).ConfigureAwait(false);
StorageFile newUpdateMetadataFile = await newUpdateFolder.CreateFileAsync(CodePushConstants.PackageFileName).AsTask().ConfigureAwait(false);
var downloadUrlString = (string)updatePackage[CodePushConstants.DownloadUrlKey];
StorageFile downloadFile = await GetDownloadFileAsync().ConfigureAwait(false);
var downloadUri = new Uri(downloadUrlString);
// Download the file and send progress event asynchronously
var request = new HttpRequestMessage(HttpMethod.Get, downloadUri);
var client = new HttpClient();
var cancellationTokenSource = new CancellationTokenSource();
using (HttpResponseMessage response = await client.SendRequestAsync(request).AsTask(cancellationTokenSource.Token, downloadProgress).ConfigureAwait(false))
using (IInputStream inputStream = await response.Content.ReadAsInputStreamAsync().AsTask().ConfigureAwait(false))
using (IRandomAccessStream downloadFileStream = await downloadFile.OpenAsync(FileAccessMode.ReadWrite).AsTask().ConfigureAwait(false))
{
await RandomAccessStream.CopyAsync(inputStream, downloadFileStream).AsTask().ConfigureAwait(false);
}
try
{
// Unzip the downloaded file and then delete the zip
StorageFolder unzippedFolder = await GetUnzippedFolderAsync().ConfigureAwait(false);
ZipFile.ExtractToDirectory(downloadFile.Path, unzippedFolder.Path);
await downloadFile.DeleteAsync().AsTask().ConfigureAwait(false);
// Merge contents with current update based on the manifest
StorageFile diffManifestFile = (StorageFile)await unzippedFolder.TryGetItemAsync(CodePushConstants.DiffManifestFileName).AsTask().ConfigureAwait(false);
if (diffManifestFile != null)
{
StorageFolder 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().AsTask().ConfigureAwait(false);
}
await FileUtils.MergeFoldersAsync(unzippedFolder, newUpdateFolder).ConfigureAwait(false);
await unzippedFolder.DeleteAsync().AsTask().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.
string 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).AsTask().ConfigureAwait(false);
await downloadFile.MoveAsync(newUpdateFolder).AsTask().ConfigureAwait(false);
}
/*TODO: ZipFile.ExtractToDirectory is not reliable and throws exceptions if:
- folder exists already
- path is too long
it needs to be handled
*/
// Save metadata to the folder
await FileIO.WriteTextAsync(newUpdateMetadataFile, JsonConvert.SerializeObject(updatePackage)).AsTask().ConfigureAwait(false);
}
internal async Task<JObject> GetCurrentPackageAsync()
{
string packageHash = await GetCurrentPackageHashAsync().ConfigureAwait(false);
return packageHash == null ? null : await GetPackageAsync(packageHash).ConfigureAwait(false);
}
internal async Task<StorageFile> GetCurrentPackageBundleAsync(string bundleFileName)
{
StorageFolder packageFolder = await GetCurrentPackageFolderAsync().ConfigureAwait(false);
if (packageFolder == null)
{
return null;
}
JObject currentPackage = await GetCurrentPackageAsync().ConfigureAwait(false);
var relativeBundlePath = (string)currentPackage[CodePushConstants.RelativeBundlePathKey];
return relativeBundlePath == null
? await packageFolder.GetFileAsync(bundleFileName).AsTask().ConfigureAwait(false)
: await packageFolder.GetFileAsync(relativeBundlePath).AsTask().ConfigureAwait(false);
}
internal async Task<string> GetCurrentPackageHashAsync()
{
JObject info = await GetCurrentPackageInfoAsync().ConfigureAwait(false);
string currentPackageShortHash = (string)info[CodePushConstants.CurrentPackageKey];
if (currentPackageShortHash == null)
{
return null;
}
JObject currentPackageMetadata = await GetPackageAsync(currentPackageShortHash).ConfigureAwait(false);
return currentPackageMetadata == null ? null : (string)currentPackageMetadata[CodePushConstants.PackageHashKey];
}
internal async Task<JObject> GetPackageAsync(string packageHash)
{
StorageFolder packageFolder = await GetPackageFolderAsync(packageHash, false).ConfigureAwait(false);
if (packageFolder == null)
{
return null;
}
try
{
StorageFile packageFile = await packageFolder.GetFileAsync(CodePushConstants.PackageFileName).AsTask().ConfigureAwait(false);
return await CodePushUtils.GetJObjectFromFileAsync(packageFile).ConfigureAwait(false);
}
catch (IOException)
{
return null;
}
}
internal async Task<StorageFolder> GetPackageFolderAsync(string packageHash, bool createIfNotExists)
{
StorageFolder codePushFolder = await GetCodePushFolderAsync().ConfigureAwait(false);
try
{
packageHash = ShortenPackageHash(packageHash);
return createIfNotExists
? await codePushFolder.CreateFolderAsync(packageHash, CreationCollisionOption.OpenIfExists).AsTask().ConfigureAwait(false)
: await codePushFolder.GetFolderAsync(packageHash).AsTask().ConfigureAwait(false);
}
catch (FileNotFoundException)
{
return null;
}
}
internal async Task<JObject> GetPreviousPackageAsync()
{
string packageHash = await GetPreviousPackageHashAsync().ConfigureAwait(false);
return packageHash == null ? null : await GetPackageAsync(packageHash).ConfigureAwait(false);
}
internal async Task<string> GetPreviousPackageHashAsync()
{
JObject info = await GetCurrentPackageInfoAsync().ConfigureAwait(false);
string previousPackageShortHash = (string)info[CodePushConstants.PreviousPackageKey];
if (previousPackageShortHash == null)
{
return null;
}
JObject 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];
JObject 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.
StorageFolder currentPackageFolder = await GetCurrentPackageFolderAsync().ConfigureAwait(false);
if (currentPackageFolder != null)
{
await currentPackageFolder.DeleteAsync().AsTask().ConfigureAwait(false);
}
}
else
{
string previousPackageHash = await GetPreviousPackageHashAsync().ConfigureAwait(false);
if (previousPackageHash != null && !previousPackageHash.Equals(packageHash))
{
StorageFolder previousPackageFolder = await GetPackageFolderAsync(previousPackageHash, false).ConfigureAwait(false);
if (previousPackageFolder != null)
{
await previousPackageFolder.DeleteAsync().AsTask().ConfigureAwait(false);
}
}
info[CodePushConstants.PreviousPackageKey] = info[CodePushConstants.CurrentPackageKey];
}
info[CodePushConstants.CurrentPackageKey] = packageHash;
await UpdateCurrentPackageInfoAsync(info).ConfigureAwait(false);
}
internal async Task RollbackPackageAsync()
{
JObject info = await GetCurrentPackageInfoAsync().ConfigureAwait(false);
StorageFolder currentPackageFolder = await GetCurrentPackageFolderAsync().ConfigureAwait(false);
if (currentPackageFolder != null)
{
await currentPackageFolder.DeleteAsync().AsTask().ConfigureAwait(false);
}
info[CodePushConstants.CurrentPackageKey] = info[CodePushConstants.PreviousPackageKey];
info[CodePushConstants.PreviousPackageKey] = null;
await UpdateCurrentPackageInfoAsync(info).ConfigureAwait(false);
}
#endregion
#region Private methods
private async Task<StorageFolder> GetCodePushFolderAsync()
{
return await ApplicationData.Current.LocalFolder.CreateFolderAsync(CodePushConstants.CodePushFolderPrefix, CreationCollisionOption.OpenIfExists).AsTask().ConfigureAwait(false);
}
private async Task<StorageFolder> GetCurrentPackageFolderAsync()
{
JObject 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()
{
StorageFile statusFile = await GetStatusFileAsync().ConfigureAwait(false);
return await CodePushUtils.GetJObjectFromFileAsync(statusFile).ConfigureAwait(false);
}
private async Task<StorageFile> GetDownloadFileAsync()
{
var codePushFolder = await GetCodePushFolderAsync().ConfigureAwait(false);
return await codePushFolder.CreateFileAsync(CodePushConstants.DownloadFileName, CreationCollisionOption.OpenIfExists).AsTask().ConfigureAwait(false);
}
private async Task<StorageFile> GetStatusFileAsync()
{
StorageFolder codePushFolder = await GetCodePushFolderAsync().ConfigureAwait(false);
return await codePushFolder.CreateFileAsync(CodePushConstants.StatusFileName, CreationCollisionOption.OpenIfExists).AsTask().ConfigureAwait(false);
}
private async Task<StorageFolder> GetUnzippedFolderAsync()
{
StorageFolder codePushFolder = await GetCodePushFolderAsync().ConfigureAwait(false);
return await codePushFolder.CreateFolderAsync(CodePushConstants.UnzippedFolderName, CreationCollisionOption.OpenIfExists).AsTask().ConfigureAwait(false);
}
private string ShortenPackageHash(string longPackageHash)
{
return longPackageHash.Substring(0, 8);
}
private async Task UpdateCurrentPackageInfoAsync(JObject packageInfo)
{
await FileIO.WriteTextAsync(await GetStatusFileAsync().ConfigureAwait(false), JsonConvert.SerializeObject(packageInfo)).AsTask().ConfigureAwait(false);
}
#endregion
}
}

View File

@@ -0,0 +1,46 @@
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;
namespace CodePush.ReactNative
{
internal class UpdateUtils
{
internal async static Task CopyNecessaryFilesFromCurrentPackageAsync(StorageFile diffManifestFile, StorageFolder currentPackageFolder, StorageFolder 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)
{
StorageFile fileToDelete = await newPackageFolder.GetFileAsync(fileNameToDelete).AsTask().ConfigureAwait(false);
await fileToDelete.DeleteAsync().AsTask().ConfigureAwait(false);
}
}
internal async static Task<string> FindJSBundleInUpdateContentsAsync(StorageFolder updateFolder, string expectedFileName)
{
foreach (StorageFile file in await updateFolder.GetFilesAsync().AsTask().ConfigureAwait(false))
{
string fileName = file.Name;
if (fileName.Equals(expectedFileName))
{
return fileName;
}
}
foreach (StorageFolder folder in await updateFolder.GetFoldersAsync().AsTask().ConfigureAwait(false))
{
string mainBundlePathInSubFolder = await FindJSBundleInUpdateContentsAsync(folder, expectedFileName).ConfigureAwait(false);
if (mainBundlePathInSubFolder != null)
{
return Path.Combine(folder.Name, mainBundlePathInSubFolder);
}
}
return null;
}
}
}

View File

@@ -0,0 +1,17 @@
{
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2",
"Newtonsoft.Json": "9.0.1"
},
"frameworks": {
"uap10.0": {}
},
"runtimes": {
"win10-arm": {},
"win10-arm-aot": {},
"win10-x86": {},
"win10-x86-aot": {},
"win10-x64": {},
"win10-x64-aot": {}
}
}