Repository navigation
Expand file tree
/
Copy pathPowerShellDataFile.cs
More file actions
161 lines (144 loc) · 4.27 KB
/
Copy pathPowerShellDataFile.cs
File metadata and controls
161 lines (144 loc) · 4.27 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2023 Roger Brown.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.IO;
using System.Management.Automation;
using System.Text;
namespace RhubarbGeekNz.PowerShellDataFile
{
internal class TypeWriter
{
internal readonly Type Type;
internal readonly Action<object, OutputBuffer, int> Writer;
internal TypeWriter(Type t, Action<object, OutputBuffer, int> w)
{
Type = t;
Writer = w;
}
}
internal class OutputBuffer
{
StringBuilder Value;
PSCmdlet Cmdlet;
internal void Flush()
{
if (Value != null)
{
Cmdlet.WriteObject(Value.ToString());
Value = null;
}
}
void Append(string s, int tabs)
{
if (Value == null)
{
Value = new StringBuilder();
while (tabs-- > 0)
{
Value.Append("\t");
}
}
Value.Append(s);
}
internal OutputBuffer(PSCmdlet cmdlet)
{
Cmdlet = cmdlet;
}
static readonly TypeWriter[] TypeWriters =
{
new TypeWriter(typeof(string),(o,e,t)=>{
string s=(string)o;
e.Append("'", t);
if (s.Contains("\'")) s = s.Replace("\'","\'\'");
e.Append(s, t);
e.Append("'", t);
}),
new TypeWriter(typeof(bool),(o,e,t)=>{
e.Append((bool)o ? "$True" : "$False", t);
}),
new TypeWriter(typeof(Int32),(o,e,t)=>{
e.Append(o.ToString(), t);
}),
new TypeWriter(typeof(Double),(o,e,t)=>{
e.Append(o.ToString(), t);
}),
new TypeWriter(typeof(Hashtable),(o,e,t)=>{
e.Append("@{", t);
e.Flush();
foreach (DictionaryEntry i in (Hashtable)o)
{
e.Append((string)i.Key, t+1);
e.Append(" = ", t+1);
e.WriteObject(i.Value,t+1);
e.Flush();
}
e.Append("}", t);
}),
new TypeWriter(typeof(object[]),(o,e,t)=>{
object [] a= (object[])o;
if (a.Length == 0)
{
e.Append("@()", t);
}
else
{
e.Append("@(", t);
e.Flush();
int j = 0;
foreach (var i in a)
{
e.WriteObject(i,t+1);
j++;
if (j < a.Length) e.Append(",", t+1);
e.Flush();
}
e.Append(")", t);
}
})
};
internal void WriteObject(object value, int tabs)
{
if (value == null)
{
Append("$null", tabs);
}
else
{
Type type = value.GetType();
foreach (var typeWriter in TypeWriters)
{
if (typeWriter.Type.IsAssignableFrom(type))
{
typeWriter.Writer(value, this, tabs);
return;
}
}
throw new InvalidDataException(type.FullName);
}
}
}
[Cmdlet("Export", "PowerShellDataFile")]
[OutputType(typeof(string))]
public class ExportPowerShellDataFile : PSCmdlet
{
[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
public Hashtable InputObject { get; set; }
protected override void BeginProcessing()
{
}
protected override void ProcessRecord()
{
OutputBuffer outputBuffer = new OutputBuffer(this);
outputBuffer.WriteObject(InputObject, 0);
outputBuffer.Flush();
}
protected override void EndProcessing()
{
}
}
}