Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Adding dotnet add package sub command #5122

Merged
merged 6 commits into from
Dec 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions src/dotnet/MsbuildProject.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

This comment was marked as spam.

This comment was marked as spam.

using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Exceptions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common;
using Microsoft.DotNet.Tools.ProjectExtensions;
using NuGet.Frameworks;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Microsoft.DotNet.Tools
{
Expand Down Expand Up @@ -61,6 +61,19 @@ public static MsbuildProject FromFile(ProjectCollection projects, string project
}

public static MsbuildProject FromDirectory(ProjectCollection projects, string projectDirectory)
{
FileInfo projectFile = GetProjectFileFromDirectory(projectDirectory);

var project = TryOpenProject(projects, projectFile.FullName);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

if (project == null)
{
throw new GracefulException(CommonLocalizableStrings.FoundInvalidProject, projectFile.FullName);
}

return new MsbuildProject(projects, project);
}

public static FileInfo GetProjectFileFromDirectory(string projectDirectory)
{
DirectoryInfo dir;
try
Expand Down Expand Up @@ -90,22 +103,7 @@ public static MsbuildProject FromDirectory(ProjectCollection projects, string pr
throw new GracefulException(CommonLocalizableStrings.MoreThanOneProjectInDirectory, projectDirectory);
}

FileInfo projectFile = files.First();

if (!projectFile.Exists)
{
throw new GracefulException(
CommonLocalizableStrings.CouldNotFindAnyProjectInDirectory,
projectDirectory);
}

var project = TryOpenProject(projects, projectFile.FullName);
if (project == null)
{
throw new GracefulException(CommonLocalizableStrings.FoundInvalidProject, projectFile.FullName);
}

return new MsbuildProject(projects, project);
return files.First();

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

}

public int AddProjectToProjectReferences(string framework, IEnumerable<string> refs)
Expand All @@ -120,7 +118,7 @@ public int AddProjectToProjectReferences(string framework, IEnumerable<string> r
if (ProjectRootElement.HasExistingItemWithCondition(framework, @ref))
{
Reporter.Output.WriteLine(string.Format(
CommonLocalizableStrings.ProjectAlreadyHasAreference,
CommonLocalizableStrings.ProjectAlreadyHasAreference,
@ref));
continue;
}
Expand Down Expand Up @@ -270,4 +268,4 @@ private static ProjectRootElement TryOpenProject(ProjectCollection projects, str
}
}
}
}
}

This comment was marked as spam.

4 changes: 3 additions & 1 deletion src/dotnet/commands/dotnet-add/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Tools.Add.PackageReference;
using Microsoft.DotNet.Tools.Add.ProjectToProjectReference;
using Microsoft.DotNet.Tools.Add.ProjectToSolution;

Expand All @@ -18,6 +19,7 @@ public class AddCommand : DotNetTopLevelCommandBase
{
AddProjectToSolutionCommand.Create,
AddProjectToProjectReferenceCommand.Create,
AddPackageReferenceCommand.Create
};

public static int Run(string[] args)
Expand All @@ -26,4 +28,4 @@ public static int Run(string[] args)
return command.RunCommand(args);
}
}
}
}

This comment was marked as spam.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.DotNet.Tools.Add.PackageReference
{
internal class LocalizableStrings

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

{
public const string AppFullName = ".NET Add Package reference Command";

public const string AppDescription = "Command to add package reference";

public const string AppHelpText = "Package references to add";

public const string SpecifyExactlyOnePackageReference = "Please specify one package reference to add.";

public const string CmdFrameworkDescription = "Add reference only when targetting a specific framework";

public const string CmdNoRestoreDescription = "Add reference without performing restore preview and compatibility check.";

public const string CmdSourceDescription = "Use specific NuGet package sources to use during the restore.";

public const string CmdPackageDirectoryDescription = "Restore the packages to this Directory .";

public const string CmdVersionDescription = "Version for the package to be added.";

public const string CmdDGFileException = "Unable to Create Dependency graph file for project '{0}'. Cannot add package reference.";

public const string CmdVersion = "VERSION";

public const string CmdFramework = "FRAMEWORK";

public const string CmdSource = "SOURCE";

public const string CmdPackageDirectory = "PACKAGE_DIRECTORY";
}
}
176 changes: 176 additions & 0 deletions src/dotnet/commands/dotnet-add/dotnet-add-package/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Build.Evaluation;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Tools.NuGet;
using NuGet.Frameworks;

