private static string GetCachedPath(string tool, FourPartVersion minimumToolVersion,Architecture architecture) { if( architecture == Architecture.Auto ) { return (from arch in Architecture.Architectures let p = GetCachedPath(tool, minimumToolVersion, arch) where p != null select p) .FirstOrDefault(); } var view = RegistryView.CoAppUser[@"Tools#{0}/{1}/{2}".format(tool, minimumToolVersion,architecture)]; var result = view.StringValue; // if we've remembered that we've not found something... if ("NOT-FOUND".Equals(result, StringComparison.CurrentCultureIgnoreCase)) { return result; } if (null != result && !File.Exists(result)) { view.StringValue = null; result = null; } return result; }
private static string GetCachedPath(string tool, FourPartVersion minimumToolVersion, Architecture architecture) { if (architecture == Architecture.Auto) { return((from arch in Architecture.Architectures let p = GetCachedPath(tool, minimumToolVersion, arch) where p != null select p) .FirstOrDefault()); } var view = RegistryView.CoAppUser[@"Tools#{0}/{1}/{2}".format(tool, minimumToolVersion, architecture)]; var result = view.StringValue; // if we've remembered that we've not found something... if ("NOT-FOUND".Equals(result, StringComparison.CurrentCultureIgnoreCase)) { return(result); } if (null != result && !File.Exists(result)) { view.StringValue = null; result = null; } return(result); }
public void AddDependency(string name, FourPartVersion version, Architecture arch, string publicKeyToken, string language = "*", AssemblyType assemblyType = AssemblyType.win32, BindingRedirect bindingRedirect = null) { if (!(from dat in DependentAssemblyElements let assemblyIdentity = new AssemblyIdentity(dat) where assemblyIdentity.Name == name && assemblyIdentity.Version == version && assemblyIdentity.Architecture == arch && assemblyIdentity.PublicKeyToken == publicKeyToken && ((language == "*" && string.IsNullOrEmpty(assemblyIdentity.Language)) || assemblyIdentity.Language == language) select assemblyIdentity).Any()) { // add another. var dat = new XElement(DependencyTag, new XElement(DependentAssemblyTag)); var identity = new AssemblyIdentity(dat.Elements().FirstOrDefault()) { AssemblyType = assemblyType, Name = name, Version = version, Architecture = arch, PublicKeyToken = publicKeyToken, Language = language, BindingRedirect = bindingRedirect }; _parentElement.Add(dat); } }
private static void ProcessServicesProvider(IServicesProvider provider, YieldServicesProvider yieldServicesProvider, FourPartVersion asmVersion, IRequest request) { try { provider.InitializeProvider(DynamicInterface.Instance, request); FourPartVersion ver = provider.GetProviderVersion(); yieldServicesProvider(provider.GetServicesProviderName(), provider, ver == 0 ? asmVersion : ver, request); } catch (Exception e) { e.Dump(); } }
internal bool RegisterProvidersViaMetaProvider(IMetaProvider provider, FourPartVersion asmVersion, IHostApi request) { var found = false; var metaProviderName = provider.GetMetaProviderName(); FourPartVersion metaProviderVersion = provider.GetProviderVersion(); if (!_metaProviders.ContainsKey(metaProviderName)) { // Meta Providers can't be replaced at this point _metaProviders.AddOrSet(metaProviderName, provider); } try { provider.InitializeProvider(request.As <IRequest>()); var metaProvider = provider; provider.GetProviderNames().ParallelForEach(name => { // foreach (var name in provider.GetProviderNames()) { var instance = metaProvider.CreateProvider(name); if (instance != null) { // check if it's a Package Provider if (typeof(IPackageProvider).CanDynamicCastFrom(instance)) { try { found = found | RegisterPackageProvider(instance.As <IPackageProvider>(), asmVersion, request); } catch (Exception e) { e.Dump(); } } // check if it's a Services Provider if (typeof(IArchiver).CanDynamicCastFrom(instance)) { try { found = found | RegisterArchiver(instance.As <IArchiver>(), asmVersion, request); } catch (Exception e) { e.Dump(); } } if (typeof(IDownloader).CanDynamicCastFrom(instance)) { try { found = found | RegisterDownloader(instance.As <IDownloader>(), asmVersion, request); } catch (Exception e) { e.Dump(); } } } }); } catch (Exception e) { e.Dump(); } return(found); }
private bool LoadViaMetaProvider(IMetaProvider metaProvider, string name, FourPartVersion asmVersion, IHostApi request) { var found = false; var instance = metaProvider.CreateProvider(name); if (instance != null) { // check if it's a Package Provider if (typeof(IPackageProvider).CanDynamicCastFrom(instance)) { try { var packageProvider = RegisterPackageProvider(instance.As <IPackageProvider>(), asmVersion, request); if (packageProvider != null) { found = true; packageProvider.ProviderPath = metaProvider.GetProviderPath(name); } } catch (Exception e) { e.Dump(); } } // check if it's a Services Provider if (typeof(IArchiver).CanDynamicCastFrom(instance)) { try { var archiver = RegisterArchiver(instance.As <IArchiver>(), asmVersion, request); if (archiver != null) { found = true; archiver.ProviderPath = metaProvider.GetProviderPath(name); } } catch (Exception e) { e.Dump(); } } if (typeof(IDownloader).CanDynamicCastFrom(instance)) { try { var downloader = RegisterDownloader(instance.As <IDownloader>(), asmVersion, request); if (downloader != null) { found = true; downloader.ProviderPath = metaProvider.GetProviderPath(name); } } catch (Exception e) { e.Dump(); } } } return(found); }
/// <summary> /// This initializes the provider registry with the list of package providers. /// (currently a hardcoded list, soon, registry driven) /// </summary> /// <param name="request"></param> internal void LoadProviders(IRequest request) { var providerAssemblies = (_initialized ? Enumerable.Empty <string>() : _defaultProviders) .Concat(GetProvidersFromRegistry(Registry.LocalMachine, "SOFTWARE\\MICROSOFT\\ONEGET")) .Concat(GetProvidersFromRegistry(Registry.CurrentUser, "SOFTWARE\\MICROSOFT\\ONEGET")) .Concat(AutoloadedAssemblyLocations.SelectMany(location => { if (Directory.Exists(location)) { return(Directory.EnumerateFiles(location).Where(each => !IsExcluded(each) && (each.EndsWith(".exe", StringComparison.CurrentCultureIgnoreCase) || each.EndsWith(".dll", StringComparison.CurrentCultureIgnoreCase)))); } return(Enumerable.Empty <string>()); })); providerAssemblies = providerAssemblies.OrderByDescending(each => { try { // try to get a version from the file first return((ulong)(FourPartVersion)FileVersionInfo.GetVersionInfo(each)); } catch { // otherwise we can't make a distinction. return((ulong)0); } }); providerAssemblies = providerAssemblies.Distinct(new PathEqualityComparer(PathCompareOption.FileWithoutExtension)); // hack to make sure we don't load the old version of the nuget provider // when we have the ability to examine a plugin without dragging it into the // primary appdomain, this won't be needed. FourPartVersion minimumnugetversion = "2.8.3.6"; providerAssemblies = providerAssemblies.Where(assemblyFile => { try { if ("nuget-anycpu".EqualsIgnoreCase(Path.GetFileNameWithoutExtension(assemblyFile)) && ((FourPartVersion)FileVersionInfo.GetVersionInfo(assemblyFile)) < minimumnugetversion) { return(false); } } catch { } return(true); }); // there is no trouble with loading providers concurrently. providerAssemblies.ParallelForEach(providerAssemblyName => { try { request.Debug(request.FormatMessageString("Trying provider assembly: {0}", providerAssemblyName)); if (TryToLoadProviderAssembly(providerAssemblyName, request)) { request.Debug(request.FormatMessageString("SUCCESS provider assembly: {0}", providerAssemblyName)); } } catch { request.Error(Constants.Messages.ProviderPluginLoadFailure, ErrorCategory.InvalidOperation.ToString(), providerAssemblyName, request.FormatMessageString(Constants.Messages.ProviderPluginLoadFailure, providerAssemblyName)); } }); }
private Downloader RegisterDownloader(IDownloader provider, FourPartVersion asmVersion, IHostApi request) { string name = null; try { FourPartVersion ver = provider.GetProviderVersion(); var version = ver == 0 ? asmVersion : ver; name = provider.GetDownloaderName(); if (string.IsNullOrWhiteSpace(name)) { return(null); } // Initialize the provider before locking the collection // that way we're not blocking others on non-deterministic actions. request.Debug("Initializing provider '{0}'".format(name)); provider.InitializeProvider(request.As <IRequest>()); request.Debug("Provider '{0}' Initialized".format(name)); lock (Downloaders) { if (Downloaders.ContainsKey(name)) { if (version > Downloaders[name].Version) { // remove the old provider first. // todo: this won't remove the plugin domain and unload the code yet // we'll have to do that later. Downloaders.Remove(name); } else { return(null); } } request.Debug("Using Downloader Provider {0}".format(name)); var downloader = new Downloader(provider) { Version = version }; downloader.Initialize(request); Downloaders.AddOrSet(name, downloader); return(downloader); } } catch (Exception e) { request.Debug("Provider '{0}' Failed".format(name)); e.Dump(); } return(null); }
public void ReadXml(System.Xml.XmlReader reader) { reader.MoveToContent(); Minimum = reader.GetAttribute("Minimum"); Maximum = reader.GetAttribute("Maximum"); var isEmptyElement = reader.IsEmptyElement; reader.ReadStartElement(); if (!isEmptyElement) { reader.ReadEndElement(); } }
internal static bool AcquireProvidersViaMetaProvider(IMetaProvider provider, YieldMetaProvider yieldMetaProvider, YieldPackageProvider yieldPackageProvider, YieldArchiver yieldArchiver, YieldDownloader yieldDownloader, FourPartVersion asmVersion, IRequest request) { var found = false; var metaProviderName = provider.GetMetaProviderName(); FourPartVersion metaProviderVersion = provider.GetProviderVersion(); var reloading = yieldMetaProvider(metaProviderName, provider, (metaProviderVersion == 0 ? asmVersion : metaProviderVersion), request); try { provider.InitializeProvider(request); var metaProvider = provider; Parallel.ForEach(provider.GetProviderNames(), name => { // foreach (var name in provider.GetProviderNames()) { var instance = metaProvider.CreateProvider(name); if (instance != null) { // check if it's a Package Provider if (DynamicInterface.Instance.IsInstanceCompatible <IPackageProvider>(instance)) { try { found = found | ProcessPackageProvider(DynamicInterface.Instance.Create <IPackageProvider>(instance), yieldPackageProvider, asmVersion, request); } catch (Exception e) { e.Dump(); } } // check if it's a Services Provider if (DynamicInterface.Instance.IsInstanceCompatible <IArchiver>(instance)) { try { found = found | ProcessArchiver(DynamicInterface.Instance.Create <IArchiver>(instance), yieldArchiver, asmVersion, request); } catch (Exception e) { e.Dump(); } } if (DynamicInterface.Instance.IsInstanceCompatible <IDownloader>(instance)) { try { found = found | ProcessDownloader(DynamicInterface.Instance.Create <IDownloader>(instance), yieldDownloader, asmVersion, request); } catch (Exception e) { e.Dump(); } } } }); } catch (Exception e) { e.Dump(); } return(found); }
private static bool ProcessArchiver(IArchiver provider, YieldArchiver yieldArchiver, FourPartVersion asmVersion, IRequest request) { try { FourPartVersion ver = provider.GetProviderVersion(); if (yieldArchiver(provider.GetArchiverName(), provider, ver == 0 ? asmVersion : ver, request)) { // provider.InitializeProvider(request); return(true); } } catch (Exception e) { e.Dump(); } return(false); }
private static bool ProcessDownloader(IDownloader provider, YieldDownloader yieldDownloader, FourPartVersion asmVersion, IRequest request) { try { // provider.InitializeProvider(DynamicInterface.Instance, request); FourPartVersion ver = provider.GetProviderVersion(); if (yieldDownloader(provider.GetDownloaderName(), provider, ver == 0 ? asmVersion : ver, request)) { // provider.InitializeProvider(request); return(true); } } catch (Exception e) { e.Dump(); } return(false); }
public void RemoveDependency(string name, FourPartVersion version, Architecture arch, string publicKeyToken, string language = "*") { var deleteThis = (from dat in DependentAssemblyElements let assemblyIdentity = new AssemblyIdentity(dat) where assemblyIdentity.Name == name && assemblyIdentity.Version == version && assemblyIdentity.Architecture == arch && assemblyIdentity.PublicKeyToken == publicKeyToken && ((language == "*" && string.IsNullOrEmpty(assemblyIdentity.Language)) || assemblyIdentity.Language == language) select dat).FirstOrDefault(); if (deleteThis != null) { deleteThis.Parent.Remove(); } }
private static FourPartVersion GetAssemblyVersion(Assembly asm) { FourPartVersion result = 0; var attribute = asm.GetCustomAttributes(typeof(AssemblyVersionAttribute), true).FirstOrDefault() as AssemblyVersionAttribute; if (attribute != null) { result = attribute.Version; } if (result == 0) { // what? No assembly version? // fallback to the file version of the assembly var assemblyLocation = asm.Location; if (assemblyLocation.Is() && File.Exists(assemblyLocation)) { result = FileVersionInfo.GetVersionInfo(assemblyLocation); if (result == 0) { // no file version either? // use the date I guess. try { result = new FileInfo(assemblyLocation).LastWriteTime; } catch { } } } if (result == 0) { // still no version? // I give up. call it 0.0.0.1 result = "0.0.0.1"; } } return(result); }
private bool RegisterDownloader(IDownloader provider, FourPartVersion asmVersion, IHostApi request) { try { FourPartVersion ver = provider.GetProviderVersion(); var version = ver == 0 ? asmVersion : ver; var name = provider.GetDownloaderName(); lock (Downloaders) { if (Downloaders.ContainsKey(name)) { if (version > Downloaders[name].Version) { // remove the old provider first. // todo: this won't remove the plugin domain and unload the code yet // we'll have to do that later. Downloaders.Remove(name); } else { return(false); } } request.Debug("Loading Downloader {0}".format(name, provider.GetDownloaderName())); provider.InitializeProvider(request.As <IRequest>()); Downloaders.AddOrSet(name, new Downloader(provider) { Version = version }).Initialize(request); } return(true); } catch (Exception e) { e.Dump(); } return(false); }
internal bool RegisterProvidersViaMetaProvider(IMetaProvider provider, FourPartVersion asmVersion, IHostApi request) { var found = false; var metaProviderName = provider.GetMetaProviderName(); lock (_metaProviders) { if (!_metaProviders.ContainsKey(metaProviderName)) { // Meta Providers can't be replaced at this point _metaProviders.AddOrSet(metaProviderName, provider); } } try { provider.InitializeProvider(request.As <IRequest>()); provider.GetProviderNames().ParallelForEach(name => { found = LoadViaMetaProvider(provider, name, asmVersion, request); }); } catch (Exception e) { e.Dump(); } return(found); }
private int Startup(IEnumerable<string> args) { var options = args.Where(each => each.StartsWith("--")).Switches(); var parameters = args.Parameters(); foreach (var arg in options.Keys) { var argumentParameters = options[arg]; switch (arg) { /* global switches */ case "load-config": // all ready done, but don't get too picky. break; case "nologo": this.Assembly().SetLogo(string.Empty); break; case "help": return Help(); case "certificate-path": var cert = argumentParameters.Last(); _signingCertPath = _signingCertPath.IndexOf(":") > 1 ? cert : Path.GetFullPath(argumentParameters.Last()); break; case "password": _signingCertPassword = argumentParameters.Last(); break; case "remember": _remember = true; break; case "auto": break; case "sign": _sign = true; break; case "just-sign": _sign = true; _justsign = true; break; case "strong-name": _strongname = true; break; case "verbose": _verbose = true; break; case "company": _company = argumentParameters.Last(); break; case "description": _description = argumentParameters.Last(); break; case "internal-name": _internalName = argumentParameters.Last(); break; case "copyright": _copyright = argumentParameters.Last(); break; case "original-filename": _originalFilename = argumentParameters.Last(); break; case "product-name": _productName = argumentParameters.Last(); break; case "verify": _verify = true; break; case "reference-assembly": foreach (var asmRef in argumentParameters) { if (string.IsNullOrEmpty(asmRef)) { return Fail("Missing assembly information for --assembly-reference."); } var parts = asmRef.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var assemblyref = new AssemblyReference {Name = parts[0]}; foreach (var part in parts.Skip(1)) { var kp = part.Split("= ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (kp.Length != 2) { return Fail("Invalid option '{0}' in assembly reference '{1}'.", part, asmRef); } switch (kp[0].ToLower()) { case "version": case "ver": assemblyref.Version = kp[1]; if (assemblyref.Version == 0L) { return Fail("Invalid Version '{0}' in assembly reference '{1}'.", kp[1], asmRef); } break; case "publickeytoken": case "pkt": case "token": if (kp[1].Length != 16) { return Fail("Invalid publicKeyToken '{0}' in assembly reference '{1}'.", kp[1], asmRef); } assemblyref.PublicKeyToken = kp[1]; break; case "processorarchitecture": case "architecture": case "arch": assemblyref.Architecture = kp[1]; if (assemblyref.Architecture == Architecture.Auto || assemblyref.Architecture == Architecture.Unknown) { return Fail("Invalid processorArchitecture '{0}' in assembly reference '{1}'.", kp[1], asmRef); } break; } } if (assemblyref.Version == 0 || assemblyref.Architecture == Architecture.Unknown || string.IsNullOrEmpty(assemblyref.PublicKeyToken)) { return Fail("Invalid assembly reference '{0}' ", asmRef); } assemblyReferences.Add(assemblyref); } break; case "product-version": _productVersion = argumentParameters.Last(); if (_productVersion == 0L) { return Fail("--product-version must be in the form ##.##.##.##"); } break; case "file-version": _fileVersion = argumentParameters.Last(); if (_fileVersion == 0L) { return Fail("--file-version must be in the form ##.##.##.##"); } break; case "execution-level": switch (argumentParameters.Last()) { case "administrator": case "admin": case "requires-admin": case "requiresadmin": case "requiresadministrator": case "requires-administrator": _executionLevel = ExecutionLevel.requireAdministrator; break; case "invoker": case "asinvoker": case "as-invoker": _executionLevel = ExecutionLevel.asInvoker; break; case "highest-available": case "highest": case "highestavailable": _executionLevel = ExecutionLevel.highestAvailable; break; } break; case "dpi-aware": if (argumentParameters.Last().IsTrue()) { _dpiAware = true; } if (argumentParameters.Last().IsFalse()) { _dpiAware = false; } break; default: return Fail("Unknown parameter [--{0}]", arg); } } Logo(); if (_verify) { // return Verify(parameters); } if (string.IsNullOrEmpty(_signingCertPath)) { _certificate = CertificateReference.Default; if (_certificate == null) { return Fail("No default certificate stored in the registry"); } } else if (string.IsNullOrEmpty(_signingCertPassword)) { _certificate = new CertificateReference(_signingCertPath); } else { _certificate = new CertificateReference(_signingCertPath, _signingCertPassword); } using (new ConsoleColors(ConsoleColor.White, ConsoleColor.Black)) { Verbose("Loaded certificate with private key {0}", _certificate.Location); } if (_remember) { Verbose("Storing certificate details in the registry."); _certificate.RememberPassword(); CertificateReference.Default = _certificate; } if (parameters.Count() < 1) { return Fail("Missing files to sign/name. \r\n\r\n Use --help for command line help."); } var tasks = new List<Task>(); if (_company != null && _company.Equals("auto", StringComparison.CurrentCultureIgnoreCase)) { _company = _certificate.CommonName; } var failures = 0; try { var allFiles = parameters.FindFilesSmarter().ToArray(); var origMD5 = new XDictionary<string, string>(); var loading = allFiles.Select(each => Binary.Load(each, BinaryLoadOptions.PEInfo | BinaryLoadOptions.VersionInfo | BinaryLoadOptions.Managed | BinaryLoadOptions.Resources | BinaryLoadOptions.Manifest | BinaryLoadOptions.UnsignedManagedDependencies | BinaryLoadOptions.MD5).ContinueWith(antecedent => { lock (allFiles) { if (antecedent.IsFaulted) { Console.WriteLine("Failed to load file '{0}'", each); var e = antecedent.Exception.Flatten().InnerExceptions.First(); Console.WriteLine("{0}--{1}", e.Message, e.StackTrace); return; } try { var binary = antecedent.Result; origMD5.Add(each, binary.MD5); if (binary.IsPEFile && !_justsign) { // do PE file stuff if (_sign) { binary.SigningCertificate = _certificate; } if (binary.IsManaged && _strongname) { binary.StrongNameKeyCertificate = _certificate; } if (!assemblyReferences.IsNullOrEmpty()) { foreach (var asmRef in assemblyReferences) { binary.Manifest.Value.AddDependency(asmRef.Name, asmRef.Version, asmRef.Architecture, asmRef.PublicKeyToken); } } if (_company != null) { binary.CompanyName = _company; } if (_description != null) { binary.FileDescription = _description; } if (_internalName != null) { binary.InternalName = _internalName; } if (_copyright != null) { binary.LegalCopyright = _copyright; } if (_originalFilename != null) { binary.OriginalFilename = _originalFilename; } if (_productName != null) { binary.ProductName = _productName; } if (_productVersion != 0) { binary.ProductVersion = _productVersion; } if (_fileVersion != 0) { binary.FileVersion = _fileVersion; } if (_dpiAware != null) { binary.Manifest.Value.DpiAware = _dpiAware == true; } if (_executionLevel != ExecutionLevel.none) { binary.Manifest.Value.RequestedExecutionLevel = _executionLevel; } } else { // do stuff for non-pe files // we can try to apply a signature, and that's about it. if (_sign) { binary.SigningCertificate = _certificate; } } binary.Save().Wait(); } catch (Exception e) { while (e.GetType() == typeof (AggregateException)) { e = (e as AggregateException).Flatten().InnerExceptions[0]; } failures += Fail("{0}--{1}", e.Message, e.StackTrace); } } }, TaskContinuationOptions.AttachedToParent)).ToArray(); // Thread.Sleep(1000); // wait for loading. return Task.Factory.ContinueWhenAll(loading, tsks => { Console.WriteLine("Done {0} files", tsks.Length); (from each in Binary.Files select new { Filename = Path.GetFileName(each.Filename), Original_MD5 = origMD5[each.Filename], New_MD5 = each.MD5, // Status = each.Message, }).ToTable().ConsoleOut(); if (failures > 0) { Console.WriteLine("*** Bad News. Failed. *** "); } if (Binary.IsAnythingStillLoading) { Console.WriteLine("\r\n==== Uh, stuff is still in the loading state?! ====\r\n"); } return failures; }).Result; } catch (Exception e) { Console.WriteLine("{0}--{1}", e.Message, e.StackTrace); return Fail("not good."); } }
private bool LoadViaMetaProvider(IMetaProvider metaProvider, string name, FourPartVersion asmVersion, IHostApi request ) { var found = false; var instance = metaProvider.CreateProvider(name); if (instance != null) { // check if it's a Package Provider if (typeof (IPackageProvider).CanDynamicCastFrom(instance)) { try { var packageProvider = RegisterPackageProvider(instance.As<IPackageProvider>(), name, asmVersion, request); if (packageProvider != null) { found = true; packageProvider.IsLoaded = true; packageProvider.ProviderPath = metaProvider.GetProviderPath(name); } } catch (Exception e) { e.Dump(); } } // check if it's a Services Provider if (typeof (IArchiver).CanDynamicCastFrom(instance)) { try { var archiver = RegisterArchiver(instance.As<IArchiver>(), asmVersion, request); if (archiver != null) { found = true; archiver.ProviderPath = metaProvider.GetProviderPath(name); archiver.IsLoaded = true; } } catch (Exception e) { e.Dump(); } } if (typeof (IDownloader).CanDynamicCastFrom(instance)) { try { var downloader = RegisterDownloader(instance.As<IDownloader>(), asmVersion, request); if (downloader != null) { found = true; downloader.ProviderPath = metaProvider.GetProviderPath(name); downloader.IsLoaded = true; } } catch (Exception e) { e.Dump(); } } } return found; }
private Downloader RegisterDownloader(IDownloader provider, FourPartVersion asmVersion, IHostApi request) { string name = null; try { FourPartVersion ver = provider.GetProviderVersion(); var version = ver == 0 ? asmVersion : ver; name = provider.GetDownloaderName(); if (String.IsNullOrWhiteSpace(name)) { return null; } // Initialize the provider before locking the collection // that way we're not blocking others on non-deterministic actions. request.Debug("Initializing provider '{0}'".format(name)); provider.InitializeProvider(request.As<IRequest>()); request.Debug("Provider '{0}' Initialized".format(name)); lock (Downloaders) { if (Downloaders.ContainsKey(name)) { if (version > Downloaders[name].Version) { // remove the old provider first. // todo: this won't remove the plugin domain and unload the code yet // we'll have to do that later. Downloaders.Remove(name); } else { return null; } } request.Debug("Using Downloader Provider {0}".format(name)); var downloader = new Downloader(provider) { Version = version, IsLoaded = true }; downloader.Initialize(request); Downloaders.AddOrSet(name, downloader); return downloader; } } catch (Exception e) { request.Debug("Provider '{0}' Failed".format(name)); e.Dump(); } return null; }
private Task LoadManagedData() { return _loadingManagedData ?? (_loadingManagedData = _tasks.Start(() => { if (_loadOptions.HasFlag(BinaryLoadOptions.NoManaged)) { return; } // load managed data from working file if (!IsManaged) { ILOnly.Value = false; return; } // copy this to a temporary file, because it locks the file until we're *really* done. try { var module = _host.LoadUnitFrom(WorkingCopy) as IModule; if (module == null || module is Dummy) { throw new CoAppException("{0} is not a PE file containing a CLR module or assembly.".format(Filename)); } ILOnly.Value = module.ILOnly; //Make a mutable copy of the module. var copier = new MetadataDeepCopier(_host); var mutableModule = copier.Copy(module); //Traverse the module. In a real application the MetadataVisitor and/or the MetadataTravers will be subclasses //and the traversal will gather information to use during rewriting. var traverser = new MetadataTraverser { PreorderVisitor = new MetadataVisitor(), TraverseIntoMethodBodies = true }; traverser.Traverse(mutableModule); //Rewrite the mutable copy. In a real application the rewriter would be a subclass of MetadataRewriter that actually does something. var rewriter = new MetadataRewriter(_host); _mutableAssembly = rewriter.Rewrite(mutableModule) as Assembly; AssemblyCulture.Value = _mutableAssembly.Culture; AssemblyVersion.Value = _mutableAssembly.Version; } finally { // delete it, or at least trash it & queue it up for next reboot. // temporaryCopy.TryHardToDelete(); } try { if (_mutableAssembly != null) { // we should see if we can get assembly attributes, since sometimes they can be set, but not the native ones. foreach (var a in _mutableAssembly.ContainingAssembly.AssemblyAttributes) { var attributeArgument = (a.Arguments.FirstOrDefault() as MetadataConstant); if (attributeArgument != null) { var attributeValue = attributeArgument.Value.ToString(); if (!string.IsNullOrEmpty(attributeValue)) { switch (a.Type.ToString()) { case "System.Reflection.AssemblyTitleAttribute": _fileDescription = _fileDescription ?? attributeValue; break; case "System.Reflection.AssemblyCompanyAttribute": _companyName = _companyName ?? attributeValue; break; case "System.Reflection.AssemblyProductAttribute": _productName = _productName ?? attributeValue; break; // case "System.Reflection.AssemblyVersionAttribute": // _assemblyVersion = _assemblyVersion == 0L ? (FourPartVersion)attributeValue : _assemblyVersion; // break; case "System.Reflection.AssemblyFileVersionAttribute": _fileVersion = _fileVersion == 0L ? (FourPartVersion)attributeValue : _fileVersion; _productVersion = _productVersion == 0L ? (FourPartVersion)attributeValue : _productVersion; break; case "System.Reflection.AssemblyCopyrightAttribute": _legalCopyright = _legalCopyright ?? attributeValue; break; case "System.Reflection.AssemblyTrademarkAttribute": _legalTrademarks = _legalTrademarks ?? attributeValue; break; case "System.Reflection.AssemblyDescriptionAttribute": _comments = _comments ?? attributeValue; break; case "BugTrackerAttribute": _bugTracker = _bugTracker ?? attributeValue; break; } } } } } } catch (Exception e) { Console.WriteLine("{0} -- {1}", e.Message, e.StackTrace); } // if there are dependencies, this will load them. if (_loadOptions.HasFlag(BinaryLoadOptions.UnsignedManagedDependencies)) { LoadUnsignedManagedDependencies(); } })); }
public Task<IEnumerable<Package>> GetPackages( string packageName, FourPartVersion? minVersion = new FourPartVersion?(), FourPartVersion? maxVersion = new FourPartVersion?(), bool? dependencies = new bool?(), bool? installed = new bool?(), bool? active = new bool?(), bool? requested = new bool?(), bool? blocked = new bool?(), bool? latest = new bool?(), string locationFeed = null, bool? updates = new bool?(), bool? upgrades = new bool?(), bool? trimable = new bool?()) { return Task.Factory.StartNew(() => { IEnumerable<PackageDecl> packages = null; if (packageName == "*") packages = _prods; if (installed != null) packages = packages.Where(p => p.IsInstalled == installed); if (locationFeed != null) { if (_feedToPackages.ContainsKey(locationFeed)) { packages = packages.Where(p => _feedToPackages[locationFeed].Contains(p)); } else { packages = Enumerable.Empty<PackageDecl>(); } } return packages.Cast<Package>(); }); }
/// <summary> /// This initializes the provider registry with the list of package providers. /// (currently a hardcoded list, soon, registry driven) /// </summary> /// <param name="request"></param> internal void LoadProviders(IHostApi request) { if (request == null) { throw new ArgumentNullException("request"); } var baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var providerAssemblies = (_initialized ? Enumerable.Empty <string>() : _defaultProviders) .Concat(GetProvidersFromRegistry(Registry.LocalMachine, "SOFTWARE\\MICROSOFT\\ONEGET")) .Concat(GetProvidersFromRegistry(Registry.CurrentUser, "SOFTWARE\\MICROSOFT\\ONEGET")) .Concat(AutoloadedAssemblyLocations.SelectMany(location => { if (Directory.Exists(location)) { return(Directory.EnumerateFiles(location).Where(each => !IsExcluded(each) && (each.EndsWith(".exe", StringComparison.CurrentCultureIgnoreCase) || each.EndsWith(".dll", StringComparison.CurrentCultureIgnoreCase)))); } return(Enumerable.Empty <string>()); })); #if DEEP_DEBUG providerAssemblies = providerAssemblies.ToArray(); foreach (var each in providerAssemblies) { request.Debug("possible assembly: {0}".format(each)); } #endif // find modules that have manifests // todo: expand this out to validate the assembly is ok for this instance of OneGet. providerAssemblies = providerAssemblies.Where(each => Manifest.LoadFrom(each).Any(manifest => Swidtag.IsSwidtag(manifest) && new Swidtag(manifest).IsApplicable(new Hashtable()))); // add inbox assemblies (don't require manifests, because they are versioned with the core) #if !COMMUNITY_BUILD // todo: these should just be strong-named references. for now, just load them from the same directory. providerAssemblies = providerAssemblies.Concat(new[] { Path.Combine(baseDir, "Microsoft.OneGet.MetaProvider.PowerShell.dll"), Path.Combine(baseDir, "Microsoft.OneGet.ArchiverProviders.dll"), Path.Combine(baseDir, "Microsoft.OneGet.CoreProviders.dll"), Path.Combine(baseDir, "Microsoft.OneGet.MsuProvider.dll"), #if !CORE_CLR // can't load these providers here. Path.Combine(baseDir, "Microsoft.OneGet.MsiProvider.dll"), #endif }); #endif #if DEEP_DEBUG providerAssemblies = providerAssemblies.ToArray(); foreach (var each in providerAssemblies) { request.Debug("possible assembly with manifest: {0}".format(each)); } #endif providerAssemblies = providerAssemblies.OrderByDescending(each => { try { // try to get a version from the file first return((ulong)(FourPartVersion)FileVersionInfo.GetVersionInfo(each)); } catch { // otherwise we can't make a distinction. return((ulong)0); } }); providerAssemblies = providerAssemblies.Distinct(new PathEqualityComparer(PathCompareOption.FileWithoutExtension)); #if BEFORE_WE_HAD_MANIFESTS // hack to make sure we don't load the old version of the nuget provider // when we have the ability to examine a plugin without dragging it into the // primary appdomain, this won't be needed. FourPartVersion minimumnugetversion = "2.8.3.6"; providerAssemblies = providerAssemblies.Where(assemblyFile => { try { if ("nuget-anycpu".EqualsIgnoreCase(Path.GetFileNameWithoutExtension(assemblyFile)) && ((FourPartVersion)FileVersionInfo.GetVersionInfo(assemblyFile)) < minimumnugetversion) { return(false); } } catch { } return(true); }); #endif // there is no trouble with loading providers concurrently. #if DEBUG providerAssemblies.SerialForEach(providerAssemblyName => { #else providerAssemblies.ParallelForEach(providerAssemblyName => { #endif try { request.Debug(request.FormatMessageString("Trying provider assembly: {0}", providerAssemblyName)); if (TryToLoadProviderAssembly(providerAssemblyName, request)) { request.Debug(request.FormatMessageString("SUCCESS provider assembly: {0}", providerAssemblyName)); } else { request.Debug(request.FormatMessageString("FAILED provider assembly: {0}", providerAssemblyName)); } } catch { request.Error(Constants.Messages.ProviderPluginLoadFailure, ErrorCategory.InvalidOperation.ToString(), providerAssemblyName, request.FormatMessageString(Constants.Messages.ProviderPluginLoadFailure, providerAssemblyName)); } }); }
internal static Package GetPackage(string packageName, FourPartVersion version, Architecture architecture, string publicKeyToken, Guid? productCode) { Package pkg; // try via just the package product code if (productCode != null) { lock (_packages) { pkg = _packages.FirstOrDefault(package => package.ProductCode == productCode); } if (pkg != null) { // if we *have* this package somewhere, but don't know its name, // we can now fill *that* in. if (string.IsNullOrEmpty(pkg.Name)) { pkg.Name = packageName; pkg.Architecture = architecture; pkg.Version = version; pkg.PublicKeyToken = publicKeyToken; } return pkg; } } lock (_packages) { pkg = (_packages.Where(package => package.Architecture == architecture && package.Version == version && package.PublicKeyToken == publicKeyToken && package.Name.Equals(packageName, StringComparison.CurrentCultureIgnoreCase))).FirstOrDefault(); // we've tried finding it a couple of ways, and got back nothing for our trouble. // we'll create an package with the details we have, and pass that back. if (pkg == null) { pkg = new Package(packageName, architecture, version, publicKeyToken, productCode); _packages.Add(pkg); } } // if we did find a package and its product code was empty, we can fill that in now if we have it. if (productCode !=null && pkg.ProductCode == null) { pkg.ProductCode = productCode; } return pkg; }
private Package(string name, Architecture architecture, FourPartVersion version, string publicKeyToken, Guid? productCode) : this(productCode) { Name = name; Version = version; Architecture = architecture; PublicKeyToken = publicKeyToken; }
public IEnumerable<FileInformation> this[string name, FourPartVersion version, Architecture architecture] { get { return null; } }
public IEnumerable<FileInformation> this[string name, FourPartVersion version] { get { return this[name, version, Architecture.Auto]; } }
public IEnumerable <FileInformation> this[string name, FourPartVersion version] { get { return(this[name, version, Architecture.Auto]); } }
public IEnumerable <FileInformation> this[string name, FourPartVersion version, Architecture architecture] { get { return(null); } }
protected override void ProcessRecord() { #if USING_RESTABLE_CMDLET if (Remote) { ProcessRecordViaRest(); return; } #endif var wc = new WebClient(); var tmp = "coapp.tools.powershell.msi".GenerateTemporaryFilename(); if (Development) { wc.DownloadFile(@"http://downloads.coapp.org/files/Development.CoApp.Tools.Powershell.msi", tmp); } else if (Beta) { wc.DownloadFile(@"http://downloads.coapp.org/files/Beta.CoApp.Tools.Powershell.msi", tmp); } else { wc.DownloadFile(@"http://downloads.coapp.org/files/CoApp.Tools.Powershell.msi", tmp); } FourPartVersion ver = FileVersionInfo.GetVersionInfo(tmp); if (ver == 0) { using (Database db = new Database(tmp)) { ver = db.ExecuteScalar("SELECT `Value` FROM `Property` WHERE `Property` = 'ProductVersion'") as string; } } FourPartVersion thisVer = this.Assembly().Version(); if (ver < thisVer) { WriteObject("The current version {0} is newer than the version on the web {1}.".format(thisVer, ver)); return; } if (ver == thisVer) { WriteObject("The current version {0} is the current version.".format(thisVer, ver)); return; } WriteObject("The current version {0} will be replaced with the newer than the version from the web {1}.".format(thisVer, ver)); using (dynamic ps = Runspace.DefaultRunspace.Dynamic()) { ps.InvokeExpression(@"msiexec.exe /i ""{0}""".format(tmp)); } if (!KillPowershells) { WriteObject("FYI, the installer can't actually update without killing all the powershell tasks."); WriteObject("If you are running as admin, you can do this automatically with the -KillPowershells switch on this command."); } else { using (var ps = Runspace.DefaultRunspace.Dynamic()) { ps.StopProcess(name: "powershell"); } } }
/// <summary> /// Register the package provider /// </summary> /// <param name="provider">Package provider object</param> /// <param name="asmVersion">assembly version info</param> /// <param name="request"></param> /// <param name="shouldRefreshCache">should refresh the internal provider list</param> /// <returns></returns> private PackageProvider RegisterPackageProvider(IPackageProvider provider, FourPartVersion asmVersion, IHostApi request, bool shouldRefreshCache) { string name = null; try { if (provider == null) { return null; } FourPartVersion ver = provider.GetProviderVersion(); var version = ver == 0 ? asmVersion : ver; name = provider.GetPackageProviderName(); if (string.IsNullOrWhiteSpace(name)) { return null; } // Initialize the provider before locking the collection // that way we're not blocking others on non-deterministic actions. request.Debug("Initializing provider '{0}'".format(name)); provider.InitializeProvider(request.As<IRequest>()); request.Debug("Provider '{0}' Initialized".format(name)); lock (_packageProviders) { //Check if the provider is loaded already. if (_packageProviders.ContainsKey(name)) { //if no -force, do nothing if (!shouldRefreshCache) { request.Verbose(string.Format(CultureInfo.CurrentCulture, Resources.Messages.SkipPreviousProcessedProvider, name)); //add the provider to the list var pkgprovider = new PackageProvider(provider) { Version = version, IsLoaded = true }; AddToProviderCacheTable(name, pkgprovider); return pkgprovider; } else { //looks like -force is used, we need to remove the old provider first. // this won't remove the plugin domain and unload the code yet _packageProviders.Remove(name); } } } request.Debug("Using Package Provider {0}".format(name)); var packageProvider = new PackageProvider(provider) { Version = version }; //initialize the package provider packageProvider.Initialize(request); // addOrSet locks the collection anyway. _packageProviders.AddOrSet(name, packageProvider); packageProvider.IsLoaded = true; request.Debug("The provider '{0}' is imported".format(name)); //add the provider to the list AddToProviderCacheTable(name, packageProvider); return packageProvider; } catch (Exception e) { request.Debug("Provider '{0}' Failed to import".format(name)); e.Dump(); } return null; }
public void AddDependency(string name, FourPartVersion version, Architecture arch, string publicKeyToken, string language = "*", AssemblyType assemblyType = AssemblyType.win32, BindingRedirect redirect = null) { Modified = true; _dependentAssemblies.AddDependency(name, version, arch, publicKeyToken, language, assemblyType, redirect); }
private Task LoadResourceData() { return _loadingResourceData ?? (_loadingResourceData = _tasks.Start(() => { // load resource data from working file if (_loadOptions.HasFlag(BinaryLoadOptions.NoResources)) { NativeResources.Value = null; return; } var resinfo = new ResourceInfo(); try { resinfo.Load(WorkingCopy); } catch { // even though nothing was loaded, let's keep the blank resources object around. _modifiedResources = false; NativeResources.Value = resinfo; return; } // lets pull out the relevant resources first. var versionKey = resinfo.Resources.Keys.FirstOrDefault(each => each.ResourceType == ResourceTypes.RT_VERSION); try { var versionResource = resinfo.Resources[versionKey].First() as VersionResource; var versionStringTable = (versionResource["StringFileInfo"] as StringFileInfo).Strings.Values.First(); _comments = _comments ?? TryGetVersionString(versionStringTable, "Comments"); _companyName = _companyName ?? TryGetVersionString(versionStringTable, "CompanyName"); _productName = _productName ?? TryGetVersionString(versionStringTable, "ProductName"); // _assemblyVersion = _assemblyVersion == 0L ? (FourPartVersion)TryGetVersionString(versionStringTable, "Assembly Version") : _assemblyVersion; _fileVersion = _fileVersion == 0L ? (FourPartVersion)TryGetVersionString(versionStringTable, "FileVersion") : _fileVersion; //Console.WriteLine("VER: {0}", _fileVersion); _internalName = _internalName ?? TryGetVersionString(versionStringTable, "InternalName"); _originalFilename = _originalFilename ?? TryGetVersionString(versionStringTable, "OriginalFilename"); _legalCopyright = _legalCopyright ?? TryGetVersionString(versionStringTable, "LegalCopyright"); _legalTrademarks = _legalTrademarks ?? TryGetVersionString(versionStringTable, "LegalTrademarks"); _fileDescription = _fileDescription ?? TryGetVersionString(versionStringTable, "FileDescription"); _bugTracker = _bugTracker ?? TryGetVersionString(versionStringTable, "BugTracker"); _productVersion = _productVersion == 0L ? (FourPartVersion)TryGetVersionString(versionStringTable, "ProductVersion") : _productVersion; } catch { // no version resources it seems. } NativeResources.Value = resinfo; })); }
internal bool RegisterProvidersViaMetaProvider(IMetaProvider provider, FourPartVersion asmVersion, IHostApi request) { var found = false; var metaProviderName = provider.GetMetaProviderName(); lock (_metaProviders) { if (!_metaProviders.ContainsKey(metaProviderName)) { // Meta Providers can't be replaced at this point _metaProviders.AddOrSet(metaProviderName.ToLowerInvariant(), provider); } } try { provider.InitializeProvider(request.As<IRequest>()); provider.GetProviderNames().ParallelForEach(name => { found = LoadViaMetaProvider(provider, name,asmVersion, request); }); } catch (Exception e) { e.Dump(); } return found; }
private PackageProvider RegisterPackageProvider(IPackageProvider provider, string name, FourPartVersion asmVersion, IHostApi request) { // string name = null; try { FourPartVersion ver = provider.GetProviderVersion(); var version = ver == 0 ? asmVersion : ver; //TODO: Need to write a blog file name is the provider name. Provider is no longer required to impl ProvierName property //name = provider.GetPackageProviderName(); //if (String.IsNullOrWhiteSpace(name)) { // return null; //} // Initialize the provider before locking the collection // that way we're not blocking others on non-deterministic actions. request.Debug("Initializing provider '{0}'".format(name)); provider.InitializeProvider(request.As<IRequest>()); request.Debug("Provider '{0}' Initialized".format(name)); lock (_packageProviders) { if (_packageProviders.ContainsKey(name)) { if (version > _packageProviders[name].Version) { // remove the old provider first. // todo: this won't remove the plugin domain and unload the code yet // we'll have to do that later. _packageProviders.Remove(name); } else { return null; } } } request.Debug("Using Package Provider {0}".format(name)); var packageProvider = new PackageProvider(provider) { Version = version, IsLoaded = true }; packageProvider.Initialize(request); // addOrSet locks the collection anyway. _packageProviders.AddOrSet(name, packageProvider); return packageProvider; } catch (Exception e) { request.Debug("Provider '{0}' Failed".format(name)); e.Dump(); } return null; }
private void InsertIntoFeed(CanonicalName pkgCanonicalName, FourPartVersion pkgVersion, Uri location, AtomItem item = null) { FeedBlob.Lock(blob => { // update the feed var feed = new AtomFeed(); //load the feed from the _canonicalFeedUrl if we can try { var originalFeed = LoadFeed(blob); foreach (AtomItem i in originalFeed.Items.Where(each => each is AtomItem)) { if (_feedName == "current") { // if an older version of this package is in the current feed, if (i.Model.CanonicalName.DiffersOnlyByVersion(pkgCanonicalName) && i.Model.CanonicalName.Version < pkgVersion) { // push it to the archive feed. try { if (FeedHandlers["archive"] != null) { FeedHandlers["archive"].InsertIntoFeed(i.Model.CanonicalName, i.Model.Version, i.Model.Locations[0]); // and skip it continue; } } catch { } } } feed.Add(i); } } catch { } item = item ?? _packageManager.GetAtomItem(pkgCanonicalName).Result; if (item != null) { // first, make sure that the feeds contains the intended feed location. item.Model.Feeds = item.Model.Feeds ?? new XList<Uri>(); if (!item.Model.Feeds.Contains(_canonicalFeedUrl)) { item.Model.Feeds.Insert(0, _canonicalFeedUrl); } item.Model.Locations = item.Model.Locations ?? new XList<Uri>(); if (!item.Model.Locations.Contains(location)) { item.Model.Locations.Insert(0, location); } // drop dead urls item.Model.Feeds = item.Model.Feeds.Distinct().Where(Peek).ToXList(); item.Model.Locations = item.Model.Locations.Distinct().Where(Peek).ToXList(); foreach (var l in item.Links.ToArray().Where(each => !Peek(each.Uri))) { item.Links.Remove(l); } if (item.Model.Locations.Any()) { // if we've got at least one valid location, add the item to the feed. feed.Add(item); } } SaveFeed(blob, feed); // regenerate the webpi feed based on the items in this feed SaveWebPiFeed(feed); }); }
public bool YieldFromSwidtag(DynamicElement provider, string requiredVersion, string minimumVersion, string maximumVersion, string searchKey) { if (provider == null) { throw new ArgumentNullException("provider"); } var name = provider.Attributes["name"]; FourPartVersion version = provider.Attributes["version"]; var versionScheme = provider.Attributes["versionScheme"]; var packageFilename = provider.XPath("/swid:SoftwareIdentity/swid:Meta[@targetFilename]").GetAttribute("targetFilename"); var summary = provider.XPath("/swid:SoftwareIdentity/swid:Meta[@summary]").GetAttribute("summary"); if (AnyNullOrEmpty(name, version, versionScheme, packageFilename)) { Debug("Skipping yield on swid due to missing field \r\n", provider.ToString()); return(true); } if (!string.IsNullOrEmpty(requiredVersion) && version != requiredVersion) { return(true); } if (!string.IsNullOrEmpty(minimumVersion) && version < minimumVersion) { return(true); } if (!string.IsNullOrEmpty(maximumVersion) && version > maximumVersion) { return(true); } if (YieldSoftwareIdentity(name, name, version, versionScheme, summary, null, searchKey, null, packageFilename)) { // note: temporary until we actaully support swidtags in the core. // yield all the meta/attributes if (provider.XPath("/swid:SoftwareIdentity/swid:Meta").Any( meta => meta.Attributes.Any(attribute => !YieldSoftwareMetadata(name, attribute.Name.LocalName, attribute.Value)))) { return(false); } if (provider.XPath("/swid:SoftwareIdentity/swid:Link").Any( link => !YieldLink(name, link.Attributes["href"], link.Attributes["rel"], link.Attributes["type"], link.Attributes["ownership"], link.Attributes["use"], link.Attributes["media"], link.Attributes["artifact"]))) { return(false); } if (provider.XPath("/swid:SoftwareIdentity/swid:Entity").Any( entity => !YieldEntity(name, entity.Attributes["name"], entity.Attributes["regid"], entity.Attributes["role"], entity.Attributes["thumbprint"]))) { return(false); } if (!YieldSoftwareMetadata(name, "FromTrustedSource", true.ToString())) { return(false); } } return(true); }
private int Startup(IEnumerable<string> args) { var options = args.Switches(); var parameters = args.Parameters(); foreach (var arg in options.Keys) { var argumentParameters = options[arg]; switch (arg) { /* global switches */ case "load-config": // all ready done, but don't get too picky. break; case "nologo": this.Assembly().SetLogo(string.Empty); break; case "help": return Help(); case "certificate-path": _signingCertPath = Path.GetFullPath(argumentParameters.Last()); break; case "password": _signingCertPassword = argumentParameters.Last(); break; case "remember": _remember = true; break; case "auto": _auto = true; break; case "sign": _sign = true; break; case "strong-name": _strongname = true; break; case "verbose": _verbose = true; break; case "company": _company = argumentParameters.Last(); break; case "description": _description = argumentParameters.Last(); break; case "internal-name": _internalName = argumentParameters.Last(); break; case "copyright": _copyright = argumentParameters.Last(); break; case "original-filename": _originalFilename = argumentParameters.Last(); break; case "product-name" : _productName = argumentParameters.Last(); break; case "verify" : _verify = true; break; case "reference-assembly" : foreach (var asmRef in argumentParameters) { if( string.IsNullOrEmpty(asmRef)) { return Fail("Missing assembly information for --assembly-reference."); } var parts = asmRef.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var assemblyref = new AssemblyReference { Name = parts[0] }; foreach( var part in parts.Skip(1)) { var kp = part.Split("= ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if( kp.Length != 2) { return Fail("Invalid option '{0}' in assembly reference '{1}'.", part, asmRef); } switch( kp[0].ToLower()) { case "version": case "ver": assemblyref.Version = kp[1]; if (assemblyref.Version == 0L) { return Fail("Invalid Version '{0}' in assembly reference '{1}'.", kp[1], asmRef); } break; case "publickeytoken": case "pkt": case "token": if( kp[1].Length != 16) { return Fail("Invalid publicKeyToken '{0}' in assembly reference '{1}'.", kp[1], asmRef); } assemblyref.PublicKeyToken = kp[1]; break; case "processorarchitecture": case "architecture": case "arch": assemblyref.Architecture = kp[1]; if (assemblyref.Architecture == Architecture.Auto || assemblyref.Architecture == Architecture.Unknown) { return Fail("Invalid processorArchitecture '{0}' in assembly reference '{1}'.", kp[1], asmRef); } break; } } if (assemblyref.Version == 0 || assemblyref.Architecture == Architecture.Unknown || string.IsNullOrEmpty(assemblyref.PublicKeyToken)) { return Fail("Invalid assembly reference '{0}' ", asmRef); } assemblyReferences.Add(assemblyref); } break; case "product-version": _productVersion = argumentParameters.Last(); if (_productVersion == 0L ) { return Fail("--product-version must be in the form ##.##.##.##"); } break; case "file-version": _fileVersion = argumentParameters.Last(); if (_fileVersion == 0L ) { return Fail("--file-version must be in the form ##.##.##.##"); } break; case "execution-level": switch( argumentParameters.Last() ) { case "administrator": case "admin": case "requires-admin": case "requiresadmin": case "requiresadministrator": case "requires-administrator": _executionLevel = ExecutionLevel.requireAdministrator; break; case "invoker": case "asinvoker": case "as-invoker": _executionLevel = ExecutionLevel.asInvoker; break; case "highest-available": case "highest": case "highestavailable": _executionLevel = ExecutionLevel.highestAvailable; break; } break; case "dpi-aware" : if (argumentParameters.Last().IsTrue()) { _dpiAware = true; } if (argumentParameters.Last().IsFalse()) { _dpiAware = false; } break; default: return Fail("Unknown parameter [--{0}]", arg); } } Logo(); if( _verify ) { // return Verify(parameters); } if( string.IsNullOrEmpty(_signingCertPath) ) { _certificate = CertificateReference.Default; if( _certificate == null ) { return Fail("No default certificate stored in the registry"); } } else if( string.IsNullOrEmpty(_signingCertPassword) ) { _certificate = new CertificateReference(_signingCertPath); } else { _certificate = new CertificateReference(_signingCertPath,_signingCertPassword); } using (new ConsoleColors(ConsoleColor.White, ConsoleColor.Black)) { Verbose("Loaded certificate with private key {0}", _certificate.Location); } if (_remember) { Verbose("Storing certificate details in the registry."); _certificate.RememberPassword(); CertificateReference.Default = _certificate; } if (parameters.Count() < 1) { return Fail("Missing files to sign/name. \r\n\r\n Use --help for command line help."); } var tasks = new List<Task>(); if( _company != null && _company.Equals("auto", StringComparison.CurrentCultureIgnoreCase) ) { _company = _certificate.CommonName; } var failures = 0; try { var allFiles = parameters.FindFilesSmarter().ToArray(); var origMD5 = new Dictionary<string, string>(); var loading = allFiles.Select(each => Binary.Load(each, BinaryLoadOptions.PEInfo | BinaryLoadOptions.VersionInfo | BinaryLoadOptions.Managed | BinaryLoadOptions.Resources | BinaryLoadOptions.Manifest | BinaryLoadOptions.UnsignedManagedDependencies | BinaryLoadOptions.MD5 ).ContinueWith(antecedent => { lock (allFiles) { if (antecedent.IsFaulted) { Console.WriteLine("Failed to load file '{0}'", each); var e = antecedent.Exception.Flatten().InnerExceptions.First(); Console.WriteLine("{0}--{1}", e.Message, e.StackTrace); return; } try { var binary = antecedent.Result; origMD5.Add(each, binary.MD5); if (binary.IsPEFile) { // do PE file stuff if (_sign) { binary.SigningCertificate = _certificate; } if (binary.IsManaged && _strongname) { binary.StrongNameKeyCertificate = _certificate; } if( !assemblyReferences.IsNullOrEmpty()) { foreach( var asmRef in assemblyReferences ) { binary.Manifest.Value.AddDependency( asmRef.Name , asmRef.Version, asmRef.Architecture, asmRef.PublicKeyToken); } } if (_company != null) { binary.CompanyName = _company; } if (_description != null) { binary.FileDescription = _description; } if (_internalName != null) { binary.InternalName = _internalName; } if (_copyright != null) { binary.LegalCopyright = _copyright; } if (_originalFilename != null) { binary.OriginalFilename = _originalFilename; } if (_productName != null) { binary.ProductName = _productName; } if (_productVersion != 0) { binary.ProductVersion = _productVersion; } if (_fileVersion != 0) { binary.FileVersion = _fileVersion; } if (_dpiAware != null) { binary.Manifest.Value.DpiAware = _dpiAware == true; } if (_executionLevel != ExecutionLevel.none) { binary.Manifest.Value.RequestedExecutionLevel = _executionLevel; } } else { // do stuff for non-pe files // we can try to apply a signature, and that's about it. if (_sign) { binary.SigningCertificate = _certificate; } } binary.Save().Wait(); } catch(Exception e) { while( e.GetType() == typeof(AggregateException)) { e = (e as AggregateException).Flatten().InnerExceptions[0]; } failures += Fail("{0}--{1}", e.Message, e.StackTrace); } } }, TaskContinuationOptions.AttachedToParent)).ToArray(); // Thread.Sleep(1000); // wait for loading. return Task.Factory.ContinueWhenAll(loading, tsks => { Console.WriteLine("Done {0} files", tsks.Length); (from each in Binary.Files select new { Filename = Path.GetFileName(each.Filename), Original_MD5 = origMD5[each.Filename], New_MD5 = each.MD5, // Status = each.Message, }).ToTable().ConsoleOut(); if (failures > 0) { Console.WriteLine("*** Bad News. Failed. *** "); } if (Binary.IsAnythingStillLoading) { Console.WriteLine("\r\n==== Uh, stuff is still in the loading state?! ====\r\n"); } return failures; }).Result; } catch( Exception e ) { Console.WriteLine("{0}--{1}", e.Message,e.StackTrace); return Fail("not good."); } /* try { var binaries = new List<PeBinary>(); var nonBinaries = new List<PEInfo>(); var results = new List<FileResult>(); foreach (var f in allFiles) { Verbose("Inspecting File: {0}", f); var filename = f; // first, load all the binaries // tasks.Add(Task.Factory.StartNew(() => { if (CoApp.Toolkit.Crypto.Verifier.HasValidSignature(filename) && !_resign) { results.Add( new FileResult {FullPath= filename, AlreadySigned = true, OriginalMD5 = filename.GetFileMD5(), Message = "Already Signed (skipped)", Color = ConsoleColor.Yellow}); return; } try { var info = PEInfo.Scan(filename); if (info.IsPEBinary) { var peBinary = PeBinary.Load(filename); lock (binaries) { if (!binaries.Contains(peBinary)) { binaries.Add(peBinary); } foreach (var depBinary in peBinary.UnsignedDependentBinaries.Where(depBinary => !binaries.Contains(depBinary))) { binaries.Add(depBinary); } } } else { if (!nonBinaries.Contains(info)) { nonBinaries.Add(info); } } } catch (Exception e) { results.Add(new FileResult { FullPath = filename, Message = "Failed to load--{0}".format(e.GetType()), Color = ConsoleColor.Red }); } })); } Task.Factory.ContinueWhenAll(tasks.ToArray(), (antecedent) => Verbose("Completed loading files.")).Wait(); tasks.Clear(); // Now, go ahead and modify all the binaries // and sign all the files. foreach( var nBin in nonBinaries ) { var nonBinary = nBin; tasks.Add(Task.Factory.StartNew(() => { var filename = nonBinary.Filename; try { PeBinary.SignFile(filename, _certificate); results.Add( new FileResult { FullPath = filename, OriginalMD5 = nonBinary.MD5, NewMD5 = filename.GetFileMD5(), Message = "Success." }); } catch (DigitalSignFailure exc) { if (exc.Win32Code == 0x800b0003) { results.Add(new FileResult { FullPath = filename, OriginalMD5 = nonBinary.MD5, Message = "Unable to sign unrecognized file", Color = ConsoleColor.Red }); } else { results.Add(new FileResult {FullPath = filename, OriginalMD5 = nonBinary.MD5, Message = exc.Message, Color = ConsoleColor.Red}); } } catch (CoAppException exc) { results.Add(new FileResult { FullPath = filename, OriginalMD5 = nonBinary.MD5, Message = exc.Message, Color = ConsoleColor.Red }); } catch (Exception exc) { results.Add(new FileResult { FullPath = filename, OriginalMD5 = nonBinary.MD5, Message = "Unable to sign unrecognized, non-binary file--{0}".format(exc.GetType()), Color = ConsoleColor.Red}); } })); } binaries.Reverse(); foreach (var bin in binaries) { var peBinary = bin; tasks.Add(Task.Factory.StartNew(() => { var filename = peBinary.Filename; try { if (_strongname) { peBinary.StrongNameKeyCertificate = _certificate; } if (_sign) { peBinary.SigningCertificate = _certificate; } if (_company != null) { peBinary.CompanyName = _company; } if (_description != null) { peBinary.FileDescription = _description; } if (_internalName != null) { peBinary.InternalName = _internalName; } if (_copyright != null) { peBinary.LegalCopyright = _copyright; } if (_originalFilename != null) { peBinary.OriginalFilename = _originalFilename; } if (_productName != null) { peBinary.ProductName = _productName; } if (_productVersion != null) { peBinary.ProductVersion = _productVersion; } if (_fileVersion != null) { peBinary.FileVersion = _fileVersion; } peBinary.Save(_auto); results.Add(new FileResult { FullPath = filename, OriginalMD5 = peBinary.Info.MD5, NewMD5 = filename.GetFileMD5(), Message = "Success" }); } catch (CoAppException exc) { results.Add(new FileResult { FullPath = filename, OriginalMD5 = peBinary.Info.MD5, Message = exc.Message, Color = ConsoleColor.Red }); } catch (Exception exc) { results.Add(new FileResult { FullPath = filename, OriginalMD5 = peBinary.Info.MD5, Message = "Unable to sign PE Binary file--{0}".format(exc.GetType()), Color = ConsoleColor.Red }); } })); } if (tasks.Any()) { // wait for all the work to be done. Task.Factory.ContinueWhenAll(tasks.ToArray(), (antecedent) => { Verbose("Completed Signing Files."); }).Wait(); } var output = results.OrderByDescending(each => each.Color).ToArray(); var outputLines = (from each in output select new { Filename = Path.GetFileName(each.FullPath), Original_MD5 = each.OriginalMD5, New_MD5 = each.NewMD5, Status = each.Message, }).ToTable().ToArray(); Console.WriteLine(outputLines[0]); var footer = outputLines[1]; Console.WriteLine(footer); // trim the header/footer outputLines = outputLines.Skip(2).Reverse().Skip(1).Reverse().ToArray(); for (int i = 0; i < outputLines.Length; i++ ) { using (new ConsoleColors(output[i].Color, ConsoleColor.Black)) { Console.WriteLine(outputLines[i]); } } Console.WriteLine(footer); } catch (Exception e) { return Fail(e.Message); } */ return 0; }