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

Start exposing lists and package results via the API #132 #137

Merged
merged 4 commits into from
Mar 5, 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
Expand Up @@ -355,7 +355,7 @@ public void should_call_service_source_add_when_command_is_add()
because();
configSettingsService.Verify(c => c.source_add(configuration), Times.Once);
}

[Fact]
public void should_call_service_source_remove_when_command_is_remove()
{
Expand All @@ -380,5 +380,23 @@ public void should_call_service_source_enable_when_command_is_enable()
configSettingsService.Verify(c => c.source_enable(configuration), Times.Once);
}
}

public class when_list_is_called : ChocolateySourceCommandSpecsBase
{
private Action because;

public override void Because()
{
because = () => command.list(configuration);
}

[Fact]
public void should_call_service_source_list_when_command_is_list()
{
configuration.SourceCommand.Command = SourceCommandType.list;
because();
configSettingsService.Verify(c => c.source_list(configuration), Times.Once);
}
}
}
}
11 changes: 10 additions & 1 deletion src/chocolatey/GetChocolatey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,16 @@ private void extract_resources()

AssemblyFileExtractor.extract_all_resources_to_relative_directory(_container.GetInstance<IFileSystem>(), Assembly.GetAssembly(typeof(ChocolateyResourcesAssembly)), ApplicationParameters.InstallLocation, folders, ApplicationParameters.ChocolateyFileResources);
}

public IEnumerable<T> List<T>()
{
extract_resources();
var configuration = create_configuration(new List<string>());
configuration.RegularOuptut = true;
var runner = new GenericRunner();
return runner.list<T>(configuration, _container, isConsole: false, parseArgs: null);
}
Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

Choose a reason for hiding this comment

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

I tend to push return statements to have an empty line before. This looks good though.

}

// ReSharper restore InconsistentNaming
}
}
2 changes: 2 additions & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<Compile Include="infrastructure.app\commands\ChocolateyUpdateCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyUpgradeCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyVersionCommand.cs" />
<Compile Include="infrastructure.app\configuration\ChocolateySource.cs" />
<Compile Include="infrastructure.app\configuration\ConfigFileFeatureSetting.cs" />
<Compile Include="infrastructure.app\configuration\PackagesConfigFilePackageSetting.cs" />
<Compile Include="infrastructure.app\configuration\PackagesConfigFileSettings.cs" />
Expand Down Expand Up @@ -160,6 +161,7 @@
<Compile Include="infrastructure\commandline\ExitScenarioHandler.cs" />
<Compile Include="infrastructure\commandline\InteractivePrompt.cs" />
<Compile Include="infrastructure\commands\ICommandExecutor.cs" />
<Compile Include="infrastructure\commands\IListCommand.cs" />
<Compile Include="infrastructure\commands\PowershellExecutor.cs" />
<Compile Include="infrastructure\guards\Ensure.cs" />
<Compile Include="infrastructure\information\ProcessInformation.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace chocolatey.infrastructure.app.commands

[CommandFor(CommandNameType.sources)]
[CommandFor(CommandNameType.source)]
public sealed class ChocolateySourceCommand : ICommand
public sealed class ChocolateySourceCommand : IListCommand<ChocolateySource>
Copy link
Member

Choose a reason for hiding this comment

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

What benefit does this change have?

Copy link
Member

Choose a reason for hiding this comment

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

I mean if you implemented a separate command, what are you gaining here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This extends ICommand by adding a list function without forcing every command to implement list. I cannot make it an additional interface instead of an inherited one, because you need to be able to search for commands based on classes that implement the interface.

This seemed natural to me. Is there some objection?

Copy link
Member

Choose a reason for hiding this comment

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

I think I am starting to understand it, I'm just trying to understand it here.

{
private readonly IChocolateyConfigSettingsService _configSettingsService;

Expand Down Expand Up @@ -139,5 +139,10 @@ public void run(ChocolateyConfiguration configuration)
break;
}
}

