Adding some package metadata services

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4039316
This commit is contained in:
loudej 2009-11-10 06:15:21 +00:00
parent 68fd501b24
commit 0eaf1ccb30
18 changed files with 345 additions and 1 deletions

BIN
lib/yaml/Yaml.dll Normal file

Binary file not shown.

View File

@ -53,6 +53,11 @@ namespace Orchard.Tests.Environment {
public IEnumerable<RouteDescriptor> GetRoutes() {
return _routes;
}
public void GetRoutes(ICollection<RouteDescriptor> routes) {
foreach (var routeDescriptor in GetRoutes())
routes.Add(routeDescriptor);
}
}
public class StubRoutePublisher : IRoutePublisher {

View File

@ -95,6 +95,10 @@
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Yaml, Version=1.0.3370.39839, Culture=neutral, PublicKeyToken=187a3d240e44a135, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\yaml\Yaml.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="DataUtility.cs" />
@ -120,6 +124,8 @@
<Compile Include="Mvc\RouteCollectionPublisherTests.cs" />
<Compile Include="Notify\NotifierTests.cs" />
<Compile Include="Notify\NotifyFilterTests.cs" />
<Compile Include="Packages\PackageFoldersTests.cs" />
<Compile Include="Packages\PackageManagerTests.cs" />
<Compile Include="Services\ClockTests.cs" />
<Compile Include="Storage\FileSystemStorageProviderTests.cs" />
<Compile Include="Stubs\StubClock.cs" />
@ -141,6 +147,13 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Packages\FoldersData\Sample2\Two.txt" />
<EmbeddedResource Include="Packages\FoldersData\Sample1\Package.txt" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Packages\FoldersData\Sample3\Package.txt" />
</ItemGroup>
<ItemGroup>
<Folder Include="Utility\" />
</ItemGroup>

View File

@ -0,0 +1,8 @@
name: Le plug-in français
author: Bertrand Le Roy
description:
This plug-in replaces
'the' with 'le'
version: 1.0
tags: plug-in, français, the, le
homepage: http://weblogs.asp.net/bleroy

View File

@ -0,0 +1 @@
This is another test.txt

View File

@ -0,0 +1 @@
This is another test.txt

View File

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Orchard.Packages;
using Yaml.Grammar;
namespace Orchard.Tests.Packages {
[TestFixture]
public class PackageFoldersTests {
private const string DataPrefix = "Orchard.Tests.Packages.FoldersData.";
private string _tempFolderName;
[SetUp]
public void Init() {
_tempFolderName = Path.GetTempFileName();
File.Delete(_tempFolderName);
var assembly = GetType().Assembly;
foreach (var name in assembly.GetManifestResourceNames()) {
if (name.StartsWith(DataPrefix)) {
var text = "";
using (var stream = assembly.GetManifestResourceStream(name)) {
using (var reader = new StreamReader(stream))
text = reader.ReadToEnd();
}
var relativePath = name
.Substring(DataPrefix.Length)
.Replace(".txt", ":txt")
.Replace('.', Path.DirectorySeparatorChar)
.Replace(":txt", ".txt");
var targetPath = Path.Combine(_tempFolderName, relativePath);
Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
using (var stream = new FileStream(targetPath, FileMode.Create)) {
using (var writer = new StreamWriter(stream)) {
writer.Write(text);
}
}
}
}
}
[TearDown]
public void Term() {
Directory.Delete(_tempFolderName, true);
}
[Test]
public void NamesFromFoldersWithPackageTxtShouldBeListed() {
var folders = new PackageFolders(new[] { _tempFolderName });
var names = folders.ListNames();
Assert.That(names.Count(), Is.EqualTo(2));
Assert.That(names, Has.Some.EqualTo("Sample1"));
Assert.That(names, Has.Some.EqualTo("Sample3"));
}
[Test]
public void PackageTxtShouldBeParsedAndReturnedAsYamlDocument() {
var folders = new PackageFolders(new[] { _tempFolderName });
var sample1 = folders.ParseManifest("Sample1");
var mapping = (Mapping)sample1.Root;
var entities = mapping.Entities
.Where(x=>x.Key is Scalar)
.ToDictionary(x => ((Scalar)x.Key).Text, x => x.Value);
Assert.That(entities.Keys, Has.Some.EqualTo("name"));
Assert.That(entities.Keys, Has.Some.EqualTo("author"));
}
}
}

View File

@ -0,0 +1,78 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using Autofac.Builder;
using NUnit.Framework;
using Orchard.Packages;
using Yaml.Grammar;
namespace Orchard.Tests.Packages {
[TestFixture]
public class PackageManagerTests {
private IContainer _container;
private IPackageManager _manager;
private StubFolders _folders;
[SetUp]
public void Init() {
var builder = new ContainerBuilder();
_folders = new StubFolders();
builder.Register(_folders).As<IPackageFolders>();
builder.Register<PackageManager>().As<IPackageManager>();
_container = builder.Build();
_manager = _container.Resolve<IPackageManager>();
}
public class StubFolders : IPackageFolders {
public StubFolders() {
Manifests = new Dictionary<string, string>();
}
public IDictionary<string, string> Manifests { get; set; }
public IEnumerable<string> ListNames() {
return Manifests.Keys;
}
public YamlDocument ParseManifest(string name) {
var parser = new YamlParser();
bool success;
var stream = parser.ParseYamlStream(new TextInput(Manifests[name]), out success);
return success ? stream.Documents.Single() : null;
}
}
[Test]
public void AvailablePackagesShouldFollowCatalogLocations() {
_folders.Manifests.Add("foo", "name: Foo");
_folders.Manifests.Add("bar", "name: Bar");
_folders.Manifests.Add("frap", "name: Frap");
_folders.Manifests.Add("quad", "name: Quad");
var available = _manager.AvailablePackages();
Assert.That(available.Count(), Is.EqualTo(4));
Assert.That(available, Has.Some.Property("Name").EqualTo("foo"));
}
[Test]
public void PackageDescriptorsShouldHaveNameAndDescription() {
_folders.Manifests.Add("Sample", @"
name: Sample Package
description: This is the description
version: 2.x
");
var descriptor = _manager.AvailablePackages().Single();
Assert.That(descriptor.Name, Is.EqualTo("Sample"));
Assert.That(descriptor.DisplayName, Is.EqualTo("Sample Package"));
Assert.That(descriptor.Description, Is.EqualTo("This is the description"));
Assert.That(descriptor.Version, Is.EqualTo("2.x"));
}
}
}

View File

@ -15,6 +15,11 @@ namespace Orchard.CmsPages {
_pageManager = pageManager;
}
public void GetRoutes(ICollection<RouteDescriptor> routes) {
foreach (var routeDescriptor in GetRoutes())
routes.Add(routeDescriptor);
}
public IEnumerable<RouteDescriptor> GetRoutes() {
IRouteConstraint slugConstraint = this;
return new[] {

View File

@ -28,6 +28,11 @@ namespace Orchard.Mvc.Routes {
};
}
public void GetRoutes(ICollection<RouteDescriptor> routes) {
foreach(var routeDescriptor in GetRoutes())
routes.Add(routeDescriptor);
}
//TEMP: this is hardcoded to allow base web app controllers to pass
public class HomeOrAccount : IRouteConstraint {
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {

View File

@ -2,6 +2,11 @@
namespace Orchard.Mvc.Routes {
public interface IRouteProvider : IDependency {
/// <summary>
/// obsolete, prefer other format for extension methods
/// </summary>
IEnumerable<RouteDescriptor> GetRoutes();
void GetRoutes(ICollection<RouteDescriptor> routes);
}
}

View File

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Orchard.Mvc.Routes {
public static class RouteExtensions {
}
}

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
@ -74,5 +75,11 @@ namespace Orchard.Mvc.Routes {
},
};
}
public void GetRoutes(ICollection<RouteDescriptor> routes) {
foreach (var routeDescriptor in GetRoutes())
routes.Add(routeDescriptor);
}
}
}

View File

@ -83,6 +83,10 @@
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Yaml, Version=1.0.3370.39839, Culture=neutral, PublicKeyToken=187a3d240e44a135, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\yaml\Yaml.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Data\HackSessionLocator.cs" />
@ -144,12 +148,17 @@
<Compile Include="Mvc\ModelBinders\KeyedListModelBinder.cs" />
<Compile Include="Mvc\ModelBinders\ModelBinderDescriptor.cs" />
<Compile Include="Mvc\OrchardControllerIdentificationStrategy.cs" />
<Compile Include="Mvc\Routes\RouteExtensions.cs" />
<Compile Include="Mvc\ViewModels\AdminViewModel.cs" />
<Compile Include="Mvc\ViewModels\BaseViewModel.cs" />
<Compile Include="Notify\Notifier.cs" />
<Compile Include="Notify\NotifierExtensions.cs" />
<Compile Include="Notify\NotifyEntry.cs" />
<Compile Include="Notify\NotifyFilter.cs" />
<Compile Include="Packages\PackageEntry.cs" />
<Compile Include="Packages\PackageFolders.cs" />
<Compile Include="Packages\PackageDescriptor.cs" />
<Compile Include="Packages\PackageManager.cs" />
<Compile Include="Security\IAuthorizationService.cs" />
<Compile Include="Security\IMembershipService.cs" />
<Compile Include="Security\IUser.cs" />

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Orchard.Packages {
public class PackageDescriptor {
public string Name { get; set; }
public string DisplayName { get; set; }
public string Description { get; set; }
public string Version { get; set; }
}
}

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Orchard.Packages {
public class PackageEntry {
public PackageDescriptor Descriptor { get; set; }
}
}

View File

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Hosting;
using Yaml.Grammar;
namespace Orchard.Packages {
public interface IPackageFolders : IDependency {
IEnumerable<string> ListNames();
YamlDocument ParseManifest(string name);
}
public class PackageFolders : IPackageFolders {
private readonly IEnumerable<string> _physicalPaths;
public PackageFolders(IEnumerable<string> physicalPaths) {
_physicalPaths = physicalPaths;
}
public IEnumerable<string> ListNames() {
foreach (var path in _physicalPaths) {
foreach (var directoryName in Directory.GetDirectories(path)) {
if (File.Exists(Path.Combine(directoryName, "Package.txt")))
yield return Path.GetFileName(directoryName);
}
}
}
public YamlDocument ParseManifest(string name) {
foreach(var path in _physicalPaths) {
var packageDirectoryPath = Path.Combine(path, name);
var packageManifestPath = Path.Combine(packageDirectoryPath, "Package.txt");
if (!File.Exists(packageManifestPath)) {
continue;
}
var yamlStream = YamlParser.Load(packageManifestPath);
return yamlStream.Documents.Single();
}
return null;
}
}
}

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yaml.Grammar;
namespace Orchard.Packages {
public interface IPackageManager : IDependency {
IEnumerable<PackageDescriptor> AvailablePackages();
IEnumerable<PackageEntry> ActivePackages();
}
public class PackageManager : IPackageManager {
private readonly IPackageFolders _folders;
public PackageManager(IPackageFolders folders) {
_folders = folders;
}
public IEnumerable<PackageDescriptor> AvailablePackages() {
var names = _folders.ListNames();
foreach (var name in names) {
var document = _folders.ParseManifest(name);
var mapping = (Mapping)document.Root;
var fields = mapping.Entities
.Where(x => x.Key is Scalar)
.ToDictionary(x => ((Scalar)x.Key).Text, x => x.Value);
yield return new PackageDescriptor {
Name = name,
DisplayName = GetValue(fields, "name"),
Description = GetValue(fields, "description"),
Version = GetValue(fields, "version")
};
}
}
private static string GetValue(
IDictionary<string, DataItem> fields,
string key) {
DataItem value;
return fields.TryGetValue(key, out value) ? value.ToString() : null;
}
public IEnumerable<PackageEntry> ActivePackages() {
throw new NotImplementedException();
}
}
}