namespace Microsoft.DotNet.Tools.Add.PackageReference
{
internal class AddPackageReferenceCommand : DotNetSubCommandBase
{
private CommandOption _versionOption;
private CommandOption _frameworkOption;
private CommandOption _noRestoreOption;
private CommandOption _sourceOption;
private CommandOption _packageDirectoryOption;

public static DotNetSubCommandBase Create()
{
var command = new AddPackageReferenceCommand
{
Name = "package",
FullName = LocalizableStrings.AppFullName,
Description = LocalizableStrings.AppDescription,
HandleRemainingArguments = true,
ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText,
};

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

command._versionOption = command.Option(
$"-v|--version <{LocalizableStrings.CmdVersion}>",
LocalizableStrings.CmdVersionDescription,
CommandOptionType.SingleValue);

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

command._noRestoreOption = command.Option(
"-n|--no-restore ",
LocalizableStrings.CmdNoRestoreDescription,
CommandOptionType.NoValue);

command._sourceOption = command.Option(
$"-s|--source <{LocalizableStrings.CmdSource}>",
LocalizableStrings.CmdSourceDescription,
CommandOptionType.SingleValue);

command._packageDirectoryOption = command.Option(
$"--package-directory <{LocalizableStrings.CmdPackageDirectory}>",
LocalizableStrings.CmdPackageDirectoryDescription,
CommandOptionType.SingleValue);

return command;
}

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;

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

}

var tempDgFilePath = string.Empty;

if (!_noRestoreOption.HasValue())
{
// Create a Dependency Graph file for the project
tempDgFilePath = Path.GetTempFileName();
GetProjectDependencyGraph(projectFilePath, tempDgFilePath);
}

var result = NuGetCommand.Run(TransformArgs(RemainingArguments.First(), tempDgFilePath, projectFilePath));

This comment was marked as spam.

DisposeTemporaryFile(tempDgFilePath);

return result;
}

private void GetProjectDependencyGraph(string projectFilePath, string dgFilePath)
{
var args = new List<string>();

// Pass the project file path
args.Add(projectFilePath);

// Pass the task as generate restore Dependency Graph file
args.Add("/t:GenerateRestoreGraphFile");

// Pass Dependency Graph file output path
args.Add($"/p:RestoreGraphOutputPath=\"{dgFilePath}\"");

var result = new MSBuildForwardingApp(args).Execute();

if (result != 0)
{
throw new GracefulException(string.Format(LocalizableStrings.CmdDGFileException, projectFilePath));

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

}
}

private void DisposeTemporaryFile(string filePath)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}

private string[] TransformArgs(string packageId, string tempDgFilePath, string projectFilePath)
{
var args = new List<string>(){
"package",
"add",
"--package",
packageId,

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

"--project",
projectFilePath
};

This comment was marked as spam.


if (_versionOption.HasValue())
{
args.Add("--version");
args.Add(_versionOption.Value());
}
if (_sourceOption.HasValue())
{
args.Add("--source");
args.Add(_sourceOption.Value());
}
if (_frameworkOption.HasValue())
{
args.Add("--framework");
args.Add(_frameworkOption.Value());
}
if (_packageDirectoryOption.HasValue())
{
args.Add("--package-directory");
args.Add(_packageDirectoryOption.Value());
}
if (_noRestoreOption.HasValue())
{
args.Add("--no-restore");
}
else
{
args.Add("--dg-file");
args.Add(tempDgFilePath);
}

return args.ToArray();
}
}
}