private static string GetProjectNotCompatibleWithFrameworksDisplayString(MsbuildProject project, IEnumerable <string> frameworksDisplayStrings) { var sb = new StringBuilder(); sb.AppendLine(string.Format(CommonLocalizableStrings.ProjectNotCompatibleWithFrameworks, project.ProjectRootElement.FullPath)); foreach (var tfm in frameworksDisplayStrings) { sb.AppendLine($" - {tfm}"); } return(sb.ToString()); }
public static int Run(string[] args) { DebugHelper.HandleDebugSwitch(ref args); CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false) { Name = "dotnet list p2ps", FullName = LocalizableStrings.AppFullName, Description = LocalizableStrings.AppDescription }; app.HelpOption("-h|--help"); CommandArgument projectArgument = app.Argument($"<{LocalizableStrings.ProjectArgumentValueName}>", LocalizableStrings.ProjectArgumentDescription); app.OnExecute(() => { if (string.IsNullOrEmpty(projectArgument.Value)) { throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, $"<{LocalizableStrings.ProjectArgumentValueName}>"); } var msbuildProj = MsbuildProject.FromFileOrDirectory(projectArgument.Value); var p2ps = msbuildProj.GetProjectToProjectReferences(); if (p2ps.Count() == 0) { Reporter.Output.WriteLine(string.Format(LocalizableStrings.NoReferencesFound, CommonLocalizableStrings.P2P, projectArgument.Value)); return(0); } Reporter.Output.WriteLine($"{CommonLocalizableStrings.ProjectReferenceOneOrMore}"); Reporter.Output.WriteLine(new string('-', CommonLocalizableStrings.ProjectReferenceOneOrMore.Length)); foreach (var p2p in p2ps) { Reporter.Output.WriteLine(p2p.Include); } return(0); }); try { return(app.Execute(args)); } catch (GracefulException e) { Reporter.Error.WriteLine(e.Message.Red()); app.ShowHelp(); return(1); } }
private static MsbuildProject GetMSBuildProject() { try { return(MsbuildProject.FromFileOrDirectory( new ProjectCollection(), Directory.GetCurrentDirectory(), interactive: false)); } catch (Exception e) { Report(e); return(null); } }
public void GetPkgReferenceAndVersionInfo() { string sdkPropFilePath = this.FileSys.FindFilePath(RepoRootDirPath, "AzSdk.reference.props"); Assert.NotNull(sdkPropFilePath); MsbuildProject sdkProp = new MsbuildProject(sdkPropFilePath); Assert.NotNull(sdkProp); Dictionary <string, string> refVer = sdkProp.GetNugetPkgRefsAndVersionInfo(); Assert.NotNull(refVer); Assert.True(refVer.Count > 2); sdkProp.Dispose(); }
public override int Execute() { var msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), _fileOrDirectory); int numberOfRemovedReferences = msbuildProj.RemoveProjectToProjectReferences( _appliedCommand.ValueOrDefault <string>("framework"), _appliedCommand.Arguments); if (numberOfRemovedReferences != 0) { msbuildProj.ProjectRootElement.Save(); } return(0); }
private void AddOrUpdateRequiredPackage(MsbuildProject msbuildProject, string packageId, Referer referer) { var packageOfHighestVersion = GetPackageOfHighestVersion(packageId); var package = packageOfHighestVersion != null && referer.VersionSpec.IsSatisfiedBy(packageOfHighestVersion.Version) ? packageOfHighestVersion : _nugetServices.GetPackage( packageId: packageId, versionSpec: referer.VersionSpec, allowPrereleaseVersions: _allowPreReleaseVersions); var packageNode = msbuildProject.PackageNodes .SingleOrDefault(x => x.RequiredPackage.Id.Equals(packageId, StringComparison.InvariantCultureIgnoreCase)); if (packageNode == null) { packageNode = PackageNode.Create(package); msbuildProject.PackageNodes.Add(packageNode); } packageNode.AddReferer(referer); if (packageOfHighestVersion != null) { packageNode.AddReferer(Referer.CreateFromSolutionHighestVersion(packageOfHighestVersion)); } if (referer.VersionSpec.RequiresLowerThan(packageNode.RequiredPackage.Version)) { throw new Exception("Installing package would require a downgrade."); } if (packageNode.RequiredPackage.Version < package.Version) { packageNode.UpdateRequiredPackage(package); } if (!packageNode.Referers.All(x => x.VersionSpec.IsSatisfiedBy(packageNode.RequiredPackage.Version))) { throw new Exception("Could not resolve dependencies."); } foreach (var dependency in packageNode.RequiredPackage.GetDependencies(_nugetServices.GetTargetFrameworkName(msbuildProject.Path))) { AddOrUpdateRequiredPackage(msbuildProject, dependency.Id, Referer.Create(packageNode, dependency.VersionSpec)); } }
public void VerifyTargetFx() { string sdkPropFilePath = this.FileSys.FindFilePath(RepoRootDirPath, "AzSdk.reference.props"); Assert.NotNull(sdkPropFilePath); MsbuildProject sdkProp = new MsbuildProject(sdkPropFilePath); Assert.NotNull(sdkProp); string targetFx = sdkProp.GetPropertyValue("targetframework"); Assert.Empty(targetFx); targetFx = sdkProp.GetPropertyValue("SdkTargetFx"); Assert.NotNull(@"net452;net461;netstandard1.4;netstandard2.0"); sdkProp.Dispose(); }
private void ProcessPendingInstalls(MsbuildProject project) { while (project.PackageNodes.Any(x => !x.IsInstalled())) { var pendingNodesWithDependenciesMet = project.PackageNodes .Where(x => !x.IsInstalled() && x.Dependencies.All(y => y.SelfAndDependenciesInstalled())); foreach (var packageNode in pendingNodesWithDependenciesMet) { _nugetServices.Install(project.Path, packageNode.RequiredPackage); packageNode.UpdateInstalledPackage(packageNode.RequiredPackage); } } }
public void UpdateApiTag() { foreach (dynamic asmInfoItem in AssemblyInfoList) { string apiTag = GetApiFromSdkInfo(asmInfoItem.AssemblyFilePath); if (!File.Exists(asmInfoItem.AzPropFilePath)) { MsbuildProject buildProj = new MsbuildProject(); buildProj.CreateAzPropsfile(asmInfoItem.AzPropFilePath); } MsbuildProject msbProj = new MsbuildProject(asmInfoItem.AzPropFilePath); msbProj.AddUpdateProperty(API_TAG_PROPERTYNAME, apiTag); } }
void UpdatePropertyValue(MsbuildProject proj, string propertyName, bool newPropValue) { string existingVal = proj.GetPropertyValue(propertyName); if (string.IsNullOrWhiteSpace(existingVal) && newPropValue == false) { TaskLogger.LogInfo(MessageImportance.Low, "'{0}' property is not currently set and the new value is '{1}'. No changes will be made", propertyName, newPropValue.ToString()); } else if (existingVal.Equals(newPropValue.ToString(), StringComparison.OrdinalIgnoreCase)) { TaskLogger.LogInfo(MessageImportance.Low, "'{0}' current value is '{1}'. New value requested is '{2}'", propertyName, existingVal, newPropValue.ToString()); } else if (!existingVal.Equals(newPropValue.ToString(), StringComparison.OrdinalIgnoreCase)) { TaskLogger.LogInfo(MessageImportance.Low, "{0} current value:'{1}', new value:'{2}'", propertyName, existingVal, newPropValue.ToString()); proj.AddUpdateProperty(propertyName, newPropValue.ToString()); } }
public override int Execute() { var projectFilePath = string.Empty; if (!File.Exists(_fileOrDirectory)) { projectFilePath = MsbuildProject.GetProjectFileFromDirectory(_fileOrDirectory).FullName; } else { projectFilePath = _fileOrDirectory; } var packageToRemove = _appliedCommand.Arguments.Single(); var result = NuGetCommand.Run(TransformArgs(packageToRemove, projectFilePath)); return(result); }
public override int Run(string fileOrDirectory) { var msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), fileOrDirectory); if (RemainingArguments.Count == 0) { throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneReferenceToRemove); } int numberOfRemovedReferences = msbuildProj.RemoveProjectToProjectReferences( _frameworkOption.Value(), RemainingArguments); if (numberOfRemovedReferences != 0) { msbuildProj.ProjectRootElement.Save(); } return(0); }
void UpdateProjects(List <string> projectList) { foreach (string projectPath in projectList) { TaskLogger.LogInfo("Updating '{0}'", projectPath); MsbuildProject msbp = new MsbuildProject(projectPath); // We will only set SkipBuild for sdk projects, we will never set skipTestexecution for sdk projects if (msbp.IsProjectSdkType) { UpdatePropertyValue(msbp, PROPNAME_SKIP_BUILD, SkipFromBuild); } if (msbp.IsProjectTestType) { UpdatePropertyValue(msbp, PROPNAME_SKIP_BUILD, SkipFromBuild); UpdatePropertyValue(msbp, PROPNAME_SKIP_TEST_EXECUTION, SkipFromTestExecution); } } }
public override int Execute() { var projectFilePath = string.Empty; if (!File.Exists(_fileOrDirectory)) { projectFilePath = MsbuildProject.GetProjectFileFromDirectory(_fileOrDirectory).FullName; } else { projectFilePath = _fileOrDirectory; } var tempDgFilePath = string.Empty; if (!_appliedCommand.HasOption("no-restore")) { try { // Create a Dependency Graph file for the project tempDgFilePath = Path.GetTempFileName(); } catch (IOException ioex) { // Catch IOException from Path.GetTempFileName() and throw a graceful exception to the user. throw new GracefulException(string.Format(LocalizableStrings.CmdDGFileIOException, projectFilePath), ioex); } GetProjectDependencyGraph(projectFilePath, tempDgFilePath); } var result = NuGetCommand.Run( TransformArgs( _packageId, tempDgFilePath, projectFilePath)); DisposeTemporaryFile(tempDgFilePath); return(result); }
public override int Execute() { SlnFile slnFile = SlnFileFactory.CreateFromFileOrDirectory(_fileOrDirectory); var arguments = (_parseResult.ValueForArgument <IEnumerable <string> >(SlnAddParser.ProjectPathArgument) ?? Array.Empty <string>()).ToList().AsReadOnly(); if (arguments.Count == 0) { throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToAdd); } PathUtility.EnsureAllPathsExist(arguments, CommonLocalizableStrings.CouldNotFindProjectOrDirectory, true); var fullProjectPaths = arguments.Select(p => { var fullPath = Path.GetFullPath(p); return(Directory.Exists(fullPath) ? MsbuildProject.GetProjectFileFromDirectory(fullPath).FullName : fullPath); }).ToList(); var preAddProjectCount = slnFile.Projects.Count; foreach (var fullProjectPath in fullProjectPaths) { // Identify the intended solution folders var solutionFolders = DetermineSolutionFolder(slnFile, fullProjectPath); slnFile.AddProject(fullProjectPath, solutionFolders); } if (slnFile.Projects.Count > preAddProjectCount) { slnFile.Write(); } return(0); }
public override int Run(string fileOrDirectory) { if (RemainingArguments.Count != 1) { throw new GracefulException(LocalizableStrings.SpecifyExactlyOnePackageReference); } var projectFilePath = string.Empty; if (!File.Exists(fileOrDirectory)) { projectFilePath = MsbuildProject.GetProjectFileFromDirectory(fileOrDirectory).FullName; } else { projectFilePath = fileOrDirectory; } var packageToRemove = RemainingArguments.First(); var result = NuGetCommand.Run(TransformArgs(packageToRemove, projectFilePath)); return(result); }
public override int Execute() { SlnFile slnFile = SlnFileFactory.CreateFromFileOrDirectory(_fileOrDirectory); if (_appliedCommand.Arguments.Count == 0) { throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToAdd); } PathUtility.EnsureAllPathsExist(_appliedCommand.Arguments, CommonLocalizableStrings.CouldNotFindProjectOrDirectory, true); var fullProjectPaths = _appliedCommand.Arguments.Select(p => { var fullPath = Path.GetFullPath(p); return(Directory.Exists(fullPath) ? MsbuildProject.GetProjectFileFromDirectory(fullPath).FullName : fullPath); }).ToList(); var preAddProjectCount = slnFile.Projects.Count; foreach (var fullProjectPath in fullProjectPaths) { // Identify the intended solution folders var solutionFolders = DetermineSolutionFolder(slnFile, fullProjectPath); slnFile.AddProject(fullProjectPath, solutionFolders); } if (slnFile.Projects.Count > preAddProjectCount) { slnFile.Write(); } return(0); }
void InitBaselineTargetFx() { string azSdkFilePath = string.Empty; string azTestFilePath = string.Empty; var azSdkRef = Directory.EnumerateFiles(RepositoryRootDirPath, "AzSdk.reference.props", SearchOption.AllDirectories); if (azSdkRef.Any <string>()) { azSdkFilePath = azSdkRef.FirstOrDefault <string>(); MsbuildProject msbSdk = new MsbuildProject(azSdkFilePath); BaseLineSdkTargetFx = msbSdk.GetPropertyValue("SdkTargetFx"); } var azTestRef = Directory.EnumerateFiles(RepositoryRootDirPath, "AzSdk.test.reference.props", SearchOption.AllDirectories); if (azTestRef.Any <string>()) { azTestFilePath = azTestRef.FirstOrDefault <string>(); MsbuildProject msbTest = new MsbuildProject(azTestFilePath); BaseLineTestTargetFx = msbTest.GetPropertyValue("TargetFrameworks"); } }
public override int Execute() { var msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), _fileOrDirectory, false); var p2ps = msbuildProj.GetProjectToProjectReferences(); if (!p2ps.Any()) { Reporter.Output.WriteLine(string.Format( CommonLocalizableStrings.NoReferencesFound, CommonLocalizableStrings.P2P, _fileOrDirectory)); return(0); } Reporter.Output.WriteLine($"{CommonLocalizableStrings.ProjectReferenceOneOrMore}"); Reporter.Output.WriteLine(new string('-', CommonLocalizableStrings.ProjectReferenceOneOrMore.Length)); foreach (var p2p in p2ps) { Reporter.Output.WriteLine(p2p.Include); } return(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); }
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); } }
public static int Run(string[] args) { DebugHelper.HandleDebugSwitch(ref args); CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false) { Name = "dotnet remove p2p", FullName = LocalizableStrings.AppFullName, Description = LocalizableStrings.AppDescription, AllowArgumentSeparator = true, ArgumentSeparatorHelpText = LocalizableStrings.AppArgumentSeparatorHelpText }; app.HelpOption("-h|--help"); CommandArgument projectArgument = app.Argument( $"<{LocalizableStrings.CmdArgProject}>", LocalizableStrings.CmdArgumentDescription); CommandOption frameworkOption = app.Option( $"-f|--framework <{LocalizableStrings.CmdFramework}>", LocalizableStrings.CmdFrameworkDescription, CommandOptionType.SingleValue); app.OnExecute(() => { if (string.IsNullOrEmpty(projectArgument.Value)) { throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, $"<{LocalizableStrings.ProjectException}>"); } var msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), projectArgument.Value); if (app.RemainingArguments.Count == 0) { throw new GracefulException(LocalizableStrings.SpecifyAtLeastOneReferenceToRemove); } List <string> references = app.RemainingArguments; int numberOfRemovedReferences = msbuildProj.RemoveProjectToProjectReferences( frameworkOption.Value(), references); if (numberOfRemovedReferences != 0) { msbuildProj.ProjectRootElement.Save(); } return(0); }); try { return(app.Execute(args)); } catch (GracefulException e) { Reporter.Error.WriteLine(e.Message.Red()); app.ShowHelp(); return(1); } }
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); }
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); }
private MsbuildProject LoadMsbuildProject() { var collection = new ProjectCollection(); return(MsbuildProject.FromFile(collection, ProjectFileAbsolutePath)); }
public override int Execute() { var projects = new ProjectCollection(); bool interactive = _parseResult.HasOption(AddProjectToProjectReferenceParser.InteractiveOption); MsbuildProject msbuildProj = MsbuildProject.FromFileOrDirectory( projects, _fileOrDirectory, interactive); var frameworkString = _parseResult.ValueForOption <string>(AddProjectToProjectReferenceParser.FrameworkOption); var arguments = _parseResult.ValueForArgument <IReadOnlyCollection <string> >(AddProjectToProjectReferenceParser.ProjectPathArgument); PathUtility.EnsureAllPathsExist(arguments, CommonLocalizableStrings.CouldNotFindProjectOrDirectory, true); List <MsbuildProject> refs = arguments .Select((r) => MsbuildProject.FromFileOrDirectory(projects, r, interactive)) .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 = refs.Select((r) => Path.GetRelativePath( msbuildProj.ProjectDirectory, r.ProjectRootElement.FullPath)).ToList(); int numberOfAddedReferences = msbuildProj.AddProjectToProjectReferences( frameworkString, relativePathReferences); if (numberOfAddedReferences != 0) { msbuildProj.ProjectRootElement.Save(); } return(0); }
private bool Equals(MsbuildProject other) { return(string.Equals(Path, other.Path)); }
public static int Run(string[] args) { DebugHelper.HandleDebugSwitch(ref args); CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false) { Name = "dotnet remove p2p", FullName = ".NET Remove Project to Project (p2p) reference Command", Description = "Command to remove project to project (p2p) reference", AllowArgumentSeparator = true, ArgumentSeparatorHelpText = "Project to project references to remove" }; 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>", "Remove reference only when targetting a specific framework", CommandOptionType.SingleValue); 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.SpecifyAtLeastOneReferenceToRemove); } List <string> references = app.RemainingArguments; int numberOfRemovedReferences = msbuildProj.RemoveProjectToProjectReferences( frameworkOption.Value(), references); if (numberOfRemovedReferences != 0) { msbuildProj.Project.Save(); } return(0); }); try { return(app.Execute(args)); } catch (GracefulException e) { Reporter.Error.WriteLine(e.Message.Red()); app.ShowHelp(); return(1); } }