mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 03:25:23 +08:00
Refactored packaging module
- Adding source and updating modules are safer --HG-- branch : dev
This commit is contained in:
@@ -2,19 +2,24 @@
|
||||
using Orchard.Environment;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.Environment.Extensions.Models;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Packaging.Services;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
namespace Orchard.Packaging {
|
||||
[OrchardFeature("Gallery")]
|
||||
public class DefaultPackagingUpdater : IFeatureEventHandler {
|
||||
private readonly IPackagingSourceManager _packagingSourceManager;
|
||||
private readonly INotifier _notifier;
|
||||
|
||||
public DefaultPackagingUpdater(IPackagingSourceManager packagingSourceManager) {
|
||||
public DefaultPackagingUpdater(IPackagingSourceManager packagingSourceManager, INotifier notifier) {
|
||||
_packagingSourceManager = packagingSourceManager;
|
||||
_notifier = notifier;
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public void Install(Feature feature) {
|
||||
// add http://orchardproject.net/gallery/feed as the default Modules Feed
|
||||
_packagingSourceManager.AddSource(new PackagingSource { Id = Guid.NewGuid(), FeedTitle = "Orchard Module Gallery", FeedUrl = "http://orchardproject.net/gallery/feed" });
|
||||
}
|
||||
|
||||
|
@@ -8,32 +8,38 @@ using System.Xml.Linq;
|
||||
using System.Xml.Serialization;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.FileSystems.AppData;
|
||||
using Orchard.Localization;
|
||||
using Orchard.UI.Notify;
|
||||
|
||||
namespace Orchard.Packaging.Services {
|
||||
[OrchardFeature("PackagingServices")]
|
||||
public class PackagingSourceManager : IPackagingSourceManager {
|
||||
private static readonly XmlSerializer _sourceSerializer = new XmlSerializer(typeof (List<PackagingSource>), new XmlRootAttribute("Sources"));
|
||||
private static readonly XmlSerializer _sourceSerializer = new XmlSerializer(typeof(List<PackagingSource>), new XmlRootAttribute("Sources"));
|
||||
private readonly IAppDataFolder _appDataFolder;
|
||||
private readonly INotifier _notifier;
|
||||
|
||||
public PackagingSourceManager(IAppDataFolder appDataFolder) {
|
||||
public PackagingSourceManager(IAppDataFolder appDataFolder, INotifier notifier) {
|
||||
_appDataFolder = appDataFolder;
|
||||
_notifier = notifier;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
Localizer T { get; set; }
|
||||
|
||||
#region IPackagingSourceManager Members
|
||||
|
||||
public IEnumerable<PackagingSource> GetSources() {
|
||||
string text = _appDataFolder.ReadFile(GetSourcesPath());
|
||||
if (string.IsNullOrEmpty(text)) {
|
||||
if ( string.IsNullOrEmpty(text) ) {
|
||||
return Enumerable.Empty<PackagingSource>();
|
||||
}
|
||||
|
||||
var textReader = new StringReader(_appDataFolder.ReadFile(GetSourcesPath()));
|
||||
return (IEnumerable<PackagingSource>) _sourceSerializer.Deserialize(textReader);
|
||||
return (IEnumerable<PackagingSource>)_sourceSerializer.Deserialize(textReader);
|
||||
}
|
||||
|
||||
public void AddSource(PackagingSource source) {
|
||||
UpdateSource(source);
|
||||
SaveSources(GetSources().Concat(new[] {source}));
|
||||
SaveSources(GetSources().Concat(new[] { source }).GroupBy(x => x.FeedUrl).Select(g => g.First()));
|
||||
}
|
||||
|
||||
public void RemoveSource(Guid id) {
|
||||
@@ -41,16 +47,16 @@ namespace Orchard.Packaging.Services {
|
||||
}
|
||||
|
||||
public void UpdateLists() {
|
||||
foreach (PackagingSource source in GetSources()) {
|
||||
foreach ( PackagingSource source in GetSources() ) {
|
||||
UpdateSource(source);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<PackagingEntry> GetModuleList(PackagingSource packagingSource = null) {
|
||||
IEnumerable<PackagingEntry> packageInfos = ( packagingSource == null ? GetSources() : new [] { packagingSource })
|
||||
IEnumerable<PackagingEntry> packageInfos = ( packagingSource == null ? GetSources() : new[] { packagingSource } )
|
||||
.SelectMany(
|
||||
source =>
|
||||
Bind(ParseFeed(_appDataFolder.ReadFile(GetFeedCachePath(source))),
|
||||
Bind(ParseFeed(GetModuleListForSource(source)),
|
||||
feed =>
|
||||
feed.Items.SelectMany(
|
||||
item =>
|
||||
@@ -65,6 +71,13 @@ namespace Orchard.Packaging.Services {
|
||||
return packageInfos.ToArray();
|
||||
}
|
||||
|
||||
private string GetModuleListForSource(PackagingSource source) {
|
||||
if ( !_appDataFolder.FileExists(GetFeedCachePath(source)) ) {
|
||||
UpdateSource(source);
|
||||
}
|
||||
return _appDataFolder.ReadFile(GetFeedCachePath(source));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private static string GetSourcesPath() {
|
||||
@@ -83,8 +96,13 @@ namespace Orchard.Packaging.Services {
|
||||
}
|
||||
|
||||
private void UpdateSource(PackagingSource source) {
|
||||
XDocument feed = XDocument.Load(source.FeedUrl, LoadOptions.PreserveWhitespace);
|
||||
_appDataFolder.CreateFile(GetFeedCachePath(source), feed.ToString(SaveOptions.DisableFormatting));
|
||||
try {
|
||||
XDocument feed = XDocument.Load(source.FeedUrl, LoadOptions.PreserveWhitespace);
|
||||
_appDataFolder.CreateFile(GetFeedCachePath(source), feed.ToString(SaveOptions.DisableFormatting));
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
_notifier.Warning(T("Error loading content of feed '{0}': {1}", source.FeedUrl, e.Message));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -93,7 +111,7 @@ namespace Orchard.Packaging.Services {
|
||||
}
|
||||
|
||||
private static IEnumerable<T> Unit<T>(T t) where T : class {
|
||||
return t != null ? new[] {t} : Enumerable.Empty<T>();
|
||||
return t != null ? new[] { t } : Enumerable.Empty<T>();
|
||||
}
|
||||
|
||||
private static IEnumerable<T2> Bind<T, T2>(T t, Func<T, IEnumerable<T2>> f) where T : class {
|
||||
@@ -101,6 +119,9 @@ namespace Orchard.Packaging.Services {
|
||||
}
|
||||
|
||||
private SyndicationFeed ParseFeed(string content) {
|
||||
if ( string.IsNullOrEmpty(( content )) )
|
||||
return new SyndicationFeed();
|
||||
|
||||
var formatter = new Atom10FeedFormatter<SyndicationFeed>();
|
||||
formatter.ReadFrom(XmlReader.Create(new StringReader(content)));
|
||||
return formatter.Feed;
|
||||
|
Reference in New Issue
Block a user