Skip to content

Clu #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 8, 2015
Merged

Clu #75

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/CLU/CLUCoreCLR.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Azure.Commands.We
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Azure.Commands.Resources.Test", "Microsoft.Azure.Commands.Resources.Test\Microsoft.Azure.Commands.Resources.Test.xproj", "{A9CC2879-D45D-4DCB-A405-6EEDB749B15F}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "clurun", "clurun\clurun.xproj", "{2D9ADC64-E70F-4F6B-A74A-0954DA127179}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.CLU.Common", "Microsoft.CLU.Common\Microsoft.CLU.Common.xproj", "{D96B77D5-C103-423B-BE16-27C3B2F6EC2F}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.CLU.Run", "Microsoft.CLU.Run\Microsoft.CLU.Run.xproj", "{4CABFB3D-3DE0-4DF3-A163-8ED48F0FC0F9}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.CLU", "Microsoft.CLU\Microsoft.CLU.xproj", "{D0A59671-088D-463B-B060-2ADAFFB9C3F6}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Azure.Commands.Compute", "Microsoft.Azure.Commands.Compute\Microsoft.Azure.Commands.Compute.xproj", "{04F9968A-5662-4508-BEE2-31F56848FCBA}"
ProjectSection(ProjectDependencies) = postProject
{99B1290D-A073-4907-8018-51C714431778} = {99B1290D-A073-4907-8018-51C714431778}
Expand Down Expand Up @@ -103,6 +111,22 @@ Global
{A9CC2879-D45D-4DCB-A405-6EEDB749B15F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A9CC2879-D45D-4DCB-A405-6EEDB749B15F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A9CC2879-D45D-4DCB-A405-6EEDB749B15F}.Release|Any CPU.Build.0 = Release|Any CPU
{2D9ADC64-E70F-4F6B-A74A-0954DA127179}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2D9ADC64-E70F-4F6B-A74A-0954DA127179}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D9ADC64-E70F-4F6B-A74A-0954DA127179}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D9ADC64-E70F-4F6B-A74A-0954DA127179}.Release|Any CPU.Build.0 = Release|Any CPU
{D96B77D5-C103-423B-BE16-27C3B2F6EC2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D96B77D5-C103-423B-BE16-27C3B2F6EC2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D96B77D5-C103-423B-BE16-27C3B2F6EC2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D96B77D5-C103-423B-BE16-27C3B2F6EC2F}.Release|Any CPU.Build.0 = Release|Any CPU
{4CABFB3D-3DE0-4DF3-A163-8ED48F0FC0F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4CABFB3D-3DE0-4DF3-A163-8ED48F0FC0F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4CABFB3D-3DE0-4DF3-A163-8ED48F0FC0F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4CABFB3D-3DE0-4DF3-A163-8ED48F0FC0F9}.Release|Any CPU.Build.0 = Release|Any CPU
{D0A59671-088D-463B-B060-2ADAFFB9C3F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0A59671-088D-463B-B060-2ADAFFB9C3F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0A59671-088D-463B-B060-2ADAFFB9C3F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0A59671-088D-463B-B060-2ADAFFB9C3F6}.Release|Any CPU.Build.0 = Release|Any CPU
{04F9968A-5662-4508-BEE2-31F56848FCBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{04F9968A-5662-4508-BEE2-31F56848FCBA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{04F9968A-5662-4508-BEE2-31F56848FCBA}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
2 changes: 0 additions & 2 deletions src/CLU/Microsoft.Azure.Commands.Compute/Tools/azure.bat

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RtPackage: Microsoft.CLU
RtPackage: Microsoft.CLU.Commands
RtEntry: Microsoft.CLU.CommandModel.CmdletCommandModel.Run
RtAssembly: Microsoft.CLU.dll
Modules: Microsoft.Azure.Commands.Management.Storage
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion src/CLU/Microsoft.Azure.Commands.Profile/Content/azure.lx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RtPackage: Microsoft.CLU
RtPackage: Microsoft.CLU.Commands
RtEntry: Microsoft.CLU.CommandModel.CmdletCommandModel.Run
RtAssembly: Microsoft.CLU.dll
Modules: Microsoft.Azure.Commands.Profile
Expand Down
2 changes: 0 additions & 2 deletions src/CLU/Microsoft.Azure.Commands.Profile/Tools/azure.bat

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RtPackage: Microsoft.CLU
RtPackage: Microsoft.CLU.Commands
RtEntry: Microsoft.CLU.CommandModel.CmdletCommandModel.Run
RtAssembly: Microsoft.CLU.dll
Modules: Microsoft.Azure.Commands.Resources
Expand Down
2 changes: 0 additions & 2 deletions src/CLU/Microsoft.Azure.Commands.Resources/Tools/azure.bat

This file was deleted.

2 changes: 1 addition & 1 deletion src/CLU/Microsoft.Azure.Commands.Websites/Content/azure.lx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RtPackage: Microsoft.CLU
RtPackage: Microsoft.CLU.Commands
RtEntry: Microsoft.CLU.CommandModel.CmdletCommandModel.Run
RtAssembly: Microsoft.CLU.dll
Modules: Microsoft.Azure.Commands.Websites
Expand Down
2 changes: 0 additions & 2 deletions src/CLU/Microsoft.Azure.Commands.Websites/Tools/azure.bat

This file was deleted.

278 changes: 278 additions & 0 deletions src/CLU/Microsoft.CLU.Common/AssemblyResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
using Microsoft.CLU.Common.Properties;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace Microsoft.CLU.Common
{
/// <summary>
/// Type used to resolve dependent assemblies.
/// </summary>
public class AssemblyResolver : IDisposable
{
/// <summary>
/// Creates an instance of AssemblyResolver.
/// </summary>
/// <param name="lookupRootDirPaths">The root directories to start assembly lookup</param>
/// <param name="recursiveLookUp">Whether to lookup recursively or not</param>
public AssemblyResolver(IEnumerable<string> lookupRootDirPaths, bool recursiveLookUp)
{
this._lookupRootDirPaths = new List<string>();
this._lookupRootDirPaths.Add(CLUEnvironment.GetRootPath());
this._lookupRootDirPaths.AddRange(lookupRootDirPaths);

this._recursiveLookUp = recursiveLookUp;

#if DESKTOPCLR
_handler = new ResolveEventHandler(ResolveAssemblyHandler);
AppDomain.CurrentDomain.AssemblyResolve += _handler;
#else
this._directoryAssemblyLoadContext = new DirectoryAssemblyLoadContext(this._lookupRootDirPaths.ToArray());
#endif
}

/// <summary>
/// String representing the directories searched.
/// </summary>
public string SearchDirectoriesMessage
{
get
{
if (_lookupRootDirPaths.Count() == 0)
{
return null;
}

var dirsAsString = (_recursiveLookUp ? Strings.AssemblyResolver_SearchDirectoriesMessage_TxtIncludingSubDirs : null) + string.Join(",", _lookupRootDirPaths);
return dirsAsString;
}
}


/// <summary>
/// Resolve and load an assembly. The assembly will be searched only under
/// the given collection of directories.
/// </summary>
/// <param name="searchDirs">The directories to search</param>
/// <param name="assemblyName">Relative path or name of the assembly file, with extension</param>
/// <returns>Returns path to assembly if found else null</returns>
internal string Resolve(string[] searchDirs, string assemblyName)
{
return LookUpFilePath(searchDirs, assemblyName);
}

/// <summary>
/// Load the assembly indicated by filePath.
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
internal Assembly Load(string filePath)
{
if (filePath == null)
{
return null;
}
else
{
#if DESKTOPCLR
return Assembly.LoadFile(filePath);
#else
return DirectoryAssemblyLoadContext.LoadFromPath(filePath);
#endif
}
}

/// <summary>
/// Resolves an entry point given a package assembly.
/// </summary>
/// <param name="assembly">The package assembly, already loaded.</param>
/// <param name="entryName">The fully qualified entry point name.</param>
/// <returns>A reference to the method information, if it exists.</returns>
public MethodInfo ResolveEntryPoint(Assembly assembly, string entryName)
{
if (assembly == null)
throw new ArgumentNullException("assembly");
if (string.IsNullOrEmpty(entryName))
throw new ArgumentNullException("entryName");

var methodNameIdx = entryName.LastIndexOf('.');
if (methodNameIdx == -1)
throw new ArgumentException(Strings.AssemblyResolver_ResolveEntryPoint_MethodNameMustBeFullyQualified);

var methodName = entryName.Substring(methodNameIdx + 1);
var className = entryName.Substring(0, methodNameIdx);

var clss = assembly.GetType(className);
if (clss != null)
{
return clss.GetMethods().Where(m => m.Name == methodName).FirstOrDefault();
}
return null;
}

/// <summary>
/// Resolves an entry point given a set of package assemblies.
/// </summary>
/// <param name="assemblies">The package assemblies, already loaded.</param>
/// <param name="entryName">The fully qualified entry point name.</param>
/// <returns>A reference to the method information, if it exists.</returns>
public MethodInfo ResolveEntryPoint(IEnumerable<Assembly> assemblies, string entryName)
{
return assemblies.Select(asm => ResolveEntryPoint(asm, entryName)).FirstOrDefault();
}

#if DESKTOPCLR
private Assembly ResolveAssemblyHandler(object sender, ResolveEventArgs resolveEventArgs)
{
var path = LookUpFilePath(_lookupRootDirPaths, new AssemblyName(resolveEventArgs.Name).Name + ".dll");
return Load(path);
}
#endif
/// <summary>
/// Search for an assembly under the given set of directories.
/// </summary>
/// <param name="dirPaths">The locations to be searched for the assembly</param>
/// <param name="assemblyName">Relative path or name of the assembly file, with extension</param>
/// <returns>Returns the path to the assembly if found else null</returns>
private string LookUpFilePath(IEnumerable<string> dirPaths, string assemblyName)
{
foreach (var dirPath in dirPaths)
{
string assemblyFilePath = Path.Combine(dirPath, assemblyName);
if (File.Exists(assemblyFilePath))
{
return assemblyFilePath;
}

if (_recursiveLookUp)
{
var subDirPaths = new DirectoryInfo(dirPath).GetDirectories().Select(d => d.FullName);
assemblyFilePath = LookUpFilePath(subDirPaths, assemblyName);
if (assemblyFilePath != null)
{
return assemblyFilePath;
}
}
}

return null;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
#if DESKTOPCLR
if (disposing)
{
if (_handler != null)
{
AppDomain.CurrentDomain.AssemblyResolve -= _handler;
_handler = null;
}
}
#else
this._directoryAssemblyLoadContext = null;
#endif
this._disposed = true;
}
}

~AssemblyResolver()
{
Dispose(false);
}

#region Private fields

/// <summary>
/// The root directories to start lookup.
/// </summary>
private List<string> _lookupRootDirPaths;

/// <summary>
/// Indicate whether to perform recursive lookup or not.
/// </summary>
private bool _recursiveLookUp;

#if DESKTOPCLR
/// <summary>
/// The assembly resolve event handler.
/// </summary>
private ResolveEventHandler _handler;
#else
private DirectoryAssemblyLoadContext _directoryAssemblyLoadContext;
#endif
/// <summary>
/// used by IDisposible::Dispose.
/// </summary>
private bool _disposed = false;

#endregion

#if !DESKTOPCLR
public class DirectoryAssemblyLoadContext : System.Runtime.Loader.AssemblyLoadContext
{
private static Dictionary<string, Assembly> s_loadedAssemblies = new Dictionary<string, Assembly>();
private string[] lookupDirPaths;

public DirectoryAssemblyLoadContext(string[] lookupDirPaths)
{
this.lookupDirPaths = lookupDirPaths;
}

public static Assembly LoadFromPath(string path)
{
var resolver = new DirectoryAssemblyLoadContext(new string[] { System.IO.Path.GetDirectoryName(path) });
var an = new System.Reflection.AssemblyName(System.IO.Path.GetFileNameWithoutExtension(path));
return resolver.LoadFromAssemblyName(an);
}

protected override Assembly Load(AssemblyName assemblyName)
{
//Console.WriteLine($"Trying to load assembly {assemblyName.FullName}");
Assembly loadedAssembly = null;

if (s_loadedAssemblies.TryGetValue(assemblyName.Name, out loadedAssembly))
{
//Console.WriteLine($"Assembly already dynamically loaded - returning instance...");
return loadedAssembly;
}

//Console.WriteLine("Looking up...");
foreach (var lookupDirPath in lookupDirPaths)
{
//Console.WriteLine($"Searching path {lookupDirPath}");
foreach (var file in System.IO.Directory.EnumerateFiles(lookupDirPath, assemblyName.Name + ".dll", System.IO.SearchOption.AllDirectories))
{
//Console.WriteLine($"Loading from file {file}");
loadedAssembly = LoadFromAssemblyPath(file);
}
}

if (loadedAssembly == null)
{
//Console.WriteLine($"Falling back to default loader...");
loadedAssembly = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName);
}

if (loadedAssembly != null)
{
//Console.WriteLine($"Caching loaded assembly...");
s_loadedAssemblies[loadedAssembly.GetName().Name] = loadedAssembly;
}

return loadedAssembly;
}
}
#endif
}
}
Loading