Skip to content

Commit

Permalink
assembly resolvation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-b-novikov committed Dec 6, 2017
1 parent 056a373 commit aa776e4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
61 changes: 41 additions & 20 deletions Reinforced.Typings.Cli/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public static class Bootstrapper
{
private static ExporterConsoleParameters _parameters;
private static readonly Dictionary<string, string> _referencesCache = new Dictionary<string, string>();
private static string _lastAssemblyLocalDir;
private static readonly HashSet<string> _allAssembliesDirs = new HashSet<string>();
private static readonly Dictionary<string, Assembly> _alreadyLoaded = new Dictionary<string, Assembly>();
private static int _totalLoadedAssemblies;
private static TextReader _profileReader;
private static string _profilePath;
Expand Down Expand Up @@ -76,7 +77,7 @@ public static void Main(string[] args)
{
if (!File.Exists(args[1]))
{
Console.WriteLine("Cannot find profile {0}, exiting",args[1]);
Console.WriteLine("Cannot find profile {0}, exiting", args[1]);
return;
}
_parameters = ExtractParametersFromFile(args[1]);
Expand Down Expand Up @@ -133,7 +134,7 @@ private static void ReleaseReferencesTempFile()
{
if (_profileReader != null) _profileReader.Dispose();
if (!string.IsNullOrEmpty(_profilePath)) File.Delete(_profilePath);
if(_parameters==null) return;
if (_parameters == null) return;
if (!string.IsNullOrEmpty(_parameters.ReferencesTmpFilePath)) File.Delete(_parameters.ReferencesTmpFilePath);
}

Expand Down Expand Up @@ -218,11 +219,12 @@ private static string LookupAssemblyPathInternal(string assemblyNameOrFullPath,
Console.WriteLine("Looking up for assembly {0}", assemblyNameOrFullPath);
#endif

if (Path.IsPathRooted(assemblyNameOrFullPath))
if (Path.IsPathRooted(assemblyNameOrFullPath) && File.Exists(assemblyNameOrFullPath))
{
if (storeIfFullName)
{
_lastAssemblyLocalDir = Path.GetDirectoryName(assemblyNameOrFullPath) + "\\";
var lastAssemblyLocalDir = Path.GetDirectoryName(assemblyNameOrFullPath) + "\\";
if (!_allAssembliesDirs.Contains(lastAssemblyLocalDir)) _allAssembliesDirs.Add(lastAssemblyLocalDir);
}
#if DEBUG
Console.WriteLine("Already have full path to assembly {0}", assemblyNameOrFullPath);
Expand All @@ -238,15 +240,20 @@ private static string LookupAssemblyPathInternal(string assemblyNameOrFullPath,
#endif
return rf;
}
var p = Path.Combine(_lastAssemblyLocalDir, assemblyNameOrFullPath);
if (File.Exists(p))

foreach (var dir in _allAssembliesDirs)
{
var p = Path.Combine(dir, assemblyNameOrFullPath);
if (File.Exists(p))
{
#if DEBUG
Console.WriteLine("Assembly {0} found at {1}", assemblyNameOrFullPath, p);
Console.WriteLine("Assembly {0} found at {1}", assemblyNameOrFullPath, p);
#endif
return p;
return p;
}
}


return null;
}

Expand All @@ -270,13 +277,7 @@ public static string LookupAssemblyPath(string assemblyNameOrFullPath, bool stor
checkResult = LookupAssemblyPathInternal(p, storeIfFullName);
if (!string.IsNullOrEmpty(checkResult)) return checkResult;

if (!string.IsNullOrEmpty(_lastAssemblyLocalDir) && !string.IsNullOrEmpty(assemblyNameOrFullPath))
{
p = Path.Combine(_lastAssemblyLocalDir, assemblyNameOrFullPath);
checkResult = LookupAssemblyPathInternal(p, storeIfFullName);
if (!string.IsNullOrEmpty(checkResult)) return checkResult;
}
BuildWarn("Assembly {0} may be resolved incorrectly", assemblyNameOrFullPath, p);

return assemblyNameOrFullPath;
}

Expand All @@ -295,6 +296,10 @@ public static Assembly[] GetAssembliesFromArgs()
{
var assemblyPath = _parameters.SourceAssemblies[i];
var path = LookupAssemblyPath(assemblyPath);
if (path == assemblyPath)
{
BuildWarn("Assembly {0} may be resolved incorrectly", assemblyPath);
}
#if NETCORE1
var a = AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
#else
Expand All @@ -312,26 +317,42 @@ public static Assembly[] GetAssembliesFromArgs()
#if NETCORE1
private static Assembly CurrentDomainOnAssemblyResolve(AssemblyLoadContext context, AssemblyName assemblyName)
{
AssemblyLoadContext.Default.Resolving -= CurrentDomainOnAssemblyResolve;
//AssemblyLoadContext.Default.Resolving -= CurrentDomainOnAssemblyResolve;
if (assemblyName.Name.StartsWith("Reinforced.Typings.XmlSerializers")) return Assembly.GetEntryAssembly();
if (_alreadyLoaded.ContainsKey(assemblyName.FullName)) return _alreadyLoaded[assemblyName.FullName];
AssemblyName nm = new AssemblyName(assemblyName.Name);
string path = LookupAssemblyPath(nm.Name, false);
var a = context.LoadFromAssemblyPath(path);
Assembly a = null;
if (path != nm.Name) //else - lookup failed, return null
{
a = context.LoadFromAssemblyPath(path);
}
else
{
BuildWarn("Assembly {0} may be resolved incorrectly", assemblyName.FullName);
}

if (a != null) _alreadyLoaded[nm.FullName] = a;
_totalLoadedAssemblies++;
#if DEBUG
Console.WriteLine("{0} additionally resolved", nm);
#endif

AssemblyLoadContext.Default.Resolving += CurrentDomainOnAssemblyResolve;
//AssemblyLoadContext.Default.Resolving += CurrentDomainOnAssemblyResolve;
return a;
}
#else
public static Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name.StartsWith("Reinforced.Typings.XmlSerializers")) return Assembly.GetExecutingAssembly();
if (_alreadyLoaded.ContainsKey(args.Name)) return _alreadyLoaded[args.Name];
AssemblyName nm = new AssemblyName(args.Name);
string path = LookupAssemblyPath(nm.Name, false);
Assembly a = Assembly.LoadFrom(path);
Assembly a = null;
if (path != nm.Name) a = Assembly.LoadFrom(path);
else BuildWarn("Assembly {0} may be resolved incorrectly", nm.Name);

if (a != null) _alreadyLoaded[args.Name] = a;
_totalLoadedAssemblies++;
#if DEBUG
Console.WriteLine("{0} additionally resolved", nm);
Expand Down
2 changes: 1 addition & 1 deletion Reinforced.Typings.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Reinforced.Typings.Cli.NETCore": {
"commandName": "Project",
"commandLineArgs": "profile \"C:\\Users\\Murcielago\\AppData\\Local\\Temp\\tmpD16F.tmp\""
"commandLineArgs": "profile \"C:\\Users\\Murcielago\\AppData\\Local\\Temp\\tmp8C46.tmp\""
}
}
}
4 changes: 2 additions & 2 deletions package/Reinforced.Typings.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>Reinforced.Typings</id>
<version>1.4.1</version>
<version>1.4.2</version>
<title>Reinforced.Typings</title>
<authors>Pavel B. Novikov</authors>
<owners>Pavel B. Novikov</owners>
Expand All @@ -16,7 +16,7 @@
<tags>mvc, web, typescript</tags>
<licenseUrl>https://github.com/reinforced/Reinforced.Typings/blob/master/LICENSE.md</licenseUrl>
<releaseNotes>
- Fix conflict between attributes and fluent configuration
- Additional assemblies resolvation fix
</releaseNotes>
<dependencies>
<group targetFramework=".NETFramework4.5" />
Expand Down
4 changes: 2 additions & 2 deletions version.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AssemblyVersion>1.4.0.0</AssemblyVersion>
<FileVersion>1.4.0.0</FileVersion>
<AssemblyVersion>1.4.2.0</AssemblyVersion>
<FileVersion>1.4.2.0</FileVersion>
</PropertyGroup>
</Project>

0 comments on commit aa776e4

Please sign in to comment.