/// <summary>
        /// Adds the given profile to the list and saves to disk. If a profile with the same
        /// name exists (case sensitive), it will be replaced with the new profile. If addToFront is
        /// true the profile will be the first one in the list. This is useful since quite often callers want
        /// their just added profile to be listed first in the start menu. If addToFront is false but there is
        /// an existing profile, the new one will be inserted at the same location rather than at the end.
        /// </summary>
        public Task AddOrUpdateProfileAsync(ILaunchProfile profile, bool addToFront)
        {
            // Updates need to be sequenced
            return(_sequentialTaskQueue.ExecuteTask(async() =>
            {
                ILaunchSettings currentSettings = await GetSnapshotThrowIfErrors();
                ILaunchProfile?existingProfile = null;
                int insertionIndex = 0;
                foreach (ILaunchProfile p in currentSettings.Profiles)
                {
                    if (LaunchProfile.IsSameProfileName(p.Name, profile.Name))
                    {
                        existingProfile = p;
                        break;
                    }
                    insertionIndex++;
                }

                ImmutableList <ILaunchProfile> profiles;
                if (existingProfile != null)
                {
                    profiles = currentSettings.Profiles.Remove(existingProfile);
                }
                else
                {
                    profiles = currentSettings.Profiles;
                }

                if (addToFront)
                {
                    profiles = profiles.Insert(0, new LaunchProfile(profile));
                }
                else
                {
                    // Insertion index will be set to the current count (end of list) if an existing item was not found otherwise
                    // it will point to where the previous one was found
                    profiles = profiles.Insert(insertionIndex, new LaunchProfile(profile));
                }

                // If the new profile is in-memory only, we don't want to touch the disk unless it replaces an existing disk based
                // profile
                bool saveToDisk = !profile.IsInMemoryObject() || (existingProfile?.IsInMemoryObject() == false);

                var newSnapshot = new LaunchSettings(profiles, currentSettings?.GlobalSettings, currentSettings?.ActiveProfile?.Name);
                await UpdateAndSaveSettingsInternalAsync(newSnapshot, saveToDisk);
            }));
        }
        public WritableLaunchProfile(ILaunchProfile profile)
        {
            Name             = profile.Name;
            ExecutablePath   = profile.ExecutablePath;
            CommandName      = profile.CommandName;
            CommandLineArgs  = profile.CommandLineArgs;
            WorkingDirectory = profile.WorkingDirectory;
            LaunchBrowser    = profile.LaunchBrowser;
            LaunchUrl        = profile.LaunchUrl;
            DoNotPersist     = profile.IsInMemoryObject();

            if (profile.EnvironmentVariables != null)
            {
                EnvironmentVariables = new Dictionary <string, string>(profile.EnvironmentVariables, StringComparer.Ordinal);
            }

            if (profile.OtherSettings != null)
            {
                OtherSettings = new Dictionary <string, object>(profile.OtherSettings, StringComparers.LaunchProfileProperties);
            }
        }
        public static LaunchProfile Clone(ILaunchProfile profile)
        {
            // LaunchProfile is immutable and doesn't need to be cloned.
            if (profile is LaunchProfile lp)
            {
                return(lp);
            }

            // Unknown implementation. Make a defensive copy to a new immutable instance.
            return(new LaunchProfile(
                       name: profile.Name,
                       executablePath: profile.ExecutablePath,
                       commandName: profile.CommandName,
                       commandLineArgs: profile.CommandLineArgs,
                       workingDirectory: profile.WorkingDirectory,
                       launchBrowser: profile.LaunchBrowser,
                       launchUrl: profile.LaunchUrl,
                       environmentVariables: profile.FlattenEnvironmentVariables(),
                       otherSettings: profile.FlattenOtherSettings(),
                       doNotPersist: profile.IsInMemoryObject()));
        }
Ejemplo n.º 4
0
 protected static void MergeExistingInMemoryProfiles(LaunchSettingsData newSnapshot, ILaunchSettings prevSnapshot)
 {
     for (int i = 0; i < prevSnapshot.Profiles.Count; i++)
     {
         ILaunchProfile profile = prevSnapshot.Profiles[i];
         if (profile.IsInMemoryObject() && !string.Equals(profile.CommandName, ErrorProfileCommandName))
         {
             // Does it already have one with this name?
             if (newSnapshot.Profiles.FirstOrDefault(p => LaunchProfile.IsSameProfileName(p.Name, profile.Name)) == null)
             {
                 // Create a new one from the existing in-memory profile and insert it in the same location, or the end if it
                 // is beyond the end of the list
                 if (i > newSnapshot.Profiles.Count)
                 {
                     newSnapshot.Profiles.Add(LaunchProfileData.FromILaunchProfile(profile));
                 }
                 else
                 {
                     newSnapshot.Profiles.Insert(i, LaunchProfileData.FromILaunchProfile(profile));
                 }
             }
         }
     }
 }
Ejemplo n.º 5
0
 private static bool ProfileShouldBePersisted(ILaunchProfile profile)
 {
     return(!profile.IsInMemoryObject());
 }