コード例 #1
0
        public async Task InstallAsync_RelativeFile()
        {
            IProvider provider = _dependencies.GetProvider("filesystem");

            var desiredState = new LibraryInstallationState
            {
                ProviderId      = "filesystem",
                LibraryId       = "folder/file.txt",
                DestinationPath = "lib",
                Files           = new[] { "relative.txt" }
            };

            ILibraryInstallationResult result = await provider.InstallAsync(desiredState, CancellationToken.None);

            Assert.IsTrue(result.Success, "Didn't install");

            string copiedFile = Path.Combine(_projectFolder, desiredState.DestinationPath, desiredState.Files[0]);

            Assert.IsTrue(File.Exists(copiedFile), "File1 wasn't copied");
            Assert.IsFalse(result.Cancelled);
            Assert.AreSame(desiredState, result.InstallationState);
            Assert.AreEqual(0, result.Errors.Count);

            var manifest = Manifest.FromJson("{}", _dependencies);

            manifest.AddLibrary(desiredState);
            await manifest.SaveAsync(_configFilePath, CancellationToken.None);

            Assert.IsTrue(File.Exists(_configFilePath));
            Assert.AreEqual(File.ReadAllText(copiedFile), "test content");
        }
コード例 #2
0
        public async Task InstallAsync_AbsoluteFolderFiles()
        {
            string folder = Path.Combine(Path.GetTempPath(), "LibraryInstaller_test");

            Directory.CreateDirectory(folder);
            File.WriteAllText(Path.Combine(folder, "file1.js"), "");
            File.WriteAllText(Path.Combine(folder, "file2.js"), "");
            File.WriteAllText(Path.Combine(folder, "file3.js"), "");

            IProvider provider = _dependencies.GetProvider("filesystem");

            var desiredState = new LibraryInstallationState
            {
                ProviderId      = "filesystem",
                LibraryId       = folder,
                DestinationPath = "lib",
                Files           = new[] { "file1.js", "file2.js" }
            };

            ILibraryInstallationResult result = await provider.InstallAsync(desiredState, CancellationToken.None);

            Assert.IsTrue(result.Success, "Didn't install");

            string file1 = Path.Combine(_projectFolder, desiredState.DestinationPath, desiredState.Files[0]);
            string file2 = Path.Combine(_projectFolder, desiredState.DestinationPath, desiredState.Files[1]);

            Assert.IsTrue(File.Exists(file1), "File1 wasn't copied");
            Assert.IsTrue(File.Exists(file2), "File2 wasn't copied");

            Assert.IsFalse(result.Cancelled);
            Assert.AreSame(desiredState, result.InstallationState);
            Assert.AreEqual(0, result.Errors.Count);
        }
コード例 #3
0
        public async Task InstallAsync_Uri()
        {
            IProvider provider = _dependencies.GetProvider("filesystem");

            var desiredState = new LibraryInstallationState
            {
                ProviderId      = "filesystem",
                LibraryId       = "https://raw.githubusercontent.com/jquery/jquery/master/src/event.js",
                DestinationPath = "lib",
                Files           = new[] { "event.js" }
            };

            ILibraryInstallationResult result = await provider.InstallAsync(desiredState, CancellationToken.None);

            Assert.IsTrue(result.Success, "Didn't install");

            string copiedFile = Path.Combine(_projectFolder, desiredState.DestinationPath, desiredState.Files[0]);

            Assert.IsTrue(File.Exists(copiedFile), "File wasn't copied");

            var manifest = Manifest.FromJson("{}", _dependencies);

            manifest.AddLibrary(desiredState);
            await manifest.SaveAsync(_configFilePath, CancellationToken.None);

            Assert.IsTrue(File.Exists(_configFilePath));
            Assert.IsTrue(File.ReadAllText(copiedFile).Length > 1000);
        }
