Exemple #1
0
 public StartupProjectSwitcher(DropdownService dropdownService, DTE dte, IVsFileChangeEx fileChangeService, SwitchStartupProjectPackage.ActivityLogger logger)
 {
     logger.LogInfo("Entering constructor of StartupProjectSwitcher");
     this.dropdownService = dropdownService;
     dropdownService.OnListItemSelected      = _ChangeStartupProject;
     dropdownService.OnConfigurationSelected = _ShowMsgOpenSolution;
     this.dte = dte;
     this.fileChangeService = fileChangeService;
     this.logger            = logger;
 }
Exemple #2
0
        public Configuration Load()
        {
            logger.LogInfo("Loading configuration for solution");
            if (!_ConfigurationFileExists())
            {
                logger.LogInfo("No configuration file found, using default configuration.");
                return(_GetDefaultConfiguration());
            }

            JObject settings = null;

            try
            {
                settings = JObject.Parse(File.ReadAllText(configurationFilename));
            }
            catch (Exception e)
            {
                MessageBox.Show("Could not parse configuration file.\nPlease check the syntax of your configuration file!\nUsing default configuration instead.\n\nError:\n" + e.ToString(), "SwitchStartupProject Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                logger.LogError("Error while parsing configuration file:");
                logger.LogError(e.ToString());
                logger.LogInfo("Using default configuration.");
                return(_GetDefaultConfiguration());
            }
            JsonSchema4 schema = null;

            try
            {
                var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("LucidConcepts.SwitchStartupProject.Configuration.ConfigurationSchema.json");
                using (var reader = new StreamReader(stream))
                {
                    schema = JsonSchema4.FromJson(reader.ReadToEnd());
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Could not parse schema.\nError:\n" + e.ToString(), "SwitchStartupProject Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                logger.LogError("Error while parsing schema:");
                logger.LogError(e.ToString());
                logger.LogInfo("Using default configuration.");
                return(_GetDefaultConfiguration());
            }

            var validationErrors = schema.Validate(settings);

            if (validationErrors.Any())
            {
                var messages = string.Join("\n", validationErrors.Select(err => string.Format("{0}: {1}", err.Path, err.Kind)));
                MessageBox.Show("Could not validate schema of configuration file.\nPlease check your configuration file!\nUsing default configuration instead.\n\nErrors:\n" + messages, "SwitchStartupProject Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                logger.LogError("Could not validate schema of configuration file.");
                logger.LogInfo("Using default configuration.");
                return(_GetDefaultConfiguration());
            }

            var version = _GetVersion(settings);

            if (version != knownVersion)
            {
                MessageBox.Show("Configuration file has unknown version " + version + ".\nVersion should be " + knownVersion + ".\nUsing default configuration instead.", "SwitchStartupProject Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                logger.LogError("Unknown configuration version " + version);
                logger.LogInfo("Using default configuration.");
                return(_GetDefaultConfiguration());
            }

            var listAllProjects            = _GetListAllProjects(settings);
            var multiProjectConfigurations = _GetMultiProjectConfigurations(settings);

            return(new Configuration(listAllProjects, multiProjectConfigurations.ToList()));
        }
Exemple #3
0
        /// <summary>
        /// Update the selection in the dropdown box when a new startup project was set
        /// </summary>
        public void UpdateStartupProject()
        {
            // When startup project is set through dropdown, don't do anything
            if (!reactToChangedEvent)
            {
                return;
            }
            // Don't react to startup project changes while opening the solution or when multi-project configurations have not yet been loaded.
            if (solution.IsOpening)
            {
                return;
            }

            // When startup project is set in solution explorer, update combobox
            var newStartupProjects = dte.Solution.SolutionBuild.StartupProjects as object[];

            if (newStartupProjects == null)
            {
                return;
            }

            var newStartupProjectPaths = newStartupProjects.Cast <string>().ToArray();
            var currentConfig          = _GetCurrentlyActiveConfiguration(newStartupProjectPaths);
            var sortedCurrentProjects  = _SortedProjects(currentConfig.Projects);

            // First try to find a matching multi-project dropdown entry
            var bestMatch = (from dropdownEntry in dropdownService.DropdownList
                             let multiProjectDropdownEntry = dropdownEntry as MultiProjectDropdownEntry
                                                             where multiProjectDropdownEntry != null
                                                             let startupConfig = multiProjectDropdownEntry.Configuration
                                                                                 let score = _EqualityScore(_SortedProjects(startupConfig.Projects), sortedCurrentProjects)
                                                                                             where score >= 0.0
                                                                                             orderby score descending
                                                                                             select multiProjectDropdownEntry)
                            .FirstOrDefault() as IDropdownEntry;

            // Then try to find a matching single-project dropdown entry (if feasible)
            if (bestMatch == null && newStartupProjectPaths.Length == 1)
            {
                var startupProjectPath = (string)newStartupProjectPaths.GetValue(0);
                bestMatch = (from dropdownEntry in dropdownService.DropdownList
                             let singleProjectDropdownEntry = dropdownEntry as SingleProjectDropdownEntry
                                                              where singleProjectDropdownEntry != null
                                                              where singleProjectDropdownEntry.Project.Path == startupProjectPath
                                                              select singleProjectDropdownEntry)
                            .FirstOrDefault();
            }
            if (bestMatch != null)
            {
                logger.LogInfo("New startup configuration was activated outside of dropdown: {0}", bestMatch.DisplayName);
                dropdownService.CurrentDropdownValue = bestMatch;
                return;
            }

            logger.LogInfo("Unknown startup configuration was activated outside of dropdown");
            dropdownService.CurrentDropdownValue = DropdownService.OtherItem;
        }