Browse Source

Support logical resource names in IronPython compiler.

Change IronPython compiler to embed a file resource as a stream not a byte array into assembly so the resource can be read using GetManifestResourceStream.
pull/2/head
mrward 15 years ago
parent
commit
4d5a72ee46
  1. 10
      src/AddIns/BackendBindings/Python/Python.Build.Tasks/Project/SharpDevelop.Build.Python.targets
  2. 42
      src/AddIns/BackendBindings/Python/Python.Build.Tasks/Project/Src/PythonCompiler.cs
  3. 13
      src/AddIns/BackendBindings/Python/Python.Build.Tasks/Project/Src/PythonCompilerTask.cs
  4. 44
      src/AddIns/BackendBindings/Python/Python.Build.Tasks/Test/LogicalResourceNamesTests.cs
  5. 1
      src/AddIns/BackendBindings/Python/Python.Build.Tasks/Test/Python.Build.Tasks.Tests.csproj

10
src/AddIns/BackendBindings/Python/Python.Build.Tasks/Project/SharpDevelop.Build.Python.targets

@ -1,8 +1,10 @@ @@ -1,8 +1,10 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<UsingTask TaskName="ICSharpCode.Python.Build.Tasks.PythonCompilerTask"
AssemblyFile="$(PythonBinPath)\Python.Build.Tasks.dll"/>
<UsingTask TaskName="CreateCSharpManifestResourceName"
AssemblyName="Microsoft.Build.Tasks.v3.5, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<UsingTask
TaskName="ICSharpCode.Python.Build.Tasks.PythonCompilerTask"
AssemblyFile="$(PythonBinPath)\Python.Build.Tasks.dll"/>
<UsingTask
TaskName="Microsoft.Build.Tasks.CreateCSharpManifestResourceName"
AssemblyName="Microsoft.Build.Tasks.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(PythonBinPath)\SharpDevelop.Build.Python.targets</MSBuildAllProjects>

42
src/AddIns/BackendBindings/Python/Python.Build.Tasks/Project/Src/PythonCompiler.cs

@ -233,28 +233,34 @@ namespace ICSharpCode.Python.Build.Tasks @@ -233,28 +233,34 @@ namespace ICSharpCode.Python.Build.Tasks
void AddResource(ModuleBuilder moduleBuilder, ResourceFile resourceFile)
{
string fileName = resourceFile.FileName;
IResourceWriter resourceWriter = moduleBuilder.DefineResource(Path.GetFileName(fileName), resourceFile.Name, ResourceAttributes.Public);
string extension = Path.GetExtension(fileName).ToLowerInvariant();
if (extension == ".resources") {
ResourceReader resourceReader = new ResourceReader(fileName);
using (resourceReader) {
IDictionaryEnumerator enumerator = resourceReader.GetEnumerator();
while (enumerator.MoveNext()) {
string key = enumerator.Key as string;
Stream resourceStream = enumerator.Value as Stream;
if (resourceStream != null) {
BinaryReader reader = new BinaryReader(resourceStream);
MemoryStream stream = new MemoryStream();
byte[] bytes = reader.ReadBytes((int)resourceStream.Length);
stream.Write(bytes, 0, bytes.Length);
resourceWriter.AddResource(key, stream);
} else {
resourceWriter.AddResource(key, enumerator.Value);
}
string fullFileName = Path.GetFileName(fileName);
IResourceWriter resourceWriter = moduleBuilder.DefineResource(fullFileName, resourceFile.Name, ResourceAttributes.Public);
AddResources(resourceWriter, fileName);
} else {
moduleBuilder.DefineManifestResource(resourceFile.Name, new FileStream(fileName, FileMode.Open), ResourceAttributes.Public);
}
}
void AddResources(IResourceWriter resourceWriter, string fileName)
{
ResourceReader resourceReader = new ResourceReader(fileName);
using (resourceReader) {
IDictionaryEnumerator enumerator = resourceReader.GetEnumerator();
while (enumerator.MoveNext()) {
string key = enumerator.Key as string;
Stream resourceStream = enumerator.Value as Stream;
if (resourceStream != null) {
BinaryReader reader = new BinaryReader(resourceStream);
MemoryStream stream = new MemoryStream();
byte[] bytes = reader.ReadBytes((int)resourceStream.Length);
stream.Write(bytes, 0, bytes.Length);
resourceWriter.AddResource(key, stream);
} else {
resourceWriter.AddResource(key, enumerator.Value);
}
}
} else {
resourceWriter.AddResource(resourceFile.Name, File.ReadAllBytes(fileName));
}
}
}

13
src/AddIns/BackendBindings/Python/Python.Build.Tasks/Project/Src/PythonCompilerTask.cs

@ -266,13 +266,22 @@ namespace ICSharpCode.Python.Build.Tasks @@ -266,13 +266,22 @@ namespace ICSharpCode.Python.Build.Tasks
if (taskItems != null) {
foreach (ITaskItem item in taskItems) {
string resourceFileName = GetFullPath(item.ItemSpec);
string resourceName = Path.GetFileName(resourceFileName);
string resourceName = GetResourceName(item);
ResourceFile resourceFile = new ResourceFile(resourceName, resourceFileName);
files.Add(resourceFile);
}
}
return files;
}
}
string GetResourceName(ITaskItem item)
{
string logicalResourceName = item.GetMetadata("LogicalName");
if (!String.IsNullOrEmpty(logicalResourceName)) {
return logicalResourceName;
}
return Path.GetFileName(item.ItemSpec);
}
/// <summary>
/// Takes a relative path to a file and turns it into the full path using the current folder

44
src/AddIns/BackendBindings/Python/Python.Build.Tasks/Test/LogicalResourceNamesTests.cs

@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using ICSharpCode.Python.Build.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using NUnit.Framework;
namespace Python.Build.Tasks.Tests
{
[TestFixture]
public class LogicalResourceNamesTests
{
MockPythonCompiler mockCompiler;
PythonCompilerTask compilerTask;
void CreatePythonCompilerTask()
{
mockCompiler = new MockPythonCompiler();
compilerTask = new PythonCompilerTask(mockCompiler);
compilerTask.TargetType = "Exe";
compilerTask.OutputAssembly = "test.exe";
}
[Test]
public void Execute_ResourceHasLogicalNameSetInTaskItemMetadata_ResourceNamePassedToCompilerUsesLogicalName()
{
CreatePythonCompilerTask();
TaskItem resourceTaskItem = new TaskItem("test.xaml");
resourceTaskItem.SetMetadata("LogicalName", "MyLogicalResourceName");
compilerTask.Resources = new ITaskItem[] {resourceTaskItem};
compilerTask.Execute();
ResourceFile resourceFile = mockCompiler.ResourceFiles[0];
string resourceName = resourceFile.Name;
string expectedResourceName = "MyLogicalResourceName";
Assert.AreEqual(expectedResourceName, resourceName);
}
}
}

1
src/AddIns/BackendBindings/Python/Python.Build.Tasks/Test/Python.Build.Tasks.Tests.csproj

@ -67,6 +67,7 @@ @@ -67,6 +67,7 @@
<Compile Include="DummyPythonCompilerTask.cs" />
<Compile Include="IncludeDebugInfoTestFixture.cs" />
<Compile Include="IOErrorTestFixture.cs" />
<Compile Include="LogicalResourceNamesTests.cs" />
<Compile Include="MainEntryPointTestFixture.cs" />
<Compile Include="MissingMainEntryPointTestFixture.cs" />
<Compile Include="MockPythonCompiler.cs" />

Loading…
Cancel
Save