コード例 #1
0
        private Assembly LoadAssemblyByNameOnly(string assemblyName)
        {
            var assemblyPath = Path.Combine(RegisteredInstance.MSBuildPath, assemblyName + ".dll");
            var result       = File.Exists(assemblyPath)
                ? _assemblyLoader.LoadFrom(assemblyPath)
                : null;

            if (result != null)
            {
                _logger.LogDebug($"SUCCESS: Resolved to '{assemblyPath}' (name-only).");
            }

            return(result);
        }
コード例 #2
0
        private ProjectInfo GetProject(CakeScript cakeScript, string filePath)
        {
            var name = Path.GetFileName(filePath);

            if (!File.Exists(cakeScript.Host.AssemblyPath))
            {
                throw new FileNotFoundException($"Cake is not installed. Path {cakeScript.Host.AssemblyPath} does not exist.");
            }
            var hostObjectType = Type.GetType(cakeScript.Host.TypeName, a => _assemblyLoader.LoadFrom(cakeScript.Host.AssemblyPath, dontLockAssemblyOnDisk: true), null, false);

            if (hostObjectType == null)
            {
                throw new InvalidOperationException($"Could not get host object type: {cakeScript.Host.TypeName}.");
            }

            return(ProjectInfo.Create(
                       id: ProjectId.CreateNewId(Guid.NewGuid().ToString()),
                       version: VersionStamp.Create(),
                       name: name,
                       filePath: filePath,
                       assemblyName: $"{name}.dll",
                       language: LanguageNames.CSharp,
                       compilationOptions: cakeScript.Usings == null ? _compilationOptions.Value : _compilationOptions.Value.WithUsings(cakeScript.Usings),
                       parseOptions: new CSharpParseOptions(LanguageVersion.Default, DocumentationMode.Parse, SourceCodeKind.Script),
                       metadataReferences: GetMetadataReferences(cakeScript.References),
                       // TODO: projectReferences?
                       isSubmission: true,
                       hostObjectType: hostObjectType));
        }
コード例 #3
0
 public List <Assembly> ResolveAssemblies(IPackage package, FrameworkName targetFramework)
 {
     return(package.AssemblyReferences
            .Where(val => val.SupportedFrameworks.Contains(targetFramework))
            .Select(val => _assemblyLoader.LoadFrom(_pathResolver.GetPath(package, val)))
            .ToList());
 }
コード例 #4
0
        /// <summary>
        /// Gets the types.
        /// </summary>
        /// <returns>The types.</returns>
        public IEnumerable <Type> GetTypes()
        {
            if (s_types == null)
            {
                s_types = new List <Type>(Assembly.GetExecutingAssembly().GetTypes());
                s_types.AddRange(typeof(UnityEngine.MonoBehaviour).Assembly.GetTypes());
                s_types.AddRange(typeof(UnityEngine.UI.Text).Assembly.GetTypes());
                var externalDlls = m_fs.GetFiles("*.dll");

                foreach (var dll in externalDlls)
                {
                    try
                    {
                        var assembly = m_assemblyLoader.LoadFrom(m_fs.GetFullPath(dll));
                        s_types.AddRange(assembly.GetTypes());
                    }
                    catch (ReflectionTypeLoadException ex)
                    {
                        var loaderMsg = new StringBuilder();

                        foreach (var l in ex.LoaderExceptions)
                        {
                            loaderMsg.AppendFormat("{0}:{1}", l.GetType().Name, l.Message);
                            loaderMsg.AppendLine();
                        }

                        throw new InvalidOperationException(
                                  "Error trying to load assembly '{0}':{1}. LoaderExceptions: {2}".With(dll, ex.Message, loaderMsg),
                                  ex);
                    }
                }
            }

            return(s_types);
        }
コード例 #5
0
 public static Assembly LoadByAssemblyNameOrPath(
     this IAssemblyLoader loader,
     string assemblyName)
 {
     if (File.Exists(assemblyName))
     {
         return(loader.LoadFrom(assemblyName));
     }
     else
     {
         return(loader.Load(assemblyName));
     }
 }
コード例 #6
0
        private Assembly LoadMSBuildAssembly(string assemblyName)
        {
            var assemblyPath = Path.Combine(_registeredInstance.MSBuildPath, assemblyName + ".dll");
            var result       = File.Exists(assemblyPath)
                ? _assemblyLoader.LoadFrom(assemblyPath)
                : null;

            if (result != null)
            {
                _logger.LogDebug($"Resolved '{assemblyName}' to '{assemblyPath}'");
            }

            return(result);
        }
コード例 #7
0
 public void SetAssemblies(IEnumerable <string> assemblyPaths)
 {
     foreach (string assemblyPath in assemblyPaths)
     {
         try
         {
             assemblies.Add(assemblyLoader.LoadFrom(assemblyPath));
         }
         catch (BadImageFormatException)
         {
             RaiseBadFileEvent(assemblyPath);
         }
     }
 }