コード例 #4
0
ファイル: Manifest.cs プロジェクト: justcla/LibraryManager
        private ILibraryInstallationResult DeleteLibraryFiles(ILibraryInstallationState state, Action <string> deleteFileAction)
        {
            int filesDeleted = 0;

            IProvider provider = _dependencies.GetProvider(state.ProviderId);
            ILibraryInstallationResult updatedStateResult = provider.UpdateStateAsync(state, CancellationToken.None).Result;

            if (updatedStateResult.Success)
            {
                state = updatedStateResult.InstallationState;
                foreach (string file in state.Files)
                {
                    var url = new Uri(file, UriKind.RelativeOrAbsolute);

                    if (!url.IsAbsoluteUri)
                    {
                        string relativePath = Path.Combine(state.DestinationPath, file).Replace('\\', '/');
                        deleteFileAction?.Invoke(relativePath);
                        filesDeleted++;
                    }
                }

                if (state.Files != null && filesDeleted == state.Files.Count())
                {
                    return(LibraryInstallationResult.FromSuccess(updatedStateResult.InstallationState));
                }
            }

            return(updatedStateResult);
        }
コード例 #5
0
        public async Task SaveAsync_Success()
        {
            var manifest = new Manifest(_dependencies);

            IProvider provider     = _dependencies.GetProvider("cdnjs");
            var       desiredState = new LibraryInstallationState
            {
                LibraryId       = "[email protected]",
                ProviderId      = "cdnjs",
                DestinationPath = "lib",
                Files           = new[] { "jquery.min.js" }
            };

            ILibraryInstallationResult result = await provider.InstallAsync(desiredState, CancellationToken.None).ConfigureAwait(false);

            Assert.IsTrue(result.Success);

            manifest.AddLibrary(desiredState);
            await manifest.SaveAsync(_filePath, CancellationToken.None).ConfigureAwait(false);

            Manifest newManifest = await Manifest.FromFileAsync(_filePath, _dependencies, CancellationToken.None).ConfigureAwait(false);

            Assert.IsTrue(File.Exists(_filePath));
            Assert.AreEqual(manifest.Libraries.Count(), newManifest.Libraries.Count());
            Assert.AreEqual(manifest.Version, newManifest.Version);
        }
コード例 #6
0
        private async Task <IEnumerable <FileIdentifier> > GetAllManifestFilesWithVersionsAsync(Manifest manifest)
        {
            var files = new List <FileIdentifier>();

            if (manifest != null)
            {
                foreach (ILibraryInstallationState state in manifest.Libraries.Where(l => l.IsValid(out IEnumerable <IError> errors)))
                {
                    IProvider provider = _dependencies.GetProvider(state.ProviderId);

                    if (provider != null)
                    {
                        ILibraryInstallationResult updatedStateResult = provider.UpdateStateAsync(state, CancellationToken.None).Result;

                        if (updatedStateResult.Success)
                        {
                            IEnumerable <FileIdentifier> stateFiles = await GetFilesWithVersionsAsync(updatedStateResult.InstallationState).ConfigureAwait(false);

                            foreach (FileIdentifier fileIdentifier in stateFiles)
                            {
                                if (!files.Contains(fileIdentifier))
                                {
                                    files.Add(fileIdentifier);
                                }
                            }
                        }
                    }
                }
            }

            return(files);
        }
コード例 #7
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>
        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));
        }
コード例 #8
0
        public async Task InstallAsync_NoPathDefined()
        {
            var desiredState = new LibraryInstallationState
            {
                ProviderId = "cdnjs",
                LibraryId  = "[email protected]"
            };

            // Install library
            ILibraryInstallationResult result = await _provider.InstallAsync(desiredState, CancellationToken.None).ConfigureAwait(false);

            Assert.IsFalse(result.Success);
            Assert.AreEqual("LIB005", result.Errors.First().Code);
        }
