Browse Source

ResourceToolkit:

Implemented SD2-1237 - Support accessing linked resource files. Resource files that are accessed using a ResourceManager (except the designer generated ones) are now searched in the project instead of directly on disk.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2320 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Christian Hornung 19 years ago
parent
commit
4198ba3c8a
  1. 129
      src/AddIns/Misc/ResourceToolkit/Project/Src/Resolver/NRefactoryResourceResolver.cs
  2. 18
      src/AddIns/Misc/ResourceToolkit/Project/Src/ResourceFileContent/ResourceFileContentRegistry.cs

129
src/AddIns/Misc/ResourceToolkit/Project/Src/Resolver/NRefactoryResourceResolver.cs

@ -180,46 +180,10 @@ namespace Hornung.ResourceToolkit.Resolver @@ -180,46 +180,10 @@ namespace Hornung.ResourceToolkit.Resolver
if (p != null) {
string fileName = null;
string fileName;
if (resourceName.StartsWith(p.RootNamespace, StringComparison.InvariantCultureIgnoreCase)) {
if (p.Language == "VBNet") {
// SD2-1239
// The VB MSBuild tasks do not use the folder names
// in the manifest resource names.
// We have to look in all folders of the project
// for a file with the specified resource name.
// Need to add a dummy extension so that FindResourceFileName
// does not remove parts of the actual file name when it
// contains a dot.
string fileNameWithDummyExtension = String.Concat(resourceName.Substring(p.RootNamespace.Length+1), ".x");
// Search in the project root folder
// (this folder is not specified explicitly in
// the MSBuild project)
if ((fileName = FindResourceFileName(Path.Combine(p.Directory, fileNameWithDummyExtension))) != null) {
return new ResourceSetReference(resourceName, fileName);
}
// Search in all project folders
foreach (ProjectItem folder in p.GetItemsOfType(ItemType.Folder)) {
if ((fileName = FindResourceFileName(Path.Combine(folder.FileName, fileNameWithDummyExtension))) != null) {
return new ResourceSetReference(resourceName, fileName);
}
}
} else {
// Look for a resource file in the project with the exact name.
if ((fileName = FindResourceFileName(Path.Combine(p.Directory, resourceName.Substring(p.RootNamespace.Length+1).Replace('.', Path.DirectorySeparatorChar)))) != null) {
return new ResourceSetReference(resourceName, fileName);
}
}
if ((fileName = TryGetResourceFileNameFromProjectDirect(resourceName, p)) != null) {
return new ResourceSetReference(resourceName, fileName);
}
// SharpDevelop silently strips the (hard-coded) folder names
@ -348,6 +312,93 @@ namespace Hornung.ResourceToolkit.Resolver @@ -348,6 +312,93 @@ namespace Hornung.ResourceToolkit.Resolver
return new ResourceSetReference(resourceName, null);
}
/// <summary>
/// Tries to find a resource file name for the manifest resources specified by
/// <paramref name="resourceName"/> according to the default MSBuild rules
/// for embedding resources.
/// The reference to these resources occurs within the project <paramref name="p"/>.
/// </summary>
/// <param name="resourceName">The name of the manifest resources to find the resource file for.</param>
/// <param name="p">The project where the reference to these resources occurs.</param>
/// <returns>The full path and name of the resource file that contains the specified resources, or <c>null</c> if no suitable resource file is found.</returns>
static string TryGetResourceFileNameFromProjectDirect(string resourceName, IProject p)
{
if (!resourceName.StartsWith(p.RootNamespace + ".", StringComparison.InvariantCultureIgnoreCase)) {
return null;
}
// Strip root namespace + "." from resourceName
resourceName = resourceName.Substring(p.RootNamespace.Length+1);
switch(p.Language) {
case "VBNet":
// SD2-1239
// The VB MSBuild tasks do not use the folder names
// in the manifest resource names.
// We have to look in all folders of the project
// for a file with the specified resource name.
foreach (ProjectItem item in p.Items) {
FileProjectItem fpi = item as FileProjectItem;
if (fpi == null) continue;
string virtualName = fpi.VirtualName;
if (String.IsNullOrEmpty(virtualName)) continue;
if (Path.GetFileNameWithoutExtension(virtualName).Equals(resourceName, StringComparison.OrdinalIgnoreCase) &&
File.Exists(fpi.FileName) &&
ResourceFileContentRegistry.GetResourceFileContentFactory(fpi.FileName) != null) {
return fpi.FileName;
}
}
break;
case "C#":
default:
// The C# MSBuild tasks use the folder names in the
// manifest resource names.
// For example:
// RootNamespace.Resources.SomeResources
// is normally found in
// <ProjectRootDir>\Resources\SomeResources.{resx|resources}
foreach (ProjectItem item in p.Items) {
FileProjectItem fpi = item as FileProjectItem;
if (fpi == null) continue;
string virtualName = FileUtility.NormalizePath(fpi.VirtualName);
if (String.IsNullOrEmpty(virtualName)) continue;
int lastDotIndex = virtualName.LastIndexOf('.');
if (lastDotIndex == -1) continue;
if (virtualName.Substring(0, lastDotIndex).Replace('\\', '.').Equals(resourceName, StringComparison.OrdinalIgnoreCase) &&
File.Exists(fpi.FileName) &&
ResourceFileContentRegistry.GetResourceFileContentFactory(fpi.FileName) != null) {
return fpi.FileName;
}
}
break;
}
return null;
}
// ********************************************************************************************************************************
/// <summary>

18
src/AddIns/Misc/ResourceToolkit/Project/Src/ResourceFileContent/ResourceFileContentRegistry.cs

@ -63,10 +63,26 @@ namespace Hornung.ResourceToolkit.ResourceFileContent @@ -63,10 +63,26 @@ namespace Hornung.ResourceToolkit.ResourceFileContent
/// <param name="fileName">The name of the file to create a resource content for.</param>
/// <returns>The resource content for the specified file, or <c>null</c>, if the resource file format cannot be handled.</returns>
static IResourceFileContent CreateResourceFileContent(string fileName)
{
IResourceFileContentFactory factory = GetResourceFileContentFactory(fileName);
if (factory == null) {
return null;
} else {
return factory.CreateContentForFile(fileName);
}
}
/// <summary>
/// Gets a <see cref="IResourceFileContentFactory"/> that can create the resource file content
/// for the specified resource file.
/// </summary>
/// <param name="fileName">The resource file to get a <see cref="IResourceFileContentFactory"/> for.</param>
/// <returns>A <see cref="IResourceFileContentFactory"/> that can create the resource file content for the specified file, or <c>null</c> if the specified file is not supported by any registered resource file content factory.</returns>
public static IResourceFileContentFactory GetResourceFileContentFactory(string fileName)
{
foreach (IResourceFileContentFactory factory in Factories) {
if (factory.CanCreateContentForFile(fileName)) {
return factory.CreateContentForFile(fileName);
return factory;
}
}
return null;

Loading…
Cancel
Save