예제 #1
0
        public static int Main(string[] args)
        {
            var logLevel = LogLevel.Information;

            if (CmdUtils.IsDebugModeEnabled())
            {
                logLevel = LogLevel.Debug;
            }

            var log = new ConsoleLogger(logLevel);

            var task = MainCore(args, log);

            return(task.Result);
        }
예제 #2
0
        public static Task <int> MainCore(string[] args, ILogger log)
        {
            CmdUtils.LaunchDebuggerIfSet(ref args, log);

            var app = new CommandLineApplication
            {
                Name        = "NupkgWrench",
                FullName    = "nupkg wrench",
                Description = "A powertool for modifying nupkg files."
            };

            app.HelpOption(Constants.HelpOption);
            app.VersionOption("--version", (new NuGetVersion(CmdUtils.GetAssemblyVersion())).ToNormalizedString());

            NuspecCommand.Register(app, log);
            FilesCommand.Register(app, log);
            IdCommand.Register(app, log);
            VersionCommand.Register(app, log);
            ExtractCommand.Register(app, log);
            CompressCommand.Register(app, log);
            ListCommand.Register(app, log);
            UpdateFileNameCommand.Register(app, log);
            ReleaseCommand.Register(app, log);
            ValidateCommand.Register(app, log);

            app.OnExecute(() =>
            {
                app.ShowHelp();
                return(1);
            });

            var exitCode = 1;

            try
            {
                exitCode = app.Execute(args);
            }
            catch (CommandParsingException ex)
            {
                ex.Command.ShowHelp();
            }
            catch (Exception ex)
            {
                ExceptionUtils.LogException(ex, log);
            }

            return(Task.FromResult(exitCode));
        }
        private static void Run(CommandLineApplication cmd, ILogger log)
        {
            cmd.Description = "Add framework assemblies to the nuspec file for desktop frameworks.";

            var idFilter             = cmd.Option(Constants.IdFilterTemplate, Constants.IdFilterDesc, CommandOptionType.SingleValue);
            var versionFilter        = cmd.Option(Constants.VersionFilterTemplate, Constants.VersionFilterDesc, CommandOptionType.SingleValue);
            var excludeSymbolsFilter = cmd.Option(Constants.ExcludeSymbolsTemplate, Constants.ExcludeSymbolsDesc, CommandOptionType.NoValue);
            var highestVersionFilter = cmd.Option(Constants.HighestVersionFilterTemplate, Constants.HighestVersionFilterDesc, CommandOptionType.NoValue);

            var assemblyNames    = cmd.Option("-n|--assembly-name", "AssemblyName value of the FrameworkAssembly entry. May be specified multiple times.", CommandOptionType.MultipleValue);
            var targetFrameworks = cmd.Option("-f|--framework", "TargetFramework value of the FrameworkAssembly entry. If no frameworks are given this command will automatically add the reference for all desktop frameworks. Framework may be specified multiple time.", CommandOptionType.MultipleValue);
            var noFrameworks     = cmd.Option("--no-frameworks", "Exclude the TargetFramework attribute.", CommandOptionType.NoValue);

            var argRoot = cmd.Argument(
                "[root]",
                Constants.MultiplePackagesRootDesc,
                multipleValues: true);

            cmd.HelpOption(Constants.HelpOption);

            cmd.OnExecute(() =>
            {
                try
                {
                    // Validate required parameters
                    CmdUtils.VerifyRequiredOptions(assemblyNames);
                    CmdUtils.VerifyMutallyExclusiveOptions(targetFrameworks, noFrameworks);

                    var inputs = argRoot.Values;

                    if (inputs.Count < 1)
                    {
                        inputs.Add(Directory.GetCurrentDirectory());
                    }

                    var packages = Util.GetPackagesWithFilter(idFilter, versionFilter, excludeSymbolsFilter, highestVersionFilter, inputs.ToArray());

                    foreach (var package in packages)
                    {
                        log.LogMinimal($"modifying {package}");

                        // Get nuspec file path
                        string nuspecPath         = null;
                        XDocument nuspecXml       = null;
                        NuspecReader nuspecReader = null;
                        var packageFrameworks     = new List <NuGetFramework>();
                        using (var stream = File.OpenRead(package))
                            using (var packageReader = new PackageArchiveReader(stream, leaveStreamOpen: false))
                            {
                                nuspecPath   = packageReader.GetNuspecFile();
                                nuspecXml    = XDocument.Load(packageReader.GetNuspec());
                                nuspecReader = packageReader.NuspecReader;

                                packageFrameworks.AddRange(packageReader.GetSupportedFrameworks().Where(e => e.IsSpecificFramework));
                            }

                        var frameworks = new HashSet <NuGetFramework>();

                        if (!noFrameworks.HasValue())
                        {
                            if (targetFrameworks.HasValue())
                            {
                                // Validate user input
                                ValidateTargetFrameworkInputs(targetFrameworks.Values);

                                // Add user input frameworks
                                frameworks.AddRange(targetFrameworks.Values.Select(e => NuGetFramework.Parse(e)));
                            }
                            else
                            {
                                frameworks.AddRange(packageFrameworks);
                            }
                        }

                        // Remove unknown frameworks and package based frameworks.
                        frameworks.RemoveWhere(e => !e.IsSpecificFramework || e.IsPackageBased);

                        var assemblyNamesUnique = new HashSet <string>(assemblyNames.Values, StringComparer.OrdinalIgnoreCase);

                        log.LogMinimal($"Adding framework assemblies: {string.Join(", ", assemblyNamesUnique)}");

                        // Modify nuspec
                        Util.AddFrameworkAssemblyReferences(nuspecXml, assemblyNamesUnique, frameworks);

                        // Update zip
                        Util.AddOrReplaceZipEntry(package, nuspecPath, nuspecXml, log);
                    }

                    return(0);
                }
                catch (Exception ex)
                {
                    log.LogError(ex.Message);
                    log.LogDebug(ex.ToString());
                }

                return(1);
            });
        }
