chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using OpenTap.Cli;
using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
 
namespace OpenTap.Sdk.New
{
    [Display("dut", "C# template for a DUT plugin.", Groups: new[] { "sdk", "new" })]
    public class GenerateDut : GenerateType
    {
        [UnnamedCommandLineArgument("name", Required = true, Description = "The name of the new DUT.")]
        public string Name { get; set; }
 
        public override int Execute(CancellationToken cancellationToken)
        {
            if (!Validate(name: Name, allowWhiteSpace: false, allowLeadingNumbers: false, allowAlphaNumericOnly: true))
            {
                return (int)ExitCodes.ArgumentError;
            }
 
            using (var reader = new StreamReader(Assembly.GetExecutingAssembly()
                .GetManifestResourceStream("OpenTap.Sdk.New.Resources.DutTemplate.txt")))
            {
                var content = ReplaceInTemplate(reader.ReadToEnd(), TryGetNamespace(), Name);
                WriteFile(output ?? Path.Combine(WorkingDirectory, Name + ".cs"), content);
            }
 
            return (int)ExitCodes.Success;
 
        }
    }
    [Display("instrument", "C# template for a Instrument plugin.", Groups: new[] { "sdk", "new" })]
    public class GenerateInstrument : GenerateType
    {
        [UnnamedCommandLineArgument("name", Required = true, Description = "The name of the new Instrument.")]
        public string Name { get; set; }
 
        public override int Execute(CancellationToken cancellationToken)
        {
            if (!Validate(name: Name, allowWhiteSpace: false, allowLeadingNumbers: false, allowAlphaNumericOnly: true))
            {
                return (int)ExitCodes.ArgumentError;
            }
 
            using (var reader = new StreamReader(Assembly.GetExecutingAssembly()
                .GetManifestResourceStream("OpenTap.Sdk.New.Resources.InstrumentTemplate.txt")))
            {
                var content = ReplaceInTemplate(reader.ReadToEnd(), TryGetNamespace(), Name);
                WriteFile(output ?? Path.Combine(WorkingDirectory, Name + ".cs"), content);
            }
 
            return (int)ExitCodes.Success;
        }
    }
    [Display("resultlistener", "C# template for a ResultListener plugin.", Groups: new[] { "sdk", "new" })]
    public class GenerateResultListener : GenerateType
    {
        [UnnamedCommandLineArgument("name", Required = true, Description = "The name of the new ResultListener.")]
        public string Name { get; set; }
 
        public override int Execute(CancellationToken cancellationToken)
        {
            if (!Validate(name: Name, allowWhiteSpace: false, allowLeadingNumbers: false, allowAlphaNumericOnly: true))
            {
                return (int)ExitCodes.ArgumentError;
            }
 
            using (var reader = new StreamReader(Assembly.GetExecutingAssembly()
                .GetManifestResourceStream("OpenTap.Sdk.New.Resources.ResultListenerTemplate.txt")))
            {
                var content = ReplaceInTemplate(reader.ReadToEnd(), TryGetNamespace(), Name);
                WriteFile(output ?? Path.Combine(WorkingDirectory, Name + ".cs"), content);
            }
 
            return (int)ExitCodes.Success;
        }
    }
    [Display("settings", "C# template for a ComponentSetting plugin.", Groups: new[] { "sdk", "new" })]
    public class GenerateSetting : GenerateType
    {
        [UnnamedCommandLineArgument("name", Required = true, Description = "The name of the new ComponentSetting.")]
        public string Name { get; set; }
 
        public override int Execute(CancellationToken cancellationToken)
        {
            if (!Validate(name: Name, allowWhiteSpace: false, allowLeadingNumbers: false, allowAlphaNumericOnly: true))
            {
                return (int)ExitCodes.ArgumentError;
            }
 
            using (var reader = new StreamReader(Assembly.GetExecutingAssembly()
                .GetManifestResourceStream("OpenTap.Sdk.New.Resources.SettingsTemplate.txt")))
            {
                var content = ReplaceInTemplate(reader.ReadToEnd(), TryGetNamespace(), Name);
                WriteFile(output ?? Path.Combine(WorkingDirectory, Name + ".cs"), content);
            }
 
            return (int)ExitCodes.Success;
        }
    }
    [Display("teststep", "C# template for a TestStep plugin.", Groups: new[] { "sdk", "new" })]
    public class GenerateTestStep : GenerateType
    {
        [UnnamedCommandLineArgument("name", Required = true, Description = "The name of the new TestStep.")]
        public string Name { get; set; }
 
