public void CanResolveParentOfRootFolder()
        {
            string dir = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "C:\\" : "/";

            IEnumerable <string> installPath = InstallRequestPathResolution.ExpandMaskedPath(dir + "..", _engineEnvironmentSettings);

            Assert.Equal(dir, installPath.Single());
        }
        public void CannotResolveNonExistingPath()
        {
            Assert.False(File.Exists("path"));
            IEnumerable <string> installPath = InstallRequestPathResolution.ExpandMaskedPath("path", _engineEnvironmentSettings);

            Assert.Equal("path", installPath.Single());

            installPath = InstallRequestPathResolution.ExpandMaskedPath("path\\", _engineEnvironmentSettings);
            Assert.Equal("path\\", installPath.Single());
        }
        public void CannotResolveMaskedPathInFolder()
        {
            var testRootDir = TestUtils.CreateTemporaryFolder();

            Directory.CreateDirectory(Path.Combine(testRootDir, "dir"));
            File.Create(Path.Combine(testRootDir, "dir", "1.nupkg"));
            File.Create(Path.Combine(testRootDir, "dir", "2.nupkg"));

            IEnumerable <string> installPath = InstallRequestPathResolution.ExpandMaskedPath(Path.Combine(testRootDir, "*", "*.nupkg"), _engineEnvironmentSettings);

            Assert.Equal(Path.Combine(testRootDir, "*", "*.nupkg"), installPath.Single());
        }
        public void CanResolveSubdirectories()
        {
            var testRootDir = TestUtils.CreateTemporaryFolder();

            Directory.CreateDirectory(Path.Combine(testRootDir, "dir1"));
            Directory.CreateDirectory(Path.Combine(testRootDir, "dir2"));
            Directory.CreateDirectory(Path.Combine(testRootDir, "dir3"));

            IEnumerable <string> installPath = InstallRequestPathResolution.ExpandMaskedPath(Path.Combine(testRootDir, "*"), _engineEnvironmentSettings);

            Assert.Equal(3, installPath.Count());
            Assert.Contains(Path.Combine(testRootDir, "dir1"), installPath);
        }
        public void CanResolveMaskedFiles()
        {
            var testRootDir = TestUtils.CreateTemporaryFolder();

            File.Create(Path.Combine(testRootDir, "1.nupkg"));
            File.Create(Path.Combine(testRootDir, "2.nupkg"));
            File.Create(Path.Combine(testRootDir, "3.txt"));

            IEnumerable <string> installPath = InstallRequestPathResolution.ExpandMaskedPath(Path.Combine(testRootDir, "*.nupkg"), _engineEnvironmentSettings);

            Assert.Equal(2, installPath.Count());
            Assert.Contains(Path.Combine(testRootDir, "1.nupkg"), installPath);
            Assert.Contains(Path.Combine(testRootDir, "2.nupkg"), installPath);
        }
            public Task <IReadOnlyList <ITemplatePackage> > GetAllTemplatePackagesAsync(CancellationToken cancellationToken)
            {
                List <ITemplatePackage> templatePackages = new List <ITemplatePackage>();
                string?assemblyLocation = Path.GetDirectoryName(typeof(Program).Assembly.Location);
                string?dn3Path          = _settings.Environment.GetEnvironmentVariable("DN3");

                if (string.IsNullOrEmpty(dn3Path))
                {
                    string?path = assemblyLocation;
                    while (path != null && !File.Exists(Path.Combine(path, "Microsoft.TemplateEngine.sln")))
                    {
                        path = Path.GetDirectoryName(path);
                    }
                    if (path == null)
                    {
                        _settings.Host.Logger.LogDebug("Couldn't the setup package location, because \"Microsoft.TemplateEngine.sln\" is not in any of parent directories.");
                        return(Task.FromResult((IReadOnlyList <ITemplatePackage>)templatePackages));
                    }
                    Environment.SetEnvironmentVariable("DN3", path);
                }

                string defaultPackagesFilePath = assemblyLocation != null
                    ? Path.Combine(assemblyLocation, "defaultinstall.template.list")
                    : "defaultinstall.template.list";

                if (_settings.Host.FileSystem.FileExists(defaultPackagesFilePath))
                {
                    string packagesToInstall = _settings.Host.FileSystem.ReadAllText(defaultPackagesFilePath);
                    foreach (string sourceLocation in packagesToInstall.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        string expandedPath = Environment.ExpandEnvironmentVariables(sourceLocation).Replace('\\', Path.DirectorySeparatorChar);
                        IEnumerable <string> expandedPaths = InstallRequestPathResolution.ExpandMaskedPath(expandedPath, _settings);
                        foreach (string path in expandedPaths)
                        {
                            if (_settings.Host.FileSystem.FileExists(path) || _settings.Host.FileSystem.DirectoryExists(path))
                            {
                                templatePackages.Add(new TemplatePackage(this, path, _settings.Host.FileSystem.GetLastWriteTimeUtc(path)));
                            }
                        }
                    }
                }

                return(Task.FromResult((IReadOnlyList <ITemplatePackage>)templatePackages));
            }
        private async Task <CreationResultStatus> EnterInstallFlowAsync(INewCommandInput commandInput, CancellationToken cancellationToken)
        {
            _ = commandInput ?? throw new ArgumentNullException(nameof(commandInput));
            cancellationToken.ThrowIfCancellationRequested();

            CreationResultStatus resultStatus = CreationResultStatus.Success;

            _telemetryLogger.TrackEvent(commandInput.CommandName + TelemetryConstants.InstallEventSuffix, new Dictionary <string, string> {
                { TelemetryConstants.ToInstallCount, commandInput.ToInstallList.Count.ToString() }
            });

            var details = new Dictionary <string, string>();

            if (commandInput.InstallNuGetSourceList?.Count > 0)
            {
                details[InstallerConstants.NuGetSourcesKey] = string.Join(InstallerConstants.NuGetSourcesSeparator.ToString(), commandInput.InstallNuGetSourceList);
            }
            if (commandInput.IsInteractiveFlagSpecified)
            {
                details[InstallerConstants.InteractiveModeKey] = "true";
            }

            // In future we might want give user ability to pick IManagerSourceProvider by Name or GUID
            var managedSourceProvider             = _engineEnvironmentSettings.SettingsLoader.TemplatePackagesManager.GetBuiltInManagedProvider(InstallationScope.Global);
            List <InstallRequest> installRequests = new List <InstallRequest>();

            foreach (string installArg in commandInput.ToInstallList)
            {
                string[] splitByColons = installArg.Split(new[] { "::" }, StringSplitOptions.RemoveEmptyEntries);
                string   identifier    = splitByColons[0];
                string?  version       = null;
                //'*' is placeholder for the latest version
                if (splitByColons.Length > 1 && splitByColons[1] != "*")
                {
                    version = splitByColons[1];
                }
                foreach (string expandedIdentifier in InstallRequestPathResolution.ExpandMaskedPath(identifier, _engineEnvironmentSettings))
                {
                    installRequests.Add(new InstallRequest(expandedIdentifier, version, details: details));
                }
            }

            if (!installRequests.Any())
            {
                Reporter.Error.WriteLine(LocalizableStrings.TemplatePackageCoordinator_Install_Error_FoundNoPackagesToInstall);
                return(CreationResultStatus.NotFound);
            }

            //validate if installation requests have unique identifier
            HashSet <string> identifiers = new HashSet <string>();

            foreach (InstallRequest installRequest in installRequests)
            {
                if (identifiers.Add(installRequest.PackageIdentifier))
                {
                    continue;
                }
                Reporter.Error.WriteLine(string.Format(LocalizableStrings.TemplatePackageCoordinator_Install_Error_SameInstallRequests, installRequest.PackageIdentifier));
                return(CreationResultStatus.Cancelled);
            }

            Reporter.Output.WriteLine(LocalizableStrings.TemplatePackageCoordinator_Install_Info_PackagesToBeInstalled);
            foreach (InstallRequest installRequest in installRequests)
            {
                Reporter.Output.WriteLine(installRequest.DisplayName.Indent());
            }
            Reporter.Output.WriteLine();

            IReadOnlyList <InstallResult> installResults = await managedSourceProvider.InstallAsync(installRequests, cancellationToken).ConfigureAwait(false);

            foreach (InstallResult result in installResults)
            {
                await DisplayInstallResultAsync(commandInput, result.InstallRequest.DisplayName, result, cancellationToken).ConfigureAwait(false);

                if (!result.Success)
                {
                    resultStatus = CreationResultStatus.CreateFailed;
                }
            }
            return(resultStatus);
        }
        public void CanResolveParentPath()
        {
            IEnumerable <string> installPath = InstallRequestPathResolution.ExpandMaskedPath("..", _engineEnvironmentSettings);

            Assert.Equal(Path.GetDirectoryName(Directory.GetCurrentDirectory()), installPath.Single());
        }
        public void CanTrimTrailingSeparator()
        {
            IEnumerable <string> installPath = InstallRequestPathResolution.ExpandMaskedPath(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar, _engineEnvironmentSettings);

            Assert.Equal(Directory.GetCurrentDirectory(), installPath.Single());
        }
        public void CannotResolveInvalidPath()
        {
            IEnumerable <string> installPath = InstallRequestPathResolution.ExpandMaskedPath("|path|", _engineEnvironmentSettings);

            Assert.Equal("|path|", installPath.Single());
        }