Exemple #1
0
        public int Execute(NuLinkCommandOptions options)
        {
            Console.WriteLine(
                $"Checking status of packages in {(options.ProjectIsSolution ? "solution" : "project")}: {options.ConsumerProjectPath}");

            var allProjects     = new WorkspaceLoader().LoadProjects(options.ConsumerProjectPath, options.ProjectIsSolution);
            var referenceLoader = new PackageReferenceLoader();
            var allPackages     = referenceLoader.LoadPackageReferences(allProjects);

            foreach (var package in allPackages)
            {
                Console.WriteLine();
                Console.WriteLine($"- {package.PackageId} [{package.Version}] @ {package.LibFolderPath}");

                var status = package.CheckStatus();

                Console.WriteLine($"  LibFolderExists ....... {status.LibFolderExists}");
                Console.WriteLine($"  IsLibFolderLinked ..... {status.IsLibFolderLinked}");
                Console.WriteLine($"  LibFolderLinkTargetPath {(status.IsLibFolderLinked ? $"-> {status.LibFolderLinkTargetPath}" : "N/A")}");
                Console.WriteLine($"  LibBackupFolderExists.. {status.LibBackupFolderExists}");
            }

            Console.WriteLine();

            return(0);
        }
Exemple #2
0
 private static ICommandHandler HandleStatus() =>
 CommandHandler.Create <string, string>((consumer, package) => {
     var options = new NuLinkCommandOptions(
         ValidateConsumerProject(consumer),
         package);
     return(ExecuteCommand(
                "status",
                options,
                options.ConsumerProjectPath != null));
 });
Exemple #3
0
 private static ICommandHandler HandleUnlink() =>
 CommandHandler.Create <string, string, bool>((consumer, package, dryRun) => {
     var options = new NuLinkCommandOptions(
         ValidateConsumerProject(consumer),
         package,
         dryRun: dryRun);
     return(ExecuteCommand(
                "unlink",
                options,
                options.ConsumerProjectPath != null && options.PackageId != null));
 });
Exemple #4
0
        private static ICommandHandler HandleLink() =>
        CommandHandler.Create <string, string, string, bool>((consumer, package, local, dryRun) => {
            var options = new NuLinkCommandOptions(
                ValidateConsumerProject(consumer),
                package,
                localProjectPath: ValidateTargetProject(local),

                dryRun: dryRun);
            return(ExecuteCommand(
                       "link",
                       options,
                       options.ConsumerProjectPath != null && options.LocalProjectOutputPath != null && options.PackageId != null));
        });
Exemple #5
0
        private static INuLinkCommand CreateCommand(string name, NuLinkCommandOptions options)
        {
            switch (name)
            {
            case "status":
                return(new StatusCommand());

            case "link":
                return(new LinkCommand());

            case "unlink":
                return(new UnlinkCommand());

            default:
                throw new Exception($"Command not supported: {name}.");
            }
        }
Exemple #6
0
        public int Execute(NuLinkCommandOptions options)
        {
            _ui.ReportMedium(() =>
                             $"Checking status of packages in {(options.ProjectIsSolution ? "solution" : "project")}: {options.ConsumerProjectPath}");

            var allProjects     = new WorkspaceLoader().LoadProjects(options.ConsumerProjectPath, options.ProjectIsSolution);
            var referenceLoader = new PackageReferenceLoader(_ui);
            var allPackages     = referenceLoader.LoadPackageReferences(allProjects);
            var orderedPackages = allPackages.OrderBy(p => $"{p.PackageId}@{p.Version}");

            _ui.ReportHigh(() => $"{"--- package status ---"}", ConsoleColor.Yellow);

            foreach (var package in orderedPackages)
            {
                var status = package.CheckStatus();

                if (status.IsLinkable)
                {
                    PrintPackage(package, status);
                }
            }

            return(0);

            void PrintPackage(PackageReferenceInfo reference, PackageStatusInfo status)
            {
                var statusColor = (status.IsCorrupt ? ConsoleColor.Red : ConsoleColor.Green);
                var statusText  = (status.IsCorrupt ? "corrupt" : "ok");
                var linkedPath  = status.LibFolderLinkTargetPath;

                if (status.IsLibFolderLinked)
                {
                    _ui.ReportData(
                        () => $"{reference.PackageId} {reference.Version} {statusText} -> {linkedPath}",
                        ConsoleColor.White, ConsoleColor.Cyan, statusColor, ConsoleColor.Magenta);
                }
                else
                {
                    _ui.ReportData(
                        () => $"{reference.PackageId} {reference.Version} {statusText}",
                        ConsoleColor.Gray, ConsoleColor.Cyan, statusColor);
                }
            }
        }
