Skip to content

Commit

Permalink
Support Serfs Decoders, rexml example, clean up, improved File suppor…
Browse files Browse the repository at this point in the history
…t, commit prior to refactoring boostrap
  • Loading branch information
rifraf committed May 2, 2010
1 parent da13ec2 commit f2a4bfb
Show file tree
Hide file tree
Showing 39 changed files with 442 additions and 92 deletions.
9 changes: 8 additions & 1 deletion .gitignore
@@ -1,13 +1,20 @@
.svn
prebuild.bat
postbuild.bat
_ReSharper.IREmbeddedApp/*
ExampleApps/obj/
ExampleApps/bin/
IREmbeddedApp/bin/
IREmbeddedApp/obj/
IRTestResources/bin/
IRTestResources/obj/
IREmbeddedLibraries/bin/
IREmbeddedLibraries/obj/
IRSinatra/obj/
IRSinatra/bin/
IREmbeddedApp.4.5.resharper
IREmbeddedApp.4.5.resharper.user
**/Iron*.dll
**/Micro*.dll
**/Micro*.dll
**/*.pdb
**/*.suo
34 changes: 34 additions & 0 deletions ExampleApps/Applications/file_accesses.rb
@@ -0,0 +1,34 @@
# Sample code for reading embedded resource files

# SerfSupp.Debug = true

# File.stat support
stat = File.stat("mydoc.xml")
puts "File time: #{stat.mtime}"
puts "File size: #{stat.size}"

# Read a file in text mode
puts "Text mode:"
f = File.new("mydoc.xml")
p f.readline
f.rewind
p f.readline
count = 1
until f.eof?
count += 1
f.readline
end
puts "#{count} lines read"

# Read a file in binary mode
puts "Binary mode:"
f = File.new("mydoc.xml", 'rb')
p f.readline
f.rewind
p f.readline
count = 1
until f.eof?
count += 1
f.readline
end
puts "#{count} lines read"
26 changes: 26 additions & 0 deletions ExampleApps/Applications/mydoc.xml
@@ -0,0 +1,26 @@
<inventory title="OmniCorp Store #45x10^3">
<section name="health">
<item upc="123456789" stock="12">
<name>Invisibility Cream</name>
<price>14.50</price>
<description>Makes you invisible</description>
</item>
<item upc="445322344" stock="18">
<name>Levitation Salve</name>
<price>23.99</price>
<description>Levitate yourself for up to 3 hours per application</description>
</item>
</section>
<section name="food">
<item upc="485672034" stock="653">
<name>Blork and Freen Instameal</name>
<price>4.95</price>
<description>A tasty meal in a tablet; just add water</description>
</item>
<item upc="132957764" stock="44">
<name>Grob winglets</name>
<price>3.56</price>
<description>Tender winglets of Grob. Just add water</description>
</item>
</section>
</inventory>
33 changes: 33 additions & 0 deletions ExampleApps/Applications/test_rexml.rb
@@ -0,0 +1,33 @@
# Sample code from http://germane-software.com/software/rexml/docs/tutorial.html

require "rexml/document"
include REXML

# Simple string load/write
string = <<EOF
<mydoc>
<someelement attribute="nanoo">Text, text, text</someelement>
</mydoc>
EOF
doc = Document.new(string)
doc.write $stdout

