private static string?GetKeyFilePath(WeaverConfig config, AssemblyDefinition asm, ILogger log) { var keyFilePath = config.KeyFilePath; if (!string.IsNullOrEmpty(config.KeyFilePath)) { keyFilePath = Path.GetFullPath(keyFilePath !); log.Debug("Using strong name key from KeyFilePath {KeyFilePath}.", keyFilePath); return(keyFilePath); } var keyFileSuffix = asm .CustomAttributes .FirstOrDefault(x => x.AttributeType.Name == "AssemblyKeyFileAttribute") ?.ConstructorArguments ?.First(); var intermediateDirectory = config.IntermediateDirectory; if (intermediateDirectory != null && keyFileSuffix.HasValue) { keyFilePath = Path.Combine(intermediateDirectory, (string)keyFileSuffix.Value.Value); log.Debug("Using strong name key from [AssemblyKeyFileAttribute({KeyFileSuffix})] {KeyFilePath}.", keyFileSuffix, keyFilePath); return(keyFilePath); } log.Debug("No strong name key was found."); return(null); }
/// <summary> /// Creates a <see cref="WeaverConfig"/> from the MSBuild /// items taken from <c>@(SigourneyConfiguration)</c>. /// </summary> /// <param name="items">The array of items to process. /// They have to be supplied by an MSBuild task parameter /// with a value of <c>@(SigourneyConfiguration)</c>.</param> /// <returns>A <see cref="WeaverConfig"/> or <see langword="null"/> /// if <paramref name="items"/> is invalid.</returns> public static WeaverConfig?TryCreateFromSigourneyConfiguration(ITaskItem[]?items) { if (items == null || items.Length != 1) { return(null); } var item = items[0]; var config = new WeaverConfig(); string?GetMetadata(string key) { var value = item.GetMetadata(key); return(string.IsNullOrEmpty(value) ? null : value); } if (bool.TryParse(item.GetMetadata(nameof(SignAssembly)), out var result)) { config.SignAssembly = result; } var keyOriginatorFile = GetMetadata("KeyOriginatorFile"); var assemblyOriginatorKeyFile = GetMetadata("AssemblyOriginatorKeyFile"); config.KeyFilePath = keyOriginatorFile ?? assemblyOriginatorKeyFile; config.IntermediateDirectory = GetMetadata(nameof(IntermediateDirectory)); var referencesMetadata = GetMetadata(nameof(References)); if (referencesMetadata != null) { var references = from x in referencesMetadata.Split(_pathSeparator, StringSplitOptions.RemoveEmptyEntries) where !string.IsNullOrWhiteSpace(x) select new AssemblyReference(x); config.References.AddRange(references); } return(config); }
/// <summary> /// Creates an <see cref="MSBuildWeaver"/>. /// </summary> public MSBuildWeaver() { // We assume that the Configuration items are // not modified by user code; they are a black box. _configThunk = new Lazy <WeaverConfig?>(() => WeaverConfig.TryCreateFromSigourneyConfiguration(Configuration)); }