public IEnumerable<ChocolateySource> list(ChocolateyConfiguration configuration)
{
return _configSettingsService.source_list(configuration);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright © 2015 - Present RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.app.configuration
{
public class ChocolateySource
{
public string Id { get; set; }
public string Value { get; set; }
public bool Disabled { get; set; }
public bool Authenticated { get; set; }
}
}
40 changes: 33 additions & 7 deletions src/chocolatey/infrastructure.app/runners/GenericRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.


namespace chocolatey.infrastructure.app.runners
{
using System;
using System.Linq;
using System.Collections.Generic;
using SimpleInjector;
using adapters;
using attributes;
Expand All @@ -28,7 +30,7 @@ namespace chocolatey.infrastructure.app.runners

public sealed class GenericRunner
{
public void run(ChocolateyConfiguration config, Container container, bool isConsole, Action<ICommand> parseArgs)
private ICommand find_command(ChocolateyConfiguration config, Container container, bool isConsole, Action<ICommand> parseArgs)
{
var commands = container.GetAllInstances<ICommand>();
var command = commands.Where((c) =>
Expand Down Expand Up @@ -83,7 +85,7 @@ Chocolatey is not an official build (bypassed with --allow-unofficial).
If you are seeing this message and it is not expected, your system may
now be in a bad state. Only official builds are to be trusted.
"
);
);

}
}
Expand All @@ -96,14 +98,38 @@ now be in a bad state. Only official builds are to be trusted.
}

command.noop(config);
return null;
}
else
}
return command;
}

public void run(ChocolateyConfiguration config, Container container, bool isConsole, Action<ICommand> parseArgs)
{
var command = find_command(config, container, isConsole, parseArgs);
if(command != null)
{
this.Log().Debug("_ {0}:{1} - Normal Run Mode _".format_with(ApplicationParameters.Name, command.GetType().Name));
command.run(config);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Need a space below this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check.


public IEnumerable<T> list<T>(ChocolateyConfiguration config, Container container, bool isConsole, Action<ICommand> parseArgs)
{
var command = find_command(config, container, isConsole, parseArgs) as IListCommand<T>;
if (command == null)
{
if (!string.IsNullOrWhiteSpace(config.CommandName))
{
this.Log().Debug("_ {0}:{1} - Normal Run Mode _".format_with(ApplicationParameters.Name, command.GetType().Name));
command.run(config);
throw new Exception("The implementation of '{0}' does not support listing '{1}'".format_with(config.CommandName, typeof(T).Name));
}
return new List<T>();
}
else
{
this.Log().Debug("_ {0}:{1} - Normal List Mode _".format_with(ApplicationParameters.Name, command.GetType().Name));
return command.list(config);
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace chocolatey.infrastructure.app.services
{
using System;
using System.Collections.Generic;
using System.Linq;
using configuration;
using infrastructure.services;
Expand Down Expand Up @@ -43,12 +44,22 @@ public void noop(ChocolateyConfiguration configuration)
this.Log().Info("Would have made a change to the configuration.");
}

public void source_list(ChocolateyConfiguration configuration)
public IEnumerable<ChocolateySource> source_list(ChocolateyConfiguration configuration)
{
var list = new List<ChocolateySource>();
foreach (var source in configFileSettings.Sources)
{
this.Log().Info(() => "{0}{1} - {2}".format_with(source.Id, source.Disabled ? " [Disabled]" : string.Empty, source.Value));
}
if (configuration.RegularOuptut) {
this.Log().Info(() => "{0}{1} - {2}".format_with(source.Id, source.Disabled ? " [Disabled]" : string.Empty, source.Value));
}
list.Add(new ChocolateySource {
Id = source.Id,
Value = source.Value,
Disabled = source.Disabled,
Authenticated = string.IsNullOrWhiteSpace(source.Password)
});
}
return list;
Copy link
Member

Choose a reason for hiding this comment

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

👍

}

public void source_add(ChocolateyConfiguration configuration)
Expand Down Expand Up @@ -232,4 +243,4 @@ public void set_api_key(ChocolateyConfiguration configuration)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
namespace chocolatey.infrastructure.app.services
{
using System;
using System.Collections.Generic;
using configuration;

public interface IChocolateyConfigSettingsService
{
void noop(ChocolateyConfiguration configuration);
void source_list(ChocolateyConfiguration configuration);
IEnumerable<ChocolateySource> source_list(ChocolateyConfiguration configuration);
Copy link
Member

Choose a reason for hiding this comment

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

👍

void source_add(ChocolateyConfiguration configuration);
void source_remove(ChocolateyConfiguration configuration);
void source_disable(ChocolateyConfiguration configuration);
Expand All @@ -32,4 +33,4 @@ public interface IChocolateyConfigSettingsService
string get_api_key(ChocolateyConfiguration configuration, Action<ConfigFileApiKeySetting> keyAction);
void set_api_key(ChocolateyConfiguration configuration);
}
}
}
25 changes: 25 additions & 0 deletions src/chocolatey/infrastructure/commands/IListCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright © 2015 - Present RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.commands
{
using System.Collections.Generic;
using app.configuration;

public interface IListCommand<out T> : ICommand
{
IEnumerable<T> list(ChocolateyConfiguration config);
}
}