/// <summary> /// Applies an <see cref="EnvironmentBinding"/> by modifying environment variables. /// </summary> /// <param name="binding">The binding to apply.</param> /// <param name="implementation">The implementation to be made available.</param> /// <param name="startInfo">The process launch environment to apply the binding to.</param> /// <exception cref="ExecutorException"><see cref="EnvironmentBinding.Name"/> or other data is invalid.</exception> private void ApplyEnvironmentBinding(EnvironmentBinding binding, ImplementationSelection implementation, ProcessStartInfo startInfo) { Log.Debug("Applying " + binding + " for " + implementation); if (string.IsNullOrEmpty(binding.Name)) { throw new ExecutorException(string.Format(Resources.MissingBindingName, @"<environment>")); } var environmentVariables = startInfo.EnvironmentVariables; string newValue; if (binding.Value != null && binding.Insert != null) { // Conflict throw new ExecutorException(Resources.EnvironmentBindingValueInvalid); } else if (binding.Value != null) { // Static value newValue = binding.Value; } else { // Path inside the implementation newValue = Path.Combine(GetImplementationPath(implementation), FileUtils.UnifySlashes(binding.Insert ?? "")); } // Set the default value if the variable is not already set on the system if (!environmentVariables.ContainsKey(binding.Name)) { environmentVariables.Add(binding.Name, binding.Default); } string previousValue = environmentVariables[binding.Name]; string separator = (string.IsNullOrEmpty(binding.Separator) ? Path.PathSeparator.ToString(CultureInfo.InvariantCulture) : binding.Separator); switch (binding.Mode) { default: case EnvironmentMode.Prepend: environmentVariables[binding.Name] = string.IsNullOrEmpty(previousValue) // No exisiting value, just set new one ? newValue // Prepend new value to existing one seperated by path separator : newValue + separator + environmentVariables[binding.Name]; break; case EnvironmentMode.Append: environmentVariables[binding.Name] = string.IsNullOrEmpty(previousValue) // No exisiting value, just set new one ? newValue // Append new value to existing one seperated by path separator : environmentVariables[binding.Name] + separator + newValue; break; case EnvironmentMode.Replace: // Overwrite any existing value environmentVariables[binding.Name] = newValue; break; } }
/// <summary> /// Applies an <see cref="EnvironmentBinding"/> by modifying environment variables. /// </summary> /// <param name="binding">The binding to apply.</param> /// <param name="implementation">The implementation to be made available.</param> /// <exception cref="ExecutorException"><see cref="EnvironmentBinding.Name"/> or other data is invalid.</exception> private void ApplyEnvironmentBinding(EnvironmentBinding binding, ImplementationSelection implementation) { Log.Debug("Applying " + binding + " for " + implementation); if (string.IsNullOrEmpty(binding.Name)) { throw new ExecutorException(string.Format(Resources.MissingBindingName, @"<environment>")); } var environmentVariables = _startInfo.EnvironmentVariables; string newValue; if (binding.Value != null && binding.Insert != null) { // Conflict throw new ExecutorException(Resources.EnvironmentBindingValueInvalid); } else if (binding.Value != null) { // Static value newValue = binding.Value; } else { // Path inside the implementation newValue = Path.Combine(_implementationStore.GetPath(implementation), FileUtils.UnifySlashes(binding.Insert ?? "")); } // Set the default value if the variable is not already set on the system if (!environmentVariables.ContainsKey(binding.Name)) { environmentVariables.Add(binding.Name, binding.Default); } string previousValue = environmentVariables[binding.Name]; string separator = (string.IsNullOrEmpty(binding.Separator) ? Path.PathSeparator.ToString(CultureInfo.InvariantCulture) : binding.Separator); environmentVariables[binding.Name] = binding.Mode switch { _ when string.IsNullOrEmpty(previousValue) => newValue, EnvironmentMode.Replace => newValue, EnvironmentMode.Prepend => newValue + separator + environmentVariables[binding.Name], EnvironmentMode.Append => environmentVariables[binding.Name] + separator + newValue, _ => throw new InvalidOperationException($"Unknown {nameof(EnvironmentBinding)} value: {binding.Mode}") }; }
/// <summary> /// Applies an <see cref="EnvironmentBinding"/> by modifying environment variables. /// </summary> /// <param name="binding">The binding to apply.</param> /// <param name="implementation">The implementation to be made available.</param> /// <param name="startInfo">The process launch environment to apply the binding to.</param> /// <exception cref="ExecutorException"><see cref="EnvironmentBinding.Name"/> or other data is invalid.</exception> private void ApplyEnvironmentBinding(EnvironmentBinding binding, ImplementationSelection implementation, ProcessStartInfo startInfo) { Log.Debug("Applying " + binding + " for " + implementation); if (string.IsNullOrEmpty(binding.Name)) throw new ExecutorException(string.Format(Resources.MissingBindingName, @"<environment>")); var environmentVariables = startInfo.EnvironmentVariables; string newValue; if (binding.Value != null && binding.Insert != null) { // Conflict throw new ExecutorException(Resources.EnvironmentBindingValueInvalid); } else if (binding.Value != null) { // Static value newValue = binding.Value; } else { // Path inside the implementation newValue = Path.Combine(GetImplementationPath(implementation), FileUtils.UnifySlashes(binding.Insert ?? "")); } // Set the default value if the variable is not already set on the system if (!environmentVariables.ContainsKey(binding.Name)) environmentVariables.Add(binding.Name, binding.Default); string previousValue = environmentVariables[binding.Name]; string separator = (string.IsNullOrEmpty(binding.Separator) ? Path.PathSeparator.ToString(CultureInfo.InvariantCulture) : binding.Separator); switch (binding.Mode) { default: case EnvironmentMode.Prepend: environmentVariables[binding.Name] = string.IsNullOrEmpty(previousValue) // No exisiting value, just set new one ? newValue // Prepend new value to existing one seperated by path separator : newValue + separator + environmentVariables[binding.Name]; break; case EnvironmentMode.Append: environmentVariables[binding.Name] = string.IsNullOrEmpty(previousValue) // No exisiting value, just set new one ? newValue // Append new value to existing one seperated by path separator : environmentVariables[binding.Name] + separator + newValue; break; case EnvironmentMode.Replace: // Overwrite any existing value environmentVariables[binding.Name] = newValue; break; } }