public void Setup()
 {
     _cacheFolder     = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");
     _projectFolder   = Path.Combine(Path.GetTempPath(), "LibraryManager");
     _hostInteraction = new HostInteraction(_projectFolder, _cacheFolder);
     _dependencies    = new Dependencies(_hostInteraction, new CdnjsProviderFactory(), new FileSystemProviderFactory(), new UnpkgProviderFactory());
 }
        private CdnjsCatalog SetupCatalog(ICacheService testCacheService = null, Dictionary <string, string> prepopulateCacheFiles = null)
        {
            string        projectFolder   = Path.Combine(Path.GetTempPath(), "LibraryManager");
            string        cacheFolder     = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");
            var           hostInteraction = new HostInteraction(projectFolder, cacheFolder);
            ICacheService cacheService    = testCacheService ?? new Mock <ICacheService>().SetupCatalog()
                                            .SetupSampleLibrary()
                                            .Object;

            if (prepopulateCacheFiles != null)
            {
                foreach (KeyValuePair <string, string> item in prepopulateCacheFiles)
                {
                    // put the provider IdText into the path to mimic the provider implementation
                    string filePath      = Path.Combine(cacheFolder, CdnjsProvider.IdText, item.Key);
                    string directoryPath = Path.GetDirectoryName(filePath);
                    Directory.CreateDirectory(directoryPath);
                    File.WriteAllText(filePath, item.Value);
                    _prepopulatedFiles.Add(filePath);
                }
            }

            var provider = new CdnjsProvider(hostInteraction, cacheService: null);

            return(new CdnjsCatalog(provider, cacheService, new VersionedLibraryNamingScheme()));
        }
示例#3
0
 public void Setup()
 {
     _cacheFolder     = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");
     _projectFolder   = Path.Combine(Path.GetTempPath(), "LibraryManager");
     _hostInteraction = new HostInteraction(_projectFolder, _cacheFolder);
     _dependencies    = new Dependencies(_hostInteraction, new CdnjsProviderFactory(), new FileSystemProviderFactory(), new UnpkgProviderFactory(), new JsDelivrProviderFactory());
     LibraryIdToNameAndVersionConverter.Instance.Reinitialize(_dependencies);
 }
示例#4
0
        /// <summary>
        /// Installs a library as specified in the <paramref name="desiredState" /> parameter.
        /// </summary>
        /// <param name="desiredState">The details about the library to install.</param>
        /// <param name="cancellationToken">A token that allows for the operation to be cancelled.</param>
        /// <returns>
        /// The <see cref="T:Microsoft.Web.LibraryManager.Contracts.ILibraryInstallationResult" /> from the installation process.
        /// </returns>
        /// <exception cref="InvalidLibraryException"></exception>
        public async Task <ILibraryInstallationResult> InstallAsync(ILibraryInstallationState desiredState, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(LibraryInstallationResult.FromCancelled(desiredState));
            }

            if (!desiredState.IsValid(out IEnumerable <IError> errors))
            {
                return(new LibraryInstallationResult(desiredState, errors.ToArray()));
            }

            try
            {
                ILibraryInstallationResult result = await UpdateStateAsync(desiredState, cancellationToken);

                if (!result.Success)
                {
                    return(result);
                }

                desiredState = result.InstallationState;

                foreach (string file in desiredState.Files)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(LibraryInstallationResult.FromCancelled(desiredState));
                    }

                    if (string.IsNullOrEmpty(file))
                    {
                        return(new LibraryInstallationResult(desiredState, PredefinedErrors.CouldNotWriteFile(file)));
                    }

                    string path         = Path.Combine(desiredState.DestinationPath, file);
                    var    sourceStream = new Func <Stream>(() => GetStreamAsync(desiredState, file, cancellationToken).Result);
                    bool   writeOk      = await HostInteraction.WriteFileAsync(path, sourceStream, desiredState, cancellationToken).ConfigureAwait(false);

                    if (!writeOk)
                    {
                        return(new LibraryInstallationResult(desiredState, PredefinedErrors.CouldNotWriteFile(file)));
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
                return(new LibraryInstallationResult(desiredState, PredefinedErrors.PathOutsideWorkingDirectory()));
            }
            catch (Exception ex)
            {
                HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error);
                return(new LibraryInstallationResult(desiredState, PredefinedErrors.UnknownException()));
            }

            return(LibraryInstallationResult.FromSuccess(desiredState));
        }
