2008-06-19 Mike Kestner <mkestner@novell.com>

* libsteticui/AssemblyWidgetLibrary.cs: refactor to use LibraryCache.
	* libsteticui/CecilWidgetLibrary.cs: use singleton Cache. Refine 
	NeedsReload.  Call Load (XmlDocument) conditionally.
	* libsteticui/LibraryCache.cs: hide Load and expose a singleton Cache
	field to share between AssemblyWidgetLibrary and CecilWidgetLibrary.
	Remove Gtk reference checking, we will use explicit ToolboxItem attrs
	instead.  Make HasWidgets a check for an objects.xml file, since we
	will be constructing one for attr-only assemblies.

svn path=/trunk/stetic/; revision=106221
This commit is contained in:
Mike Kestner 2008-06-19 15:19:46 +00:00
Родитель d341500a74
Коммит df1b685097
4 изменённых файлов: 89 добавлений и 91 удалений

Просмотреть файл

@ -1,3 +1,14 @@
2008-06-19 Mike Kestner <mkestner@novell.com>
* libsteticui/AssemblyWidgetLibrary.cs: refactor to use LibraryCache.
* libsteticui/CecilWidgetLibrary.cs: use singleton Cache. Refine
NeedsReload. Call Load (XmlDocument) conditionally.
* libsteticui/LibraryCache.cs: hide Load and expose a singleton Cache
field to share between AssemblyWidgetLibrary and CecilWidgetLibrary.
Remove Gtk reference checking, we will use explicit ToolboxItem attrs
instead. Make HasWidgets a check for an objects.xml file, since we
will be constructing one for attr-only assemblies.
2008-06-18 Mike Kestner <mkestner@novell.com>
* libstetic/WidgetLibrary.cs: refactor class addition logic from Load

Просмотреть файл

