Skip to content

Commit d5c437a

Browse files
Merge pull request #9 from FriedrichWeinmann/development
1.1.5
2 parents a397e4f + 34e923e commit d5c437a

11 files changed

+690
-196
lines changed

changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.1.5 (2024-03-11)
4+
5+
+ Upd: Set-String - added `-Case` parameter to allow setting the string casing
6+
+ Fix: Add-String - throws when providing an empty array as input
7+
38
## 1.1.3 (2023-01-20)
49

510
+ Upd: Set-String - now accepts file info objects and replaces their content

docs/Set-String.md

+27-20
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,18 @@ Implements string replacement in the pipeline.
1212

1313
## SYNTAX
1414

15-
### regexString (Default)
15+
### regex (Default)
1616
```
17-
Set-String [-OldValue] <String> [[-NewValue] <Object>] [-Options <RegexOptions>]
18-
[-InputFile <FileSystemInfo[]>] [-InputString <String[]>] [-Force] [<CommonParameters>]
17+
Set-String [-OldValue] <String> [[-NewValue] <Object>] [-Options <RegexOptions>] [-Case <Case>]
18+
[-InputFile <FileSystemInfo[]>] [-InputString <String[]>] [-Force]
19+
[<CommonParameters>]
1920
```
2021

21-
### simpleString
22+
### simple
2223
```
23-
Set-String [-OldValue] <String> [[-NewValue] <Object>] [-DoNotUseRegex] [-InputFile <FileSystemInfo[]>]
24-
[-InputString <String[]>] [-Force] [<CommonParameters>]
25-
```
26-
27-
### simpleFile
28-
```
29-
Set-String [-OldValue] <String> [[-NewValue] <Object>] [-DoNotUseRegex] [-InputFile <FileSystemInfo[]>]
30-
[-InputString <String[]>] [-Force] [<CommonParameters>]
31-
```
32-
33-
### regexFile
34-
```
35-
Set-String [-OldValue] <String> [[-NewValue] <Object>] [-Options <RegexOptions>]
36-
[-InputFile <FileSystemInfo[]>] [-InputString <String[]>] [-Force] [<CommonParameters>]
24+
Set-String [-OldValue] <String> [[-NewValue] <Object>] [-DoNotUseRegex] [-Case <Case>]
25+
[-InputFile <FileSystemInfo[]>] [-InputString <String[]>] [-Force]
26+
[<CommonParameters>]
3727
```
3828

3929
## DESCRIPTION
@@ -100,7 +90,7 @@ Useful when trying to not do escapes and not depending on regex.
10090

10191
```yaml
10292
Type: SwitchParameter
103-
Parameter Sets: simpleString, simpleFile
93+
Parameter Sets: simple
10494
Aliases: simple
10595

10696
Required: False
@@ -163,7 +153,7 @@ Defaults to IgnoreCase, enabling the default PowerShell -replace behavior.
163153
164154
```yaml
165155
Type: RegexOptions
166-
Parameter Sets: regexString, regexFile
156+
Parameter Sets: regex
167157
Aliases:
168158
Accepted values: None, IgnoreCase, Multiline, ExplicitCapture, Compiled, Singleline, IgnorePatternWhitespace, RightToLeft, ECMAScript, CultureInvariant
169159

