winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

WinGetFactory.cs (9097B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="WinGetFactory.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.WinGetUtil.Api
      8 {
      9     using System;
     10     using System.Runtime.InteropServices;
     11     using Microsoft.WinGetUtil.Common;
     12     using Microsoft.WinGetUtil.Exceptions;
     13     using Microsoft.WinGetUtil.Interfaces;
     14 
     15     /// <summary>
     16     /// Factory methods.
     17     /// </summary>
     18     public sealed class WinGetFactory : IWinGetFactory
     19     {
     20         private const uint IndexLatestVersion = unchecked((uint)-1);
     21 
     22         /// <inheritdoc/>
     23         public IWinGetSQLiteIndex SQLiteIndexCreate(string indexFile, uint majorVersion, uint minorVersion)
     24         {
     25             try
     26             {
     27                 WinGetSQLiteIndexCreate(
     28                     indexFile,
     29                     majorVersion,
     30                     minorVersion,
     31                     out IntPtr index);
     32                 return new WinGetSQLiteIndex(index);
     33             }
     34             catch (Exception e)
     35             {
     36                 throw new WinGetSQLiteIndexException(e);
     37             }
     38         }
     39 
     40         /// <inheritdoc/>
     41         public IWinGetSQLiteIndex SQLiteIndexCreateLatestVersion(string indexFile)
     42         {
     43             return this.SQLiteIndexCreate(indexFile, IndexLatestVersion, IndexLatestVersion);
     44         }
     45 
     46         /// <inheritdoc/>
     47         public IWinGetSQLiteIndex SQLiteIndexOpen(string indexFile)
     48         {
     49             try
     50             {
     51                 WinGetSQLiteIndexOpen(indexFile, out IntPtr index);
     52                 return new WinGetSQLiteIndex(index);
     53             }
     54             catch (Exception e)
     55             {
     56                 throw new WinGetSQLiteIndexException(e);
     57             }
     58         }
     59 
     60         /// <inheritdoc/>
     61         public IWinGetLogging LoggingInit(string indexLogFile)
     62         {
     63             try
     64             {
     65                 WinGetLoggingInit(indexLogFile);
     66                 return new WinGetLogging(indexLogFile);
     67             }
     68             catch (Exception e)
     69             {
     70                 throw new WinGetLoggingException(e);
     71             }
     72         }
     73 
     74         /// <inheritdoc/>
     75         public CreateManifestResult CreateManifest(string manifestPath, string mergedManifestPath, WinGetCreateManifestOption option)
     76         {
     77             try
     78             {
     79                 bool succeeded = false;
     80                 IntPtr manifestHandle = IntPtr.Zero;
     81                 string failureOrWarningMessage = null;
     82                 WinGetCreateManifest(
     83                     manifestPath,
     84                     out succeeded,
     85                     out manifestHandle,
     86                     out failureOrWarningMessage,
     87                     mergedManifestPath,
     88                     option);
     89 
     90                 // WinGetUtil uses exceptions for passing warning messages. When a warning is detected,
     91                 // WinGetUtil will throw so a manifest handle will not be returned, in this case, we try
     92                 // to create the manifest handle without any validation.
     93                 if (succeeded && manifestHandle == IntPtr.Zero)
     94                 {
     95                     WinGetCreateManifest(
     96                         manifestPath,
     97                         out succeeded,
     98                         out manifestHandle,
     99                         out string failureNotExpected,
    100                         null,
    101                         WinGetCreateManifestOption.NoValidation);
    102                 }
    103 
    104                 return new CreateManifestResult(succeeded, failureOrWarningMessage, succeeded ? new WinGetManifest(manifestHandle) : null);
    105             }
    106             catch (Exception e)
    107             {
    108                 throw new WinGetManifestException(e);
    109             }
    110         }
    111 
    112         /// <summary>
    113         /// Begins the installer metadata collection process.
    114         /// </summary>
    115         /// <param name="input">input.</param>
    116         /// <param name="logFilePath">Log file path.</param>
    117         /// <param name="options">An enum of type <see cref="WinGetBeginInstallerMetadataCollectionOptions"/>.</param>
    118         /// <param name="outputFilePath">Metadata output file path.</param>
    119         /// <returns>IWinGetInstallerMetadata with the handle to the installer metadata collection.</returns>
    120         public IWinGetInstallerMetadata BeginInstallerMetadataCollection(
    121             string input,
    122             string logFilePath,
    123             WinGetBeginInstallerMetadataCollectionOptions options,
    124             string outputFilePath)
    125         {
    126             try
    127             {
    128                 IntPtr collectionHandle = IntPtr.Zero;
    129                 WinGetBeginInstallerMetadataCollection(
    130                     input,
    131                     logFilePath,
    132                     options,
    133                     out collectionHandle);
    134 
    135                 return new WinGetInstallerMetadata(collectionHandle, outputFilePath);
    136             }
    137             catch (Exception e)
    138             {
    139                 throw new WinGetInstallerMetadataException(e);
    140             }
    141         }
    142 
    143         /// <summary>
    144         /// Creates a new index file at filePath with the given version.
    145         /// </summary>
    146         /// <param name="filePath">File path to create index.</param>
    147         /// <param name="majorVersion">Major version.</param>
    148         /// <param name="minorVersion">Minor version.</param>
    149         /// <param name="index">Out handle of the index.</param>
    150         /// <returns>HRESULT.</returns>
    151         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    152         private static extern IntPtr WinGetSQLiteIndexCreate(string filePath, uint majorVersion, uint minorVersion, out IntPtr index);
    153 
    154         /// <summary>
    155         /// Opens an existing index at filePath.
    156         /// </summary>
    157         /// <param name="filePath">File path of index.</param>
    158         /// <param name="index">Out handle of the index.</param>
    159         /// <returns>HRESULT.</returns>
    160         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    161         private static extern IntPtr WinGetSQLiteIndexOpen(string filePath, out IntPtr index);
    162 
    163         /// <summary>
    164         /// Initializes the logging infrastructure.
    165         /// </summary>
    166         /// <param name="logPath">Log path.</param>
    167         /// <returns>HRESULT.</returns>
    168         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    169         private static extern IntPtr WinGetLoggingInit(string logPath);
    170 
    171         /// <summary>
    172         /// Creates a manifest with schema or semantic validation if needed.
    173         /// </summary>
    174         /// <param name="inputPath">Path of manifest.</param>
    175         /// <param name="succeeded">If create manifest succeeded.</param>
    176         /// <param name="manifestHandle">Out bool is validation succeeded.</param>
    177         /// <param name="failureMessage">Out string failure message, if any.</param>
    178         /// <param name="mergedManifestPath">Path to merged manifest file. Empty means no merged manifest needed.</param>
    179         /// <param name="option">Validate manifest option.</param>
    180         /// <returns>HRESULT.</returns>
    181         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    182         private static extern IntPtr WinGetCreateManifest(
    183             string inputPath,
    184             [MarshalAs(UnmanagedType.U1)] out bool succeeded,
    185             out IntPtr manifestHandle,
    186             [MarshalAs(UnmanagedType.BStr)] out string failureMessage,
    187             string mergedManifestPath,
    188             WinGetCreateManifestOption option);
    189 
    190         /// <summary>
    191         /// Begins the installer metadata collection process. By default, inputJSON is expected to be a JSON string.
    192         /// See the WinGetBeginInstallerMetadataCollectionOptions for more options.
    193         /// logFilePath optionally specifies where to write the log file for the collection operation.
    194         /// The collectionHandle is owned by the caller and must be passed to WinGetCompleteInstallerMetadataCollection to free it.
    195         /// </summary>
    196         /// <param name="inputJSON">Input Json.</param>
    197         /// <param name="logFilePath">Log file path.</param>
    198         /// <param name="options">An enum of type <see cref="WinGetBeginInstallerMetadataCollectionOptions"/>.</param>
    199         /// <param name="collectionHandle">Collection handle.</param>
    200         /// <returns>Metadata Collection handle.</returns>
    201         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    202         private static extern IntPtr WinGetBeginInstallerMetadataCollection(
    203             string inputJSON,
    204             string logFilePath,
    205             WinGetBeginInstallerMetadataCollectionOptions options,
    206             out IntPtr collectionHandle);
    207     }
    208 }