Exemple #1
0
        /// <summary>Executes the task.</summary>
        /// <param name="includePatterns">The include patterns.</param>
        /// <param name="excludePatterns">The exclude patterns.</param>
        /// <param name="keyFileName">Name of the key file.</param>
        /// <param name="password">The password.</param>
        /// <param name="force">
        ///     if set to <c>true</c> assemblies will be signed even if they are already signed.
        /// </param>
        public virtual void Execute(
            string[] includePatterns, string[] excludePatterns,
            string keyFileName, string password,
            bool force)
        {
            var keyPair = MsilUtilities.LoadKeyPair(keyFileName, password);

            foreach (var fileName in FindFiles(includePatterns, excludePatterns))
            {
                var assembly = MsilUtilities.LoadAssembly(fileName);

                if (!MsilUtilities.IsManaged(assembly))
                {
                    Log.Warn("Assembly '{0}' is unmanaged, thus cannot be resigned", fileName);
                    continue;
                }

                if (MsilUtilities.IsSigned(assembly))
                {
                    if (force)
                    {
                        Log.Warn("Assembly '{0}' was previously signed, but it going to be resigned with new key", fileName);
                    }
                    else
                    {
                        Log.Debug("Assembly '{0}' is already signed so it does not need resigning", fileName);
                        continue;
                    }
                }

                MsilUtilities.SaveAssembly(assembly, fileName, keyPair);
            }
        }
        private void SignAndFixAssemblies(StrongNameKeyPair keyPair)
        {
            var sorted = TopologicalSort <AssemblyInfo> .Sort(_assemblyInfos, GetReferencedAssemblies).ToArray();

            foreach (var assemblyInfo in sorted)
            {
                var needsRewrite =
                    !MsilUtilities.IsSigned(assemblyInfo.Assembly) ||
                    assemblyInfo.References.Any(r => r.Invalid) ||
                    assemblyInfo.References.Any(r => r.Target.Rewritten);

                if (!needsRewrite)
                {
                    continue;
                }

                if (!assemblyInfo.CanBeSigned)
                {
                    Log.Warn(
                        "Assembly '{0}' or one of its dependencies is unmanaged or thus it cannot be signed.",
                        assemblyInfo.FileName);

                    assemblyInfo.ReferencedBy
                    .Select(r => r.Source).ToList()
                    .ForEach(a => a.CanBeSigned = false);
                }

                MsilUtilities.SaveAssembly(assemblyInfo.Assembly, assemblyInfo.FileName, keyPair);
                assemblyInfo.Assembly = MsilUtilities.LoadAssembly(assemblyInfo.FileName);
                assemblyInfo.ReferencedBy.ForEach(r => r.Invalid = true);
                assemblyInfo.Rewritten = true;

                FixupReferencesTo(assemblyInfo);
            }
        }