PackageWrapper.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.IO;
 7  using System.Reflection;
 8  
 9  using Microsoft.Plugin.Program.Logger;
10  using Windows.Foundation.Metadata;
11  
12  using Package = Windows.ApplicationModel.Package;
13  
14  namespace Microsoft.Plugin.Program.Programs
15  {
16      public class PackageWrapper : IPackage
17      {
18          public string Name { get; } = string.Empty;
19  
20          public string FullName { get; } = string.Empty;
21  
22          public string FamilyName { get; } = string.Empty;
23  
24          public bool IsFramework { get; }
25  
26          public bool IsDevelopmentMode { get; }
27  
28          public string InstalledLocation { get; } = string.Empty;
29  
30          public PackageWrapper()
31          {
32          }
33  
34          public PackageWrapper(string name, string fullName, string familyName, bool isFramework, bool isDevelopmentMode, string installedLocation)
35          {
36              Name = name;
37              FullName = fullName;
38              FamilyName = familyName;
39              IsFramework = isFramework;
40              IsDevelopmentMode = isDevelopmentMode;
41              InstalledLocation = installedLocation;
42          }
43  
44          private static readonly Lazy<bool> IsPackageDotInstallationPathAvailable = new Lazy<bool>(() =>
45              ApiInformation.IsPropertyPresent(typeof(Package).FullName, nameof(Package.InstalledLocation.Path)));
46  
47          public static PackageWrapper GetWrapperFromPackage(Package package)
48          {
49              ArgumentNullException.ThrowIfNull(package);
50  
51              string path;
52              try
53              {
54                  path = IsPackageDotInstallationPathAvailable.Value ? GetInstalledPath(package) : package.InstalledLocation.Path;
55              }
56              catch (Exception e) when (e is ArgumentException || e is FileNotFoundException || e is DirectoryNotFoundException)
57              {
58                  ProgramLogger.Exception($"Exception {package.Id.Name}", e, MethodBase.GetCurrentMethod().DeclaringType, "Path could not be determined");
59                  return new PackageWrapper(
60                      package.Id.Name,
61                      package.Id.FullName,
62                      package.Id.FamilyName,
63                      package.IsFramework,
64                      package.IsDevelopmentMode,
65                      string.Empty);
66              }
67  
68              return new PackageWrapper(
69                      package.Id.Name,
70                      package.Id.FullName,
71                      package.Id.FamilyName,
72                      package.IsFramework,
73                      package.IsDevelopmentMode,
74                      path);
75          }
76  
77          // 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)
78          private static string GetInstalledPath(Package package)
79              => package.InstalledLocation.Path;
80      }
81  }