Skip to content

Commit

Permalink
Restored support for WPF
Browse files Browse the repository at this point in the history
  • Loading branch information
taras-au committed Jun 30, 2016
1 parent 39da859 commit ec68d82
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 22 deletions.
5 changes: 3 additions & 2 deletions Source/CSSCodeProvider.v4.0/CSSCodeProvider.csproj
Expand Up @@ -9,11 +9,11 @@
<OutputType>library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Scripting</RootNamespace>
<AssemblyName>CSSCodeProvider.v3.5</AssemblyName>
<AssemblyName>CSSCodeProvider.v4.0</AssemblyName>
<FileUpgradeFlags>
</FileUpgradeFlags>
<OldToolsVersion>3.5</OldToolsVersion>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<PublishUrl>publish\</PublishUrl>
Expand All @@ -31,6 +31,7 @@
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
104 changes: 87 additions & 17 deletions Source/CSSCodeProvider.v4.0/xamlcompiler.cs
Expand Up @@ -97,17 +97,20 @@ CompilerResults CompileAssemblyFromFileBatchImpl(CompilerParameters options, str
string outputName = Path.GetFileNameWithoutExtension(options.OutputAssembly);
string tempDir = Path.Combine(Path.GetTempPath(), "CSSCRIPT\\CPP\\" + System.Guid.NewGuid().ToString());
string tempProj = Path.Combine(tempDir, outputName + ".csproj");
string tempSln = Path.Combine(tempDir, outputName + ".sln");
string outputFile = Path.Combine(tempDir, outputName + (options.GenerateExecutable ? ".exe" : ".dll"));

if (Directory.Exists(tempDir))
Directory.Delete(tempDir, true);

Directory.CreateDirectory(tempDir);

using (StreamReader sr = new StreamReader(ProjTemplateFile))
File.WriteAllText(tempSln, GetSolutionTemplateContent().Replace("$PROJECT_FILE$", outputName + ".csproj"));

string content = GetProjTemplateContent();

using (StreamWriter sw = new StreamWriter(tempProj))
{
string content = sr.ReadToEnd();
content = content.Replace("$NAME$", outputName);
content = content.Replace("$DEBUG_TYPE$", options.IncludeDebugInformation ? "<DebugType>full</DebugType>" : "");
content = content.Replace("$OPTIMIZE$", options.IncludeDebugInformation ? "false" : "true");
Expand Down Expand Up @@ -137,7 +140,7 @@ CompilerResults CompileAssemblyFromFileBatchImpl(CompilerParameters options, str
references += string.Format(reference_template, Path.GetFileName(file), file);
content = content.Replace("$REFERENCES$", references);

content = content.Replace("$MIN_CLR_VER$", "<MinFrameworkVersionRequired>3.0</MinFrameworkVersionRequired>");
content = content.Replace("$MIN_CLR_VER$", "<MinFrameworkVersionRequired>4.0</MinFrameworkVersionRequired>");
//content = content.Replace("$IMPORT_PROJECT$", "<Import Project=\"$(MSBuildBinPath)\\Microsoft.WinFX.targets\" />");
content = content.Replace("$IMPORT_PROJECT$", "");
string sources = "";
Expand All @@ -158,14 +161,17 @@ CompilerResults CompileAssemblyFromFileBatchImpl(CompilerParameters options, str

sw.Write(content);
}

string compileLog = "";
//Stopwatch sw1 = new Stopwatch();
//sw1.Start();

string msbuild = Path.Combine(Path.GetDirectoryName("".GetType().Assembly.Location), "MSBuild.exe");

compileLog = RunApp(Path.GetDirectoryName(tempProj), msbuild, "\"" + tempProj + "\" /p:Configuration=\"CSSBuild\" /nologo /verbosity:m").Trim();
string args = string.Format(@"/nologo /verbosity:minimal /t:Clean,Build /p:Configuration=CSSBuild /p:Platform=""Any CPU"" ""{0}""", tempSln);

compileLog = RunApp(Path.GetDirectoryName(tempProj), msbuild, args).Trim();
//compileLog = RunApp(Path.GetDirectoryName(tempProj), msbuild, "\"" + tempProj + "\" /p:Configuration=\"CSSBuild\" /nologo /verbosity:m").Trim();

//sw1.Stop();

Expand Down Expand Up @@ -248,15 +254,79 @@ CompilerResults CompileAssemblyFromFileBatchImpl(CompilerParameters options, str
return retval;
}

static string GetSolutionTemplateContent()
{
return @"Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
MinimumVisualStudioVersion = 10.0.40219.1
Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""onfly.xaml.cs"", ""$PROJECT_FILE$"", ""{31BEEBF9-835A-4A03-BBB6-EFC6A9CB293F}""
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
CSSBuild|Any CPU = CSSBuild|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{31BEEBF9-835A-4A03-BBB6-EFC6A9CB293F}.CSSBuild|Any CPU.ActiveCfg = CSSBuild|Any CPU
{31BEEBF9-835A-4A03-BBB6-EFC6A9CB293F}.CSSBuild|Any CPU.Build.0 = CSSBuild|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal";
}
static string GetProjTemplateContent()
{
var file = ProjTemplateFile;
if (File.Exists(file))
{
using (StreamReader sr = new StreamReader(file))
return sr.ReadToEnd();
}
else
return @"<?xml version=""1.0"" encoding=""utf-8""?>
<Project DefaultTargets=""Build"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" ToolsVersion=""4.0"">
<PropertyGroup>
<Configuration Condition="" '$(Configuration)' == '' "">CSSBuild</Configuration>
<Platform Condition="" '$(Platform)' == '' "">AnyCPU</Platform>
<ProjectGuid>{31BEEBF9-835A-4A03-BBB6-EFC6A9CB293F}</ProjectGuid>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<AssemblyName>$NAME$</AssemblyName>
<WarningLevel>4</WarningLevel>
<OutputType>$TYPE$</OutputType>
$MIN_CLR_VER$
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition="" '$(Configuration)|$(Platform)' == 'CSSBuild|AnyCPU' "">
<DebugSymbols>$DEBUG$</DebugSymbols>
$DEBUG_TYPE$
<Optimize>$OPTIMIZE$</Optimize>
<OutputPath>$OUPTUT_DIR$</OutputPath>
<DefineConstants>$DEBUG_CONST$TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
$REFERENCES$
</ItemGroup>
<ItemGroup>
$SOURCE_FILES$
</ItemGroup>
<Import Project=""$(MSBuildBinPath)\Microsoft.CSharp.targets"" />
$IMPORT_PROJECT$
</Project>";
}

static string ProjTemplateFile
{
get
{
string file;
//return @"C:\cs-script\Dev\WPF\VS\xaml.template";
if (Environment.GetEnvironmentVariable("CSScriptDebugging") != null || Environment.GetEnvironmentVariable("CSScriptDebugging") == null)
return Environment.ExpandEnvironmentVariables(@"%CSSCRIPT_DIR%\Lib\xaml.template");
file = Environment.ExpandEnvironmentVariables(@"%CSSCRIPT_DIR%\Lib\xaml.template");
else
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"xaml.template");
file = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"xaml.template");

return file;
}
}
static string RunApp(string workingDir, string app, string args)
Expand Down Expand Up @@ -295,13 +365,13 @@ static void _Main()

CompilerParameters options = new CompilerParameters(
new string[]
{
@"C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll",
@"C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll",
@"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll",
@"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationCore.dll",
@"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework.dll"
},
{
@"C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll",
@"C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll",
@"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll",
@"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationCore.dll",
@"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework.dll"
},
Path.ChangeExtension(source1, dll ? ".dll" : ".exe"),
false);

Expand All @@ -310,10 +380,10 @@ static void _Main()
options.IncludeDebugInformation = true;

CompilerResults result = new CSCompiler("v3.5").CompileAssemblyFromFileBatch(options, new string[]
{
{
//source3,
source2, source1
});
source2, source1
});
}
}
}
2 changes: 2 additions & 0 deletions Source/CSScriptLibrary/CSScriptLib.Eval.Roslyn.cs
Expand Up @@ -622,6 +622,8 @@ public IEvaluator ReferenceAssembly(Assembly assembly)
"which are not loaded from the file location. You may want to use CS-Script MonoEvaluator (Mono.CSharp)");