예제 #4
0
        private static void Run(CommandLineApplication cmd, ILogger log)
        {
            cmd.Description = "Add package dependencies";

            // Filters
            var idFilter             = cmd.Option(Constants.IdFilterTemplate, Constants.IdFilterDesc, CommandOptionType.SingleValue);
            var versionFilter        = cmd.Option(Constants.VersionFilterTemplate, Constants.VersionFilterDesc, CommandOptionType.SingleValue);
            var excludeSymbolsFilter = cmd.Option(Constants.ExcludeSymbolsTemplate, Constants.ExcludeSymbolsDesc, CommandOptionType.NoValue);
            var highestVersionFilter = cmd.Option(Constants.HighestVersionFilterTemplate, Constants.HighestVersionFilterDesc, CommandOptionType.NoValue);

            // Command options
            var frameworkOption         = cmd.Option(Constants.FrameworkOptionTemplate, Constants.FrameworkOptionDesc, CommandOptionType.MultipleValue);
            var editExclude             = cmd.Option(Constants.DependencyExcludeAttributeTemplate, Constants.DependencyExcludeAttributeDesc, CommandOptionType.SingleValue);
            var editInclude             = cmd.Option(Constants.DependencyIncludeAttributeTemplate, Constants.DependencyIncludeAttributeDesc, CommandOptionType.SingleValue);
            var dependencyVersionOption = cmd.Option(Constants.DependencyVersionRangeTemplate, Constants.DependencyVersionRangeDesc, CommandOptionType.SingleValue);
            var dependencyIdOption      = cmd.Option(Constants.DependencyIdTemplate, Constants.DependencyIdDesc, CommandOptionType.SingleValue);

            var argRoot = cmd.Argument(
                "[root]",
                Constants.MultiplePackagesRootDesc,
                true);

            cmd.HelpOption(Constants.HelpOption);

            cmd.OnExecute(() =>
            {
                try
                {
                    // Validate parameters
                    CmdUtils.VerifyRequiredOptions(dependencyIdOption, dependencyVersionOption);

                    var inputs = argRoot.Values;

                    if (inputs.Count < 1)
                    {
                        inputs.Add(Directory.GetCurrentDirectory());
                    }

                    var dependencyId = dependencyIdOption.Value();

                    var editForFrameworks = new HashSet <NuGetFramework>();
                    if (frameworkOption.HasValue())
                    {
                        editForFrameworks.UnionWith(frameworkOption.Values.Select(NuGetFramework.Parse));

                        log.LogInformation($"adding dependency {dependencyId} to {string.Join(", ", editForFrameworks.Select(e => e.GetShortFolderName()))}");
                    }
                    else
                    {
                        log.LogInformation($"adding dependency {dependencyId} to all frameworks");
                    }

                    var version = dependencyVersionOption.Value();
                    var exclude = editExclude.HasValue() ? editExclude.Value() : null;
                    var include = editInclude.HasValue() ? editInclude.Value() : null;

                    var packages = Util.GetPackagesWithFilter(idFilter, versionFilter, excludeSymbolsFilter, highestVersionFilter, inputs.ToArray());

                    foreach (var package in packages)
                    {
                        log.LogMinimal($"processing {package}");

                        var nuspecXml = Util.GetNuspec(package);

                        DependenciesUtil.Process(nuspecXml, DependenciesUtil.EditType.Add, editForFrameworks, dependencyId, version, exclude, include, false, false, log);

                        Util.ReplaceNuspec(package, nuspecXml, log);
                    }

                    return(0);
                }
                catch (Exception ex)
                {
                    log.LogError(ex.Message);
                    log.LogError(ex.ToString());
                }

                return(1);
            });
        }