Exemple #7
0
        public int Execute(NuLinkCommandOptions options)
        {
            Console.WriteLine(
                $"Checking package references in {(options.ProjectIsSolution ? "solution" : "project")}: {options.ConsumerProjectPath}");

            var allProjects     = new WorkspaceLoader().LoadProjects(options.ConsumerProjectPath, options.ProjectIsSolution);
            var referenceLoader = new PackageReferenceLoader();
            var allPackages     = referenceLoader.LoadPackageReferences(allProjects);

            var requestedPackage = allPackages.FirstOrDefault(p => p.PackageId == options.PackageId);

            if (requestedPackage == null)
            {
                throw new Exception($"Error: Package not referenced: {options.PackageId}");
            }

            var status = requestedPackage.CheckStatus();

            if (!status.LibFolderExists)
            {
                throw new Exception($"Error: Cannot link package {options.PackageId}: 'lib' folder not found, expected {requestedPackage.LibFolderPath}");
            }

            if (status.IsLibFolderLinked)
            {
                throw new Exception($"Error: Package {requestedPackage.PackageId} is already linked to {status.LibFolderLinkTargetPath}");
            }

            if (!status.LibBackupFolderExists)
            {
                Directory.Move(requestedPackage.LibFolderPath, requestedPackage.LibBackupFolderPath);
            }
            else
            {
                Console.WriteLine($"Warning: backup folder was not expected to exist: {requestedPackage.LibBackupFolderPath}");
            }

            var linkTargetPath = Path.Combine(Path.GetDirectoryName(options.LocalProjectPath), "bin", "Debug");

            CreateSymbolicLink(linkPath: requestedPackage.LibFolderPath, targetPath: linkTargetPath);

            Console.WriteLine($"Linked {requestedPackage.LibFolderPath} -> {linkTargetPath}");
            return(0);
        }
Exemple #8
0
        private static INuLinkCommand CreateCommand(string name, NuLinkCommandOptions options)
        {
            var ui = (options.BareUI ? new BareUI() : new FullUI() as IUserInterface);

            switch (name)
            {
            case "status":
                return(new StatusCommand(ui));

            case "link":
                return(new LinkCommand(ui));

            case "unlink":
                return(new UnlinkCommand(ui));

            default:
                throw new Exception($"Command not supported: {name}.");
            }
        }
Exemple #9
0
        public int Execute(NuLinkCommandOptions options)
        {
            _ui.ReportMedium(() =>
                             $"Checking package references in {(options.ProjectIsSolution ? "solution" : "project")}: {options.ConsumerProjectPath}");

            var allProjects     = new WorkspaceLoader().LoadProjects(options.ConsumerProjectPath, options.ProjectIsSolution);
            var referenceLoader = new PackageReferenceLoader(_ui);
            var allPackages     = referenceLoader.LoadPackageReferences(allProjects);

            var requestedPackage = allPackages.FirstOrDefault(p => p.PackageId == options.PackageId);

            if (requestedPackage == null)
            {
                throw new Exception($"Error: Package not referenced: {options.PackageId}");
            }

            var status = requestedPackage.CheckStatus();

            if (!status.LibFolderExists)
            {
                throw new Exception($"Error: Cannot unlink package {options.PackageId}: 'lib' folder not found, expected {requestedPackage.LibFolderPath}");
            }

            if (!status.IsLibFolderLinked)
            {
                throw new Exception($"Error: Package {requestedPackage.PackageId} is not linked.");
            }

            if (!status.LibBackupFolderExists)
            {
                throw new Exception($"Error: Cannot unlink package {options.PackageId}: backup folder not found, expected {requestedPackage.LibBackupFolderPath}");
            }

            Directory.Delete(requestedPackage.LibFolderPath);
            Directory.Move(requestedPackage.LibBackupFolderPath, requestedPackage.LibFolderPath);

            _ui.ReportSuccess(() => $"Unlinked {requestedPackage.PackageId}");
            _ui.ReportSuccess(() => $" {"-X->"} {status.LibFolderLinkTargetPath}", ConsoleColor.Red, ConsoleColor.DarkYellow);
            return(0);
        }
