Repository navigation
Expand file tree
/
Copy pathTemplate.cs
More file actions
143 lines (124 loc) · 4.88 KB
/
Copy pathTemplate.cs
File metadata and controls
143 lines (124 loc) · 4.88 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using PSModuleDevelopment.Utility;
namespace PSModuleDevelopment.Template
{
/// <summary>
/// The master class containing a template
/// </summary>
[Serializable]
public class Template
{
/// <summary>
/// The name of the template
/// </summary>
public string Name;
/// <summary>
/// What kind of template is this?
/// </summary>
public TemplateType Type;
/// <summary>
/// What version is the template
/// </summary>
public Version Version;
/// <summary>
/// Text describing the template
/// </summary>
public string Description;
/// <summary>
/// Author of the template
/// </summary>
public string Author;
/// <summary>
/// When was the template originally created?
/// </summary>
public DateTime CreatedOn;
/// <summary>
/// List of tags that have been assigned to the template
/// </summary>
public List<string> Tags = new List<string>();
/// <summary>
/// List of parameters the template accepts
/// </summary>
public List<string> Parameters = new List<string>();
/// <summary>
/// List of scripts that will be invoked on initialization
/// </summary>
public Dictionary<string, ParameterScript> Scripts = new Dictionary<string, ParameterScript>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// List of generation 2 parameters to include
/// </summary>
public Dictionary<string, Parameter.ParameterBase> Parameters2 = new Dictionary<string, Parameter.ParameterBase>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Items in the root directory of the template (which may contain children themselves).
/// </summary>
public List<TemplateItemBase> Children = new List<TemplateItemBase>();
/// <summary>
/// What design generation is the template?
/// </summary>
public int Generation = 1;
/// <summary>
/// Returns the template digest used as index file.
/// </summary>
/// <returns>A TemplateInfo object describing this template.</returns>
public TemplateInfo ToTemplateInfo()
{
List<string> parameters = new List<string>(Parameters);
if (Parameters2.Count > 0)
parameters.AddRange(Parameters2.Values.Where(o => o.GetType().Name == "ParameterPrompt").Select(o => o.Name));
TemplateInfo info = new TemplateInfo();
info.Author = Author;
info.CreatedOn = CreatedOn;
info.Description = Description;
info.Name = Name;
info.Parameters = parameters;
info.Tags = Tags;
info.Type = Type;
info.Version = Version;
info.Generation = Generation;
return info;
}
/// <summary>
/// Create a blank template object
/// </summary>
public Template()
{
}
/// <summary>
/// Create a template object based on a deserialized template object
/// </summary>
/// <param name="Item">The deserialized tample object to restore</param>
/// <exception cref="ArgumentException">If anything at all is not as it should be.</exception>
public Template(PSObject Item)
{
try
{
Name = Item.GetValue<string>("Name");
Type = (TemplateType)Item.GetValue<int>("Type");
Version = Item.GetValue<Version>("Version");
Description = Item.GetValue<string>("Description");
Author = Item.GetValue<string>("Author");
CreatedOn = Item.GetValue<DateTime>("CreatedOn");
foreach (object item in Item.GetValues("Tags"))
Tags.Add((string)item);
foreach (object item in Item.GetValues("Parameters"))
Parameters.Add((string)item);
foreach (KeyValuePair<string, ParameterScript> entry in Item.GetParameterScriptDictionary("Scripts"))
Scripts[entry.Key] =entry.Value;
// Parameters2 not used
foreach (object item in Item.GetValues("Children"))
Children.Add(TemplateHost.GetTemplateItem(item));
Generation = Item.GetValue<int>("Generation");
}
catch (Exception e)
{
throw new ArgumentException($"Cannot convert {Item} of type {Item.BaseObject.GetType().FullName} to type PSModuleDevelopment.Template.Template! {e.Message}", e);
}
}
}
}