Packaging script source files in IronPython and IronRuby - c#

Does anyone know how to add python and ruby libs as a resource in a dll for deployment? I want to host a script engine in my app, but dont want to have to deploy the entire standard libraries of the respective languages in source files. Is there a simple way to do this so that a require or import statement will find the embedded resources?

You could add custom import hook that looks for embedded resources when an import is executed. This is slightly complex and probably not worth the trouble.
A better technique would be to fetch all of the embedded modules at startup time, execute them with the ScriptEngine and put the modules you have created into the sys.modules dictionary associated with the engine. This automatically makes them available for import by Python code executed by the engine.

You can create StreamContentProviders for example
In the ironrubymvc project under IronRubyMVC/Core/ you will find what you need.
AssemblyStreamContentProvider
Usage of the ContentProvider

IronPython 2.0 has a sample compiler called PYC on Codeplex.com/ironpython which can create DLL's (and applications if you need them too).
IronPython 2.6 has a newer version of PYC under Tools\script.
Cheers,
Davy

Related

Loading an whole Python project when hosting IronPython in C#

I've seen that there is documentation about how to execute or compile a Python script in C# by reading about Hosting approach for IronPython inside a .Net project: http://www.voidspace.org.uk/ironpython/dlr_hosting.shtml
But nothing is explaining clearly how to load an whole Python/IronPython project that contains several folders and files with a lot of import.
Is this something possible, or this approach is not appropriate for large project, and more for short scripts?

How to compile c#/c++ files from Java Application

I am just looking into compilers and I was wondering is it possible to compile both c# and c++ files from a Java Application (e.g. to compile java from a java application you can use the JavaCompiler API). I have looked online for this but all i can find is ways to compile java files from c# and c++ and not the other way around.
If so, what API's can you use for this?
If you know the system commands for compiling and executing .cpp files(don't know much about c#) you might want to check out this. It details how to execute system commands from a Java program. Pass the system commands for compiling the required file in Runtime.getRuntime().exec().
Consider learning how to call ant from Java code and using something like this ant enhancement.
Disclaimer: I don't know anything about this product, but found it by searching for "can ant build c++?"
For C# in Windows: compiler (csc.exe) is part of .Net install on Windows and can be found at well known location (like %windir%\Microsoft.NET\Framework\v3.5 for .Net 3.5). The same place also contains MSBuild.exe that can build project files (*.csproj).
Your code may need to provide locations for referenced libraries if using Csc.exe to compile individual files.

How can I integrate an external assembly as a resource into a .net executable

I have an application that makes use of/consumes services from a number of external .net libraries, which in turn calls certain executables. Would it be possible to integrate these into one huge executable and load them from my application to the disk as required? Resources come to mind but not sure how that is done.
Thanks,
You have several options to do that:
use ILMerge (free)
For howto see here and here
OR
use some tool like SmartAssembly (commercial)
it can embed and merge among other things (no need to change your source code)
OR
code that yourself in less than 10 lines (free but minimal source code change)
mark all needed dependencies as "embedded resource" - this way they are included in the EXE file... you need to setup an AssemblyResolve handler which at runtime reads from Resources and returns the needed DLLs to the .NET runtime...
You can use ILMerge to merge all your assemblies into a single executable file.
You can find a complete example at CodeProject.

Lua .NET How to use the standard and third party libraries

I am using Lua inside C# WinForms application for GUI automation testing. I want to use the logging library http://www.keplerproject.org/lualogging/
But I don't know where to copy those logging library files and other standard lua files so that I can use the standard lua logging within the lua scripts.
I see something like LUA_PATH but still I don't understand how to make the lua packing strcuture without installing it.
Did you tried to use LuaRocks.
Most libraries (including lualogging) are in the repositories, and the install is straightforward : luarocks install lualogging.
About the environment variables, you have to define two : LUA_PATH and LUA_CPATH.
If i'm correct, LUA_PATH is for the Lua libraries (i.e. mylib.lua), and LUA_CPATH is for the compiled binding to a native library (e.g. lsqlite3.so for the Sqlite binding).
Hope this helps.

Linking statically in C#

I'm working on a module for a CMS. This module is distributed as a class library DLL.
I have several utility libraries I'd like to use in this module. Is there anyway I can link these libraries statically so I won't have to distribute several DLL's (thereby distributing my utility libraries separately)?
I would like to have only one DLL.
You can merge your many DLLs with ILMERGE:
http://research.microsoft.com/~mbarnett/ILMerge.aspx
Haven't tried it myself. Hope it helps.
Download here:
http://www.microsoft.com/downloads/details.aspx?familyid=22914587-B4AD-4EAE-87CF-B14AE6A939B0&displaylang=en
Brief Description (from download-page)
ILMerge is a utility for merging multiple .NET assemblies into a single .NET assembly. It works on executables and DLLs alike and comes with several options for controlling the processing and format of the output. See the accompanying documentation for details.
If you don't want to use ILMerge, see this page:
http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx
editor's note: Jeffrey Richter advices to put your dlls into exe file as resources (For each DLL file you add, display its properties and change its “Build Action” to “Embedded Resource.”). Then a custom class loader is needed to make the executable work (At runtime, the CLR won’t be able to find the dependent DLL assemblies, which is a problem. To fix this, when your application initializes, register a callback method with the AppDomain’s ResolveAssembly event).
Be sure to change the resourceName string to point to your actual resources. (e.g. change AssemblyLoadingAndReflection to your project name.)
The short answer for this is no!
You can not link in a dll during compilation.
I don't know if there is some subtle way to do this, but you would probably have to distribute the dlls along with your cms.
The best way to do this is to make some kind of re-distributable.

Categories