Пример #1
0
        public override int Execute()
        {
            var            projects    = new ProjectCollection();
            MsbuildProject msbuildProj = MsbuildProject.FromFileOrDirectory(projects, _fileOrDirectory);

            var frameworkString = _appliedCommand.ValueOrDefault <string>("framework");

            PathUtility.EnsureAllPathsExist(_appliedCommand.Arguments, CommonLocalizableStrings.ReferenceDoesNotExist);
            List <MsbuildProject> refs = _appliedCommand.Arguments
                                         .Select((r) => MsbuildProject.FromFile(projects, r))
                                         .ToList();

            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.IsTargetingFramework(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);
                    }
                }
            }

            var relativePathReferences = _appliedCommand.Arguments.Select((r) =>
                                                                          PathUtility.GetRelativePath(msbuildProj.ProjectDirectory, Path.GetFullPath(r)))
                                         .ToList();

            int numberOfAddedReferences = msbuildProj.AddProjectToProjectReferences(
                frameworkString,
                relativePathReferences);

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

            return(0);
        }
Пример #2
0
        public override int Run(string fileOrDirectory)
        {
            var            projects    = new ProjectCollection();
            MsbuildProject msbuildProj = MsbuildProject.FromFileOrDirectory(projects, fileOrDirectory);

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

            string frameworkString = _frameworkOption.Value();

            PathUtility.EnsureAllPathsExist(RemainingArguments, CommonLocalizableStrings.ReferenceDoesNotExist);
            List <MsbuildProject> refs = RemainingArguments
                                         .Select((r) => MsbuildProject.FromFile(projects, r))
                                         .ToList();

            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);
                    }
                }
            }

            var relativePathReferences = RemainingArguments.Select((r) =>
                                                                   PathUtility.GetRelativePath(msbuildProj.ProjectDirectory, Path.GetFullPath(r))).ToList();

            int numberOfAddedReferences = msbuildProj.AddProjectToProjectReferences(
                _frameworkOption.Value(),
                relativePathReferences);

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

            return(0);
        }
Пример #3
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);
            }
        }
        private MsbuildProject LoadMsbuildProject()
        {
            var collection = new ProjectCollection();

            return(MsbuildProject.FromFile(collection, ProjectFileAbsolutePath));
        }
Пример #5
0
        internal static CommandLineApplication CreateApplication(CommandLineApplication parentApp)
        {
            CommandLineApplication app = parentApp.Command("p2p", throwOnUnexpectedArg: false);

            app.FullName    = LocalizableStrings.AppFullName;
            app.Description = LocalizableStrings.AppDescription;
            app.HandleRemainingArguments  = true;
            app.ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText;

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

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

            app.OnExecute(() =>
            {
                try
                {
                    if (!parentApp.Arguments.Any())
                    {
                        throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, Constants.ProjectOrSolutionArgumentName);
                    }

                    var projectOrDirectory = parentApp.Arguments.First().Value;
                    if (string.IsNullOrEmpty(projectOrDirectory))
                    {
                        projectOrDirectory = PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory());
                    }

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

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

                    string frameworkString   = frameworkOption.Value();
                    List <string> references = app.RemainingArguments;
                    PathUtility.EnsureAllPathsExist(references, CommonLocalizableStrings.ReferenceDoesNotExist);
                    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);
                            }
                        }
                    }

                    var relativePathReferences = references.Select((r) =>
                                                                   PathUtility.GetRelativePath(msbuildProj.ProjectDirectory, Path.GetFullPath(r))).ToList();

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

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

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

            return(app);
        }