public AssemblyDefinition OptimizeSnapDllForPackageArchive([NotNull] AssemblyDefinition assemblyDefinition, OSPlatform osPlatform) { if (assemblyDefinition == null) { throw new ArgumentNullException(nameof(assemblyDefinition)); } if (!osPlatform.IsSupportedOsVersion()) { throw new PlatformNotSupportedException(); } var cecilReflector = new CecilAssemblyReflector(assemblyDefinition); var cecilResourceReflector = cecilReflector.GetResourceReflector(); cecilResourceReflector.RemoveAllOrThrow(typeof(SnapEmbeddedResourcesTypeRoot).Namespace); cecilReflector.RewriteOrThrow <SnapEmbeddedResources>(x => x.IsOptimized, (typedDefinition, getterName, setterName, propertyDefinition) => { var getIlProcessor = propertyDefinition.GetMethod.Body.GetILProcessor(); getIlProcessor.Body.Instructions.Clear(); getIlProcessor.Emit(OpCodes.Ldc_I4_1); getIlProcessor.Emit(OpCodes.Ret); }); return(assemblyDefinition); }
internal static SnapApp GetSnapApp([NotNull] this AssemblyDefinition assemblyDefinition, [NotNull] ISnapAppReader snapAppReader) { if (assemblyDefinition == null) { throw new ArgumentNullException(nameof(assemblyDefinition)); } if (snapAppReader == null) { throw new ArgumentNullException(nameof(snapAppReader)); } var assemblyReflector = new CecilAssemblyReflector(assemblyDefinition); var snapReleaseDetailsAttribute = assemblyReflector.GetAttribute <SnapAppReleaseDetailsAttribute>(); if (snapReleaseDetailsAttribute == null) { throw new Exception($"Unable to find {nameof(SnapAppReleaseDetailsAttribute)} in assembly {assemblyReflector.FullName}"); } var snapSpecResource = assemblyReflector.MainModule.Resources.SingleOrDefault(x => x.Name == SnapConstants.SnapAppLibraryName); if (!(snapSpecResource is EmbeddedResource snapSpecEmbeddedResource)) { throw new Exception($"Unable to find resource {SnapConstants.SnapAppLibraryName} in assembly {assemblyReflector.FullName}"); } using var resourceStream = snapSpecEmbeddedResource.GetResourceStream(); using var snapAppMemoryStream = new MemoryStream(); resourceStream.CopyTo(snapAppMemoryStream); snapAppMemoryStream.Seek(0, SeekOrigin.Begin); return(snapAppReader.BuildSnapAppFromStream(snapAppMemoryStream)); }
public AssemblyDefinition BuildSnapAppAssembly([NotNull] SnapApp snapApp) { if (snapApp == null) { throw new ArgumentNullException(nameof(snapApp)); } snapApp = new SnapApp(snapApp); foreach (var channel in snapApp.Channels) { if (channel.PushFeed == null) { throw new Exception($"{nameof(channel.PushFeed)} cannot be null. Channel: {channel.Name}. Application id: {snapApp.Id}"); } if (channel.UpdateFeed == null) { throw new Exception($"{nameof(channel.UpdateFeed)} cannot be null. Channel: {channel.Name}. Application id: {snapApp.Id}"); } channel.PushFeed.ApiKey = null; channel.PushFeed.Username = null; channel.PushFeed.Password = null; if (channel.UpdateFeed.Source == null) { throw new Exception( $"Update feed {nameof(channel.UpdateFeed.Source)} cannot be null. Channel: {channel.Name}. Application id: {snapApp.Id}"); } // Prevent publishing nuget.org credentials. if (channel.UpdateFeed is SnapNugetFeed updateFeed && updateFeed.Source.Host.IndexOf("nuget.org", StringComparison.OrdinalIgnoreCase) != -1) { updateFeed.ApiKey = null; updateFeed.Username = null; updateFeed.Password = null; } } var snapAppYamlStr = ToSnapAppYamlString(snapApp); var currentVersion = snapApp.Version; var assembly = AssemblyDefinition.CreateAssembly( new AssemblyNameDefinition(SnapConstants.SnapAppLibraryName, new Version(currentVersion.Major, currentVersion.Minor, currentVersion.Patch)), SnapConstants.SnapAppLibraryName, ModuleKind.Dll); var assemblyReflector = new CecilAssemblyReflector(assembly); var snapAppReleaseDetailsAttributeMethodDefinition = assemblyReflector.MainModule.ImportReference( typeof(SnapAppReleaseDetailsAttribute).GetConstructor(Type.EmptyTypes)); assemblyReflector.AddCustomAttribute(new CustomAttribute(snapAppReleaseDetailsAttributeMethodDefinition)); assemblyReflector.AddResource(new EmbeddedResource(SnapConstants.SnapAppLibraryName, ManifestResourceAttributes.Public, Encoding.UTF8.GetBytes(snapAppYamlStr))); return(assembly); }