@ -8,19 +8,19 @@ namespace Stetic
{
internal class AssemblyWidgetLibrary: WidgetLibrary
{
static LibraryCache Cache = LibraryCache.Cache;
Assembly assembly;
DateTime timestamp;
string name;
bool isWidgetLibrary;
XmlDocument objectsDoc;
ImportContext importContext;
XmlDocument objectsDoc;
LibraryCache.LibraryInfo cache_info;
public AssemblyWidgetLibrary (string name, Assembly assembly)
{
this.name = name;
this.assembly = assembly;
timestamp = System.IO.File.GetLastWriteTime (assembly.Location);
Init ();
UpdateCache ();
}
public AssemblyWidgetLibrary (ImportContext importContext, string assemblyPath)
@ -38,31 +38,29 @@ namespace Stetic
} else
assembly = Assembly.Load (assemblyPath);
if (assembly != null)
timestamp = System.IO.File.GetLastWriteTime (assembly.Location);
else
timestamp = DateTime.MinValue;
if (assembly == null)
throw new InvalidOperationException ("Couldn't load assembly at " + assemblyPath);
Init ();
UpdateCache ();
}
void Init ()
void UpdateCache ()
{
objectsDoc = new XmlDocument ();
System.IO.Stream stream = null;
if (assembly != null)
stream = assembly.GetManifestResourceStream ("objects.xml");
bool is_current = Cache.IsCurrent (assembly.Location);
cache_info = Cache [assembly.Location];
if (is_current)
return;
Stream stream = assembly.GetManifestResourceStream ("objects.xml");
if (stream == null) {
isWidgetLibrary = false;
}
else {
using (stream) {
if (stream != null) {
objectsDoc = new XmlDocument ();
using (stream)
objectsDoc.Load (stream);
}
isWidgetLibrary = true;
}
if (objectsDoc != null)
objectsDoc.Save (cache_info.ObjectsPath);
}
public override string Name {
@ -71,9 +69,7 @@ namespace Stetic
public override bool NeedsReload {
get {
if (!System.IO.File.Exists (assembly.Location))
return false;
return System.IO.File.GetLastWriteTime (assembly.Location) != timestamp;
return File.Exists (assembly.Location) && !Cache.IsCurrent (assembly.Location);
}
}
@ -81,18 +77,13 @@ namespace Stetic
get { return false; }
}
public Assembly Assembly {
get { return assembly; }
}
public DateTime TimeStamp {
get { return timestamp; }
}
public override void Load ()
{
if (objectsDoc == null)
Init ();
if (objectsDoc == null) {
objectsDoc = new XmlDocument ();
using (FileStream stream = File.Open (cache_info.ObjectsPath, FileMode.Open))
objectsDoc.Load (stream);
}
Load (objectsDoc);
objectsDoc = null;
}
@ -128,10 +119,7 @@ namespace Stetic
public override string[] GetLibraryDependencies ()
{
if (objectsDoc == null)
Init ();
if (!isWidgetLibrary)
if (!cache_info.HasWidgets)
return new string [0];
ArrayList list = new ArrayList ();

Просмотреть файл

@ -1,9 +1,36 @@
// CecilWidgetLibrary.cs
//
// Authors:
// Lluis Sanchez Gual <lluis@novell.com>
// Mike Kestner <mkestner@novell.com>
//
// Copyright (c) 2008 Novell, Inc
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection;
using System.Xml;
using Mono.Cecil;
@ -11,7 +38,7 @@ namespace Stetic
{
internal class CecilWidgetLibrary: WidgetLibrary
{
static LibraryCache cache = LibraryCache.Load ();
static LibraryCache cache = LibraryCache.Cache;
AssemblyDefinition assembly;
string name;
@ -42,7 +69,7 @@ namespace Stetic
ScanDependencies ();
}
void ReadCachedDescription (string assemblyPath)
{
if (!cache.IsCurrent (assemblyPath))
@ -97,9 +124,7 @@ namespace Stetic
public override bool NeedsReload {
get {
if (!System.IO.File.Exists (fileName))
return false;
return cache.IsCurrent (fileName);
return File.Exists (fileName) && !cache.IsCurrent (fileName);
}
}
@ -133,6 +158,7 @@ namespace Stetic
MemoryStream ms = new MemoryStream (eres.Data);
objects = new XmlDocument ();
objects.Load (ms);
Load (objects);
}
if (eres.Name == "gui.stetic") {
@ -143,8 +169,6 @@ namespace Stetic
}
}
Load (objects);
canGenerateCode = true;
// If it depends on libraries which can't generate code,
// this one can't

Просмотреть файл

@ -44,7 +44,6 @@ namespace Stetic {
string file;
Guid guid;
bool has_widgets;
DateTime timestamp;
string CacheDirectory {
@ -77,10 +76,8 @@ namespace Stetic {
get { return Path.Combine (CacheDirectory, "steticGui"); }
}
[XmlAttribute]
public bool HasWidgets {
get { return has_widgets; }
set { has_widgets = value; }
get { return System.IO.File.Exists (ObjectsPath); }
}
public string ObjectsPath {
@ -131,6 +128,8 @@ namespace Stetic {
}
}
public static LibraryCache Cache = Load ();
[XmlArray]
[XmlArrayItem (ElementName="LibraryInfo", Type=typeof(LibraryInfo))]
public LibraryInfoCollection Members = new LibraryInfoCollection ();
@ -155,8 +154,6 @@ namespace Stetic {
return info != null && info.Timestamp == File.GetLastWriteTime (file).ToUniversalTime ();
}
AssemblyResolver resolver = new AssemblyResolver ();
EmbeddedResource GetResource (AssemblyDefinition asm, string name)
{
foreach (Resource res in asm.MainModule.Resources) {
@ -167,47 +164,22 @@ namespace Stetic {
return null;
}
bool HasGtkReference (AssemblyDefinition assm, StringCollection visited)
{
visited.Add (assm.Name.Name);
foreach (AssemblyNameReference nameRef in assm.MainModule.AssemblyReferences) {
if (visited.Contains (nameRef.Name))
continue;
else if (nameRef.Name == "gtk-sharp" || HasGtkReference (resolver.Resolve (nameRef), visited))
return true;
}
return false;
}
bool CheckForWidgets (string path)
XmlDocument GetObjectsDoc (string path)
{
XmlDocument result = null;
try {
AssemblyDefinition adef = AssemblyFactory.GetAssembly (path);
if (GetResource (adef, "objects.xml") != null)
return true;
if (adef.Name.Name == "gtk-sharp")
return false; // Gtk is special-cased, so ignore it.
if (!HasGtkReference (adef, new StringCollection ()))
return false;
foreach (TypeDefinition type in adef.MainModule.Types) {
TypeReference tref = type.BaseType;
while (tref != null) {
if (tref.FullName == "Gtk.Window") {
break;
} else if (tref.FullName == "Gtk.Widget") {
return true;
}
tref = resolver.Resolve (tref).BaseType;
}
EmbeddedResource res = GetResource (adef, "objects.xml");
if (res != null) {
MemoryStream stream = new MemoryStream (res.Data);
result = new XmlDocument ();
using (stream)
result.Load (stream);
}
} catch {
result = null;
}
return false;
return result;
}
void RefreshFile (string assembly)
@ -221,8 +193,10 @@ namespace Stetic {
}
info.Timestamp = File.GetLastWriteTime (assembly).ToUniversalTime ();
info.Guid = Guid.NewGuid ();
info.HasWidgets = CheckForWidgets (assembly);
Save ();
XmlDocument objects = GetObjectsDoc (assembly);
if (objects != null)
objects.Save (info.ObjectsPath);
}
void Save ()
@ -249,7 +223,7 @@ namespace Stetic {
serializer.Serialize (fs, this);
}
public static LibraryCache Load ()
static LibraryCache Load ()
{
string index_path = Path.Combine (dir, "index.xml");
if (File.Exists (index_path)) {
@ -266,5 +240,6 @@ namespace Stetic {
return new LibraryCache ();
}
}
}