Skip to content

Commit

Permalink
adding humps-based input matching & system / project install of packages
Browse files Browse the repository at this point in the history
  • Loading branch information
serialseb committed Jun 23, 2010
1 parent 4303f90 commit ea9db52
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 33 deletions.
29 changes: 16 additions & 13 deletions src/OpenWrap.Commands/Wrap/AddWrapCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class AddWrapCommand : ICommand
public string Version { get; set; }

[CommandInput]
public bool LocalOnly { get; set; }
public bool ProjectOnly { get; set; }

[CommandInput]
public bool SystemOnly { get; set; }
Expand All @@ -40,30 +40,29 @@ public IEnumerable<ICommandResult> Execute()

yield return VerifyWrapFile();
yield return VeryfyWrapRepository();
if (_environment.Descriptor != null)
if (_environment.Descriptor != null && !SystemOnly)
{
AddInstructionToWrapFile();
foreach (var nestedResult in SyncWrapFileWithWrapDirectory())
yield return nestedResult;
}
else
{
var resolvedDependencies = _packageManager.TryResolveDependencies(GetDescriptor(), new[]{ _environment.SystemRepository}.Concat(_environment.RemoteRepositories));
var resolvedDependencies = _packageManager.TryResolveDependencies(GetDescriptor(), _environment.RepositoriesToReadFrom());

if (!resolvedDependencies.IsSuccess)
{
yield return new GenericError("The dependency couldn't be found.");
yield break;
}
foreach (var dependency in resolvedDependencies.Dependencies)
var repositoriesToCopyTo = _environment.RemoteRepositories.Concat(new[]
{
yield return new Result("Copying {0} to user repository.", dependency.Package.FullName);
using (var packageStream = dependency.Package.Load().OpenStream())
{
_environment.SystemRepository.Publish(dependency.Package.FullName, packageStream);
yield return new Result("Done.");
}
}
_environment.CurrentDirectoryRepository,
ProjectOnly ? null : _environment.SystemRepository,
_environment.ProjectRepository
});
foreach(var msg in _packageManager.CopyResolvedDependenciesToRepositories(resolvedDependencies, repositoriesToCopyTo))
yield return msg;
}
}

Expand All @@ -76,14 +75,17 @@ WrapDescriptor GetDescriptor()
new WrapDependency
{
Name = Name,
VersionVertices = WrapDependencyParser.ParseVersions(Version.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)).ToList()
VersionVertices = Version != null
? WrapDependencyParser.ParseVersions(Version.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)).ToList()
: new List<VersionVertice>()
}
}
};
}

