Ejemplo n.º 1
0
        private ApplicationManifest CreateApplicationManifest(string entryPoint, out string configFileName, out string entryPointFilePath)
        {
            entryPointFilePath = null;
            string frameworkVersion;

            if (string.IsNullOrEmpty(TargetFramework))
            {
                frameworkVersion = "3.5";
            }
            else
            {
                FrameworkName fn = new FrameworkName(TargetFramework);
                frameworkVersion = fn.Version.ToString();
            }
            ApplicationManifest manifest = new ApplicationManifest(frameworkVersion);

            manifest.IsClickOnceManifest = true;
            manifest.IconFile            = IconFile;
            configFileName = null;

            Dictionary <string, AssemblyIdentity> addedIdentities = new Dictionary <string, AssemblyIdentity>();
            string basePath = Path.GetFullPath(BasePath);

            foreach (var taskItem in Files.Where(MatchFilter))
            {
                string        filePath   = taskItem.GetMetadata("FullPath");
                string        targetPath = null;
                string        dir        = Path.GetDirectoryName(filePath);
                string        fileName   = Path.GetFileName(filePath);
                BaseReference reference  = null;
                if (!dir.Equals(basePath, StringComparison.InvariantCultureIgnoreCase) &&
                    dir.StartsWith(basePath, StringComparison.InvariantCultureIgnoreCase))
                {
                    int index = basePath.Length;
                    if (dir[index] == Path.DirectorySeparatorChar)
                    {
                        index++;
                    }

                    targetPath = Path.Combine(dir.Substring(index), fileName);
                }


                AssemblyIdentity identity = null;
                try
                {
                    identity = AssemblyIdentity.FromFile(filePath);
                    if (LinkAssembliesWithManifestAsFile && HasEmbeddedManifest(filePath))
                    {
                        identity = null;
                    }
                }
                catch (BadImageFormatException)
                {
                }

                if (identity != null)
                {
                    string identityFullName = identity.GetFullName(AssemblyIdentity.FullNameFlags.All);
                    if (addedIdentities.ContainsKey(identityFullName))
                    {
                        throw new DuplicateAssemblyReferenceException(identityFullName);
                    }
                    else
                    {
                        addedIdentities.Add(identityFullName, identity);
                    }

                    AssemblyReference asmRef = new AssemblyReference(fileName);
                    reference = asmRef;
                    asmRef.AssemblyIdentity = identity;
                    manifest.AssemblyReferences.Add(asmRef);
                    if (manifest.EntryPoint == null &&
                        (string.IsNullOrEmpty(entryPoint) || string.Equals(entryPoint, fileName, StringComparison.InvariantCultureIgnoreCase)) &&
                        Path.GetExtension(fileName).Equals(".exe", StringComparison.InvariantCultureIgnoreCase))
                    {
                        configFileName     = SetEntryPointAndConfig(manifest, filePath, asmRef);
                        entryPointFilePath = filePath;
                    }
                }
                else
                {
                    FileReference fileRef = new FileReference(fileName);
                    reference = fileRef;
                    manifest.FileReferences.Add(fileRef);
                }

                Log.LogMessage(MessageImportance.Low, "TargetPath for {0}: {1}", fileName, targetPath);
                reference.TargetPath = targetPath;
            }

            List <string> searchPaths = new List <string>();

            searchPaths.Add(BasePath);
            if (ConfigFile != null)
            {
                searchPaths.Add(Path.GetDirectoryName(ConfigFile.ItemSpec));
            }
            manifest.ResolveFiles(searchPaths.ToArray());
            manifest.UpdateFileInfo();

            TrustInfo trust = new TrustInfo();

            trust.IsFullTrust  = true;
            manifest.TrustInfo = trust;
            if (manifest.EntryPoint == null)
            {
                Log.LogError("Cannot determine EntryPoint. EntryPoint property = '{0}'", entryPoint ?? string.Empty);
            }

            return(manifest);
        }