コード例 #8
0
        private ProjectInfo GetProject(CakeScript cakeScript, string filePath)
        {
            var name = Path.GetFileName(filePath);

            if (!File.Exists(cakeScript.Host.AssemblyPath))
            {
                throw new FileNotFoundException($"Cake is not installed. Path {cakeScript.Host.AssemblyPath} does not exist.");
            }
            var hostObjectType = Type.GetType(cakeScript.Host.TypeName, a => _assemblyLoader.LoadFrom(cakeScript.Host.AssemblyPath, dontLockAssemblyOnDisk: true), null, false);

            if (hostObjectType == null)
            {
                throw new InvalidOperationException($"Could not get host object type: {cakeScript.Host.TypeName}.");
            }

            var projectId = ProjectId.CreateNewId(Guid.NewGuid().ToString());
            var analyzerConfigDocuments = _workspace.EditorConfigEnabled
                ? EditorConfigFinder
                                          .GetEditorConfigPaths(filePath)
                                          .Select(path =>
                                                  DocumentInfo.Create(
                                                      DocumentId.CreateNewId(projectId),
                                                      name: ".editorconfig",
                                                      loader: new FileTextLoader(path, Encoding.UTF8),
                                                      filePath: path))
                                          .ToImmutableArray()
                : ImmutableArray <DocumentInfo> .Empty;

            return(ProjectInfo.Create(
                       id: projectId,
                       version: VersionStamp.Create(),
                       name: name,
                       filePath: filePath,
                       assemblyName: $"{name}.dll",
                       language: LanguageNames.CSharp,
                       compilationOptions: cakeScript.Usings == null ? _compilationOptions.Value : _compilationOptions.Value.WithUsings(cakeScript.Usings),
                       parseOptions: new CSharpParseOptions(LanguageVersion.Latest, DocumentationMode.Parse, SourceCodeKind.Script),
                       metadataReferences: GetMetadataReferences(cakeScript.References),
                       // TODO: projectReferences?
                       isSubmission: true,
                       hostObjectType: hostObjectType)
                   .WithAnalyzerConfigDocuments(analyzerConfigDocuments));
        }
コード例 #9
0
        private ProjectInfo GetProject(CakeScript cakeScript, string filePath)
        {
            var name = Path.GetFileName(filePath);

            var assembly       = _assemblyLoader.LoadFrom(cakeScript.Host.AssemblyPath);
            var hostObjectType = Type.GetType(cakeScript.Host.TypeName, a => assembly, null, true);

            return(ProjectInfo.Create(
                       id: ProjectId.CreateNewId(Guid.NewGuid().ToString()),
                       version: VersionStamp.Create(),
                       name: name,
                       filePath: filePath,
                       assemblyName: $"{name}.dll",
                       language: LanguageNames.CSharp,
                       compilationOptions: cakeScript.Usings == null ? _compilationOptions.Value : _compilationOptions.Value.WithUsings(cakeScript.Usings),
                       parseOptions: new CSharpParseOptions(LanguageVersion.Default, DocumentationMode.Parse, SourceCodeKind.Script),
                       metadataReferences: cakeScript.References.Select(reference => MetadataReference.CreateFromFile(reference, documentation: GetDocumentationProvider(reference))),
                       // TODO: projectReferences?
                       isSubmission: true,
                       hostObjectType: hostObjectType));
        }
コード例 #10
0
        public Assembly Compile(Microsoft.CodeAnalysis.Compilation compilation, string outputPath)
        {
            var pdbPath    = Path.ChangeExtension(outputPath, "pdb");
            var xmlDocPath = Path.ChangeExtension(outputPath, "xml");

            using (MemoryStream dllStream = new MemoryStream(), pdbStream = new MemoryStream(), xmlStream = new MemoryStream())
            {
                using (var win32ResStream = compilation.CreateDefaultWin32Resources(
                           versionResource: true, // Important!
                           noManifest: false,
                           manifestContents: null,
                           iconInIcoFormat: null))
                {
                    var result = compilation.Emit(
                        peStream: dllStream,
                        pdbStream: pdbStream,
                        xmlDocumentationStream: xmlStream,
                        win32Resources: win32ResStream);

                    if (!result.Success)
                    {
                        var failures = result.Diagnostics.Where(diagnostic =>
                                                                diagnostic.IsWarningAsError ||
                                                                diagnostic.Severity == DiagnosticSeverity.Error);

                        foreach (var diagnostic in failures)
                        {
                            Logger.Error(diagnostic);
                        }

                        return(null);
                    }

                    _fileSystem.WriteAllBytes(outputPath, dllStream.ToArray());
                    _fileSystem.WriteAllBytes(pdbPath, pdbStream.ToArray());
                    _fileSystem.WriteAllBytes(xmlDocPath, xmlStream.ToArray());
                    return(_assemblyLoader.LoadFrom(outputPath));
                }
            }
        }