private static string GetConfigurationFileLocation(Request request) { var filePath = request.GetOptionValue("ConfigFile"); if (String.IsNullOrEmpty(filePath)) { //otherwise, use %APPDATA%/EXE/Smart.Config filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "EXE", "Smart.config"); } Directory.CreateDirectory(Path.GetDirectoryName(filePath)); // TODO: this can fail if we don't have permissions - need to verify if OneGet would display a good error message using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) { if (fs.Length == 0) { using (var sw = new StreamWriter(fs)) { sw.WriteLine(DefaultConfig); sw.Flush(); } } fs.Close(); } return filePath; }
internal static void RemovePackageSource(string name, Request request) { IDictionary<string, PackageSource> packageSources = GetPackageSources(request); if (!packageSources.ContainsKey(name)) { request.Error(ErrorCategory.ResourceUnavailable.ToString(), name, Strings.PackageSourceNotFound, name); return; } packageSources.Remove(name); SavePackageSources(packageSources, request); }
internal static void AddPackageSource(string name, string location, bool trusted, Request request) { IDictionary<string, PackageSource> packageSources = GetPackageSources(request); if (packageSources.ContainsKey(name)) { request.Error(ErrorCategory.ResourceExists.ToString(), name, Strings.PackageSourceExists, name); return; } packageSources.Add(name, new PackageSource() { Name = name, Location = location, Trusted = trusted, IsRegistered = true, IsValidated = true }); SavePackageSources(packageSources, request); }
/// <summary> /// This is called when the user is adding (or updating) a package source /// </summary> /// <param name="name">The name of the package source. If this parameter is null or empty the PROVIDER should use the location as the name (if the PROVIDER actually stores names of package sources)</param> /// <param name="location">The location (ie, directory, URL, etc) of the package source. If this is null or empty, the PROVIDER should use the name as the location (if valid)</param> /// <param name="trusted">A boolean indicating that the user trusts this package source. Packages returned from this source should be marked as 'trusted'</param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void AddPackageSource(string name, string location, bool trusted, Request request) { // TODO: improve this debug message that tells us what's going on. request.Debug("Calling '{0}::AddPackageSource' '{1}','{2}','{3}'", PackageProviderName, name, location, trusted); // TODO: support user-defined package sources OR remove this method }
/// <summary> /// Resolves and returns Package Sources to the client. /// /// Specified sources are passed in via the request object (<c>request.GetSources()</c>). /// /// Sources are returned using <c>request.YieldPackageSource(...)</c> /// </summary> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void ResolvePackageSources(Request request) { // TODO: improve this debug message that tells us what's going on. request.Debug("Calling '{0}::ResolvePackageSources'", PackageProviderName); // TODO: resolve package sources if (request.Sources.Any()) { // the system is requesting sources that match the values passed. // if the value passed can be a legitimate source, but is not registered, return a package source marked unregistered. } else { // the system is requesting all the registered sources } }
/// <summary> /// Installs a given package. /// </summary> /// <param name="fastPackageReference">A provider supplied identifier that specifies an exact package</param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void InstallPackage(string fastPackageReference, Request request) { request.Debug("Calling '{0}::InstallPackage' '{1}'", PackageProviderName, fastPackageReference); string source; //ignore string id; //url or file path string version; if (!fastPackageReference.TryParseFastPath(out source, out id, out version)) { request.Error("", ErrorCategory.InvalidArgument.ToString(), fastPackageReference, Strings.InvalidFastPath); } var error = DscInvoker.SetLCMToDisabled(); if(error != null) { request.Error("", ErrorCategory.InvalidOperation.ToString(), fastPackageReference, error.ErrorDetails.Message); } request.Debug("set the local LCM to disabled mode"); request.Debug("try getting silent install option"); var option = FindAppNameSilentArgs(id); if (option != null) { error = DscInvoker.InvokeDscPackegeResource(option.Item1, option.Item2, id); if (error != null) { request.Error("", ErrorCategory.InvalidOperation.ToString(), fastPackageReference, error.ErrorDetails.Message); } } else { // Prepare the process to run ProcessStartInfo start = new ProcessStartInfo(); // Enter the executable to run, including the complete path start.FileName = id; // Do you want to show a console window? start.WindowStyle = ProcessWindowStyle.Normal; //start.CreateNoWindow = true; // Run the external process & wait for it to finish using (Process proc = Process.Start(start)) { proc.WaitForExit(); // Retrieve the app's exit code var exitCode = proc.ExitCode; if(exitCode != 0) { request.Error("", ErrorCategory.InvalidOperation.ToString(), fastPackageReference, string.Format("running installation package {0} failed, error detail is {1}",id, proc.StandardError.ReadToEnd())); } } } }
/// <summary> /// Finalizes a batch search request. /// </summary> /// <param name="id"></param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> /// <returns></returns> public void CompleteFind(int id, Request request) { // TODO: improve this debug message that tells us what's going on. request.Debug("Calling '{0}::CompleteFind' '{1}'", PackageProviderName, id); // TODO: batch search implementation }
/// <summary> /// /// </summary> /// <param name="fastPackageReference"></param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void GetPackageDetails(string fastPackageReference, Request request) { // TODO: improve this debug message that tells us what's going on. request.Debug("Calling '{0}::GetPackageDetails' '{1}'", PackageProviderName, fastPackageReference); // TODO: This method is for fetching details that are more expensive than FindPackage* (if you don't need that, remove this method) }
/// <summary> /// Uninstalls a package /// </summary> /// <param name="fastPackageReference"></param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void UninstallPackage(string fastPackageReference, Request request) { // TODO: improve this debug message that tells us what's going on. request.Debug("Calling '{0}::UninstallPackage' '{1}'", PackageProviderName, fastPackageReference); // TODO: Uninstall the package }
/// <summary> /// Downloads a remote package file to a local location. /// </summary> /// <param name="fastPackageReference"></param> /// <param name="location"></param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void DownloadPackage(string fastPackageReference, string location, Request request) { // TODO: improve this debug message that tells us what's going on. request.Debug("Calling '{0}::DownloadPackage' '{1}','{2}'", PackageProviderName, fastPackageReference, location); // TODO: actually download the package ... }
/// <summary> /// Finds packages given a locally-accessible filename /// /// Package information must be returned using <c>request.YieldPackage(...)</c> function. /// </summary> /// <param name="file">the full path to the file to determine if it is a package</param> /// <param name="id">if this is greater than zero (and the number should have been generated using <c>StartFind(...)</c>, the core is calling this multiple times to do a batch search request. The operation can be delayed until <c>CompleteFind(...)</c> is called</param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void FindPackageByFile(string file, int id, Request request) { // TODO: improve this debug message that tells us what's going on. request.Debug("Calling '{0}::FindPackageByFile' '{1}','{2}'", PackageProviderName, file, id); // TODO: implement searching for a package by analyzing the package file, or remove this method }
/// <summary> /// Removes/Unregisters a package source /// </summary> /// <param name="name">The name or location of a package source to remove.</param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void RemovePackageSource(string name, Request request) { // TODO: improve this debug message that tells us what's going on. request.Debug("Calling '{0}::RemovePackageSource' '{1}'", PackageProviderName, name); // TODO: support user-defined package sources OR remove this method }
internal static IDictionary<string, PackageSource> GetPackageSources(Request request) { var filePath = GetConfigurationFileLocation(request); var packageSources = JsonConvert.DeserializeObject<IDictionary<string, PackageSource>>(File.ReadAllText(filePath)); return packageSources ?? new Dictionary<string, PackageSource>(); }
private static void SavePackageSources(IDictionary<string, PackageSource> packageSources, Request request) { var filePath = GetConfigurationFileLocation(request); string json = JsonConvert.SerializeObject(packageSources, Formatting.Indented); File.WriteAllText(filePath, json); }
/// <summary> /// This is called when the user is adding (or updating) a package source /// </summary> /// <param name="name">The name of the package source. If this parameter is null or empty the PROVIDER should use the location as the name (if the PROVIDER actually stores names of package sources)</param> /// <param name="location">The location (ie, directory, URL, etc) of the package source. If this is null or empty, the PROVIDER should use the name as the location (if valid)</param> /// <param name="trusted">A boolean indicating that the user trusts this package source. Packages returned from this source should be marked as 'trusted'</param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void AddPackageSource(string name, string location, bool trusted, Request request) { request.Debug("Calling '{0}::AddPackageSource' '{1}','{2}','{3}'", PackageProviderName, name, location, trusted); ProviderStorage.AddPackageSource(name, location, trusted, request); }
/// <summary> /// Uninstalls a package /// </summary> /// <param name="fastPackageReference"></param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void UninstallPackage(string fastPackageReference, Request request) { request.Debug("Calling '{0}::UninstallPackage' '{1}'", PackageProviderName, fastPackageReference); string source; string id; string version; if (!fastPackageReference.TryParseFastPath(out source, out id, out version)) { request.Error(ErrorCategory.InvalidArgument.ToString(), fastPackageReference, Strings.InvalidFastPath, fastPackageReference); } // TODO: uninstall }
/// <summary> /// Resolves and returns Package Sources to the client. /// /// Specified sources are passed in via the request object (<c>request.GetSources()</c>). /// /// Sources are returned using <c>request.YieldPackageSource(...)</c> /// </summary> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void ResolvePackageSources(Request request) { request.Debug("Calling '{0}::ResolvePackageSources'", PackageProviderName); if (request.Sources.Any()) { // the system is requesting sources that match the values passed. // if the value passed can be a legitimate source, but is not registered, return a package source marked unregistered. var packageSources = ProviderStorage.GetPackageSources(request); if (request.IsCanceled) { return; } foreach (var source in request.Sources.AsNotNull()) { if (packageSources.ContainsKey(source)) { var packageSource = packageSources[source]; // YieldPackageSource returns false when operation was cancelled if (!request.YieldPackageSource(packageSource.Name, packageSource.Location, packageSource.Trusted, packageSource.IsRegistered, packageSource.IsValidated)) { return; } } else { request.Warning("Package Source '{0}' not found.", source); } } } else { // the system is requesting all the registered sources var packageSources = ProviderStorage.GetPackageSources(request); foreach (var entry in packageSources.AsNotNull()) { var packageSource = entry.Value; // YieldPackageSource returns false when operation was cancelled if (!request.YieldPackageSource(packageSource.Name, packageSource.Location, packageSource.Trusted, packageSource.IsRegistered, packageSource.IsValidated)) { return; } } } }
/// <summary> /// Removes/Unregisters a package source /// </summary> /// <param name="name">The name or location of a package source to remove.</param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void RemovePackageSource(string name, Request request) { request.Debug("Calling '{0}::RemovePackageSource' '{1}'", PackageProviderName, name); ProviderStorage.RemovePackageSource(name, request); }
/// <summary> /// Searches package sources given name and version information /// /// Package information must be returned using <c>request.YieldPackage(...)</c> function. /// </summary> /// <param name="name">a name or partial name of the package(s) requested</param> /// <param name="requiredVersion">A specific version of the package. Null or empty if the user did not specify</param> /// <param name="minimumVersion">A minimum version of the package. Null or empty if the user did not specify</param> /// <param name="maximumVersion">A maximum version of the package. Null or empty if the user did not specify</param> /// <param name="id">if this is greater than zero (and the number should have been generated using <c>StartFind(...)</c>, the core is calling this multiple times to do a batch search request. The operation can be delayed until <c>CompleteFind(...)</c> is called</param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void FindPackage(string name, string requiredVersion, string minimumVersion, string maximumVersion, int id, Request request) { // TODO: improve this debug message that tells us what's going on. request.Debug("Calling '{0}::FindPackage' '{1}','{2}','{3}','{4}'", PackageProviderName, requiredVersion, minimumVersion, maximumVersion, id); // TODO: find package by name (and version) or id... }
/// <summary> /// Returns a collection of strings to the client advertizing features this provider supports. /// </summary> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void GetFeatures(Request request) { // TODO: improve this debug message that tells what's going on. request.Debug("Calling '{0}::GetFeatures' ", PackageProviderName); foreach (var feature in Features) { request.Yield(feature); } }
/// <summary> /// Finds packages given a URI. /// /// The function is responsible for downloading any content required to make this work /// /// Package information must be returned using <c>request.YieldPackage(...)</c> function. /// </summary> /// <param name="uri">the URI the client requesting a package for.</param> /// <param name="id">if this is greater than zero (and the number should have been generated using <c>StartFind(...)</c>, the core is calling this multiple times to do a batch search request. The operation can be delayed until <c>CompleteFind(...)</c> is called</param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void FindPackageByUri(Uri uri, int id, Request request) { // TODO: improve this debug message that tells us what's going on. request.Debug("Calling '{0}::FindPackageByUri' '{1}','{2}'", PackageProviderName, uri, id); // TODO: implement searching for a package by it's unique uri (or remove this method) }
/// <summary> /// Performs one-time initialization of the $provider. /// </summary> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void InitializeProvider(Request request) { // TODO: improve this debug message that tells what's going on. request.Debug("Calling '{0}::InitializeProvider'", PackageProviderName); // TODO: add any one-time initialization code here, or remove this method }
/// <summary> /// Returns package references for all the dependent packages /// </summary> /// <param name="fastPackageReference"></param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void GetPackageDependencies(string fastPackageReference, Request request) { // TODO: improve this debug message that tells us what's going on. request.Debug("Calling '{0}::GetPackageDependencies' '{1}'", PackageProviderName, fastPackageReference); // TODO: check dependencies }
public RequestLogger(Request request) { _request = request; }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void GetInstalledPackages(string name, Request request) { // TODO: improve this debug message that tells us what's going on. request.Debug("Calling '{0}::GetInstalledPackages' '{1}'", PackageProviderName, name); // TODO: list all installed packages }
/// <summary> /// Returns dynamic option definitions to the HOST /// /// example response: /// request.YieldDynamicOption( "MySwitch", OptionType.String.ToString(), false); /// /// </summary> /// <param name="category">The category of dynamic options that the HOST is interested in</param> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void GetDynamicOptions(string category, Request request) { // TODO: improve this debug message that tells us what's going on. request.Debug("Calling '{0}::GetDynamicOptions' {1}", PackageProviderName, category); switch ((category ?? string.Empty).ToLowerInvariant()) { case "install": // TODO: put the options supported for install/uninstall/getinstalledpackages break; case "provider": // TODO: put the options used with this provider (so far, not used by OneGet?). break; case "source": // TODO: put any options for package sources break; case "package": // TODO: put any options used when searching for packages break; default: request.Debug("Unknown category for '{0}::GetDynamicOptions': {1}", PackageProviderName, category); break; } }
/// <summary> /// Initializes a batch search request. /// </summary> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> /// <returns></returns> public int StartFind(Request request) { // TODO: improve this debug message that tells us what's going on. request.Debug("Calling '{0}::StartFind'", PackageProviderName); // TODO: batch search implementation return default(int); }
/// <summary> /// Returns a collection of strings to the client advertizing features this provider supports. /// </summary> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void GetFeatures(Request request) { request.Debug("Calling '{0}::GetFeatures' ", PackageProviderName); foreach (var feature in Features) { request.Yield(feature); } }
/// <summary> /// Performs one-time initialization of the $provider. /// </summary> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void InitializeProvider(Request request) { request.Debug("Calling '{0}::InitializeProvider' to set up a chocolatey with custom logging", PackageProviderName); _chocolatey = Lets.GetChocolatey().SetCustomLogging(new RequestLogger(request)); }
/// <summary> /// Performs one-time initialization of the $provider. /// </summary> /// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param> public void InitializeProvider(Request request) { request.Debug("Calling '{0}::InitializeProvider'", PackageProviderName); }