示例#5
0
        public void Setup()
        {
            _projectFolder = Path.Combine(Path.GetTempPath(), "LibraryManager");

            var hostInteraction = new HostInteraction(_projectFolder, "");
            var dependencies    = new Dependencies(hostInteraction, new FileSystemProviderFactory());

            _provider = dependencies.GetProvider("filesystem");
            _catalog  = new FileSystemCatalog((FileSystemProvider)_provider, true);
        }
        public void Setup()
        {
            _projectFolder = Path.Combine(Path.GetTempPath(), "LibraryInstaller");

            var hostInteraction = new HostInteraction(_projectFolder, "");
            var dependencies    = new Dependencies(hostInteraction, new FileSystemProvider());

            _provider = dependencies.GetProvider("filesystem");
            _catalog  = _provider.GetCatalog();
        }
        public void Setup()
        {
            string projectFolder   = Path.Combine(Path.GetTempPath(), "LibraryManager");
            string cacheFolder     = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");
            var    hostInteraction = new HostInteraction(projectFolder, cacheFolder);
            var    dependencies    = new Dependencies(hostInteraction, new CdnjsProviderFactory());

            _provider = dependencies.GetProvider("cdnjs");
            _catalog  = _provider.GetCatalog();
        }
        public void Setup()
        {
            _cacheFolder   = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");
            _projectFolder = Path.Combine(Path.GetTempPath(), "LibraryInstaller");
            var hostInteraction = new HostInteraction(_projectFolder, _cacheFolder);

            _dependencies = new Dependencies(hostInteraction, new CdnjsProvider());

            Directory.CreateDirectory(_projectFolder);
        }
示例#9
0
        public void Setup()
        {
            _projectFolder = Path.Combine(Path.GetTempPath(), "LibraryManager");

            var hostInteraction = new HostInteraction(_projectFolder, "");
            var dependencies    = new Dependencies(hostInteraction, new UnpkgProviderFactory());

            _provider = dependencies.GetProvider("unpkg");
            _catalog  = new UnpkgCatalog((UnpkgProvider)_provider);
        }
示例#10
0
        private async Task <Stream> GetStreamAsync(ILibraryInstallationState state, string sourceFile, CancellationToken cancellationToken)
        {
            string absolute = Path.Combine(CacheFolder, state.Name, state.Version, sourceFile);

            if (File.Exists(absolute))
            {
                return(await HostInteraction.ReadFileAsync(absolute, cancellationToken).ConfigureAwait(false));
            }

            return(null);
        }
示例#11
0
        private CdnjsCatalog Initialize()
        {
            string projectFolder   = Path.Combine(Path.GetTempPath(), "LibraryManager");
            string cacheFolder     = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");
            var    hostInteraction = new HostInteraction(projectFolder, cacheFolder);
            var    cacheService    = new FakeCdnjsCacheService();

            var provider = new CdnjsProvider(hostInteraction, cacheService: null);

            return(new CdnjsCatalog(provider, cacheService, new VersionedLibraryNamingScheme()));
        }
        public void Setup()
        {
            string projectFolder   = Path.Combine(Path.GetTempPath(), "LibraryManager");
            string cacheFolder     = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");
            var    hostInteraction = new HostInteraction(projectFolder, cacheFolder);
            var    dependencies    = new Dependencies(hostInteraction, new UnpkgProviderFactory());

            LibraryIdToNameAndVersionConverter.Instance.EnsureInitialized(dependencies);

            _provider = dependencies.GetProvider("unpkg");
            _catalog  = _provider.GetCatalog();
        }
        public void Setup()
        {
            _cacheFolder   = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");
            _projectFolder = Path.Combine(Path.GetTempPath(), "LibraryManager");
            var hostInteraction = new HostInteraction(_projectFolder, _cacheFolder);

            _dependencies = new Dependencies(hostInteraction, new CdnjsProviderFactory());
            _provider     = _dependencies.GetProvider("cdnjs");
            LibraryIdToNameAndVersionConverter.Instance.EnsureInitialized(_dependencies);

            Directory.CreateDirectory(_projectFolder);
        }
