Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for dotnet add package command #1063

Closed
wants to merge 45 commits into from
Closed

Conversation

mishra14
Copy link
Contributor

@mishra14 mishra14 commented Dec 17, 2016

Fixes : NuGet/Home#3751

This PR adds a new xplat command to allow users to add package references though dotnet cli.

Objectives -

  1. Add an add package command to allow users to add package reference into csproj files through dotnet cli as part of https://github.com/dotnet/cli/issues/4521
  2. Edit build and test scripts to download a newer version of dotnet cli into root/cli_test to enable testing.
  3. Add a new csproj file as a template for SimplePackageContext()
  4. Add tests for add package command.

//cc: @emgarten @joelverhagen @rohit21agrawal @rrelyea @zhili1208 @jainaashish @alpaix @nkolev92

Nvm the number of commits, will squash and merge!

Copy link
Member

@emgarten emgarten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major changes needed:

  1. Pass a delegate in for dotnet msbuild
  2. Make methods async instead of calling .Result


$installDotnet = Join-Path $CLIRootTest "dotnet-install.ps1"

wget 'https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0/scripts/obtain/dotnet-install.ps1' -OutFile $installDotnet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a url containing the commit hash, this script commonly changes

var sources = addpkg.Option(
"-s|--sources",
Strings.AddPkg_SourcesDescription,
CommandOptionType.SingleValue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var frameworks = addpkg.Option(
"-f|--frameworks",
Strings.AddPkg_FrameworksDescription,
CommandOptionType.SingleValue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this allow multiple?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And why is the name plural for a single value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the same framework parameter as build does

CommandOptionType.SingleValue);

var packageDirectory = addpkg.Option(
"--package-directory",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use --packages
This command should match the other commands as close as possible
https://github.com/dotnet/cli/blob/rel/1.0.0/src/dotnet/commands/dotnet-restore/Program.cs#L38-L41

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats misleading. --packages implies you want to add reference to multiple packages!

var packageRefArgs = new PackageReferenceArgs(dotnetPath.Value(), projectPath.Value(), packageDependency, logger)
{
Frameworks = StringUtility.Split(frameworks.Value()),
Sources = StringUtility.Split(sources.Value()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just allow multiple values instead of splitting this yourself, it is inconsistent with the other commands

@@ -337,4 +337,80 @@ For more information, visit http://docs.nuget.org/docs/reference/command-line-re
usage: NuGet locals <all | http-cache | global-packages | temp> [--clear | -c | --list | -l]
For more information, visit http://docs.nuget.org/docs/reference/command-line-reference</value>
</data>
<data name="Error_MSBuildUnableToOpenProject" xml:space="preserve">
<value>MSBuild was unable to open Project '{0}'.</value>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: lowecase p

<value>Failed to locate dotnet in dotnet add pkg command.</value>
</data>
<data name="Error_FailedToCreateRandomFile" xml:space="preserve">
<value>Failed to create random file for dotnet add pkg command.</value>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not know what to do with this message as a user. Write out the path instead.

var projectRootElement = TryOpenProjectRootElement(projectCSProjPath);
if (projectCSProjPath == null)
{
throw new Exception(string.Format(CultureInfo.CurrentCulture, Strings.Error_MSBuildUnableToOpenProject));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the format needed here?

var projectRootElement = TryOpenProjectRootElement(projectCSProjPath);
if (projectCSProjPath == null)
{
throw new Exception(string.Format(CultureInfo.CurrentCulture, Strings.Error_MSBuildUnableToOpenProject));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove format


namespace NuGet.Common
{
public class TempFile : IDisposable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs clean up, copying classes from test to production code doesn't work. This shouldn't use any sub folders under /tmp for linux to avoid permission issues

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used anywhere now?

.Where(t => t.Success)
.Select(t => t.Graph.Framework));

if (packageReferenceArgs.Frameworks?.Any() == true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for == true

Copy link
Contributor Author

@mishra14 mishra14 Dec 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packageReferenceArgs.Frameworks?.Any() is a bool? and if(bool?) is not a valid statement.

Copy link
Member

@emgarten emgarten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall great ✨

This needs a few small fixes and then :shipit:

  1. Check GetAllUnresolved() and handle errors
  2. Remove unused loc strings/helper methods
  3. TempFile doesn't look like it is used now, it might be better to avoid that change for now.

@@ -164,6 +178,41 @@ public LockFile AssetsFile
}
}

public PackageSpec PackageSpec
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


namespace NuGet.Common
{
public class TempFile : IDisposable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used anywhere now?

{
if (File.Exists(_filePath))
{
File.Delete(_filePath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't clean up the folder

var tempDirectory = Path.GetTempPath();
var randomFolderName = Guid.NewGuid().ToString();
var randomFileName = Guid.NewGuid().ToString();
Directory.CreateDirectory(Path.Combine(tempDirectory, randomFolderName));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a need for a sub directory here. Just create it in the temp root.


namespace NuGet.Common
{
public static class StringUtility
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSBuild helpers should not be in Common

@@ -337,4 +337,81 @@ For more information, visit http://docs.nuget.org/docs/reference/command-line-re
usage: NuGet locals &lt;all | http-cache | global-packages | temp&gt; [--clear | -c | --list | -l]
For more information, visit http://docs.nuget.org/docs/reference/command-line-reference</value>
</data>
<data name="Error_MSBuildUnableToOpenProject" xml:space="preserve">
<value>msbuild was unable to open Project '{0}'.</value>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: lowercase project

<value>msbuild was unable to open Project '{0}'.</value>
<comment>{0} - Project/csproj path</comment>
</data>
<data name="Error_DotnetNotFound" xml:space="preserve">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unused strings I see quite a few in here that are no longer used.

packageReferenceArgs.Logger.LogDebug("Project Dependency Graph Read");

var projectName = dgSpec.Restore.FirstOrDefault();
var originalPackageSpec = dgSpec.GetProjectSpec(projectName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like it could crash if the dg spec wasn't passed in and an empty one was returned, maybe throw a helpful error message instead

}
}

private static void LogQueue(ConcurrentQueue<string> outputs, ILogger logger)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see these helper methods being used anywhere now


// 2. Run Restore Preview
packageReferenceArgs.Logger.LogDebug("Running Restore preview");
var restorePreviewResult = await PreviewAddPackageReference(packageReferenceArgs,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking Success wont work here, but you should verify GetAllUnresolved() == 0

It is possible the packages were missing or couldn't be downloaded. If that happens they won't show in the compat check since there was nothing to verify. Later it will null ref when they are missing from the graph.

@mishra14
Copy link
Contributor Author

merged : dd0f0aa

@mishra14 mishra14 closed this Dec 21, 2016
@mishra14 mishra14 deleted the dev-anmishr-ref branch December 21, 2016 23:07
@mishra14 mishra14 restored the dev-anmishr-ref branch December 22, 2016 20:40
@mishra14 mishra14 deleted the dev-anmishr-ref branch January 5, 2017 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants