예제 #1
0
        public static int Run(string[] args)
        {
            DebugHelper.HandleDebugSwitch(ref args);

            CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false)
            {
                Name                      = "dotnet add p2p",
                FullName                  = LocalizableStrings.AppFullName,
                Description               = LocalizableStrings.AppDescription,
                AllowArgumentSeparator    = true,
                ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText
            };

            app.HelpOption("-h|--help");

            CommandArgument projectArgument = app.Argument(
                $"<{LocalizableStrings.CmdProject}>",
                LocalizableStrings.CmdProjectDescription);

            CommandOption frameworkOption = app.Option(
                $"-f|--framework <{LocalizableStrings.CmdFramework}>",
                LocalizableStrings.CmdFrameworkDescription,
                CommandOptionType.SingleValue);

            CommandOption forceOption = app.Option(
                "--force",
                LocalizableStrings.CmdForceDescription,
                CommandOptionType.NoValue);

            app.OnExecute(() => {
                if (string.IsNullOrEmpty(projectArgument.Value))
                {
                    throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, $"<{LocalizableStrings.ProjectException}>");
                }

                var projects    = new ProjectCollection();
                var msbuildProj = MsbuildProject.FromFileOrDirectory(projects, projectArgument.Value);

                if (app.RemainingArguments.Count == 0)
                {
                    throw new GracefulException(LocalizableStrings.SpecifyAtLeastOneReferenceToAdd);
                }

                string frameworkString   = frameworkOption.Value();
                List <string> references = app.RemainingArguments;
                if (!forceOption.HasValue())
                {
                    MsbuildProject.EnsureAllReferencesExist(references);
                    IEnumerable <MsbuildProject> refs = references.Select((r) => MsbuildProject.FromFile(projects, r));

                    if (frameworkString == null)
                    {
                        foreach (var tfm in msbuildProj.GetTargetFrameworks())
                        {
                            foreach (var @ref in refs)
                            {
                                if ([email protected](tfm))
                                {
                                    Reporter.Error.Write(GetProjectNotCompatibleWithFrameworksDisplayString(
                                                             @ref,
                                                             msbuildProj.GetTargetFrameworks().Select((fx) => fx.GetShortFolderName())));
                                    return(1);
                                }
                            }
                        }
                    }
                    else
                    {
                        var framework = NuGetFramework.Parse(frameworkString);
                        if (!msbuildProj.IsTargettingFramework(framework))
                        {
                            Reporter.Error.WriteLine(string.Format(CommonLocalizableStrings.ProjectDoesNotTargetFramework, msbuildProj.ProjectRootElement.FullPath, frameworkString));
                            return(1);
                        }

                        foreach (var @ref in refs)
                        {
                            if ([email protected](framework))
                            {
                                Reporter.Error.Write(GetProjectNotCompatibleWithFrameworksDisplayString(
                                                         @ref,
                                                         new string[] { frameworkString }));
                                return(1);
                            }
                        }
                    }

                    msbuildProj.ConvertPathsToRelative(ref references);
                }

                int numberOfAddedReferences = msbuildProj.AddProjectToProjectReferences(
                    frameworkOption.Value(),
                    references);

                if (numberOfAddedReferences != 0)
                {
                    msbuildProj.ProjectRootElement.Save();
                }

                return(0);
            });

            try
            {
                return(app.Execute(args));
            }
            catch (GracefulException e)
            {
                Reporter.Error.WriteLine(e.Message.Red());
                app.ShowHelp();
                return(1);
            }
        }