示例#14
0
        public void Setup()
        {
            _cacheFolder   = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");
            _projectFolder = Path.Combine(Path.GetTempPath(), "LibraryManager");
            _filePath      = Path.Combine(_projectFolder, "library.json");

            _hostInteraction = new HostInteraction(_projectFolder, _cacheFolder);
            _dependencies    = new Dependencies(_hostInteraction, new CdnjsProviderFactory(), new FileSystemProviderFactory());

            Directory.CreateDirectory(_projectFolder);
            File.WriteAllText(_filePath, _doc);
        }
示例#15
0
        public void Setup()
        {
            _cacheFolder   = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");
            _projectFolder = Path.Combine(Path.GetTempPath(), "LibraryManager");
            _filePath      = Path.Combine(_projectFolder, "libman.json");

            var npmPackageSearch   = new Mock <INpmPackageSearch>();
            var packageInfoFactory = new Mock <INpmPackageInfoFactory>();

            _hostInteraction = new HostInteraction(_projectFolder, _cacheFolder);
            _dependencies    = new Dependencies(_hostInteraction, new CdnjsProviderFactory(), new FileSystemProviderFactory(), new UnpkgProviderFactory(npmPackageSearch.Object, packageInfoFactory.Object));
            LibraryIdToNameAndVersionConverter.Instance.Reinitialize(_dependencies);
        }
示例#16
0
        public void Setup()
        {
            _cacheFolder      = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");
            _catalogCacheFile = Path.Combine(_cacheFolder, "TestCatalog.json");
            _projectFolder    = Path.Combine(Path.GetTempPath(), "LibraryManager");
            _filePath         = Path.Combine(_projectFolder, "libman.json");

            _hostInteraction = new HostInteraction(_projectFolder, _cacheFolder);
            _dependencies    = new Dependencies(_hostInteraction, new CdnjsProviderFactory(), new FileSystemProviderFactory());
            _cacheService    = new CacheService(new Mocks.WebRequestHandler());

            Directory.CreateDirectory(_projectFolder);
        }
        public void Setup()
        {
            string cacheFolder = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");

            _projectFolder = Path.Combine(Path.GetTempPath(), "LibraryManager");

            var hostInteraction = new HostInteraction(_projectFolder, cacheFolder);
            var dependencies    = new Dependencies(hostInteraction, new JsDelivrProviderFactory());

            _provider = dependencies.GetProvider("jsdelivr");

            LibraryIdToNameAndVersionConverter.Instance.Reinitialize(dependencies);
            Directory.CreateDirectory(_projectFolder);
        }
示例#18
0
        public void Setup()
        {
            string cacheFolder = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\");

            _projectFolder = Path.Combine(Path.GetTempPath(), "LibraryManager");

            var hostInteraction = new HostInteraction(_projectFolder, cacheFolder);

            var requestHandler     = new Mocks.WebRequestHandler();
            var npmPackageSearch   = new NpmPackageSearch(requestHandler);
            var packageInfoFactory = new NpmPackageInfoFactory(requestHandler);

            var dependencies = new Dependencies(hostInteraction, new UnpkgProviderFactory(npmPackageSearch, packageInfoFactory));

            _provider = dependencies.GetProvider("unpkg");

            LibraryIdToNameAndVersionConverter.Instance.Reinitialize(dependencies);
            Directory.CreateDirectory(_projectFolder);
        }
示例#19
0
        /// <summary>
        /// Copy files from the download cache to the desired installation state
        /// </summary>
        /// <remarks>Precondition: all files must already exist in the cache</remarks>
        protected async Task <ILibraryOperationResult> WriteToFilesAsync(ILibraryInstallationState state, CancellationToken cancellationToken)
        {
            if (state.Files != null)
            {
                try
                {
                    foreach (string file in state.Files)
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            return(LibraryOperationResult.FromCancelled(state));
                        }

                        if (string.IsNullOrEmpty(file))
                        {
                            string id = LibraryNamingScheme.GetLibraryId(state.Name, state.Version);
                            return(new LibraryOperationResult(state, PredefinedErrors.FileNameMustNotBeEmpty(id)));
                        }

                        string sourcePath      = GetCachedFileLocalPath(state, file);
                        string destinationPath = Path.Combine(state.DestinationPath, file);
                        bool   writeOk         = await HostInteraction.CopyFileAsync(sourcePath, destinationPath, cancellationToken);

                        if (!writeOk)
                        {
                            return(new LibraryOperationResult(state, PredefinedErrors.CouldNotWriteFile(file)));
                        }
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    return(new LibraryOperationResult(state, PredefinedErrors.PathOutsideWorkingDirectory()));
                }
                catch (Exception ex)
                {
                    HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error);
                    return(new LibraryOperationResult(state, PredefinedErrors.UnknownException()));
                }
            }

            return(LibraryOperationResult.FromSuccess(state));
        }
        public void Setup()
        {
            _projectFolder  = Path.Combine(Path.GetTempPath(), "LibraryManager\\");
            _configFilePath = Path.Combine(_projectFolder, "libman.json");
            _relativeSrc    = Path.Combine(_projectFolder, "folder", "file.txt");

            var hostInteraction = new HostInteraction(_projectFolder, "");

            _dependencies = new Dependencies(hostInteraction, new FileSystemProviderFactory());

            // Create the files to install
            Directory.CreateDirectory(_projectFolder);
            _file1 = Path.GetTempFileName();
            _file2 = Path.GetTempFileName();
            File.WriteAllText(_file1, "test content");
            File.WriteAllText(_file2, "test content");

            Directory.CreateDirectory(Path.GetDirectoryName(_relativeSrc));
            File.WriteAllText(_relativeSrc, "test content");
        }
