static FileOperationResult ObservedLoadHandleException(Exception e, FileOperationDelegate loadFile, FileName fileName, string message, FileErrorPolicy policy) { message = message + Environment.NewLine + Environment.NewLine + e.Message; var messageService = ServiceSingleton.GetRequiredService <IMessageService>(); switch (policy) { case FileErrorPolicy.Inform: messageService.InformSaveError(fileName, message, "${res:FileUtilityService.ErrorWhileLoading}", e); break; case FileErrorPolicy.ProvideAlternative: ChooseSaveErrorResult r = messageService.ChooseSaveError(fileName, message, "${res:FileUtilityService.ErrorWhileLoading}", e, false); if (r.IsRetry) { return(ObservedLoad(loadFile, fileName, message, policy)); } else if (r.IsIgnore) { return(FileOperationResult.Failed); } break; } return(FileOperationResult.Failed); }
public Type FindType(string className) { foreach (Runtime runtime in runtimes) { if (!runtime.IsHostApplicationAssembly) { // Load dependencies only when a plugin library is first loaded. // This allows looking in host assemblies for service IDs. LoadDependencies(); } Type t = runtime.FindType(className); if (t != null) { return(t); } } if (hasShownErrorMessage) { LoggingService.Error("Cannot find class: " + className); } else { hasShownErrorMessage = true; var messageService = ServiceSingleton.GetRequiredService <IMessageService>(); messageService.ShowError("Cannot find class: " + className + "\nFuture missing objects will not cause an error message."); } return(null); }
static FileOperationResult ObservedSaveHandleError(Exception e, NamedFileOperationDelegate saveFileAs, FileName fileName, string message, FileErrorPolicy policy) { var messageService = ServiceSingleton.GetRequiredService <IMessageService>(); switch (policy) { case FileErrorPolicy.Inform: messageService.InformSaveError(fileName, message, "${res:FileUtilityService.ErrorWhileSaving}", e); break; case FileErrorPolicy.ProvideAlternative: ChooseSaveErrorResult r = messageService.ChooseSaveError(fileName, message, "${res:FileUtilityService.ErrorWhileSaving}", e, true); if (r.IsRetry) { return(ObservedSave(saveFileAs, fileName, message, policy)); } else if (r.IsIgnore) { return(FileOperationResult.Failed); } else if (r.IsSaveAlternative) { return(ObservedSave(saveFileAs, r.AlternativeFileName, message, policy)); } break; } return(FileOperationResult.Failed); }
public bool IsValid(object parameter) { try { var addInTree = ServiceSingleton.GetRequiredService <IAddInTree>(); return(addInTree.ConditionEvaluators[name].IsValid(parameter, this)); } catch (KeyNotFoundException) { throw new CoreException("Condition evaluator " + name + " not found!"); } }
public static bool TestFileExists(string filename) { if (!File.Exists(filename)) { var messageService = ServiceSingleton.GetRequiredService <IMessageService>(); messageService.ShowWarning(StringParser.Parse("${res:Fileutility.CantFindFileError}", new StringTagPair("FILE", filename))); return(false); } return(true); }
/// <summary> /// The specified AddIn is added to the <see cref="AddIns"/> collection. /// If the AddIn is enabled, its doozers, condition evaluators and extension /// paths are added to the AddInTree and its resources are added to the /// <see cref="ResourceService"/>. /// </summary> public void InsertAddIn(AddIn addIn) { if (addIn.Enabled) { foreach (ExtensionPath path in addIn.Paths.Values) { AddExtensionPath(path); } foreach (Runtime runtime in addIn.Runtimes) { if (runtime.IsActive) { System.Text.StringBuilder doozerName = new System.Text.StringBuilder(); foreach (var pair in runtime.DefinedDoozers) { doozerName.AppendFormat("{0}={1},", pair.Key, pair.Value); if (!doozers.TryAdd(pair.Key, pair.Value)) { throw new AddInLoadException("Duplicate doozer: " + pair.Key); } } System.Text.StringBuilder conditions = new System.Text.StringBuilder(); foreach (var pair in runtime.DefinedConditionEvaluators) { conditions.AppendFormat("{0}={1},", pair.Key, pair.Value); if (!conditionEvaluators.TryAdd(pair.Key, pair.Value)) { throw new AddInLoadException("Duplicate condition evaluator: " + pair.Key); } } } } string addInRoot = Path.GetDirectoryName(addIn.FileName); foreach (string bitmapResource in addIn.BitmapResources) { string path = Path.Combine(addInRoot, bitmapResource); ResourceManager resourceManager = ResourceManager.CreateFileBasedResourceManager(Path.GetFileNameWithoutExtension(path), Path.GetDirectoryName(path), null); ServiceSingleton.GetRequiredService <IResourceService>().RegisterNeutralImages(resourceManager); } foreach (string stringResource in addIn.StringResources) { string path = Path.Combine(addInRoot, stringResource); ResourceManager resourceManager = ResourceManager.CreateFileBasedResourceManager(Path.GetFileNameWithoutExtension(path), Path.GetDirectoryName(path), null); ServiceSingleton.GetRequiredService <IResourceService>().RegisterNeutralStrings(resourceManager); } } addIns.Add(addIn); }
/// <summary> /// Starts the core services. /// This initializes the PropertyService and ResourceService. /// </summary> public void StartCoreServices(IPropertyService propertyService) { var container = ServiceSingleton.GetRequiredService <IServiceContainer>(); var applicationStateInfoService = new ApplicationStateInfoService(); addInTree = new AddInTreeImpl(applicationStateInfoService); container.AddService(typeof(IPropertyService), propertyService); container.AddService(typeof(IResourceService), new ResourceServiceImpl( Path.Combine(propertyService.DataDirectory, "resources"), propertyService)); container.AddService(typeof(IAddInTree), addInTree); container.AddService(typeof(ApplicationStateInfoService), applicationStateInfoService); StringParser.RegisterStringTagProvider(new AppNameProvider { appName = applicationName }); }
static bool UninstallAddIn(List <string> disabled, string addInName, string targetDir) { if (Directory.Exists(targetDir)) { try { Directory.Delete(targetDir, true); } catch (Exception ex) { disabled.Add(addInName); var messageService = ServiceSingleton.GetRequiredService <IMessageService>(); messageService.ShowError("Error removing " + addInName + ":\n" + ex.Message + "\nThe AddIn will be " + "removed on the next start of " + messageService.ProductName + " and is disabled for now."); return(false); } } return(true); }
/// <summary> /// Allow special syntax to retrieve property values: /// ${property:PropertyName} /// ${property:PropertyName??DefaultValue} /// ${property:ContainerName/PropertyName} /// ${property:ContainerName/PropertyName??DefaultValue} /// A container is a Properties instance stored in the PropertyService. This is /// used by many AddIns to group all their properties into one container. /// </summary> static string GetProperty(string propertyName) { string defaultValue = ""; int pos = propertyName.LastIndexOf("??", StringComparison.Ordinal); if (pos >= 0) { defaultValue = propertyName.Substring(pos + 2); propertyName = propertyName.Substring(0, pos); } Properties properties = ServiceSingleton.GetRequiredService <IPropertyService>().MainPropertiesContainer; pos = propertyName.IndexOf('/'); while (pos >= 0) { properties = properties.NestedProperties(propertyName.Substring(0, pos)); propertyName = propertyName.Substring(pos + 1); pos = propertyName.IndexOf('/'); } return(properties.Get(propertyName, defaultValue)); }
/// <summary> /// Initializes the AddIn system. /// This loads the AddIns that were added to the list, /// then it executes the <see cref="ICommand">commands</see> /// in <c>/SharpDevelop/Autostart</c>. /// </summary> public void RunInitialization() { addInTree.Load(addInFiles, disabledAddIns); // perform service registration var container = (IServiceContainer)ServiceSingleton.ServiceProvider.GetService(typeof(IServiceContainer)); if (container != null) { addInTree.BuildItems <object>("/SharpDevelop/Services", container, false); } // run workspace autostart commands LoggingService.Info("Running autostart commands..."); foreach (ICommand command in addInTree.BuildItems <ICommand>("/SharpDevelop/Autostart", null, false)) { try { command.Execute(null); } catch (Exception ex) { // allow startup to continue if some commands fail ServiceSingleton.GetRequiredService <IMessageService>().ShowException(ex); } } }
public static AddInTreeNode GetTreeNode(string path, bool throwOnNotFound = true) { var addInTree = ServiceSingleton.GetRequiredService <IAddInTree>(); return(addInTree.GetTreeNode(path, throwOnNotFound)); }
public static List <T> BuildItems <T>(string path, object parameter, bool throwOnNotFound = true) { var addInTree = ServiceSingleton.GetRequiredService <IAddInTree>(); return(addInTree.BuildItems <T>(path, parameter, throwOnNotFound).ToList()); }
/// <summary> /// Evaluates a property using the StringParser. Equivalent to StringParser.Parse("${" + propertyName + "}"); /// </summary> public static string GetValue(string propertyName, params StringTagPair[] customTags) { if (propertyName == null) { throw new ArgumentNullException("propertyName"); } if (propertyName == "$") { return("$"); } if (customTags != null) { foreach (StringTagPair pair in customTags) { if (propertyName.Equals(pair.Tag, StringComparison.OrdinalIgnoreCase)) { return(pair.Value); } } } int k = propertyName.IndexOf(':'); if (k <= 0) { // it's property without prefix if (propertyName.Equals("DATE", StringComparison.OrdinalIgnoreCase)) { return(DateTime.Today.ToShortDateString()); } if (propertyName.Equals("TIME", StringComparison.OrdinalIgnoreCase)) { return(DateTime.Now.ToShortTimeString()); } if (propertyName.Equals("ProductName", StringComparison.OrdinalIgnoreCase)) { return(ServiceSingleton.GetRequiredService <IMessageService>().ProductName); } if (propertyName.Equals("GUID", StringComparison.OrdinalIgnoreCase)) { return(Guid.NewGuid().ToString().ToUpperInvariant()); } if (propertyName.Equals("USER", StringComparison.OrdinalIgnoreCase)) { return(Environment.UserName); } if (propertyName.Equals("Version", StringComparison.OrdinalIgnoreCase)) { return(RevisionClass.FullVersion); } if (propertyName.Equals("CONFIGDIRECTORY", StringComparison.OrdinalIgnoreCase)) { return(ServiceSingleton.GetRequiredService <IPropertyService>().ConfigDirectory); } foreach (IStringTagProvider provider in stringTagProviders) { string result = provider.ProvideString(propertyName, customTags); if (result != null) { return(result); } } return(null); } else { // it's a prefixed property // res: properties are quite common, so optimize by testing for them first // before allocaing the prefix/propertyName strings // All other prefixed properties {prefix:Key} shoulg get handled in the switch below. if (propertyName.StartsWith("res:", StringComparison.OrdinalIgnoreCase)) { var resourceService = (IResourceService)ServiceSingleton.ServiceProvider.GetService(typeof(IResourceService)); if (resourceService == null) { return(null); } try { return(Parse(resourceService.GetString(propertyName.Substring(4)), customTags)); } catch (ResourceNotFoundException) { return(null); } } string prefix = propertyName.Substring(0, k); propertyName = propertyName.Substring(k + 1); switch (prefix.ToUpperInvariant()) { case "SDKTOOLPATH": return(FileUtility.GetSdkPath(propertyName)); case "ADDINPATH": foreach (var addIn in ServiceSingleton.GetRequiredService <IAddInTree>().AddIns) { if (addIn.Manifest.Identities.ContainsKey(propertyName)) { return(System.IO.Path.GetDirectoryName(addIn.FileName)); } } return(null); case "DATE": try { return(DateTime.Now.ToString(propertyName, CultureInfo.CurrentCulture)); } catch (Exception ex) { return(ex.Message); } case "ENV": return(Environment.GetEnvironmentVariable(propertyName)); case "PROPERTY": return(GetProperty(propertyName)); default: IStringTagProvider provider; if (prefixedStringTagProviders.TryGetValue(prefix, out provider)) { return(provider.ProvideString(propertyName, customTags)); } else { return(null); } } } }
protected virtual void ShowError(string message) { ServiceSingleton.GetRequiredService <IMessageService>().ShowError(message); }