Example #1
0
        bool Execute(DirectoryAssemblyResolver resolver)
        {
            LogDebugMessage("ResolveAssemblies Task");
            LogDebugMessage("  ReferenceAssembliesDirectory: {0}", ReferenceAssembliesDirectory);
            LogDebugMessage("  I18nAssemblies: {0}", I18nAssemblies);
            LogDebugMessage("  LinkMode: {0}", LinkMode);
            LogDebugTaskItems("  Assemblies:", Assemblies);
            LogDebugMessage("  ProjectAssetFile: {0}", ProjectAssetFile);
            LogDebugMessage("  NuGetPackageRoot: {0}", NuGetPackageRoot);
            LogDebugMessage("  TargetMoniker: {0}", TargetMoniker);

            foreach (var dir in ReferenceAssembliesDirectory.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
            {
                resolver.SearchDirectories.Add(dir);
            }

            var assemblies = new HashSet <string> ();

            var topAssemblyReferences = new List <AssemblyDefinition> ();
            var logger = new NuGetLogger((s) => {
                LogDebugMessage("{0}", s);
            });

            LockFile lockFile = null;

            if (!string.IsNullOrEmpty(ProjectAssetFile) && File.Exists(ProjectAssetFile))
            {
                lockFile = LockFileUtilities.GetLockFile(ProjectAssetFile, logger);
            }

            try {
                foreach (var assembly in Assemblies)
                {
                    var assembly_path = Path.GetDirectoryName(assembly.ItemSpec);

                    if (!resolver.SearchDirectories.Contains(assembly_path))
                    {
                        resolver.SearchDirectories.Add(assembly_path);
                    }

                    // Add each user assembly and all referenced assemblies (recursive)
                    var assemblyDef = resolver.Load(assembly.ItemSpec);
                    if (assemblyDef == null)
                    {
                        throw new InvalidOperationException("Failed to load assembly " + assembly.ItemSpec);
                    }
                    if (MonoAndroidHelper.IsReferenceAssembly(assemblyDef))
                    {
                        // Resolve "runtime" library
                        if (lockFile != null)
                        {
                            assemblyDef = ResolveRuntimeAssemblyForReferenceAssembly(lockFile, resolver, assemblyDef.Name);
                        }
                        if (lockFile == null || assemblyDef == null)
                        {
                            LogWarning($"Ignoring {assembly_path} as it is a Reference Assembly");
                            continue;
                        }
                    }
                    topAssemblyReferences.Add(assemblyDef);
                    assemblies.Add(Path.GetFullPath(assemblyDef.MainModule.FullyQualifiedName));
                }
            } catch (Exception ex) {
                LogError("Exception while loading assemblies: {0}", ex);
                return(false);
            }
            try {
                foreach (var assembly in topAssemblyReferences)
                {
                    AddAssemblyReferences(resolver, assemblies, assembly, true);
                }
            } catch (Exception ex) {
                LogError("Exception while loading assemblies: {0}", ex);
                return(false);
            }

            // Add I18N assemblies if needed
            AddI18nAssemblies(resolver, assemblies);

            ResolvedAssemblies = assemblies.Select(a => new TaskItem(a)).ToArray();
            ResolvedSymbols    = assemblies.Select(a => a + ".mdb").Where(a => File.Exists(a)).Select(a => new TaskItem(a)).ToArray();
            ResolvedSymbols    = ResolvedSymbols.Concat(
                assemblies.Select(a => Path.ChangeExtension(a, "pdb"))
                .Where(a => File.Exists(a) && Files.IsPortablePdb(a))
                .Select(a => new TaskItem(a)))
                                 .ToArray();
            ResolvedFrameworkAssemblies    = ResolvedAssemblies.Where(p => MonoAndroidHelper.IsFrameworkAssembly(p.ItemSpec, true)).ToArray();
            ResolvedUserAssemblies         = ResolvedAssemblies.Where(p => !MonoAndroidHelper.IsFrameworkAssembly(p.ItemSpec, true)).ToArray();
            ResolvedDoNotPackageAttributes = do_not_package_atts.ToArray();

            LogDebugTaskItems("  [Output] ResolvedAssemblies:", ResolvedAssemblies);
            LogDebugTaskItems("  [Output] ResolvedUserAssemblies:", ResolvedUserAssemblies);
            LogDebugTaskItems("  [Output] ResolvedFrameworkAssemblies:", ResolvedFrameworkAssemblies);
            LogDebugTaskItems("  [Output] ResolvedDoNotPackageAttributes:", ResolvedDoNotPackageAttributes);

            return(!Log.HasLoggedErrors);
        }
        bool Execute(DirectoryAssemblyResolver resolver)
        {
            Log.LogDebugMessage("ResolveAssemblies Task");
            Log.LogDebugMessage("  ReferenceAssembliesDirectory: {0}", ReferenceAssembliesDirectory);
            Log.LogDebugMessage("  I18nAssemblies: {0}", I18nAssemblies);
            Log.LogDebugMessage("  LinkMode: {0}", LinkMode);
            Log.LogDebugTaskItems("  Assemblies:", Assemblies);

            foreach (var dir in ReferenceAssembliesDirectory.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
            {
                resolver.SearchDirectories.Add(dir);
            }

            var assemblies = new HashSet <string> ();

            var topAssemblyReferences = new List <AssemblyDefinition> ();

            try {
                foreach (var assembly in Assemblies)
                {
                    var assembly_path = Path.GetDirectoryName(assembly.ItemSpec);

                    if (!resolver.SearchDirectories.Contains(assembly_path))
                    {
                        resolver.SearchDirectories.Add(assembly_path);
                    }

                    // Add each user assembly and all referenced assemblies (recursive)
                    var assemblyDef = resolver.Load(assembly.ItemSpec);
                    if (assemblyDef == null)
                    {
                        throw new InvalidOperationException("Failed to load assembly " + assembly.ItemSpec);
                    }
                    topAssemblyReferences.Add(assemblyDef);
                    assemblies.Add(Path.GetFullPath(assemblyDef.MainModule.FullyQualifiedName));
                }
            } catch (Exception ex) {
                Log.LogError("Exception while loading assemblies: {0}", ex);
                return(false);
            }
            try {
                foreach (var assembly in topAssemblyReferences)
                {
                    AddAssemblyReferences(resolver, assemblies, assembly, true);
                }
            } catch (Exception ex) {
                Log.LogError("Exception while loading assemblies: {0}", ex);
                return(false);
            }

            // Add I18N assemblies if needed
            AddI18nAssemblies(resolver, assemblies);

            ResolvedAssemblies = assemblies.Select(a => new TaskItem(a)).ToArray();
            ResolvedSymbols    = assemblies.Select(a => a + ".mdb").Where(a => File.Exists(a)).Select(a => new TaskItem(a)).ToArray();
            ResolvedSymbols    = ResolvedSymbols.Concat(
                assemblies.Select(a => Path.ChangeExtension(a, "pdb"))
                .Where(a => File.Exists(a) && Files.IsPortablePdb(a))
                .Select(a => new TaskItem(a)))
                                 .ToArray();
            ResolvedFrameworkAssemblies    = ResolvedAssemblies.Where(p => MonoAndroidHelper.IsFrameworkAssembly(p.ItemSpec, true)).ToArray();
            ResolvedUserAssemblies         = ResolvedAssemblies.Where(p => !MonoAndroidHelper.IsFrameworkAssembly(p.ItemSpec, true)).ToArray();
            ResolvedDoNotPackageAttributes = do_not_package_atts.ToArray();

            Log.LogDebugTaskItems("  [Output] ResolvedAssemblies:", ResolvedAssemblies);
            Log.LogDebugTaskItems("  [Output] ResolvedUserAssemblies:", ResolvedUserAssemblies);
            Log.LogDebugTaskItems("  [Output] ResolvedFrameworkAssemblies:", ResolvedFrameworkAssemblies);
            Log.LogDebugTaskItems("  [Output] ResolvedDoNotPackageAttributes:", ResolvedDoNotPackageAttributes);

            return(!Log.HasLoggedErrors);
        }