protected override bool YieldDetails(PsRequest r) { if (_details != null && _details.Count > 0) { // we need to send this back as a set of key/path & value pairs. return _details.Flatten().All(kvp => r.AddMetadata(FastPackageReference,kvp.Key, kvp.Value) != null); } return true; }
public override bool YieldResult(PsRequest r) { if (r == null) { throw new ArgumentNullException("r"); } return r.YieldSoftwareIdentity(FastPackageReference, Name, Version, VersionScheme, Summary, Source, SearchKey, FullPath, Filename) != null && YieldDetails(r) && YieldEntities(r) && YieldLinks(r) && YieldDependencies(r) && r.AddMetadata(FastPackageReference, "FromTrustedSource", FromTrustedSource.ToString()) != null; }
public override bool YieldResult(PsRequest r) { if (r == null) { throw new ArgumentNullException("r"); } return r.YieldPackageSource(Name, Location, IsTrusted, IsRegistered,IsValidated) && YieldDetails(r); }
private PowerShellPackageProvider Create(PsRequest req, KeyValuePair <string, string> psModule) { var ps = PowerShell.Create(); try { // load the powershell provider functions into this runspace. if (ps.ImportModule(PowerShellProviderFunctions) != null) { var result = ps.ImportModule(psModule.Key); if (result != null) { try { return(new PowerShellPackageProvider(ps, result, psModule.Value)); } catch (Exception e) { e.Dump(); } } } } catch (Exception e) { // something didn't go well. // skip it. e.Dump(); } // didn't import correctly. ps.Dispose(); return(null); }
public override bool YieldResult(PsRequest r) { if (r == null) { throw new ArgumentNullException("r"); } return r.YieldDynamicOption(Name, ExpectedType.ToString(), IsRequired) && PermittedValues.WhereNotNull().Select(each => each.ToString()).ToArray().All(v => r.YieldKeyValuePair(Name, v)); }
public override bool YieldResult(PsRequest r) { if (r == null) { throw new ArgumentNullException("r"); } return(r.YieldDynamicOption(Name, ExpectedType.ToString(), IsRequired) && PermittedValues.WhereNotNull().Select(each => each.ToString()).ToArray().All(v => r.YieldKeyValuePair(Name, v))); }
protected virtual bool YieldDependencies(PsRequest r) { if (_dependencies != null) { return(_dependencies.OfType <Dependency>().All(dep => r.AddDependency(dep.ProviderName, dep.PackageName, dep.Version, dep.Source, dep.AppliesTo) != null)); } return(true); }
protected virtual bool YieldEntities(PsRequest r) { if (_links != null) { return(_entities.OfType <Entity>().All(entity => r.AddEntity(entity.Name, entity.RegId, entity.Role, entity.Thumbprint) != null)); } return(true); }
protected virtual bool YieldDetails(PsRequest r) { if (_details != null && _details.Count > 0) { // we need to send this back as a set of key/path & value pairs. return _details.Flatten().All(kvp => r.YieldKeyValuePair(kvp.Key, kvp.Value)); } return true; }
protected virtual bool YieldLinks(PsRequest r) { if (_links != null) { return(_links.OfType <Link>().All(link => r.AddLink(new Uri(link.HRef), link.Relationship, link.MediaType, link.Ownership, link.Use, link.AppliesToMedia, link.Artifact) != null)); } return(true); }
public override bool YieldResult(PsRequest r) { if (r == null) { throw new ArgumentNullException("r"); } if (_pair.Value.Length == 0) { return r.YieldKeyValuePair(_pair.Key, null); } return _pair.Value.All(each => r.YieldKeyValuePair(_pair.Key, each)); }
protected virtual bool YieldTagId(PsRequest r) { if (string.IsNullOrWhiteSpace(_tagId)) { return(true); } return(r.AddTagId(_tagId) != null); }
protected override bool YieldDetails(PsRequest r) { if (_details != null && _details.Count > 0) { // we need to send this back as a set of key/path & value pairs. return(_details.Flatten().All(kvp => r.AddMetadata(FastPackageReference, kvp.Key, kvp.Value) != null)); } return(true); }
public override bool YieldResult(PsRequest r) { if (r == null) { throw new ArgumentNullException("r"); } return(r.YieldSoftwareIdentity(FastPackageReference, Name, Version, VersionScheme, Summary, Source, SearchKey, FullPath, Filename) != null && YieldDetails(r) && YieldEntities(r) && YieldLinks(r) && YieldDependencies(r) && r.AddMetadata(FastPackageReference, "FromTrustedSource", FromTrustedSource.ToString()) != null); }
protected virtual bool YieldXmlLang(PsRequest r) { if (string.IsNullOrWhiteSpace(_xmlLang)) { return(true); } return(r.AddCulture(_xmlLang) != null); }
protected virtual bool YieldDetails(PsRequest r) { if (_details != null && _details.Count > 0) { // we need to send this back as a set of key/path & value pairs. return(_details.Flatten().All(kvp => r.YieldKeyValuePair(kvp.Key, kvp.Value))); } return(true); }
public override bool YieldResult(PsRequest r) { if (r == null) { throw new ArgumentNullException("r"); } return(r.YieldPackageSource(Name, Location, IsTrusted, IsRegistered, IsValidated) && YieldDetails(r)); }
public override bool YieldResult(PsRequest r) { if (r == null) { throw new ArgumentNullException("r"); } if (_pair.Value.Length == 0) { return(r.YieldKeyValuePair(_pair.Key, null)); } return(_pair.Value.All(each => r.YieldKeyValuePair(_pair.Key, each))); }
// lock is on this instance only internal void ReportErrors(PsRequest request, IEnumerable <ErrorRecord> errors) { foreach (var error in errors) { request.Error(error.FullyQualifiedErrorId, error.CategoryInfo.Category.ToString(), error.TargetObject == null ? null : error.TargetObject.ToString(), error.ErrorDetails == null ? error.Exception.Message : error.ErrorDetails.Message); if (!string.IsNullOrWhiteSpace(error.ScriptStackTrace)) { // give a debug hint if we have a script stack trace. How nice of us. request.Debug(Constants.ScriptStackTrace, error.ScriptStackTrace); } } }
public override bool YieldResult(PsRequest r) { if (r == null) { throw new ArgumentNullException("r"); } // if we get an xml swidtag string, use that if (!string.IsNullOrWhiteSpace(_xmlSwidTag)) { return(r.YieldSoftwareIdentityXml(_xmlSwidTag, _commitImmediately) != null); } return(r.YieldSoftwareIdentity(FastPackageReference, Name, Version, VersionScheme, Summary, Source, SearchKey, FullPath, Filename) != null && YieldTagId(r) && YieldDetails(r) && YieldEntities(r) && YieldLinks(r) && YieldDependencies(r) && r.AddMetadata(FastPackageReference, "FromTrustedSource", FromTrustedSource.ToString()) != null); }
public void InitializeProvider(PsRequest request) { if (request == null) { throw new ArgumentNullException("request"); } request.Debug("Initializing PowerShell MetaProvider"); var modules = ScanForModules(request).Distinct().ToArray(); // try to create each module at least once. modules.ParallelForEach(modulePath => AnalyzeModule(request, modulePath)); request.Debug("Loaded PowerShell Provider Modules "); }
internal IEnumerable <KeyValuePair <string, string> > ScanForModules(PsRequest request) { // two places we search for modules // 1. in this assembly's folder, look for all psd1 and psm1 files. // // 2. modules in the PSMODULEPATH // // Import each one of those, and check to see if they have a PackageManagementProviders section in their private data using (var ps1 = PowerShell.Create()) { if (ps1.ImportModule(PowerShellProviderFunctions) != null) { return(AlternativeModuleScan(request).SelectMany(each => ps1.TestModuleManifest(each).SelectMany(GetPackageManagementModules)).ToArray()); } } return(Enumerable.Empty <KeyValuePair <string, string> >()); }
private IEnumerable <string> AlternativeModuleScan(PsRequest request) { var psModulePath = Environment.GetEnvironmentVariable("PSModulePath") ?? ""; IEnumerable <string> paths = psModulePath.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); // add assumed paths just in case the environment variable isn't really set. try { paths = paths.ConcatSingleItem(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"WindowsPowerShell\v1.0\Modules")); } catch { // skip the folder if it's not valid } try { paths = paths.ConcatSingleItem(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"WindowsPowerShell\Modules")); } catch { // skip the folder if it's not valid } if (!string.IsNullOrWhiteSpace(BaseFolder) && BaseFolder.DirectoryExists()) { paths = paths.ConcatSingleItem(BaseFolder); } paths = paths.Distinct(new PathEqualityComparer(PathCompareOption.Full)).ToArray(); var moduleFolders = paths.Where(each => each.DirectoryExists()).SelectMany(each => Directory.EnumerateDirectories(each).Where(dir => !_exclusionList.Contains(Path.GetFileName(dir)))); var modules = moduleFolders.Select(module => { var moduleManifest = Path.Combine(module, Path.GetFileName(module) + ".psd1"); if (File.Exists(moduleManifest)) { return(moduleManifest); } var versions = Directory.EnumerateDirectories(module).Select(dir => new { folder = dir, ver = (FourPartVersion)Path.GetFileName(dir) }).Where(each => each.ver > 0L).OrderByDescending(each => each).Select(each => each.folder); return(versions.Select(version => Path.Combine(version, Path.GetFileName(Path.GetFileName(module)) + ".psd1")).FirstOrDefault(File.Exists)); }).WhereNotNull(); return(modules.Where(moduleName => File.ReadAllText(moduleName).IndexOf("PackageManagementProviders", StringComparison.OrdinalIgnoreCase) > -1)); }
private void AnalyzeModule(PsRequest request, KeyValuePair <string, string> modulePath) { request.Debug("Attempting to load PowerShell Provider Module [{0}]", modulePath); var provider = Create(request, modulePath); if (provider != null) { if (provider.GetPackageProviderName() != null) { request.Debug("Loaded PowerShell Package Provider Module: [{0}]", modulePath); // looks good to me, let's add this to the list of moduels this meta provider can create. _packageProviders.AddOrSet(provider.GetPackageProviderName(), provider); } else { provider.Dispose(); provider = null; } } }
public override bool YieldResult(PsRequest r) { if (r == null) { throw new ArgumentNullException("r"); } // if we get an xml swidtag string, use that if (!string.IsNullOrWhiteSpace(_xmlSwidTag)) { return(r.YieldSoftwareIdentityXml(_xmlSwidTag, _commitImmediately) != null); } bool result = r.YieldSoftwareIdentity(FastPackageReference, Name, Version, VersionScheme, Summary, Source, SearchKey, FullPath, Filename) != null && YieldTagId(r) && YieldXmlLang(r) && YieldDetails(r) && YieldEntities(r) && YieldLinks(r) && YieldDependencies(r) && r.AddMetadata(FastPackageReference, "FromTrustedSource", FromTrustedSource.ToString()) != null; // check whether we have destination if (!string.IsNullOrWhiteSpace(_destinationPath)) { var payload = r.AddPayload(); // could not add the payload, return false if (string.IsNullOrWhiteSpace(payload)) { return(false); } if (r.AddDirectory(payload, System.IO.Path.GetFileName(_destinationPath), System.IO.Path.GetDirectoryName(_destinationPath), null, true) == null) { // cannot add directory return(false); } } return(result); }
protected virtual bool YieldTagId(PsRequest r) { if (string.IsNullOrWhiteSpace(_tagId)) { return true; } return r.AddTagId(_tagId) != null; }
protected virtual bool YieldLinks(PsRequest r) { if( _links != null ) { return _links.OfType<Link>().All(link => r.AddLink(new Uri(link.HRef), link.Relationship, link.MediaType, link.Ownership, link.Use, link.AppliesToMedia, link.Artifact) != null); } return true; }
protected virtual bool YieldEntities(PsRequest r) { if (_links != null) { return _entities.OfType<Entity>().All(entity => r.AddEntity(entity.Name, entity.RegId, entity.Role, entity.Thumbprint) != null); } return true; }
protected virtual bool YieldDependencies(PsRequest r) { if (_dependencies != null) { return _dependencies.OfType<Dependency>().All(dep => r.AddDependency(dep.ProviderName, dep.PackageName, dep.Version, dep.Source, dep.AppliesTo ) != null); } return true; }
public override bool YieldResult(PsRequest r) { if (r == null) { throw new ArgumentNullException("r"); } // if we get an xml swidtag string, use that if (!string.IsNullOrWhiteSpace(_xmlSwidTag)) { return r.YieldSoftwareIdentityXml(_xmlSwidTag, _commitImmediately) != null; } return r.YieldSoftwareIdentity(FastPackageReference, Name, Version, VersionScheme, Summary, Source, SearchKey, FullPath, Filename) != null && YieldTagId(r) && YieldDetails(r) && YieldEntities(r) && YieldLinks(r) && YieldDependencies(r) && r.AddMetadata(FastPackageReference, "FromTrustedSource", FromTrustedSource.ToString()) != null; }
private object Call(string function, IRequest requestObject, params object[] args) { return(PsRequest.New(requestObject, this, function).CallPowerShell(args)); }
internal object CallPowerShell(PsRequest request, params object[] args) { // the lock ensures that we're not re-entrant into the same powershell runspace lock (_lock) { if (!_reentrancyLock.WaitOne(0)) { // this lock is set to false, meaning we're still in a call **ON THIS THREAD** // this is bad karma -- powershell won't let us call into the runspace again // we're going to throw an error here because this indicates that the currently // running powershell call is calling back into PM, and it has called back // into this provider. That's just bad bad bad. throw new Exception("Re-entrancy Violation in powershell module"); } try { // otherwise, this is the first time we've been here during this call. _reentrancyLock.Reset(); _powershell.SetVariable("request", request); _powershell.Streams.ClearStreams(); // request.Debug("INVOKING PowerShell Fn {0} in {1}", request.CommandInfo.Name, _module.Name); // make sure we don't pass the request to the function. var result = _powershell.InvokeFunction<object>(request.CommandInfo.Name, args); // instead, loop thru results and get if (result == null) { // failure! throw new Exception(Messages.PowershellScriptFunctionFailed.format(_module.Name, request.CommandInfo.Name)); } object finalValue = null; foreach (var value in result) { if (_powershell.Streams.Error.Any()) { ReportErrors(request, _powershell.Streams.Error); _powershell.Streams.Error.Clear(); } var y = value as Yieldable; if (y != null) { y.YieldResult(request); } else { finalValue = value; } } if (_powershell.Streams.Error.Any()) { ReportErrors(request, _powershell.Streams.Error); _powershell.Streams.Error.Clear(); } return finalValue; } catch (CmdletInvocationException cie) { var error = cie.ErrorRecord; request.Error(error.FullyQualifiedErrorId, error.CategoryInfo.Category.ToString(), error.TargetObject == null ? null : error.TargetObject.ToString(), error.ErrorDetails == null ? error.Exception.Message : error.ErrorDetails.Message); }catch (Exception e) { e.Dump(); } finally { lock (_stopLock) { if (_stopResult != null){ _powershell.EndStop(_stopResult); _stopResult = null; } } _powershell.Clear(); _powershell.SetVariable("request", null); // it's ok if someone else calls into this module now. _reentrancyLock.Set(); } return null; } }
public abstract bool YieldResult(PsRequest r);
protected virtual bool YieldXmlLang(PsRequest r) { if (string.IsNullOrWhiteSpace(_xmlLang)) { return true; } return r.AddCulture(_xmlLang) != null; }
public override bool YieldResult(PsRequest r) { if (r == null) { throw new ArgumentNullException("r"); } // if we get an xml swidtag string, use that if (!string.IsNullOrWhiteSpace(_xmlSwidTag)) { return r.YieldSoftwareIdentityXml(_xmlSwidTag, _commitImmediately) != null; } bool result = r.YieldSoftwareIdentity(FastPackageReference, Name, Version, VersionScheme, Summary, Source, SearchKey, FullPath, Filename) != null && YieldTagId(r) && YieldXmlLang(r) && YieldDetails(r) && YieldEntities(r) && YieldLinks(r) && YieldDependencies(r) && r.AddMetadata(FastPackageReference, "FromTrustedSource", FromTrustedSource.ToString()) != null; // check whether we have destination if (!string.IsNullOrWhiteSpace(_destinationPath)) { var payload = r.AddPayload(); // could not add the payload, return false if (string.IsNullOrWhiteSpace(payload)) { return false; } if (r.AddDirectory(payload, System.IO.Path.GetFileName(_destinationPath), System.IO.Path.GetDirectoryName(_destinationPath), null, true) == null) { // cannot add directory return false; } } return result; }
// lock is on this instance only internal void ReportErrors(PsRequest request, IEnumerable<ErrorRecord> errors) { foreach (var error in errors) { request.Error(error.FullyQualifiedErrorId, error.CategoryInfo.Category.ToString(), error.TargetObject == null ? null : error.TargetObject.ToString(), error.ErrorDetails == null ? error.Exception.Message : error.ErrorDetails.Message); if (!string.IsNullOrWhiteSpace(error.ScriptStackTrace)) { // give a debug hint if we have a script stack trace. How nice of us. request.Debug(Constants.ScriptStackTrace, error.ScriptStackTrace); } } }
internal object CallPowerShell(PsRequest request, params object[] args) { // the lock ensures that we're not re-entrant into the same powershell runspace lock (_lock) { if (!_reentrancyLock.WaitOne(0)) { // this lock is set to false, meaning we're still in a call **ON THIS THREAD** // this is bad karma -- powershell won't let us call into the runspace again // we're going to throw an error here because this indicates that the currently // running powershell call is calling back into PM, and it has called back // into this provider. That's just bad bad bad. throw new Exception("Re-entrancy Violation in powershell module"); } try { // otherwise, this is the first time we've been here during this call. _reentrancyLock.Reset(); _powershell.SetVariable("request", request); _powershell.Streams.ClearStreams(); // request.Debug("INVOKING PowerShell Fn {0} in {1}", request.CommandInfo.Name, _module.Name); // make sure we don't pass the request to the function. var result = _powershell.InvokeFunction <object>(request.CommandInfo.Name, args); // instead, loop thru results and get if (result == null) { // failure! throw new Exception(Messages.PowershellScriptFunctionFailed.format(_module.Name, request.CommandInfo.Name)); } object finalValue = null; foreach (var value in result) { if (_powershell.Streams.Error.Any()) { ReportErrors(request, _powershell.Streams.Error); _powershell.Streams.Error.Clear(); } var y = value as Yieldable; if (y != null) { y.YieldResult(request); } else { finalValue = value; } } if (_powershell.Streams.Error.Any()) { ReportErrors(request, _powershell.Streams.Error); _powershell.Streams.Error.Clear(); } return(finalValue); } catch (CmdletInvocationException cie) { var error = cie.ErrorRecord; request.Error(error.FullyQualifiedErrorId, error.CategoryInfo.Category.ToString(), error.TargetObject == null ? null : error.TargetObject.ToString(), error.ErrorDetails == null ? error.Exception.Message : error.ErrorDetails.Message); }catch (Exception e) { e.Dump(); } finally { lock (_stopLock) { if (_stopResult != null) { _powershell.EndStop(_stopResult); _stopResult = null; } } _powershell.Clear(); _powershell.SetVariable("request", null); // it's ok if someone else calls into this module now. _reentrancyLock.Set(); } return(null); } }