Skip to content

Commit

Permalink
refactoring coreclrembedding.cs, updating test.js
Browse files Browse the repository at this point in the history
  • Loading branch information
agracio committed Nov 27, 2024
1 parent b442b54 commit 22d4b57
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 104 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"url": "http://tomasz.janczuk.org",
"twitter": "tjanczuk"
},
"version": "23.1.2",
"version": "23.1.3",
"description": "Edge.js: run .NET and Node.js in-process on Windows, Mac OS, and Linux",
"tags": [
"owin",
Expand Down
144 changes: 63 additions & 81 deletions src/double/Edge.js/dotnetcore/coreclrembedding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,15 @@ private void AddDependencies(DependencyContext dependencyContext, bool standalon
{
DebugMessage("EdgeAssemblyResolver::AddDependencies (CLR) - Added runtime assembly {1} from {0}", CompileAssemblies[runtimeLibrary.Name], runtimeLibrary.Name);
_libraries[runtimeLibrary.Name] = CompileAssemblies[runtimeLibrary.Name];
AddNativeAssemblies(dependencyContext, runtimeLibrary);
AddSupplementaryRuntime(runtimeLibrary);
continue;

}

if (_libraries.ContainsKey(runtimeLibrary.Name) && CompileAssemblies.ContainsKey(runtimeLibrary.Name))
{
DebugMessage("EdgeAssemblyResolver::AddDependencies (CLR) - Already present in the runtime assemblies list, skipping");
AddNativeAssemblies(dependencyContext, runtimeLibrary);
AddSupplementaryRuntime(runtimeLibrary);
continue;
Expand All @@ -278,25 +283,8 @@ private void AddDependencies(DependencyContext dependencyContext, bool standalon
if (assets.Any())
{
string assetPath = assets[0];

string assemblyPath;
if(runtimeLibrary.Type == "project")
assemblyPath = Path.Combine(RuntimeEnvironment.ApplicationDirectory, assetPath);
else if (standalone)
assemblyPath = Path.Combine(RuntimeEnvironment.ApplicationDirectory, Path.GetFileName(assetPath));
else
{
assemblyPath = Path.Combine(_packagesPath, runtimeLibrary.Name.ToLower(), runtimeLibrary.Version, assetPath.Replace('/', Path.DirectorySeparatorChar).ToLower());
if(!File.Exists(assemblyPath))
assemblyPath = Path.Combine(_packagesPath, runtimeLibrary.Name.ToLower(), runtimeLibrary.Version, assetPath.Replace('/', Path.DirectorySeparatorChar));

}
string libraryNameFromPath = Path.GetFileNameWithoutExtension(assemblyPath);

if (!File.Exists(assemblyPath) && !string.IsNullOrEmpty(runtimePath))
{
assemblyPath = Path.Combine(runtimePath, Path.GetFileName(assemblyPath));
}

var assemblyPath = ResolveAssemblyPath(assetPath, runtimeLibrary.Version, runtimeLibrary.Type, standalone);

if (!_libraries.ContainsKey(runtimeLibrary.Name))
{
Expand All @@ -305,12 +293,7 @@ private void AddDependencies(DependencyContext dependencyContext, bool standalon
_libraries[runtimeLibrary.Name] = assemblyPath;
DebugMessage("EdgeAssemblyResolver::AddDependencies (CLR) - Added runtime assembly {1} from {0}", assemblyPath, runtimeLibrary.Name);
CompileAssemblies.TryAdd(runtimeLibrary.Name, assemblyPath);
if (!string.Equals(runtimeLibrary.Name, libraryNameFromPath, StringComparison.CurrentCultureIgnoreCase))
{
_libraries.TryAdd(libraryNameFromPath, assemblyPath);
CompileAssemblies.TryAdd(libraryNameFromPath, assemblyPath);
DebugMessage("EdgeAssemblyResolver::AddDependencies (CLR) - Added supplementary runtime assembly {1} from {0}", assemblyPath, libraryNameFromPath);
}
AddSupplementaryRuntime(runtimeLibrary);
}
else
{
Expand All @@ -332,6 +315,52 @@ private void AddDependencies(DependencyContext dependencyContext, bool standalon
AddNativeAssemblies(dependencyContext, runtimeLibrary);
}
}

private string ResolveAssemblyPath(string libraryName, string libraryVersion, string libraryType, bool standalone)
{
var runtimePath = Path.GetDirectoryName(RuntimeEnvironment.RuntimePath);
var assemblyPath = libraryName.Replace('/', Path.DirectorySeparatorChar);
var normalizedPath = libraryName.Replace('/', Path.DirectorySeparatorChar);

if (libraryType == "project")
{
assemblyPath = Path.Combine(RuntimeEnvironment.ApplicationDirectory, Path.GetFileName(normalizedPath));
}
if (standalone)
{
if (File.Exists(Path.Combine(RuntimeEnvironment.ApplicationDirectory, Path.GetFileName(normalizedPath))))
{
assemblyPath = Path.Combine(RuntimeEnvironment.ApplicationDirectory, Path.GetFileName(normalizedPath));
}
else if (File.Exists(Path.Combine(RuntimeEnvironment.ApplicationDirectory, "refs", Path.GetFileName(normalizedPath))))
{
assemblyPath = Path.Combine(RuntimeEnvironment.ApplicationDirectory, "refs", Path.GetFileName(normalizedPath));
}
else if(!string.IsNullOrEmpty(runtimePath))
{
assemblyPath = Path.Combine(runtimePath, Path.GetFileName(normalizedPath));
}
}
else
{
assemblyPath = Path.Combine(_packagesPath, libraryName.ToLower(), libraryVersion, normalizedPath.ToLower());
if(!File.Exists(assemblyPath))
{
assemblyPath = Path.Combine(_packagesPath, libraryName.ToLower(), libraryVersion, normalizedPath);
}
if(!File.Exists(assemblyPath) && File.Exists(Path.Combine(RuntimeEnvironment.ApplicationDirectory, Path.GetFileName(normalizedPath))))
{
assemblyPath = Path.Combine(RuntimeEnvironment.ApplicationDirectory, Path.GetFileName(normalizedPath));
}
}

if (!File.Exists(assemblyPath) && !string.IsNullOrEmpty(runtimePath))
{
assemblyPath = Path.Combine(runtimePath, Path.GetFileName(assemblyPath));
}

return assemblyPath;
}

private void AddSupplementaryRuntime(RuntimeLibrary runtimeLibrary)
{
Expand Down Expand Up @@ -360,6 +389,10 @@ private void AddNativeAssemblies(DependencyContext dependencyContext, RuntimeLib
{
nativeAssemblyPath = Path.Combine(runtimePath, nativeAssembly.Replace('/', Path.DirectorySeparatorChar));
}
if (!File.Exists(nativeAssemblyPath) && !string.IsNullOrEmpty(runtimePath))
{
nativeAssemblyPath = Path.Combine(runtimePath, Path.GetFileName(nativeAssembly.Replace('/', Path.DirectorySeparatorChar)));
}

if (File.Exists(nativeAssemblyPath))
{
Expand Down Expand Up @@ -409,20 +442,12 @@ private void AddDependencyFromRuntime(RuntimeLibrary runtimeLibrary)
_libraries.TryAdd(runtimeLibrary.Name, assemblyPath);

DebugMessage("EdgeAssemblyResolver::AddDependencies (CLR) - Added dependency {1} from {0}", assemblyPath, runtimeLibrary.Name);

var libraryNameFromPath = Path.GetFileNameWithoutExtension(assemblyPath);
if (!string.Equals(runtimeLibrary.Name, libraryNameFromPath, StringComparison.CurrentCultureIgnoreCase))
{
DebugMessage("EdgeAssemblyResolver::AddDependencies (CLR) - Added supplementary assembly {1} from {0}", assemblyPath, libraryNameFromPath);
CompileAssemblies.TryAdd(libraryNameFromPath, assemblyPath);
_libraries.TryAdd(libraryNameFromPath, assemblyPath);
}
AddSupplementaryRuntime(runtimeLibrary);
}
else
{
DebugMessage("EdgeAssemblyResolver::AddDependencies (CLR) - Could not add dependency {0}", assemblyPath);
}

}

private void AddDependencyFromAppDirectory(RuntimeLibrary runtimeLibrary)
Expand Down Expand Up @@ -454,14 +479,7 @@ private void AddDependencyFromAppDirectory(RuntimeLibrary runtimeLibrary)
_libraries.TryAdd(runtimeLibrary.Name, assemblyPath);

DebugMessage("EdgeAssemblyResolver::AddDependencies (CLR) - Added dependency {1} from {0}", assemblyPath, runtimeLibrary.Name);

var libraryNameFromPath = Path.GetFileNameWithoutExtension(assemblyPath);
if (!string.Equals(runtimeLibrary.Name, libraryNameFromPath, StringComparison.CurrentCultureIgnoreCase))
{
DebugMessage("EdgeAssemblyResolver::AddDependencies (CLR) - Added supplementary assembly {1} from {0}", assemblyPath, libraryNameFromPath);
CompileAssemblies.TryAdd(libraryNameFromPath, assemblyPath);
_libraries.TryAdd(libraryNameFromPath, assemblyPath);
}
AddSupplementaryRuntime(runtimeLibrary);
}
else
{
Expand All @@ -471,57 +489,21 @@ private void AddDependencyFromAppDirectory(RuntimeLibrary runtimeLibrary)

private void AddCompileDependencies(DependencyContext dependencyContext, bool standalone)
{
var runtimePath = Path.GetDirectoryName(RuntimeEnvironment.RuntimePath);
foreach (CompilationLibrary compileLibrary in dependencyContext.CompileLibraries)
{
if (compileLibrary.Assemblies.Count == 0 || CompileAssemblies.ContainsKey(compileLibrary.Name))
{
continue;
}

var assemblyPath = compileLibrary.Assemblies[0].Replace('/', Path.DirectorySeparatorChar);
var normalizedPath = compileLibrary.Assemblies[0].Replace('/', Path.DirectorySeparatorChar);

DebugMessage("EdgeAssemblyResolver::AddDependencies (CLR) - Processing compile assembly {1} {0} {2}", compileLibrary.Name, compileLibrary.Type, compileLibrary.Assemblies[0]);

if (standalone)
{
if (File.Exists(Path.Combine(RuntimeEnvironment.ApplicationDirectory, Path.GetFileName(normalizedPath))))
{
assemblyPath = Path.Combine(RuntimeEnvironment.ApplicationDirectory, Path.GetFileName(normalizedPath));
}
else if (File.Exists(Path.Combine(RuntimeEnvironment.ApplicationDirectory, "refs", Path.GetFileName(normalizedPath))))
{
assemblyPath = Path.Combine(RuntimeEnvironment.ApplicationDirectory, "refs", Path.GetFileName(normalizedPath));
}
else if(!string.IsNullOrEmpty(runtimePath) && File.Exists(Path.Combine(runtimePath, Path.GetFileName(normalizedPath))))
{
assemblyPath = Path.Combine(runtimePath, Path.GetFileName(normalizedPath));
}
}
else
{
assemblyPath = Path.Combine(_packagesPath, compileLibrary.Name.ToLower(), compileLibrary.Version, normalizedPath.ToLower());
if(!File.Exists(assemblyPath))
{
assemblyPath = Path.Combine(_packagesPath, compileLibrary.Name.ToLower(), compileLibrary.Version, normalizedPath);
}
if(!File.Exists(assemblyPath) && File.Exists(Path.Combine(RuntimeEnvironment.ApplicationDirectory, Path.GetFileName(normalizedPath))))
{
assemblyPath = Path.Combine(RuntimeEnvironment.ApplicationDirectory, Path.GetFileName(normalizedPath));
}
}

if (!File.Exists(assemblyPath) && !string.IsNullOrEmpty(runtimePath))
{
assemblyPath = Path.Combine(runtimePath, Path.GetFileName(assemblyPath));
}
DebugMessage("EdgeAssemblyResolver::AddDependencies (CLR) - Processing compile assembly {1} {0} {2}", compileLibrary.Name, compileLibrary.Type, compileLibrary.Assemblies[0]);
var assemblyPath = ResolveAssemblyPath(compileLibrary.Assemblies[0], compileLibrary.Version, compileLibrary.Type, standalone);

if (!CompileAssemblies.ContainsKey(compileLibrary.Name))
{
var libraryNameFromPath = Path.GetFileNameWithoutExtension(assemblyPath);
if (File.Exists(assemblyPath))
{
var libraryNameFromPath = Path.GetFileNameWithoutExtension(assemblyPath);
CompileAssemblies[compileLibrary.Name] = assemblyPath;
DebugMessage("EdgeAssemblyResolver::AddDependencies (CLR) - Added compile assembly {1} from {0}", assemblyPath, compileLibrary.Name);
if (!string.Equals(compileLibrary.Name, libraryNameFromPath, StringComparison.CurrentCultureIgnoreCase))
Expand Down
44 changes: 24 additions & 20 deletions tools/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,32 @@ if(runner === 'all' && process.platform === 'win32'){
delete process.env.EDGE_USE_CORECLR
}

if (!process.env.EDGE_USE_CORECLR) {
if (process.platform !== 'win32') {
buildParameters = buildParameters.concat(['-sdk:4.5']);
}
function build(){
if (!process.env.EDGE_USE_CORECLR) {
if (process.platform !== 'win32') {
buildParameters = buildParameters.concat(['-sdk:4.5']);
}

var compiler = 'C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\csc.exe'
var compiler = 'C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\csc.exe'

run(process.platform === 'win32' ? compiler : 'mcs', buildParameters, runOnSuccess);
}
run(process.platform === 'win32' ? compiler : 'mcs', buildParameters, runOnSuccess);
}

else {
run(process.platform === 'win32' ? 'dotnet.exe' : 'dotnet', ['restore'], function(code, signal) {
if (code === 0) {
run(process.platform === 'win32' ? 'dotnet.exe' : 'dotnet', ['build'], function(code, signal) {
if (code === 0) {
run('cp', ['../test/bin/Debug/net6.0/test.dll', '../test/Edge.Tests.CoreClr.dll'], runOnSuccess);
}
});
}
});
else {
run(process.platform === 'win32' ? 'dotnet.exe' : 'dotnet', ['restore'], function(code, signal) {
if (code === 0) {
run(process.platform === 'win32' ? 'dotnet.exe' : 'dotnet', ['build'], function(code, signal) {
if (code === 0) {
run('cp', ['../test/bin/Debug/net6.0/test.dll', '../test/Edge.Tests.CoreClr.dll'], runOnSuccess);
}
});
}
});
}
}

build();

function run(cmd, args, onClose){

var params = process.env.EDGE_USE_CORECLR ? {cwd: testDir} : {};
Expand Down Expand Up @@ -92,7 +96,7 @@ function runOnSuccess(code, signal) {

if(!runner)
{
spawn('node', [mocha, testDir, '-R', 'spec', '-t', '10000', '-n', 'expose-gc'], {
spawn('node', [mocha, testDir, '-R', 'spec', '-t', '15000', '-n', 'expose-gc'], {
stdio: 'inherit'
}).on('error', function(err) {
console.log(err);
Expand All @@ -113,7 +117,7 @@ function runOnSuccess(code, signal) {
testDir,
'--reporter', 'mocha-multi-reporters',
'--reporter-options', `configFile=./test/${config},cmrOutput=mocha-junit-reporter+mochaFile+${framework}:mochawesome+reportFilename+${framework}`,
'-t', '10000',
'-t', '15000',
'-n', 'expose-gc'
], {
stdio: 'inherit'
Expand All @@ -122,7 +126,7 @@ function runOnSuccess(code, signal) {
{
if(!process.env.EDGE_USE_CORECLR){
process.env.EDGE_USE_CORECLR = 1;
runOnSuccess(0, signal);
build();
}
else{
mergeFiles();
Expand Down

0 comments on commit 22d4b57

Please sign in to comment.