@@ -174,6 +164,23 @@ Accept pipeline input: False
174164
Accept wildcard characters: False
175165
```
176166
167+
### -Case
168+
What case to set the values provided to.
169+
Can be Lower, Upper or Title.
170+
Note: Be careful about using this parameter with files, as it will happily change the case of all text in a file.
171+
172+
```yaml
173+
Type: Case
174+
Parameter Sets: (All)
175+
Aliases:
176+
177+
Required: False
178+
Position: Named
179+
Default value: None
180+
Accept pipeline input: False
181+
Accept wildcard characters: False
182+
```
183+
177184
### -Force
178185
When replacing text in files, common binary and media files will be ignored.
179186
This detection is based on extension of the file.

src/StringModule/Case.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace StringModule
6+
{
7+
/// <summary>
8+
/// The different string casings
9+
/// </summary>
10+
public enum Case
11+
{
12+
/// <summary>
13+
/// No case preference was specified
14+
/// </summary>
15+
Unspecified = 1,
16+
17+
/// <summary>
18+
/// All letters should be converted to uppercase
19+
/// </summary>
20+
Upper = 2,
21+
22+
/// <summary>
23+
/// All letters should be converted to lowercase
24+
/// </summary>
25+
Lower = 3,
26+
27+
/// <summary>
28+
/// Only the first letter of a word should be uppercase
29+
/// </summary>
30+
Title = 4
31+
}
32+
}

src/StringModule/Commands/AddStringCommand.cs

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public sealed class AddStringCommand : PSCmdlet
5757
/// </summary>
5858
protected override void ProcessRecord()
5959
{
60+
if (InputString == null)
61+
return;
62+
6063
foreach (string line in InputString)
6164
if (ParameterSetName == "wrap")
6265
WriteObject(String.Format("{0}{1}{2}", Before, line, Behind));

src/StringModule/Commands/SetStringCommand.cs

+37-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.IO;
34
using System.Linq;
45
using System.Management.Automation;
@@ -42,6 +43,12 @@ public class SetStringCommand : PSCmdlet
4243
[Parameter(ParameterSetName = "regex")]
4344
public RegexOptions Options = RegexOptions.IgnoreCase;
4445

46+
/// <summary>
47+
/// What case should be applied
48+
/// </summary>
49+
[Parameter()]
50+
public Case Case = Case.Unspecified;
51+
4552
/// <summary>
4653
/// The files to replace in
4754
/// </summary>
@@ -119,12 +126,7 @@ protected override void ProcessRecord()
119126

120127
foreach (string item in InputString)
121128
{
122-
if (DoNotUseRegex.ToBool())
123-
WriteObject(item.Replace(OldValue, StringValue));
124-
else if (ScriptBlockValue != null)
125-
WriteObject(Regex.Replace(item, OldValue, ScriptBlockEvaluator, Options));
126-
else
127-
WriteObject(Regex.Replace(item, OldValue, StringValue, Options));
129+
WriteObject(Transform(item));
128130
}
129131

130132
if (InputFile == null)
@@ -149,12 +151,7 @@ protected override void ProcessRecord()
149151
continue;
150152
}
151153

152-
if (DoNotUseRegex.ToBool())
153-
content = content.Replace(OldValue, StringValue);
154-
else if (ScriptBlockValue != null)
155-
content = Regex.Replace(content, OldValue, ScriptBlockEvaluator, Options);
156-
else
157-
content = Regex.Replace(content, OldValue, StringValue, Options);
154+
content = Transform(content);
158155

159156
try { File.WriteAllText(info.FullName, content, GetFileEncoding(info.FullName)); }
160157
catch (Exception e)
@@ -168,6 +165,34 @@ protected override void ProcessRecord()
168165
#endregion Methods
169166

170167
#region Utility
168+
/// <summary>
169+
/// Centralizes the input conversion, respecting the parameters specified
170+
/// </summary>
171+
/// <param name="Value">The value to convert</param>
172+
/// <returns>The converted value</returns>
173+
private string Transform(string Value)
174+
{
175+
string transformed = Value;
176+
if (DoNotUseRegex.ToBool())
177+
transformed = Value.Replace(OldValue, StringValue);
178+
else if (ScriptBlockValue != null)
179+
transformed = Regex.Replace(transformed, OldValue, ScriptBlockEvaluator, Options);
180+
else
181+
transformed = Regex.Replace(transformed, OldValue, StringValue, Options);
182+
183+
if (Case == Case.Unspecified)
184+
return transformed;
185+
186+
if (Case == Case.Lower)
187+
return transformed.ToLower();
188+
if (Case == Case.Upper)
189+
return transformed.ToUpper();
190+
if (Case == Case.Title)
191+
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(transformed);
192+
193+
return transformed;
194+
}
195+
171196
/// <summary>
172197
/// Returns the assumeed encoding of the file specified, defaults to UTF8.
173198
/// </summary>

string/StringModule.dll

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)