mirror of https://github.com/winsw/winsw
Add changes for modifying environment values
- environment value can be modified by serveral rules - documentation addedpull/1075/head
parent
6cf303c1d3
commit
ff9f7dc2e3
|
@ -16,7 +16,9 @@ Example:
|
|||
<id>jenkins</id>
|
||||
<name>Jenkins</name>
|
||||
<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>
|
||||
<arguments>-Xrs -Xmx256m -jar "%BASE%\jenkins.war" --httpPort=8080</arguments>
|
||||
<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" />
|
||||
```
|
||||
|
||||
#### 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
|
||||
|
||||
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.
|
||||
|
|
|
@ -171,6 +171,10 @@ env:
|
|||
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
|
||||
|
||||
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.
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Diagnostics;
|
|||
using System.IO;
|
||||
using System.ServiceProcess;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Xml;
|
||||
using WinSW.Configuration;
|
||||
using WinSW.Native;
|
||||
|
@ -682,6 +683,25 @@ namespace WinSW
|
|||
var node = nodeList[i]!;
|
||||
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"));
|
||||
|
||||
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.SetEnvironmentVariable(key, value);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
@ -6,6 +6,7 @@ using System.IO;
|
|||
using System.Runtime.CompilerServices;
|
||||
#endif
|
||||
using System.ServiceProcess;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Xml;
|
||||
using WinSW.Native;
|
||||
using WinSW.Util;
|
||||
|
@ -219,6 +220,17 @@ namespace WinSW.Configuration
|
|||
string key = item.Name;
|
||||
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;
|
||||
Environment.SetEnvironmentVariable(key, value);
|
||||
}
|
||||
|
@ -424,6 +436,13 @@ namespace WinSW.Configuration
|
|||
{
|
||||
public string? Name;
|
||||
public string? Value;
|
||||
public List<RawYamlRule>? Rule;
|
||||
}
|
||||
|
||||
internal sealed class RawYamlRule
|
||||
{
|
||||
public string? Regex;
|
||||
public string? Replace;
|
||||
}
|
||||
|
||||
internal sealed class YamlFailureAction
|
||||
|
|
Loading…
Reference in New Issue