예제 #2
0
        public static int Run(string[] args)
        {
            DebugHelper.HandleDebugSwitch(ref args);

            CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false)
            {
                Name                      = "dotnet add p2p",
                FullName                  = LocalizableStrings.AppFullName,
                Description               = LocalizableStrings.AppDescription,
                AllowArgumentSeparator    = true,
                ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText
            };

            app.HelpOption("-h|--help");

            CommandArgument projectArgument = app.Argument(
                $"<{LocalizableStrings.CmdProject}>",
                LocalizableStrings.CmdProjectDescription);

            CommandOption frameworkOption = app.Option(
                $"-f|--framework <{LocalizableStrings.CmdFramework}>",
                LocalizableStrings.CmdFrameworkDescription,
                CommandOptionType.SingleValue);

            CommandOption forceOption = app.Option(
                "--force",
                LocalizableStrings.CmdForceDescription,
                CommandOptionType.NoValue);

            app.OnExecute(() => {
                if (string.IsNullOrEmpty(projectArgument.Value))
                {
                    throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, $"<{LocalizableStrings.ProjectException}>");
                }

                var msbuildProj = MsbuildProject.FromFileOrDirectory(projectArgument.Value);

                if (app.RemainingArguments.Count == 0)
                {
                    throw new GracefulException(LocalizableStrings.SpecifyAtLeastOneReferenceToAdd);
                }

                List <string> references = app.RemainingArguments;
                if (!forceOption.HasValue())
                {
                    MsbuildProject.EnsureAllReferencesExist(references);
                    msbuildProj.ConvertPathsToRelative(ref references);
                }

                int numberOfAddedReferences = msbuildProj.AddProjectToProjectReferences(
                    frameworkOption.Value(),
                    references);

                if (numberOfAddedReferences != 0)
                {
                    msbuildProj.Project.Save();
                }

                return(0);
            });

            try
            {
                return(app.Execute(args));
            }
            catch (GracefulException e)
            {
                Reporter.Error.WriteLine(e.Message.Red());
                app.ShowHelp();
                return(1);
            }
        }
예제 #3
0
파일: Program.cs 프로젝트: wen101016/cli
        public static int Run(string[] args)
        {
            DebugHelper.HandleDebugSwitch(ref args);

            CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false)
            {
                Name                      = "dotnet add p2p",
                FullName                  = ".NET Add Project to Project (p2p) reference Command",
                Description               = "Command to add project to project (p2p) reference",
                AllowArgumentSeparator    = true,
                ArgumentSeparatorHelpText = "Project to project references to add"
            };

            app.HelpOption("-h|--help");

            CommandArgument projectArgument = app.Argument(
                "<PROJECT>",
                "The project file to modify. If a project file is not specified," +
                " it searches the current working directory for an MSBuild file that has" +
                " a file extension that ends in `proj` and uses that file.");

            CommandOption frameworkOption = app.Option(
                "-f|--framework <FRAMEWORK>",
                "Add reference only when targetting a specific framework",
                CommandOptionType.SingleValue);

            CommandOption forceOption = app.Option(
                "--force",
                "Add reference even if it does not exist, do not convert paths to relative",
                CommandOptionType.NoValue);

            app.OnExecute(() => {
                if (string.IsNullOrEmpty(projectArgument.Value))
                {
                    throw new GracefulException(LocalizableStrings.RequiredArgumentNotPassed, "<Project>");
                }

                var msbuildProj = MsbuildProject.FromFileOrDirectory(projectArgument.Value);

                if (app.RemainingArguments.Count == 0)
                {
                    throw new GracefulException(LocalizableStrings.SpecifyAtLeastOneReferenceToAdd);
                }

                List <string> references = app.RemainingArguments;
                if (!forceOption.HasValue())
                {
                    MsbuildProject.EnsureAllReferencesExist(references);
                    msbuildProj.ConvertPathsToRelative(ref references);
                }

                int numberOfAddedReferences = msbuildProj.AddProjectToProjectReferences(
                    frameworkOption.Value(),
                    references);

                if (numberOfAddedReferences != 0)
                {
                    msbuildProj.Project.Save();
                }

                return(0);
            });

            try
            {
                return(app.Execute(args));
            }
            catch (GracefulException e)
            {
                Reporter.Error.WriteLine(e.Message.Red());
                app.ShowHelp();
                return(1);
            }
        }