Exemplo n.º 1
0
    /// <summary>
    /// Creates a temporary directory from a retrieval method. Sets missing properties in the process.
    /// </summary>
    /// <param name="retrievalMethod">The retrieval method.</param>
    /// <param name="handler">A callback object used when the the user is to be informed about progress.</param>
    /// <param name="localPath">An optional local file path where the <paramref name="retrievalMethod"/> has already been downloaded. Leave <c>null</c> to download automatically.</param>
    /// <returns>A temporary directory built using the retrieval method.</returns>
    /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
    /// <exception cref="WebException">A file could not be downloaded from the internet.</exception>
    /// <exception cref="IOException">There is a problem writing a temporary file.</exception>
    /// <exception cref="UnauthorizedAccessException">Write access to a temporary file is not permitted.</exception>
    public static TemporaryDirectory ToTempDir(this DownloadRetrievalMethod retrievalMethod, ITaskHandler handler, string?localPath = null)
    {
        #region Sanity checks
        if (retrievalMethod == null)
        {
            throw new ArgumentNullException(nameof(retrievalMethod));
        }
        if (handler == null)
        {
            throw new ArgumentNullException(nameof(handler));
        }
        #endregion

        var tempDir = new TemporaryDirectory("0publish");
        try
        {
            var builder = new DirectoryBuilder(tempDir);
            builder.Add(retrievalMethod, new SimpleCommandExecutor(), handler, localPath);
            return(tempDir);
        }
        catch
        {
            tempDir.Dispose();
            throw;
        }
    }