示例#21
0
        private async Task <ILibraryOperationResult> WriteToFilesAsync(ILibraryInstallationState state, CancellationToken cancellationToken)
        {
            if (state.Files != null)
            {
                try
                {
                    foreach (string file in state.Files)
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            return(LibraryOperationResult.FromCancelled(state));
                        }

                        if (string.IsNullOrEmpty(file))
                        {
                            return(new LibraryOperationResult(state, PredefinedErrors.CouldNotWriteFile(file)));
                        }

                        string destinationPath = Path.Combine(state.DestinationPath, file);
                        var    sourceStream    = new Func <Stream>(() => GetStreamAsync(state, file, cancellationToken).Result);
                        bool   writeOk         = await HostInteraction.WriteFileAsync(destinationPath, sourceStream, state, cancellationToken).ConfigureAwait(false);

                        if (!writeOk)
                        {
                            return(new LibraryOperationResult(state, PredefinedErrors.CouldNotWriteFile(file)));
                        }
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    return(new LibraryOperationResult(state, PredefinedErrors.PathOutsideWorkingDirectory()));
                }
                catch (Exception ex)
                {
                    HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error);
                    return(new LibraryOperationResult(state, PredefinedErrors.UnknownException()));
                }
            }

            return(LibraryOperationResult.FromSuccess(state));
        }