if (!CompilerSettings.MetadataReferences.Cast<PortableExecutableReference>().Any(r => Utils.IsSamePath(r.FilePath, assembly.Location)))
//Future assembly aliases support:
//MetadataReference.CreateFromFile("asm.dll", new MetadataReferenceProperties().WithAliases(new[] { "lib_a", "external_lib_a" } })
CompilerSettings = CompilerSettings.AddReferences(assembly);

return this;
Expand Down
29 changes: 29 additions & 0 deletions Source/Tests/MonoEval.cs
Expand Up @@ -2,6 +2,7 @@
using CSScriptLibrary;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Tests;
using Xunit;

Expand Down Expand Up @@ -161,6 +162,34 @@ public int Run()
Assert.Equal(200, result);
}

[Fact]
public void LoadCode3()
{
dynamic script = CSScript.MonoEvaluator
.LoadCode(@"
using System;
using System.Runtime.InteropServices;
public class Script
{
public TestStruct Run()
{
return new TestStruct();
}
}
[StructLayout(LayoutKind.Explicit, Size = 64)]
public struct TestStruct
{
[FieldOffset(0)]
public int Index;
}");

var result = script.Run();

Assert.NotNull(result);
}

[Fact]
public void ProcessCodeDirectives()
{
Expand Down
2 changes: 2 additions & 0 deletions Source/csparser.cs
Expand Up @@ -1081,6 +1081,8 @@ int IndexOfDelimiter(int startIndex, int endIndex, params char[] delimiters)
if (code[i] == delimiter)
{
lastDelimiter = delimiter;
if (i == endIndex)
return i;
break;
}
}
Expand Down
5 changes: 2 additions & 3 deletions Source/csscript.cs
Expand Up @@ -797,8 +797,7 @@ void ExecuteImpl()

assemblyFileName = Compile(options.scriptFileName);


Debug.WriteLine("Asm is compiled");
//Debug.WriteLine("Asm is compiled");

if (Profiler.Stopwatch.IsRunning)
{
Expand Down Expand Up @@ -1638,7 +1637,7 @@ void ProcessCompilingResult(CompilerResults results, CompilerParameters compiler
{
if (options.verbose)
{
Console.WriteLine(" Compiler Oputput: ", options);
Console.WriteLine(" Compiler Output: ", options);
foreach (CompilerError err in results.Errors)
Console.WriteLine(" {0}({1},{2}):{3} {4} {5}", err.FileName, err.Line, err.Column, (err.IsWarning ? "warning" : "error"), err.ErrorNumber, err.ErrorText);
Console.WriteLine("> ----------------", options);
Expand Down

0 comments on commit ec68d82

Please sign in to comment.