void AddInstructionToWrapFile()
{

// TODO: Make the environment descriptor separate from reader/writer,
// and remove the File property on it.
var dependLine = GetDependsLine();
Expand All @@ -101,7 +103,8 @@ string GetDependsLine()

IEnumerable<ICommandResult> SyncWrapFileWithWrapDirectory()
{
return new SyncWrapCommand().Execute();
return new SyncWrapCommand()
.Execute();
}

ICommandResult VerifyWrapFile()
Expand Down
1 change: 1 addition & 0 deletions src/OpenWrap.Commands/Wrap/PackageManagerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using OpenWrap.Commands;
using OpenWrap.Commands.Core;
using OpenWrap.Dependencies;

namespace OpenWrap.Repositories
{
Expand Down
20 changes: 20 additions & 0 deletions src/OpenWrap.Tests/Commands/CommandProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ public void parameter_is_assigned_by_name()
.HasRing.ShouldBeTrue();
}
[Test]
public void parameter_is_assigned_when_starting_with_correct_value()
{
given_command<TravelToMordor>();
when_parsing_input("lotr", "travel", "-has", "true");

result.Command.ShouldBeOfType<TravelToMordor>()
.HasRing.ShouldBeTrue();
}
[Test]
public void one_parameter_is_assigned_by_order()
{

Expand Down Expand Up @@ -106,6 +115,17 @@ public void bool_parameters_are_considered_switches()
.IsDangerous.ShouldBeTrue();
}
[Test]
public void parameter_is_assigned_using_beginning_of_input_name()
{
given_command<TravelToMordor>();

when_parsing_input("lotr", "travel", "-has", "true");

result.ShouldBeOfType<Success>()
.Command.ShouldBeOfType<TravelToMordor>()
.HasRing.ShouldBeTrue();
}
[Test]
public void parameter_is_assigned_using_camel_case_initials()
{
given_command<TravelToMordor>();
Expand Down
41 changes: 38 additions & 3 deletions src/OpenWrap.Tests/Commands/addWrapCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,50 @@ public void the_package_is_installed_on_system_repository()

class adding_wrap_from_local_package_in_project_path_with_system_parameter : context.command_context<AddWrapCommand>
{

string SAURON_NAME = "sauron";
Version SAURON_VERSION = new Version(1, 0, 0);

public adding_wrap_from_local_package_in_project_path_with_system_parameter()
{
given_dependency("depends sauron");
given_project_repository();
given_currentdirectory_package("sauron", new Version(1, 0, 0));
given_currentdirectory_package(SAURON_NAME, SAURON_VERSION);


when_executing_command("-Name", SAURON_NAME, "-System");
}
[Test]
public void installs_package_in_system_repository()
{
package_is_in_repository(Environment.SystemRepository, SAURON_NAME, SAURON_VERSION);
}

when_executing_command("-Name","sauron", "-System");
[Test]
public void doesnt_install_package_in_project_repository()
{
package_is_not_in_repository(Environment.ProjectRepository, SAURON_NAME, SAURON_VERSION);
}
}
class adding_wrap_from_local_package_in_project_path_with_project_only_parameter : context.command_context<AddWrapCommand>
{
string SAURON_NAME = "sauron";
Version SAURON_VERSION = new Version(1, 0, 0);
public adding_wrap_from_local_package_in_project_path_with_project_only_parameter()
{
given_currentdirectory_package("sauron", new Version(1,0,0));
given_project_repository();

when_executing_command("-Name", "sauron", "-Project");
}
[Test]
public void installs_package_in_project_repository()
{
package_is_in_repository(Environment.ProjectRepository, SAURON_NAME, SAURON_VERSION);
}
[Test]
public void doesnt_install_package_in_system_repository()
{
package_is_not_in_repository(Environment.SystemRepository,SAURON_NAME, SAURON_VERSION);
}
}
class adding_wrap_from_local_package_outside_of_project_path : context.command_context<AddWrapCommand>
Expand Down
20 changes: 18 additions & 2 deletions src/OpenWrap.Tests/Commands/context/command_context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using OpenWrap.Dependencies;
using OpenWrap.IO;
using OpenWrap.Repositories;
using OpenWrap.Testing;

namespace OpenWrap.Tests.Commands.context
{
Expand All @@ -28,7 +29,7 @@ public command_context()

WrapServices.Clear();
var currentDirectory = "c:\\current";
FileSystem = new InMemoryFileSystem() { CurrentDirectory = currentDirectory};
FileSystem = new InMemoryFileSystem() { CurrentDirectory = currentDirectory };
Environment = new InMemoryEnvironment(FileSystem.GetDirectory(currentDirectory));
WrapServices.RegisterService<IFileSystem>(FileSystem);
WrapServices.RegisterService<IEnvironment>(Environment);
Expand All @@ -43,7 +44,7 @@ protected void given_dependency(string dependency)

protected void given_file(string filePath, Stream stream)
{

}

protected void given_project_package(string name, Version version, params string[] dependencies)
Expand Down Expand Up @@ -90,5 +91,20 @@ protected void given_currentdirectory_package(string packageName, Version versio
{
command_context<AddWrapCommand>.AddPackage(Environment.CurrentDirectoryRepository, packageName, version, dependencies);
}

protected void package_is_not_in_repository(InMemoryRepository repository, string packageName, Version packageVersion)
{
(repository.PackagesByName.Contains("packageName")
? repository.PackagesByName[packageName].FirstOrDefault(x => x.Version.Equals(packageVersion))
: null).ShouldBeNull();


}
protected void package_is_in_repository(InMemoryRepository repository, string packageName, Version packageVersion)
{
repository.PackagesByName[packageName]
.ShouldHaveCountOf(1)
.First().Version.ShouldBe(packageVersion);
}
}
}
24 changes: 24 additions & 0 deletions src/OpenWrap.Tests/Extensions/CamelToSpacedName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,28 @@ public void should_decamelcase_a_leading_lower_string_correctly()
"iAmInCamelCase".CamelToSpacedName().ShouldBe("i am in camel case");
}
}
public class HumpsMatching : context
{
[Test]
public void humps_are_matched()
{
"g".MatchesHumps("Baggins").ShouldBeTrue();

}
[Test]
public void humps_are_matched_case_insensitively()
{
"G".MatchesHumps("Baggins").ShouldBeTrue();
}
[Test]
public void humps_are_matched_on_repetition()
{
"gi".MatchesHumps("Baggins").ShouldBeTrue();
}
[Test]
public void humps_dont_match_incompatible_strings()
{
"gk".MatchesHumps("Baggins").ShouldBeFalse();
}
}
}
2 changes: 1 addition & 1 deletion src/OpenWrap/Commands/CommandInputDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public object ValidateValue<T>(T value)

