-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathPSScriptContents.cs
More file actions
122 lines (101 loc) · 4.21 KB
/
PSScriptContents.cs
File metadata and controls
122 lines (101 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
namespace Microsoft.PowerShell.PSResourceGet.UtilClasses
{
/// <summary>
/// This class contains information for a PSScriptFileInfo (representing a .ps1 file contents).
/// </summary>
public sealed class PSScriptContents
{
#region Properties
/// <summary>
/// End of file contents for the .ps1 file.
/// </summary>
public string[] ScriptContents { get; private set; } = Utils.EmptyStrArray;
/// <summary>
/// End of file contents for the .ps1 file.
/// </summary>
public bool ContainsSignature { get; set; } = false;
#endregion
#region Private Members
private const string signatureStartString = "# SIG # Begin signature block";
private int _signatureStartIndex = -1;
#endregion
#region Constructor
/// <summary>
/// This constructor takes end of file contents as a string and checks if it has a signature.
/// </summary>
public PSScriptContents(string[] endOfFileContents)
{
ScriptContents = endOfFileContents;
ContainsSignature = CheckForSignature();
}
/// <summary>
/// This constructor creates a PSScriptContents instance with default values for its properties.
/// The calling method, like PSScriptContents.ParseContent() could then populate the properties.
/// </summary>
internal PSScriptContents() {}
#endregion
#region Internal Methods
/// <summary>
/// Parses end of file contents as a string from the file lines passed in
/// and sets property indicating whether those contents contain a signature.
/// </summary>
internal void ParseContent(string[] commentLines)
{
if (commentLines.Length != 0)
{
ScriptContents = commentLines;
ContainsSignature = CheckForSignature();
}
}
/// <summary>
/// This function is called by PSScriptFileInfo.TryCreateScriptFileInfoString(),
/// by the New-PSScriptFileInfo cmdlet (in which case EndOfFileContents is an empty string so there's no signature that'll get removed)
/// or by Update-PSScriptFileInfo cmdlet (in which case EndOfFileContents may not be empty and may contain a signature.
/// When emitting contents, any file signature is always removed because it is invalidated when the content is updated.
/// </summary>
internal string[] EmitContent()
{
RemoveSignatureString();
return ScriptContents;
}
#endregion
#region Private Methods
/// <summary>
/// Checks if the end of file contents contain a signature.
/// </summary>
private bool CheckForSignature()
{
for (int i = 0; i < ScriptContents.Length; i++)
{
if (String.Equals(ScriptContents[i], signatureStartString, StringComparison.InvariantCultureIgnoreCase))
{
_signatureStartIndex = i;
break;
}
}
return _signatureStartIndex != -1;
}
/// <summary>
/// Removes the signature from EndOfFileContents property
/// as the signature would be invalidated during update.
/// </summary>
private void RemoveSignatureString()
{
if (ContainsSignature)
{
// The script signature comment block always appears at the end of the script file,
// so its start location becomes the end of the content section after the signature
// comment block is removed, and is also the length of the content section minus the
// signature block.
string[] contentsWithoutSignature = new string[_signatureStartIndex];
Array.Copy(ScriptContents, contentsWithoutSignature, _signatureStartIndex);
ScriptContents = contentsWithoutSignature;
ContainsSignature = false;
}
}
#endregion
}
}