コード例 #9
0
        public async Task EndToEndTestAsync()
        {
            IProvider       provider = _dependencies.GetProvider("cdnjs");
            ILibraryCatalog catalog  = provider.GetCatalog();

            // Search for libraries to display in search result
            IReadOnlyList <ILibraryGroup> groups = await catalog.SearchAsync("jquery", 4, CancellationToken.None);

            Assert.AreEqual(4, groups.Count);

            // Show details for selected library
            ILibraryGroup group = groups.FirstOrDefault();

            Assert.AreEqual("jquery", group.DisplayName);
            Assert.IsNotNull(group.Description);

            // Get all libraries in group to display version list
            IEnumerable <string> libraryIds = await group.GetLibraryIdsAsync(CancellationToken.None);

            Assert.IsTrue(libraryIds.Count() >= 67);
            Assert.AreEqual("[email protected]", libraryIds.Last(), "Library version mismatch");

            // Get the library to install
            ILibrary library = await catalog.GetLibraryAsync(libraryIds.First(), CancellationToken.None);

            Assert.AreEqual(group.DisplayName, library.Name);

            var desiredState = new LibraryInstallationState
            {
                LibraryId       = "[email protected]",
                ProviderId      = "cdnjs",
                DestinationPath = "lib",
                Files           = new[] { "jquery.js", "jquery.min.js" }
            };

            // Install library
            ILibraryInstallationResult result = await provider.InstallAsync(desiredState, CancellationToken.None).ConfigureAwait(false);

            foreach (string file in desiredState.Files)
            {
                string absolute = Path.Combine(_projectFolder, desiredState.DestinationPath, file);
                Assert.IsTrue(File.Exists(absolute));
            }

            Assert.IsTrue(result.Success);
            Assert.IsFalse(result.Cancelled);
            Assert.AreSame(desiredState, result.InstallationState);
            Assert.AreEqual(0, result.Errors.Count);
        }
コード例 #10
0
        public async Task InstallAsync_InvalidState()
        {
            var desiredState = new LibraryInstallationState
            {
                LibraryId       = "*&(}:@3.1.1",
                ProviderId      = "cdnjs",
                DestinationPath = "lib",
                Files           = new[] { "jquery.min.js" }
            };

            // Install library
            ILibraryInstallationResult result = await _provider.InstallAsync(desiredState, CancellationToken.None).ConfigureAwait(false);

            Assert.IsFalse(result.Success);
        }
コード例 #11
0
        public async Task InstallAsync_IdNotDefined()
        {
            IProvider provider = _dependencies.GetProvider("filesystem");

            var desiredState = new LibraryInstallationState
            {
                ProviderId      = "filesystem",
                DestinationPath = "lib",
                Files           = new[] { "file.js" }
            };

            ILibraryInstallationResult result = await provider.InstallAsync(desiredState, CancellationToken.None);

            Assert.IsFalse(result.Success);
            Assert.AreEqual("LIB006", result.Errors[0].Code);
        }
コード例 #12
0
        public async Task InstallAsync_InvalidLibraryFiles()
        {
            var desiredState = new LibraryInstallationState
            {
                LibraryId       = "[email protected]",
                ProviderId      = "cdnjs",
                DestinationPath = "lib",
                Files           = new[] { "file1.txt", "file2.txt" }
            };

            // Install library
            ILibraryInstallationResult result = await _provider.InstallAsync(desiredState, CancellationToken.None).ConfigureAwait(false);

            Assert.IsFalse(result.Success);
            Assert.AreEqual("LIB003", result.Errors[0].Code);
        }
コード例 #13
0
        public async Task InstallAsync_UriImage()
        {
            IProvider provider = _dependencies.GetProvider("filesystem");

            var desiredState = new LibraryInstallationState
            {
                ProviderId      = "filesystem",
                LibraryId       = "http://glyphlist.azurewebsites.net/img/images/Flag.png",
                DestinationPath = "lib",
                Files           = new[] { "Flag.png" }
            };

            ILibraryInstallationResult result = await provider.InstallAsync(desiredState, CancellationToken.None);

            Assert.IsTrue(result.Success, "Didn't install");

            string copiedFile = Path.Combine(_projectFolder, desiredState.DestinationPath, desiredState.Files[0]);

            Assert.IsTrue(File.Exists(copiedFile), "File wasn't copied");
        }
コード例 #14
0
        public async Task InstallAsync_EmptyFilesArray()
        {
            var desiredState = new LibraryInstallationState
            {
                ProviderId      = "cdnjs",
                LibraryId       = "[email protected]",
                DestinationPath = "lib"
            };

            // Install library
            ILibraryInstallationResult result = await _provider.InstallAsync(desiredState, CancellationToken.None).ConfigureAwait(false);

            Assert.IsTrue(result.Success);

            foreach (string file in new[] { "jquery.js", "jquery.min.js" })
            {
                string absolute = Path.Combine(_projectFolder, desiredState.DestinationPath, file);
                Assert.IsTrue(File.Exists(absolute));
            }
        }