doc = Document.new File.new("mydoc.xml")
doc.elements.each("inventory/section") { |element| puts element.attributes["name"] }
# -> health
# -> food
doc.elements.each("*/section/item") { |element| puts element.attributes["upc"] }
# -> 123456789
# -> 445322344
# -> 485672034
# -> 132957764
root = doc.root
puts root.attributes["title"]
# -> OmniCorp Store #45x10^3
puts root.elements["section/item[@stock='44']"].attributes["upc"]
# -> 132957764
puts root.elements["section"].attributes["name"]
# -> health (returns the first encountered matching element)
puts root.elements[1].attributes["name"]
# -> health (returns the FIRST child element)
root.detect {|node| node.kind_of? Element and node.attributes["name"] == "food" }
17 changes: 17 additions & 0 deletions ExampleApps/ExampleApps.csproj
Expand Up @@ -32,6 +32,14 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\IREmbeddedApp\bin\IREmbeddedApp.dll</HintPath>
</Reference>
<Reference Include="IREmbeddedLibraries, Version=0.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\IREmbeddedLibraries\bin\IREmbeddedLibraries.dll</HintPath>
</Reference>
<Reference Include="IronRuby.Libraries, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files\IronRuby 1.0\bin\IronRuby.Libraries.dll</HintPath>
</Reference>
<Reference Include="IRTestResources, Version=0.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\IRTestResources\bin\IRTestResources.dll</HintPath>
Expand Down Expand Up @@ -60,6 +68,15 @@
<ItemGroup>
<EmbeddedResource Include="Applications\list_args.rb" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Applications\test_rexml.rb" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Applications\mydoc.xml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Applications\file_accesses.rb" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
27 changes: 26 additions & 1 deletion ExampleApps/Program.cs
Expand Up @@ -8,7 +8,7 @@ class Program {
static int Main(string[] args) {
int exitcode = 0;

// First application : bottles
// bottles
Console.WriteLine("bottles.rb");
Console.WriteLine("----------");
try {
Expand All @@ -32,6 +32,31 @@ class Program {
}
Console.WriteLine();

// file access
Console.WriteLine("file_accesses.rb");
Console.WriteLine("----------------");
try {
EmbeddedRuby er1 = new EmbeddedRuby();
er1.Mount("Applications");
exitcode = er1.Run("file_accesses.rb");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
Console.WriteLine();
return 1;
// rexml
Console.WriteLine("test_rexml.rb");
Console.WriteLine("-------------");
try {
EmbeddedRuby er1 = new EmbeddedRuby();
er1.Mount("Applications");
er1.AddAssembly("IREmbeddedLibraries").Mount("Files/site_ruby/1.8").Mount("Files/1.8");
exitcode = er1.Run("test_rexml.rb");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
Console.WriteLine();

// Demo of test::unit
Console.WriteLine("test_unit_app.rb");
Console.WriteLine("----------------");
Expand Down
Binary file removed ExampleApps/bin/ExampleApps.exe
Binary file not shown.
Binary file removed ExampleApps/bin/ExampleApps.pdb
Binary file not shown.
Binary file removed ExampleApps/bin/ExampleApps.vshost.exe
Binary file not shown.
Binary file removed ExampleApps/bin/IREmbeddedApp.dll
Binary file not shown.
Binary file removed ExampleApps/bin/IREmbeddedApp.pdb
Binary file not shown.
Binary file removed ExampleApps/bin/IRTestResources.dll
Binary file not shown.
Binary file removed ExampleApps/bin/IRTestResources.pdb
Binary file not shown.
Binary file removed ExampleApps/bin/Serfs.dll
Binary file not shown.
6 changes: 5 additions & 1 deletion IREmbeddedApp.sln
Expand Up @@ -7,8 +7,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IRTestResources", "IRTestRe
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleApps", "ExampleApps\ExampleApps.csproj", "{F81E1902-C72C-4457-A526-A49AE80FEA1F}"
ProjectSection(ProjectDependencies) = postProject
{9FE6381A-B2E1-4B08-A9CC-F62CD14B719C} = {9FE6381A-B2E1-4B08-A9CC-F62CD14B719C}
{252B7E48-4D69-4AAF-AB8F-BCA432D0BF2E} = {252B7E48-4D69-4AAF-AB8F-BCA432D0BF2E}
{68CBAEDA-18CC-4CC0-8555-942890512EF8} = {68CBAEDA-18CC-4CC0-8555-942890512EF8}
{9FE6381A-B2E1-4B08-A9CC-F62CD14B719C} = {9FE6381A-B2E1-4B08-A9CC-F62CD14B719C}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IRSinatra", "IRSinatra\IRSinatra.csproj", "{499B4084-8BA3-4B33-A146-CA32D078D961}"
Expand All @@ -18,6 +19,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IRSinatra", "IRSinatra\IRSi
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IREmbeddedLibraries", "IREmbeddedLibraries\IREmbeddedLibraries.csproj", "{68CBAEDA-18CC-4CC0-8555-942890512EF8}"
ProjectSection(ProjectDependencies) = postProject
{252B7E48-4D69-4AAF-AB8F-BCA432D0BF2E} = {252B7E48-4D69-4AAF-AB8F-BCA432D0BF2E}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Binary file removed IREmbeddedApp.suo
Binary file not shown.
28 changes: 21 additions & 7 deletions IREmbeddedApp/EmbeddedRuby.cs
Expand Up @@ -11,11 +11,25 @@
namespace IREmbeddedApp {
public class EmbeddedRuby {
private readonly Serfs _serfs;
private ScriptRuntime _runtime;
private ScriptEngine _engine;
private RubyContext _context;

public EmbeddedRuby() {
_serfs = new Serfs(null);
_serfs.IgnoreMissingAssemblies = true;
AddAssembly("IREmbeddedApp", "EmbeddedRuby");
Reset();
}

public void Reset() {
_runtime = Ruby.CreateRuntime();
_engine = _runtime.GetEngine("rb");
_context = (RubyContext)HostingHelpers.GetLanguageContext(_engine);
}

public IStreamDecoder Decoder {
set { _serfs.Decoder = value; }
}

public AssemblyInfo Mount(string topFolder) {
Expand All @@ -34,15 +48,16 @@ public class EmbeddedRuby {
return info;
}

public void SetConstant(string name, object obj) {
_context.ObjectClass.SetConstant(name, obj);
}

public int Run(string app) {
return Run(app, null);
}

public int Run(string app, string[] args) {
ScriptRuntime runtime = Ruby.CreateRuntime();
ScriptEngine engine = runtime.GetEngine("rb");
RubyContext context = (RubyContext)HostingHelpers.GetLanguageContext(engine);
context.ObjectClass.SetConstant("SerfsInstance", _serfs);
SetConstant("SerfsInstance", _serfs);

// Sort out ARGV
string argv;
Expand All @@ -57,13 +72,12 @@ public class EmbeddedRuby {
}
// Prefix bootstrap with $0 and ARGV
string boot = String.Format(
//"$0='S:/{0}'\r\n{1}{2}",
"$0='/{0}' {1}{2}",
app, argv, _serfs.Read("bootstrap.rb")
);
ScriptSource source = engine.CreateScriptSourceFromString(boot, "bootstrap.rb", SourceCodeKind.File);
ScriptSource source = _engine.CreateScriptSourceFromString(boot, "bootstrap.rb", SourceCodeKind.File);
int ex = source.ExecuteProgram();
context.Shutdown();
_context.Shutdown();
return ex;
}
}
Expand Down

0 comments on commit f2a4bfb

Please sign in to comment.