aboutsummaryrefslogtreecommitdiffstats
path: root/Tests/Test_QtMsBuild.Build/TempProject.cs
blob: 99fb658b74cc66439c14fc0f2bc9a930ab5ccf14 (plain)
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
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;

using static System.IO.Path;
using static System.IO.File;
using static System.IO.Directory;

namespace QtVsTools.Test.QtMsBuild.Build
{
    public class TempProject : Disposable
    {
        public string ProjectFileName { get; private set; } = GetRandomFileName();
        public string ProjectDir { get; } = Combine(GetTempPath(), GetRandomFileName());
        public string ProjectPath => Combine(ProjectDir, ProjectFileName);

        private void Reset() {
            var t = Stopwatch.StartNew();
            while (Directory.Exists(ProjectDir)) {
                try {
                    Delete(ProjectDir, true);
                } catch {
                    if (t.ElapsedMilliseconds > 3000)
                        break;
                    Thread.Sleep(500);
                }
            }
        }

        public void Clone(string path)
        {
            if (path is not { Length: > 0 } || !File.Exists(path))
                throw new ArgumentException();
            Reset();
            ProjectFileName = GetFileName(path);
            CreateDirectory(ProjectDir);
            GetFiles(GetDirectoryName(path), "*", SearchOption.TopDirectoryOnly)
                .ToList().ForEach(x => Copy(x, Combine(ProjectDir, GetFileName(x))));
        }

        public void Create(string xml)
        {
            Reset();
            ProjectFileName = GetRandomFileName();
            CreateDirectory(ProjectDir);
            WriteAllText(ProjectPath, xml);
        }

        public void GenerateBigSolution(string templatePath, int projectCount)
        {
            Reset();
            CreateDirectory(ProjectDir);
            BigSolution.Generate(ProjectDir, templatePath, projectCount);
            ProjectFileName = GetFileName(EnumerateFiles(ProjectDir, "*.sln").FirstOrDefault());
        }

        protected override void DisposeUnmanaged()
        {
            Reset();
        }
    }
}