Exemple #10
0
        private static int ExecuteCommand(
            string commandName,
            NuLinkCommandOptions options,
            bool isValid)
        {
            if (!isValid)
            {
                return(1);
            }

            try
            {
                var command = CreateCommand(commandName, options);
                command.Execute(options);
                return(0);
            }
            catch (Exception e)
            {
                FullUI.FatalError(() => $"Fatal error: {e.Message}\nException: {e}");
                return(100);
            }
        }
Exemple #11
0
        public int Execute(NuLinkCommandOptions options)
        {
            _ui.ReportMedium(() =>
                             $"Searching package reference in {(options.ProjectIsSolution ? "solution" : "project")}: {options.ConsumerProjectPath}");

            var requestedPackage = GetPackageInfo();
            var status           = requestedPackage.CheckStatus();
            var linkTargetPath   = options.LocalProjectOutputPath;

            ValidateOperation();
            PerformOperation();

            _ui.ReportSuccess(() => $"Linked {requestedPackage.LibFolderPath}");
            _ui.ReportSuccess(() => $" -> {linkTargetPath}", ConsoleColor.Magenta);
            return(0);

            PackageReferenceInfo GetPackageInfo()
            {
                var allProjects     = new WorkspaceLoader().LoadProjects(options.ConsumerProjectPath, options.ProjectIsSolution);
                var referenceLoader = new PackageReferenceLoader(_ui);
                var package         = referenceLoader.LoadPackageReference(allProjects, options.PackageId);

                return(package ?? throw new Exception($"Error: Package not referenced: {options.PackageId}"));
            }

            void ValidateOperation()
            {
                if (!status.LibFolderExists)
                {
                    throw new Exception($"Error: Cannot link package {options.PackageId}: 'lib' folder not found, expected {requestedPackage.LibFolderPath}");
                }

                if (status.IsLibFolderLinked)
                {
                    throw new Exception($"Error: Package {requestedPackage.PackageId} is already linked to {status.LibFolderLinkTargetPath}");
                }

                if (!Directory.Exists(linkTargetPath))
                {
                    throw new Exception($"Error: Target link directory doesn't exist: {linkTargetPath}");
                }
            }

            void PerformOperation()
            {
                if (!status.LibBackupFolderExists)
                {
                    Directory.Move(requestedPackage.LibFolderPath, requestedPackage.LibBackupFolderPath);
                }
                else
                {
                    _ui.ReportWarning(() => $"Warning: backup folder was not expected to exist: {requestedPackage.LibBackupFolderPath}");
                }

                try
                {
                    SymbolicLinkWithDiagnostics.Create(
                        fromPath: requestedPackage.LibFolderPath,
                        toPath: linkTargetPath);
                }
                catch
                {
                    RevertOperation();
                    throw;
                }
            }

            void RevertOperation()
            {
                try
                {
                    _ui.ReportError(() => "Failed to create symlink, reverting changes to package folders.");
                    Directory.Move(requestedPackage.LibBackupFolderPath, requestedPackage.LibFolderPath);
                }
                catch (Exception e)
                {
                    _ui.ReportError(() => $"FAILED to revert package changes: {e.Message}");
                    _ui.ReportError(() => $"You have to recover manually!");
                    _ui.ReportError(() => "--- MANUAL RECOVERY INSTRUCTIONS ---");
                    _ui.ReportError(() => $"1. Go to {Path.GetDirectoryName(requestedPackage.LibFolderPath)}");
                    _ui.ReportError(() => $"2. Rename '{Path.GetFileName(requestedPackage.LibBackupFolderPath)}'" +
                                    $" to '{Path.GetFileName(requestedPackage.LibFolderPath)}'");
                    _ui.ReportError(() => "--- END OF RECOVERY INSTRUCTIONS ---");
                }
            }
        }