示例#22
0
        public async Task Test_DeleteFilesAsync()
        {
            string workingDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            Directory.CreateDirectory(workingDir);

            EnvironmentSettings settings = TestEnvironmentHelper.GetTestSettings(
                workingDir,
                Path.Combine(workingDir, "cache"));

            IHostInteractionInternal hostInteraction = new HostInteraction(settings);

            Directory.CreateDirectory(Path.Combine(workingDir, "jquery"));
            string jqueryFilePath = Path.Combine("jquery", "jquery.min.js");

            File.WriteAllText(Path.Combine(workingDir, jqueryFilePath), "");

            await hostInteraction.DeleteFilesAsync(new[] { jqueryFilePath }, CancellationToken.None);

            Assert.IsFalse(File.Exists(Path.Combine(workingDir, jqueryFilePath)));
            Assert.IsFalse(Directory.Exists(Path.Combine(workingDir, "jquery")));
        }
        /// <summary>
        /// Installs a library as specified in the <paramref name="desiredState" /> parameter.
        /// </summary>
        /// <param name="desiredState">The details about the library to install.</param>
        /// <param name="cancellationToken">A token that allows for the operation to be cancelled.</param>
        /// <returns>
        /// The <see cref="T:Microsoft.Web.LibraryInstaller.Contracts.ILibraryInstallationResult" /> from the installation process.
        /// </returns>
        /// <exception cref="InvalidLibraryException"></exception>
        public async Task <ILibraryInstallationResult> InstallAsync(ILibraryInstallationState desiredState, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(LibraryInstallationResult.FromCancelled(desiredState));
            }

            if (!desiredState.IsValid(out IEnumerable <IError> errors))
            {
                return(new LibraryInstallationResult(desiredState, errors.ToArray()));
            }


            try
            {
                var      catalog = (CdnjsCatalog)GetCatalog();
                ILibrary library = await catalog.GetLibraryAsync(desiredState.LibraryId, cancellationToken).ConfigureAwait(false);

                if (library == null)
                {
                    throw new InvalidLibraryException(desiredState.LibraryId, Id);
                }

                await HydrateCacheAsync(library, cancellationToken).ConfigureAwait(false);

                var files = desiredState.Files?.ToList();
                // "Files" is optional on this provider, so when none are specified all should be used
                if (files == null || files.Count == 0)
                {
                    desiredState = new LibraryInstallationState
                    {
                        ProviderId      = Id,
                        LibraryId       = desiredState.LibraryId,
                        DestinationPath = desiredState.DestinationPath,
                        Files           = library.Files.Keys.ToList(),
                    };
                }

                foreach (string file in desiredState.Files)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(LibraryInstallationResult.FromCancelled(desiredState));
                    }

                    string path         = Path.Combine(desiredState.DestinationPath, file);
                    var    sourceStream = new Func <Stream>(() => GetStreamAsync(desiredState, file, cancellationToken).Result);
                    bool   writeOk      = await HostInteraction.WriteFileAsync(path, sourceStream, desiredState, cancellationToken).ConfigureAwait(false);

                    if (!writeOk)
                    {
                        return(new LibraryInstallationResult(desiredState, PredefinedErrors.CouldNotWriteFile(file)));
                    }
                }
            }
            catch (Exception ex) when(ex is InvalidLibraryException || ex.InnerException is InvalidLibraryException)
            {
                return(new LibraryInstallationResult(desiredState, PredefinedErrors.UnableToResolveSource(desiredState.LibraryId, desiredState.ProviderId)));
            }
            catch (UnauthorizedAccessException)
            {
                return(new LibraryInstallationResult(desiredState, PredefinedErrors.PathOutsideWorkingDirectory()));
            }
            catch (Exception ex)
            {
                HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error);
                return(new LibraryInstallationResult(desiredState, PredefinedErrors.UnknownException()));
            }

            return(LibraryInstallationResult.FromSuccess(desiredState));
        }
示例#24
0
        public async Task <ILibraryInstallationResult> InstallAsync(ILibraryInstallationState desiredState, CancellationToken cancellationToken)
        {
            try
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(LibraryInstallationResult.FromCancelled(desiredState));
                }

                var      catalog = (CdnjsCatalog)GetCatalog();
                ILibrary library = await catalog.GetLibraryAsync(desiredState.LibraryId, cancellationToken).ConfigureAwait(false);

                if (library == null)
                {
                    throw new InvalidLibraryException(desiredState.LibraryId, Id);
                }

                await HydrateCacheAsync(library, cancellationToken).ConfigureAwait(false);

                var files = desiredState.Files?.ToList();

                if (files == null || files.Count == 0)
                {
                    desiredState = new LibraryInstallationState
                    {
                        ProviderId      = Id,
                        LibraryId       = desiredState.LibraryId,
                        DestinationPath = desiredState.DestinationPath,
                        Files           = library.Files.Keys.ToList()
                    };
                }

                foreach (string file in desiredState.Files)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(LibraryInstallationResult.FromCancelled(desiredState));
                    }

                    string path    = Path.Combine(desiredState.DestinationPath, file);
                    var    func    = new Func <Stream>(() => GetStream(desiredState, file));
                    bool   writeOk = await HostInteraction.WriteFileAsync(path, func, desiredState, cancellationToken).ConfigureAwait(false);

                    if (!writeOk)
                    {
                        return(new LibraryInstallationResult(desiredState, PredefinedErrors.CouldNotWriteFile(file)));
                    }
                }
            }
            catch (Exception ex) when(ex is InvalidLibraryException || ex.InnerException is InvalidLibraryException)
            {
                return(new LibraryInstallationResult(desiredState, PredefinedErrors.UnableToResolveSource(desiredState.LibraryId, desiredState.ProviderId)));
            }
            catch (Exception ex)
            {
                HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error);
                return(new LibraryInstallationResult(desiredState, PredefinedErrors.UnknownException()));
            }

            return(LibraryInstallationResult.FromSuccess(desiredState));
        }
 /// <inheritdoc />
 public void UpdateWorkingDirectory(string directory)
 {
     EnvironmentSettings.CurrentWorkingDirectory = directory;
     HostInteraction.UpdateWorkingDirectory(directory);
 }