Add changes for modifying environment values

- environment value can be modified by serveral rules
- documentation added
pull/1075/head
Walter, Markus (DI PA DCP R&D 5) 2024-01-08 09:16:48 +01:00
parent 6cf303c1d3
commit ff9f7dc2e3
4 changed files with 57 additions and 2 deletions

View File

@ -16,7 +16,9 @@ Example:
<id>jenkins</id> <id>jenkins</id>
<name>Jenkins</name> <name>Jenkins</name>
<description>This service runs Jenkins continuous integration system.</description> <description>This service runs Jenkins continuous integration system.</description>
<env name="JENKINS_HOME" value="%BASE%"/> <env name="JENKINS_HOME" value="%BASE%">
<rule regex="\\" replace="\/"/>
</env>
<executable>java</executable> <executable>java</executable>
<arguments>-Xrs -Xmx256m -jar "%BASE%\jenkins.war" --httpPort=8080</arguments> <arguments>-Xrs -Xmx256m -jar "%BASE%\jenkins.war" --httpPort=8080</arguments>
<log mode="roll"></log> <log mode="roll"></log>
@ -139,6 +141,16 @@ This optional element can be specified multiple times if necessary to specify en
<env name="HOME" value="c:\abc" /> <env name="HOME" value="c:\abc" />
``` ```
#### Rule
This optional element can be specified multiple time within element "env". The syntax is:
```xml
<rule regex="\\" value="\/"
```
With the help of a rule replacements in a environment variable can be done after expansion. That is helpful e.g. when working with Linux tools on Windows and Windows environment variables are used in "env" for paths to change back slashes to forward slashes and mask white spaces.
### interactive ### interactive
If this optional element is specified, the service will be allowed to interact with the desktop, such as by showing a new window and dialog boxes. If this optional element is specified, the service will be allowed to interact with the desktop, such as by showing a new window and dialog boxes.

View File

@ -171,6 +171,10 @@ env:
value: host1;host2 value: host1;host2
``` ```
#### Rule
This optional element can be specified multiple time within element "env". With the help of a rule replacements in a environment variable can be done after expansion. That is helpful e.g. when working with Linux tools on Windows and Windows environment variables are used in "env" for paths to change back slashes to forward slashes and mask white spaces.
### interactive ### interactive
If this optional element is specified, the service will be allowed to interact with the desktop, such as by showing a new window and dialog boxes. If this optional element is specified, the service will be allowed to interact with the desktop, such as by showing a new window and dialog boxes.

View File

@ -4,6 +4,7 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.ServiceProcess; using System.ServiceProcess;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Xml; using System.Xml;
using WinSW.Configuration; using WinSW.Configuration;
using WinSW.Native; using WinSW.Native;
@ -682,6 +683,25 @@ namespace WinSW
var node = nodeList[i]!; var node = nodeList[i]!;
string key = node.Attributes!["name"]?.Value ?? throw new InvalidDataException("'name' is missing"); string key = node.Attributes!["name"]?.Value ?? throw new InvalidDataException("'name' is missing");
string value = Environment.ExpandEnvironmentVariables(node.Attributes["value"]?.Value ?? throw new InvalidDataException("'value' is missing")); string value = Environment.ExpandEnvironmentVariables(node.Attributes["value"]?.Value ?? throw new InvalidDataException("'value' is missing"));
if (node.HasChildNodes)
{
var ruleList = node.SelectNodes("rule")!;
for (int ruleNumber = 0; ruleNumber < ruleList.Count; ruleNumber++)
{
var rule = ruleList[ruleNumber]!;
string regularExpression = rule.Attributes!["regex"]!.Value;
string replacement = rule.Attributes!["replace"]!.Value;
if (!string.IsNullOrEmpty(regularExpression) && !string.IsNullOrEmpty(replacement))
{
value = Regex.Replace(value, regularExpression, replacement);
}
}
}
environment[key] = value; environment[key] = value;
Environment.SetEnvironmentVariable(key, value); Environment.SetEnvironmentVariable(key, value);

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
@ -6,6 +6,7 @@ using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
#endif #endif
using System.ServiceProcess; using System.ServiceProcess;
using System.Text.RegularExpressions;
using System.Xml; using System.Xml;
using WinSW.Native; using WinSW.Native;
using WinSW.Util; using WinSW.Util;
@ -219,6 +220,17 @@ namespace WinSW.Configuration
string key = item.Name; string key = item.Name;
string value = Environment.ExpandEnvironmentVariables(item.Value); string value = Environment.ExpandEnvironmentVariables(item.Value);
foreach (var ruleItem in item.Rule!)
{
string regularExpression = ruleItem.Regex!;
string replacement = ruleItem.Replace!;
if (!string.IsNullOrEmpty(regularExpression) && !string.IsNullOrEmpty(replacement))
{
value = Regex.Replace(value, regularExpression, replacement);
}
}
this.EnvironmentVariables[key] = value; this.EnvironmentVariables[key] = value;
Environment.SetEnvironmentVariable(key, value); Environment.SetEnvironmentVariable(key, value);
} }
@ -424,6 +436,13 @@ namespace WinSW.Configuration
{ {
public string? Name; public string? Name;
public string? Value; public string? Value;
public List<RawYamlRule>? Rule;
}
internal sealed class RawYamlRule
{
public string? Regex;
public string? Replace;
} }
internal sealed class YamlFailureAction internal sealed class YamlFailureAction