mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-12-03 03:58:13 +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;
|
||||||
using Orchard.Environment.Extensions;
|
using Orchard.Environment.Extensions;
|
||||||
using Orchard.Environment.Extensions.Models;
|
using Orchard.Environment.Extensions.Models;
|
||||||
|
using Orchard.Localization;
|
||||||
using Orchard.Packaging.Services;
|
using Orchard.Packaging.Services;
|
||||||
|
using Orchard.UI.Notify;
|
||||||
|
|
||||||
namespace Orchard.Packaging {
|
namespace Orchard.Packaging {
|
||||||
[OrchardFeature("Gallery")]
|
[OrchardFeature("Gallery")]
|
||||||
public class DefaultPackagingUpdater : IFeatureEventHandler {
|
public class DefaultPackagingUpdater : IFeatureEventHandler {
|
||||||
private readonly IPackagingSourceManager _packagingSourceManager;
|
private readonly IPackagingSourceManager _packagingSourceManager;
|
||||||
|
private readonly INotifier _notifier;
|
||||||
|
|
||||||
public DefaultPackagingUpdater(IPackagingSourceManager packagingSourceManager) {
|
public DefaultPackagingUpdater(IPackagingSourceManager packagingSourceManager, INotifier notifier) {
|
||||||
_packagingSourceManager = packagingSourceManager;
|
_packagingSourceManager = packagingSourceManager;
|
||||||
|
_notifier = notifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Localizer T { get; set; }
|
||||||
|
|
||||||
public void Install(Feature feature) {
|
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" });
|
_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 System.Xml.Serialization;
|
||||||
using Orchard.Environment.Extensions;
|
using Orchard.Environment.Extensions;
|
||||||
using Orchard.FileSystems.AppData;
|
using Orchard.FileSystems.AppData;
|
||||||
|
using Orchard.Localization;
|
||||||
|
using Orchard.UI.Notify;
|
||||||
|
|
||||||
namespace Orchard.Packaging.Services {
|
namespace Orchard.Packaging.Services {
|
||||||
[OrchardFeature("PackagingServices")]
|
[OrchardFeature("PackagingServices")]
|
||||||
public class PackagingSourceManager : IPackagingSourceManager {
|
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 IAppDataFolder _appDataFolder;
|
||||||
|
private readonly INotifier _notifier;
|
||||||
|
|
||||||
public PackagingSourceManager(IAppDataFolder appDataFolder) {
|
public PackagingSourceManager(IAppDataFolder appDataFolder, INotifier notifier) {
|
||||||
_appDataFolder = appDataFolder;
|
_appDataFolder = appDataFolder;
|
||||||
|
_notifier = notifier;
|
||||||
|
T = NullLocalizer.Instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Localizer T { get; set; }
|
||||||
|
|
||||||
#region IPackagingSourceManager Members
|
#region IPackagingSourceManager Members
|
||||||
|
|
||||||
public IEnumerable<PackagingSource> GetSources() {
|
public IEnumerable<PackagingSource> GetSources() {
|
||||||
string text = _appDataFolder.ReadFile(GetSourcesPath());
|
string text = _appDataFolder.ReadFile(GetSourcesPath());
|
||||||
if (string.IsNullOrEmpty(text)) {
|
if ( string.IsNullOrEmpty(text) ) {
|
||||||
return Enumerable.Empty<PackagingSource>();
|
return Enumerable.Empty<PackagingSource>();
|
||||||
}
|
}
|
||||||
|
|
||||||
var textReader = new StringReader(_appDataFolder.ReadFile(GetSourcesPath()));
|
var textReader = new StringReader(_appDataFolder.ReadFile(GetSourcesPath()));
|
||||||
return (IEnumerable<PackagingSource>) _sourceSerializer.Deserialize(textReader);
|
return (IEnumerable<PackagingSource>)_sourceSerializer.Deserialize(textReader);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddSource(PackagingSource source) {
|
public void AddSource(PackagingSource source) {
|
||||||
UpdateSource(source);
|
SaveSources(GetSources().Concat(new[] { source }).GroupBy(x => x.FeedUrl).Select(g => g.First()));
|
||||||
SaveSources(GetSources().Concat(new[] {source}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveSource(Guid id) {
|
public void RemoveSource(Guid id) {
|
||||||
@@ -41,16 +47,16 @@ namespace Orchard.Packaging.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateLists() {
|
public void UpdateLists() {
|
||||||
foreach (PackagingSource source in GetSources()) {
|
foreach ( PackagingSource source in GetSources() ) {
|
||||||
UpdateSource(source);
|
UpdateSource(source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<PackagingEntry> GetModuleList(PackagingSource packagingSource = null) {
|
public IEnumerable<PackagingEntry> GetModuleList(PackagingSource packagingSource = null) {
|
||||||
IEnumerable<PackagingEntry> packageInfos = ( packagingSource == null ? GetSources() : new [] { packagingSource })
|
IEnumerable<PackagingEntry> packageInfos = ( packagingSource == null ? GetSources() : new[] { packagingSource } )
|
||||||
.SelectMany(
|
.SelectMany(
|
||||||
source =>
|
source =>
|
||||||
Bind(ParseFeed(_appDataFolder.ReadFile(GetFeedCachePath(source))),
|
Bind(ParseFeed(GetModuleListForSource(source)),
|
||||||
feed =>
|
feed =>
|
||||||
feed.Items.SelectMany(
|
feed.Items.SelectMany(
|
||||||
item =>
|
item =>
|
||||||
@@ -65,6 +71,13 @@ namespace Orchard.Packaging.Services {
|
|||||||
return packageInfos.ToArray();
|
return packageInfos.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string GetModuleListForSource(PackagingSource source) {
|
||||||
|
if ( !_appDataFolder.FileExists(GetFeedCachePath(source)) ) {
|
||||||
|
UpdateSource(source);
|
||||||
|
}
|
||||||
|
return _appDataFolder.ReadFile(GetFeedCachePath(source));
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
private static string GetSourcesPath() {
|
private static string GetSourcesPath() {
|
||||||
@@ -83,8 +96,13 @@ namespace Orchard.Packaging.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateSource(PackagingSource source) {
|
private void UpdateSource(PackagingSource source) {
|
||||||
XDocument feed = XDocument.Load(source.FeedUrl, LoadOptions.PreserveWhitespace);
|
try {
|
||||||
_appDataFolder.CreateFile(GetFeedCachePath(source), feed.ToString(SaveOptions.DisableFormatting));
|
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 {
|
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 {
|
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) {
|
private SyndicationFeed ParseFeed(string content) {
|
||||||
|
if ( string.IsNullOrEmpty(( content )) )
|
||||||
|
return new SyndicationFeed();
|
||||||
|
|
||||||
var formatter = new Atom10FeedFormatter<SyndicationFeed>();
|
var formatter = new Atom10FeedFormatter<SyndicationFeed>();
|
||||||
formatter.ReadFrom(XmlReader.Create(new StringReader(content)));
|
formatter.ReadFrom(XmlReader.Create(new StringReader(content)));
|
||||||
return formatter.Feed;
|
return formatter.Feed;
|
||||||
|
|||||||
Reference in New Issue
Block a user