        public override int Execute(CancellationToken cancellationToken)
        {
            if (!Validate(name: Name, allowWhiteSpace: false, allowLeadingNumbers: false, allowAlphaNumericOnly: true))
            {
                return (int)ExitCodes.ArgumentError;
            }
 
            using (var reader = new StreamReader(Assembly.GetExecutingAssembly()
                .GetManifestResourceStream("OpenTap.Sdk.New.Resources.TestStepTemplate.txt")))
            {
                var content = ReplaceInTemplate(reader.ReadToEnd(), TryGetNamespace(), Name);
                WriteFile(output ?? Path.Combine(WorkingDirectory, Name + ".cs"), content);
            }
 
            return (int)ExitCodes.Success;
        }
    }
 
 
    [Display("testplan", "Deprecated! Creates a TestPlan (.TapPlan) containing all TestSteps types defined in this project.", Groups: new[] { "sdk", "new" })]
    [Obsolete("Use an editor to create TestPlans instead")]
    [Browsable(false)]
    public class GenerateTestPlan : GenerateType
    {
        [UnnamedCommandLineArgument("name", Required = true, Description = "The name of the new TapPlan.")]
        public string Name { get; set; }
 
        public override int Execute(CancellationToken cancellationToken)
        {
            if (!Validate(name: Name, allowWhiteSpace: true, allowLeadingNumbers: true, allowAlphaNumericOnly: false))
            {
                return (int)ExitCodes.ArgumentError;
            }
 
            using (var reader = new StreamReader(Assembly.GetExecutingAssembly()
                .GetManifestResourceStream("OpenTap.Sdk.New.Resources.TapPlanTemplate.txt")))
            {
                StringBuilder steps = new StringBuilder("\n");
                var ns = TryGetNamespace();
                var csFiles = Directory.GetFiles(WorkingDirectory, "*.cs", SearchOption.TopDirectoryOnly);
                foreach (var file in csFiles)
                {
                    var text = File.ReadAllText(file);
                    var match = Regex.Match(text, "public class (.*?) : I?TestStep");
                    if (match.Success)
                        steps.AppendLine($"    <TestStep type=\"{ns}.{match.Groups[1].Value}\"></TestStep>");
                }
 
                var content = ReplaceInTemplate(reader.ReadToEnd(), steps.ToString());
                WriteFile(output ?? Path.Combine(WorkingDirectory, Name + ".TapPlan"), content);
            }
 
            log.Warning("This feature is obsoleted. Use an editor to create a testplan.");
            log.Warning("For more information, see https://doc.opentap.io/User%20Guide/Editors/");
 
            return (int)ExitCodes.Success;
        }
    }
    [Display("cliaction", "C# template for a CliAction plugin.", Groups: new[] { "sdk", "new" })]
    public class GenerateCliAction : GenerateType
    {
        [UnnamedCommandLineArgument("name", Required = true, Description = "The name of the new CliAction.")]
        public string Name { get; set; }
 
        public override int Execute(CancellationToken cancellationToken)
        {
            if (!Validate(name: Name, allowWhiteSpace: false, allowLeadingNumbers: false, allowAlphaNumericOnly: true))
            {
                return (int)ExitCodes.ArgumentError;
            }
 
            using (var reader = new StreamReader(Assembly.GetExecutingAssembly()
                .GetManifestResourceStream("OpenTap.Sdk.New.Resources.CliActionTemplate.txt")))
            {
                var content = ReplaceInTemplate(reader.ReadToEnd(), TryGetNamespace(), Name);
                WriteFile(output ?? Path.Combine(WorkingDirectory, Name + ".cs"), content);
            }
 
            return (int)ExitCodes.Success;
        }
    }
    [Display("packagexml", "Package Definition file (package.xml).", Groups: new[] { "sdk", "new" })]
    public class GeneratePackageXml : GenerateType
    {
        [UnnamedCommandLineArgument("package name", Required = true, Description = "The name of the new package.xml")]
        public string Name { get; set; }
 
        public override int Execute(CancellationToken cancellationToken)
        {
            if (!Validate(name: Name, allowWhiteSpace: true, allowLeadingNumbers: true, allowAlphaNumericOnly: false))
            {
                return (int)ExitCodes.ArgumentError;
            }
 
            using (var reader = new StreamReader(Assembly.GetExecutingAssembly()
                .GetManifestResourceStream("OpenTap.Sdk.New.Resources.PackageXmlTemplate.txt")))
            {
                var content = ReplaceInTemplate(reader.ReadToEnd(), TryGetNamespace(), Name);
                WriteFile(output ?? Path.Combine(WorkingDirectory, "package.xml"), content);
            }
 
            return (int)ExitCodes.Success;
        }
    }
 
    // SerializerPlugin
}