private bool UpdateOption( bool verbose, string dir, string option, Func <ConfigurationOption, bool> updateOption) { try { if (verbose) { Logger.EnableVerbose(); } if (!PlugInConfigurationValidation.ValidateElementName(option)) { Logger.Error( "The option name is invalid '{0}'- " + PlugInConfigurationValidation.ValidCharactersMessage, option); return(false); } string file = GetFileName(dir); using var fileStream = File.Open(file, FileMode.Open, FileAccess.ReadWrite); var logger = new BridgeLogger(); var existingConfiguration = PlugInConfigurationSerializer.ReadFromStream(fileStream, logger); if (existingConfiguration == null) { return(false); } var options = new HashSet <ConfigurationOption>(existingConfiguration.Options); var targetOption = options.FirstOrDefault(o => StringComparer.InvariantCulture.Equals(o.Name, option)); if (targetOption == null) { Logger.Error($"Option {option} was not found."); return(false); } if (!updateOption(targetOption)) { return(false); } fileStream.Position = 0; var configuration = new PlugInConfiguration( existingConfiguration.PlugInName, existingConfiguration.Version, options); PlugInConfigurationSerializer.WriteToStream(configuration, fileStream, logger); } catch (Exception e) { _parsingException = e; } return(true); }
private void ProcessesCommandAddOption( bool verbose, string dir, string name, string description) { try { if (verbose) { Logger.EnableVerbose(); } if (!PlugInConfigurationValidation.ValidateElementName(name)) { Logger.Error( "The name is invalid '{0}'- " + PlugInConfigurationValidation.ValidCharactersMessage, name); return; } string file = GetFileName(dir); using var fileStream = File.Open(file, FileMode.Open, FileAccess.ReadWrite); var logger = new BridgeLogger(); var existingConfiguration = PlugInConfigurationSerializer.ReadFromStream(fileStream, logger); if (existingConfiguration == null) { return; } ConfigurationOption newOption = new ConfigurationOption(name, description); var options = new HashSet <ConfigurationOption>(existingConfiguration.Options); if (!options.Add(newOption)) { Logger.Error("Option {0} already exists.", name); return; } fileStream.Position = 0; var configuration = new PlugInConfiguration( existingConfiguration.PlugInName, existingConfiguration.Version, options); PlugInConfigurationSerializer.WriteToStream(configuration, fileStream, logger); Logger.Information($"Added option {name}."); } catch (Exception e) { _parsingException = e; } }
private void ProcessesCommandDelOption( bool verbose, string dir, string name) { try { if (verbose) { Logger.EnableVerbose(); } string file = GetFileName(dir); using var fileStream = File.Open(file, FileMode.Open, FileAccess.ReadWrite); var logger = new BridgeLogger(); var existingConfiguration = PlugInConfigurationSerializer.ReadFromStream(fileStream, logger); if (existingConfiguration == null) { return; } HashSet <ConfigurationOption> options = new HashSet <ConfigurationOption>(existingConfiguration.Options); var targetOption = options.FirstOrDefault(o => StringComparer.InvariantCulture.Equals(o.Name, name)); if (targetOption == null) { Logger.Warning("Option {0} was not found.", name); return; } if (!options.Remove(targetOption)) { Debug.Assert(false); Logger.Error("Failed to remove option {0}.", name); return; } fileStream.Position = 0; var configuration = new PlugInConfiguration( existingConfiguration.PlugInName, existingConfiguration.Version, options); PlugInConfigurationSerializer.WriteToStream(configuration, fileStream, logger); Logger.Information($"Deleted option {name}."); } catch (Exception e) { _parsingException = e; } }
private void ProcessCommandPrint( bool verbose, string dir) { try { if (verbose) { Logger.EnableVerbose(); } string file = GetFileName(dir); using var fileStream = File.Open(file, FileMode.Open, FileAccess.ReadWrite); var existingConfiguration = PlugInConfigurationSerializer.ReadFromStream(fileStream, new BridgeLogger()); if (existingConfiguration == null) { return; } Logger.Information($"Plug-In:\t{existingConfiguration.PlugInName}"); Logger.Information($"Version:\t{existingConfiguration.Version}"); foreach (var option in existingConfiguration.Options) { Logger.Information("\n"); Logger.Information($"Option:\t{option.Name}"); Logger.Information($"Description:\t{option.Description}"); string applications = "none"; if (option.Applications.Any()) { applications = string.Join(", ", option.Applications); } Logger.Information($"Applications: {applications}"); string runtimes = "none"; if (option.Runtimes.Any()) { runtimes = string.Join(", ", option.Runtimes); } Logger.Information($"Runtimes: {runtimes}"); } } catch (Exception e) { _parsingException = e; } }
private void ProcessCommandCreate( bool verbose, string dir, bool overwrite, string name, SemanticVersion version) { try { if (verbose) { Logger.EnableVerbose(); } string file = GetFileName(dir, false); if (File.Exists(file)) { if (!overwrite) { Logger.Error($"File already exists: {file}. Use option '-o' to overwrite."); return; } } else if (string.IsNullOrWhiteSpace(file)) { Logger.Error($"Configuration file name is invalid: '{file}'."); return; } var configuration = new PlugInConfiguration(name, version, new HashSet <ConfigurationOption>()); using var stream = File.Create(file); PlugInConfigurationSerializer.WriteToStream(configuration, stream, new BridgeLogger()); stream.Flush(true); Logger.Information($"New configuration logged to {file}."); } catch (Exception e) { _parsingException = e; } }