2020-08-17 10:00:56 -07:00
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
2020-07-21 10:26:37 -07:00
using System ;
2020-07-14 11:44:50 -07:00
using System.IO ;
2020-09-23 16:32:06 -07:00
using System.Reflection ;
2020-08-12 11:46:11 -07:00
using Microsoft.Plugin.Program.Logger ;
2021-06-14 10:19:05 +02:00
using Windows.Foundation.Metadata ;
2020-07-14 11:44:50 -07:00
using Package = Windows . ApplicationModel . Package ;
2020-07-10 13:43:02 -07:00
namespace Microsoft.Plugin.Program.Programs
{
public class PackageWrapper : IPackage
{
2020-07-14 11:44:50 -07:00
public string Name { get ; } = string . Empty ;
2020-07-10 13:43:02 -07:00
2020-07-14 11:44:50 -07:00
public string FullName { get ; } = string . Empty ;
2020-07-10 13:43:02 -07:00
2020-07-14 11:44:50 -07:00
public string FamilyName { get ; } = string . Empty ;
2020-07-10 13:43:02 -07:00
2020-08-21 12:40:31 -07:00
public bool IsFramework { get ; }
2020-07-10 13:43:02 -07:00
2020-08-21 12:40:31 -07:00
public bool IsDevelopmentMode { get ; }
2020-07-10 13:43:02 -07:00
2020-07-14 11:44:50 -07:00
public string InstalledLocation { get ; } = string . Empty ;
2020-08-17 10:00:56 -07:00
public PackageWrapper ( )
{
}
2020-07-10 13:43:02 -07:00
2020-08-14 12:46:23 -07:00
public PackageWrapper ( string name , string fullName , string familyName , bool isFramework , bool isDevelopmentMode , string installedLocation )
2020-07-10 13:43:02 -07:00
{
2020-08-14 12:46:23 -07:00
Name = name ;
FullName = fullName ;
FamilyName = familyName ;
IsFramework = isFramework ;
IsDevelopmentMode = isDevelopmentMode ;
InstalledLocation = installedLocation ;
2020-07-10 13:43:02 -07:00
}
2021-06-14 10:19:05 +02:00
private static readonly Lazy < bool > IsPackageDotInstallationPathAvailable = new Lazy < bool > ( ( ) = >
ApiInformation . IsPropertyPresent ( typeof ( Package ) . FullName , nameof ( Package . InstalledPath ) ) ) ;
2020-07-10 13:43:02 -07:00
public static PackageWrapper GetWrapperFromPackage ( Package package )
{
2020-08-17 10:00:56 -07:00
if ( package = = null )
{
throw new ArgumentNullException ( nameof ( package ) ) ;
2020-08-11 09:08:44 -07:00
}
2020-07-21 10:26:37 -07:00
string path ;
2020-07-14 11:44:50 -07:00
try
{
2021-06-14 10:19:05 +02:00
path = IsPackageDotInstallationPathAvailable . Value ? GetInstalledPath ( package ) : package . InstalledLocation . Path ;
2020-07-14 11:44:50 -07:00
}
2020-09-17 16:17:02 -07:00
catch ( Exception e ) when ( e is ArgumentException | | e is FileNotFoundException | | e is DirectoryNotFoundException )
2020-07-14 11:44:50 -07:00
{
2020-09-23 16:32:06 -07:00
ProgramLogger . Exception ( $"Exception {package.Id.Name}" , e , MethodBase . GetCurrentMethod ( ) . DeclaringType , "Path could not be determined" ) ;
2020-07-31 12:33:05 -07:00
return new PackageWrapper (
package . Id . Name ,
package . Id . FullName ,
package . Id . FamilyName ,
package . IsFramework ,
2020-08-13 15:31:14 -07:00
package . IsDevelopmentMode ,
2020-07-31 12:33:05 -07:00
string . Empty ) ;
2020-07-14 11:44:50 -07:00
}
2020-07-21 10:26:37 -07:00
return new PackageWrapper (
package . Id . Name ,
package . Id . FullName ,
package . Id . FamilyName ,
2020-07-22 13:27:17 -07:00
package . IsFramework ,
2020-07-21 10:26:37 -07:00
package . IsDevelopmentMode ,
2020-08-14 09:22:12 -07:00
path ) ;
2020-07-10 13:43:02 -07:00
}
2021-06-14 10:19:05 +02:00
// This is a separate method so the reference to .InstalledPath won't be loaded in API versions which do not support this API (e.g. older then Build 19041)
private static string GetInstalledPath ( Package package )
= > package . InstalledPath ;
2020-07-10 13:43:02 -07:00
}
}