public void SetValue(object target, object value)
{
Property.SetValue(target, value ?? true, null);
Property.SetValue(target, value ?? "true", null);
}

public PropertyInfo Property { get; set; }
Expand Down
25 changes: 14 additions & 11 deletions src/OpenWrap/Commands/CommandLineProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,22 @@ ICommandInputDescriptor FindCommandInputDescriptor(ICommandDescriptor command, s
{
return descriptor;
}
else // Try to match by camel case initials
{
var potentialInputs =
from input in command.Inputs
where input.Key.GetCamelCaseInitials().Equals(name, StringComparison.OrdinalIgnoreCase)
select input.Value;

// TODO: What happens if the name matches more than one input? Should this throw?
// For now, just return the first input found.
return potentialInputs.FirstOrDefault();
}
var potentialInputs = from input in command.Inputs
where name.MatchesHumps(input.Key)
select input.Value;

//var potentialInputs =
// from input in command.Inputs
// where input.Key.GetCamelCaseInitials().Equals(name, StringComparison.OrdinalIgnoreCase)
// select input.Value;

// TODO: What happens if the name matches more than one input? Should this throw?
// For now, just return the first input found.
return potentialInputs.FirstOrDefault();
}



IEnumerable<KeyValuePair<string, string>> ParseInputsFromCommandLine(IEnumerable<string> strings)
{
string key = null;
Expand Down
1 change: 1 addition & 0 deletions src/OpenWrap/Repositories/IPackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ DependencyResolutionResult TryResolveDependencies(WrapDescriptor wrapDescriptor,
void UpdateDependency(ResolvedDependency dependency,
IPackageRepository destinationRepository);
}

}
42 changes: 39 additions & 3 deletions src/OpenWrap/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;

Expand All @@ -9,21 +10,21 @@ public static string CamelToSpacedName(this string str)
{
var builder = new StringBuilder();

for(var i = 0 ; i <str.Length;i++)
for (var i = 0; i < str.Length; i++)
{
var chr = str[i];

if (str[i] >= 65 && str[i] <= 90)
{
if(i > 0)
if (i > 0)
{
builder.Append(' ');
chr = (char)(chr + 32);
}
}

builder.Append(chr);

}

return builder.ToString();
Expand All @@ -33,5 +34,40 @@ public static string GetCamelCaseInitials(this string str)
{
return new string(str.Where(char.IsUpper).ToArray());
}
public static IEnumerable<string> SplitCamelCase(this string str)
{
StringBuilder currentValue = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
if (i == 0 || !char.IsUpper(str[i]))
currentValue.Append(str[i]);
else if (char.IsUpper(str[i]))
{
yield return currentValue.ToString();
currentValue = new StringBuilder().Append(str[i]);
}
}
if (currentValue.Length > 0)
yield return currentValue.ToString();
}
public static bool MatchesHumps(this string name, string value)
{
return name.ToUpperInvariant().MatchesHumps(0, value.ToUpperInvariant(), 0);
}


static bool MatchesHumps(this string name, int namePosition, string value, int valuePosition)
{
if (valuePosition > value.Length-1) return false;
var foundPosition = value.IndexOf(name[namePosition], valuePosition);
return foundPosition != -1 &&
(namePosition == name.Length-1 ||
MatchesHumps(name, namePosition + 1, value, foundPosition + 1));
}

static bool CharEq(int namePosition, string name, int valuePosition, string value)
{
return char.ToUpperInvariant(value[valuePosition]) == char.ToUpperInvariant(name[namePosition]);
}
}
}

0 comments on commit ea9db52

Please sign in to comment.