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
//            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.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
 
namespace OpenTap.Sdk.New
{
    public abstract class GenerateType : ICliAction
    {
        internal bool Validate(string name, bool allowWhiteSpace, bool allowLeadingNumbers, bool allowAlphaNumericOnly)
        {
 
            bool anyInvalid = false;
            var invalid = Path.GetInvalidFileNameChars();
            var sb = new StringBuilder();
 
            var leading = !allowLeadingNumbers;
 
            foreach (var ch in name)
            {
                if (leading && char.IsNumber(ch))
                {
                    sb.Append("^");
                    anyInvalid = true;
                    continue;
                }
 
                leading = false;
 
                // Then detect any invalid filename or C# identifier chars
                if (invalid.Contains(ch) || (!allowWhiteSpace && char.IsWhiteSpace(ch)) ||
                    (allowAlphaNumericOnly && !char.IsLetterOrDigit(ch) && ch != '_'))
                {
                    sb.Append("^");
                    anyInvalid = true;
                }
                else sb.Append(" ");
            }
 
            if (!anyInvalid) return true;
 
            var stringStart = "Invalid name specified: '";
            log.Error($"{stringStart}{name}' contains illegal characters.");
            var hint = sb.ToString();
            log.Error(hint.PadLeft(stringStart.Length + hint.Length));
            return false;
        }
 
        public static TraceSource log = Log.CreateSource("New");
 
        [CommandLineArgument("out", ShortName = "o", Description = "Path to generated file.")]
        public virtual string output { get; set; }
 
        private string workingDirectory;
 
        internal string WorkingDirectory
        {
            get
            {
                if (string.IsNullOrEmpty(workingDirectory))
                    return Directory.GetCurrentDirectory();
                return workingDirectory;
            }
            set => workingDirectory = value;
        }
 
        public abstract int Execute(CancellationToken cancellationToken);
 
        public bool WriteFile(string filepath, string content, bool force = false)
        {
            if (File.Exists(filepath) && force == false)
            {
                log.Error("File already exists: '{0}'", Path.GetFileName(filepath));
                log.Info("Do you want to override?");
 
                var request = new OverrideRequest();
                UserInput.Request(request, true);
 
                if (request.Override == RequestEnum.No)
                {
                    log.Info("File was not overridden.");
                    return false;
                }
            }
 
            if (!Directory.Exists(Path.GetDirectoryName(filepath)) && string.IsNullOrWhiteSpace(Path.GetDirectoryName(filepath)) == false)
                Directory.CreateDirectory(Path.GetDirectoryName(filepath));
 
            File.WriteAllText(filepath, content);
            log.Info($"Generated file: '{filepath}'.");
 
            return true;
        }
 
        protected string TryGetNamespace()
        {
            string dir = output;
            if (output == null)
                dir = WorkingDirectory;
            else if (output.EndsWith("/") == false)
                dir = Path.GetDirectoryName(dir);
 
            var csprojFiles = Directory.GetFiles(dir, "*.csproj", SearchOption.AllDirectories);
            var csprojPath = csprojFiles.FirstOrDefault();
 
            if (string.IsNullOrWhiteSpace(csprojPath) == false)
            {
                var match = Regex.Match(File.ReadAllText(csprojPath), "<RootNamespace>(.*/)</RootNamespace>");
                if (match.Success)
                    return match.Groups[1].Value;
                else
                    return Path.GetFileNameWithoutExtension(csprojPath);
            }
 
            throw new Exception($"Could not find project file ('.csproj') in '{dir}'.\nNote: You can create a new project with 'tap sdk new project <name>'.");
        }
 
        protected string ReplaceInTemplate(string content, params string[] fields)
        {
            content = Regex.Replace(content, "\\{(\\d)\\}", (m) =>
            {
                if (int.TryParse(m.Groups[1].Value, out int index) && index < fields.Length)
                    return fields[index];
                else
                    return "";
            });
            return content;
        }
    }
 
    public class OverrideRequest
    {
        [Layout(LayoutMode.FloatBottom | LayoutMode.FullRow)]
        [Submit]
        public RequestEnum Override { get; set; }
    }
 
    public enum RequestEnum
    {
        Yes,
        No
    }
}