Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

fix #1113 by adding 'dnu feeds list' command #2121

Merged
merged 1 commit into from
Jun 30, 2015
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.Framework.Runtime.Common.CommandLine;

namespace Microsoft.Framework.PackageManager
{
internal static class FeedsConsoleCommand
{
public static void Register(CommandLineApplication cmdApp, ReportsFactory reportsFactory)
{
cmdApp.Command("feeds", c =>
{
c.Description = "Commands related to managing package feeds currently in use";
c.HelpOption("-?|-h|--help");
c.OnExecute(() =>
{
c.ShowHelp();
return 2;
});

RegisterListCommand(c, reportsFactory);
});
}

public static void RegisterListCommand(CommandLineApplication cmdApp, ReportsFactory reportsFactory)
{
cmdApp.Command("list", c =>
{
c.Description = "Displays a list of package sources in effect for a project";
var argRoot = c.Argument("[root]",
"The path of the project to calculate effective package sources for (defaults to the current directory)");

c.OnExecute(() =>
{
var command = new ListSourcesCommand(
reportsFactory.CreateReports(quiet: false),
string.IsNullOrEmpty(argRoot.Value) ? "." : argRoot.Value);

return command.Execute();
});
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,12 @@ private async Task<Tuple<string, string>> ResolvePackageIdAndVersion(string pack
if (string.IsNullOrEmpty(packageVersion))
{
var rootDirectory = ProjectResolver.ResolveRootDirectory(_commandsRepository.Root.Root);
var settings = SettingsUtils.ReadSettings(
rootDirectory,
RestoreCommand.NuGetConfigFile,
RestoreCommand.FileSystem,
RestoreCommand.MachineWideSettings);

var sourceProvier = PackageSourceBuilder.CreateSourceProvider(settings);
var config = NuGetConfig.ForSolution(rootDirectory, RestoreCommand.FileSystem);

var packageFeeds = new List<IPackageFeed>();

var effectiveSources = PackageSourceUtils.GetEffectivePackageSources(
sourceProvier,
config.Sources,
FeedOptions.Sources,
FeedOptions.FallbackSources);

Expand Down
47 changes: 47 additions & 0 deletions src/Microsoft.Framework.PackageManager/NuGetConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using NuGet;

namespace Microsoft.Framework.PackageManager
{
/// <summary>
/// Manages the NuGet.config hierarchy
/// </summary>
public class NuGetConfig
{
public NuGetConfig(ISettings settings, PackageSourceProvider sources)
{
Settings = settings;
Sources = sources;
}

public ISettings Settings { get; }

public IPackageSourceProvider Sources { get; }
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a new line to separate the properties.


public static NuGetConfig ForSolution(string solutionDirectory)
{
return ForSolution(solutionDirectory, new PhysicalFileSystem(Directory.GetCurrentDirectory()));
}

public static NuGetConfig ForSolution(string solutionDirectory, IFileSystem fileSystem)
{
var settings = SettingsUtils.ReadSettings(
solutionDirectory,
nugetConfigFile: null,
fileSystem: fileSystem,
machineWideSettings: new CommandLineMachineWideSettings());

// Recreate the source provider
var sources = PackageSourceBuilder.CreateSourceProvider(settings);

return new NuGetConfig(settings, sources);
}
}
}
1 change: 1 addition & 0 deletions src/Microsoft.Framework.PackageManager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public int Main(string[] args)
PublishConsoleCommand.Register(app, reportsFactory, _environment, _hostServices);
RestoreConsoleCommand.Register(app, reportsFactory, _environment);
WrapConsoleCommand.Register(app, reportsFactory);
FeedsConsoleCommand.Register(app, reportsFactory);

return app.Execute(args);
}
Expand Down
17 changes: 4 additions & 13 deletions src/Microsoft.Framework.PackageManager/Restore/RestoreCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public RestoreCommand(FrameworkName fallbackFramework)
{
FallbackFramework = fallbackFramework;
FileSystem = new PhysicalFileSystem(Directory.GetCurrentDirectory());
MachineWideSettings = new CommandLineMachineWideSettings();
ScriptExecutor = new ScriptExecutor();
ErrorMessages = new Dictionary<string, List<string>>(StringComparer.Ordinal);
InformationMessages = new Dictionary<string, List<string>>(StringComparer.Ordinal);
Expand All @@ -58,15 +57,13 @@ public RestoreCommand(FrameworkName fallbackFramework)
public FeedOptions FeedOptions { get; set; }

public List<string> RestoreDirectories { get; } = new List<string>();
public string NuGetConfigFile { get; set; }
public bool Lock { get; set; }
public bool Unlock { get; set; }

public ScriptExecutor ScriptExecutor { get; private set; }

public List<FrameworkName> TargetFrameworks { get; set; } = new List<FrameworkName>();
public FrameworkName FallbackFramework { get; set; }
public IMachineWideSettings MachineWideSettings { get; set; }
public IFileSystem FileSystem { get; set; }
public Reports Reports { get; set; }
public bool CheckHashFile { get; set; } = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nobody ever changes this value...

Expand All @@ -77,8 +74,7 @@ public RestoreCommand(FrameworkName fallbackFramework)
private Dictionary<string, List<string>> ErrorMessages { get; set; }
private Dictionary<string, List<string>> InformationMessages { get; set; }

protected internal ISettings Settings { get; set; }
protected internal IPackageSourceProvider SourceProvider { get; set; }
protected internal NuGetConfig Config { get; set; }

public async Task<bool> Execute()
{
Expand All @@ -104,7 +100,7 @@ public async Task<bool> Execute()
}
}

var settings = Settings as Settings;
var settings = Config.Settings as Settings;
if (settings != null)
{
var configFiles = settings.GetConfigFiles();
Expand Down Expand Up @@ -310,7 +306,7 @@ private async Task<bool> RestoreForProject(string projectJsonPath, string rootDi
new NuGetDependencyResolver(packageRepository)));

var effectiveSources = PackageSourceUtils.GetEffectivePackageSources(
SourceProvider,
Config.Sources,
FeedOptions.Sources,
FeedOptions.FallbackSources);

Expand Down Expand Up @@ -955,12 +951,7 @@ void Display(string indent, IEnumerable<GraphNode> graphs)

private void ReadSettings(string solutionDirectory)
{
Settings = SettingsUtils.ReadSettings(solutionDirectory, NuGetConfigFile, FileSystem, MachineWideSettings);

// Recreate the source provider and credential provider
SourceProvider = PackageSourceBuilder.CreateSourceProvider(Settings);
//HttpClient.DefaultCredentialProvider = new SettingsCredentialProvider(new ConsoleCredentialProvider(Console), SourceProvider, Console);

Config = NuGetConfig.ForSolution(solutionDirectory, FileSystem);
}

private IFileSystem CreateFileSystem(string path)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq;

namespace Microsoft.Framework.PackageManager
{
internal class ListSourcesCommand
{
public Reports Reports { get; }
public string TargetDirectory { get; }

public ListSourcesCommand(Reports reports, string targetDirectory)
{
Reports = reports;
TargetDirectory = targetDirectory;
}

public int Execute()
{
// Most of this code is from NuGet, just some refactoring for our system.
// Collect config
var config = NuGetConfig.ForSolution(TargetDirectory);

var sources = config.Sources.LoadPackageSources();
if (!sources.Any())
{
Reports.Information.WriteLine("No sources found.");
}
else
{
Reports.Information.WriteLine("Registered Sources:");

// Iterate over the sources and report them
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: add empty line before comment

int index = 1; // This is an index for display so it should be 1-based
foreach (var source in sources)
{
var enabledString = source.IsEnabled ? "" : " [Disabled]";
var line = $"{index.ToString("0\\.").PadRight(3)} {source.Name} {source.Source}{enabledString.Yellow().Bold()}";
Reports.Information.WriteLine(line);

index++;
}
}
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.IO;
using Microsoft.Framework.CommonTestUtils;
using Microsoft.Framework.PackageManager.FunctionalTests;
using Xunit;

namespace Microsoft.Framework.PackageManager
{
[Collection(nameof(PackageManagerFunctionalTestCollection))]
public class DnuFeedsTests
{
private readonly PackageManagerFunctionalTestFixture _fixture;

public DnuFeedsTests(PackageManagerFunctionalTestFixture fixture)
{
_fixture = fixture;
}

public static IEnumerable<object[]> RuntimeComponents
{
get
{
return TestUtils.GetRuntimeComponentsCombinations();
}
}

[Theory]
[MemberData(nameof(RuntimeComponents))]
public void DnuFeeds_NoSources(string flavor, string os, string architecture)
{
var environment = new Dictionary<string, string>
{
{ "DNX_TRACE", "0" },
};

var rootConfig =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<clear /> <!-- Remove the effects of any machine-level config -->
</packageSources>
</configuration>";

var projectStructure =
@"{
'root': {
'NuGet.Config': """",
}
}";

var expectedContent = @"Registered Sources:
1. https://www.nuget.org/api/v2/ https://www.nuget.org/api/v2/
";
var runtimeHomePath = _fixture.GetRuntimeHomeDir(flavor, os, architecture);
using (var testEnv = new DnuTestEnvironment(runtimeHomePath, projectName: "Project Name"))
{
var projectPath = testEnv.ProjectPath;
DirTree.CreateFromJson(projectStructure)
.WithFileContents("root/NuGet.Config", rootConfig)
.WriteTo(projectPath);

string output;
string error;
var exitCode = DnuTestUtils.ExecDnu(
runtimeHomePath,
subcommand: "feeds",
arguments: "list root",
stdOut: out output,
stdErr: out error,
environment: environment,
workingDir: projectPath);

Assert.Equal(0, exitCode);
Assert.Empty(error);
Assert.Equal(expectedContent, output);
}
}

[Theory]
[MemberData(nameof(RuntimeComponents))]
public void DnuFeeds_ListsAllSources(string flavor, string os, string architecture)
{
var environment = new Dictionary<string, string>
{
{ "DNX_TRACE", "0" },
};

var rootConfig =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<clear /> <!-- Remove the effects of any machine-level config -->
<add key=""Source1"" value=""https://source1"" />
<add key=""Source2"" value=""https://source2"" />
</packageSources>
</configuration>";

var subConfig =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<add key=""Source3"" value=""https://source3"" />
</packageSources>
<disabledPackageSources>
<add key=""Source1"" value=""https://source1"" />
</disabledPackageSources>
</configuration>";

var projectStructure =
@"{
'root': {
'NuGet.Config': """",
'sub': {
'NuGet.Config': """"
}
}
}";

var expectedContent = @"Registered Sources:
1. Source1 https://source1 [Disabled]
2. Source2 https://source2
3. Source3 https://source3
4. https://www.nuget.org/api/v2/ https://www.nuget.org/api/v2/ [Disabled]
";
var runtimeHomePath = _fixture.GetRuntimeHomeDir(flavor, os, architecture);
using (var testEnv = new DnuTestEnvironment(runtimeHomePath, projectName: "Project Name"))
{
var projectPath = testEnv.ProjectPath;
DirTree.CreateFromJson(projectStructure)
.WithFileContents("root/NuGet.Config", rootConfig)
.WithFileContents("root/sub/NuGet.Config", subConfig)
.WriteTo(projectPath);

string output;
string error;
var exitCode = DnuTestUtils.ExecDnu(
runtimeHomePath,
subcommand: "feeds",
arguments: "list root/sub",
stdOut: out output,
stdErr: out error,
environment: environment,
workingDir: projectPath);

Assert.Equal(0, exitCode);
Assert.Empty(error);
Assert.Equal(expectedContent, output);
}
}
}
}