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
//            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 System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using OpenTap;
using OpenTap.Cli;
 
namespace OpenTap.Sdk.New
{
    [Display("vs", "Solution file for Visual Studio projects.", Groups: new[] { "sdk", "new", "integration" })]
    public class GenerateVs : GenerateType
    {
        public override int Execute(CancellationToken cancellationToken)
        {
            using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("OpenTap.Sdk.New.Resources.slnTemplate.txt")))
            {
                var ns = TryGetNamespace();
                var content = ReplaceInTemplate(reader.ReadToEnd(), ns);
                WriteFile(output ?? Path.Combine(WorkingDirectory, ns + ".sln"), content);
            }
 
            return (int)ExitCodes.Success;
        }
    }
 
    [Display("gitlab-ci", "GitLab CI build script. For building and publishing the TapPackage in this project.", Groups: new[] { "sdk", "new", "integration" })]
    public class GenerateGitlab : GenerateType
    {
        public override int Execute(CancellationToken cancellationToken)
        {
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("OpenTap.Sdk.New.Resources.gitlabCiTemplate.txt"))
            using (var reader = new StreamReader(stream))
            {
                var content = ReplaceInTemplate(reader.ReadToEnd(), TryGetNamespace());
                WriteFile(output ?? Path.Combine(WorkingDirectory, ".gitlab-ci.yml"), content);
            }
 
            return (int)ExitCodes.Success;
        }
    }
 
    [Display("vscode", "Files to enable building and debugging with vscode.", Groups: new[] { "sdk", "new", "integration" })]
    public class GenerateVsCode : GenerateType
    {
        public override int Execute(CancellationToken cancellationToken)
        {
            var vsCodeDir = ".vscode";
 
            if (!string.IsNullOrWhiteSpace(output))
            {
                // TryGetNamespace assumes output to end with a slash if it is a directory, and in this case it always is
                if (!(output.EndsWith("/") || output.EndsWith("\\")))
                    output = output + '/'; 
                vsCodeDir = Path.Combine(output, vsCodeDir);
                WorkingDirectory = output;
            }
 
            // create .vscode folder
            if (Directory.Exists(vsCodeDir) == false)
                Directory.CreateDirectory(vsCodeDir);
 
            // .launch
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("OpenTap.Sdk.New.Resources.launchTemplate.txt"))
            using (var reader = new StreamReader(stream))
            {
                var content = ReplaceInTemplate(reader.ReadToEnd(), TryGetNamespace());
                var tapPlans = Directory.GetFiles(WorkingDirectory, "*.TapPlan", SearchOption.AllDirectories);
                if (tapPlans.Count() == 1)
                {
                    log.Info("Found one TapPlan. Using the plan for debugging.");
                    content = Regex.Replace(content, "<tap plan>", m => Path.GetFullPath(tapPlans.First()).Substring(Path.GetFullPath(WorkingDirectory).Length + 1).Replace("\\", "/"));
                }
                else
                    log.Info("Please change <tap plan> in the '.vscode/launch.json' file.");
 
                if (WriteFile(Path.Combine(vsCodeDir, "launch.json"), content))
                    log.Info($"Please note: The vscode integration assumes OutputPath is in '{WorkingDirectory}/bin/Debug>'.");
            }
 
            // .tasks
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("OpenTap.Sdk.New.Resources.tasksTemplate.txt"))
            using (var reader = new StreamReader(stream))
                WriteFile(Path.Combine(vsCodeDir, "tasks.json"), reader.ReadToEnd());
 
 
            return (int)ExitCodes.Success;
        }
    }
 
    [Display("gitversion", "Configures automatic version of the package using version numbers generated from git history.", Groups: new[] { "sdk", "new", "integration" })]
    public class GenerateGit : GenerateType
    {
        private IEnumerable<FileInfo> GetPackageXmlFiles(DirectoryInfo root, Func<string, bool> include)
        {
            foreach (var file in root.GetFiles("package.xml", SearchOption.TopDirectoryOnly))
            {
                yield return file;
            }
 
            foreach (var subdir in root.GetDirectories().Where(x => include(x.Name)))
            {
                foreach (var result in GetPackageXmlFiles(subdir, include))
                {
                    yield return result;
                }
            }
        }
        public override int Execute(CancellationToken cancellationToken)
        {
            var target = string.IsNullOrWhiteSpace(output) ? Path.Combine(WorkingDirectory, ".gitversion") : output;
            var targetDir = new FileInfo(target).Directory;
            
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("OpenTap.Sdk.New.Resources.GitVersionTemplate.txt"))
            using (var reader = new StreamReader(stream))
            {
                foreach (var packageXml in GetPackageXmlFiles(targetDir, (dirName) => dirName != "bin"))
                {
                    var filename = packageXml.FullName;
                    log.Info($"Found {filename}. Do you want to enable automatic version update using a '.gitversion' file?");
 
                    var request = new OverrideRequest();
                    UserInput.Request(request, true);
 
                    if (request.Override == RequestEnum.Yes)
                    {
                        var text = File.ReadAllText(filename);
                        text = Regex.Replace(text, "(<Package.*?Version=\")(.*?)(\".*?>)", (m) => $"{m.Groups[1].Value}$(GitVersion){m.Groups[3].Value}");
                        File.WriteAllText(filename, text);
                    }
                }
                WriteFile(target, reader.ReadToEnd());
            }
 
            return (int)ExitCodes.Success;
        }
    }
}