-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathScriptUtilities.cs
More file actions
200 lines (174 loc) · 6.17 KB
/
Copy pathScriptUtilities.cs
File metadata and controls
200 lines (174 loc) · 6.17 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
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
// Shared utilities for C# build scripts
using System;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Linq;
using System.Collections.Generic;
public static class ScriptUtilities
{
/// <summary>
/// Get the current Runtime Identifier (RID) based on the current platform
/// </summary>
public static string GetCurrentRid()
{
var os = RuntimeInformation.OSArchitecture switch
{
Architecture.X64 => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win-x64" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx-x64" : "linux-x64",
Architecture.Arm64 => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win-arm64" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx-arm64" : "linux-arm64",
_ => throw new NotSupportedException($"Unsupported architecture: {RuntimeInformation.OSArchitecture}")
};
return os;
}
/// <summary>
/// Get the executable name based on RID and whether it's AOT
/// </summary>
public static string GetExecutableName(string rid, bool isAot = false)
{
var baseName = isAot ? "Morphir" : "morphir";
if (rid.StartsWith("win-"))
{
return $"{baseName}.exe";
}
return baseName;
}
/// <summary>
/// Get the project root directory (parent of scripts directory)
/// </summary>
public static string GetProjectRoot()
{
// Get the directory where this script is located
var scriptPath = Environment.GetCommandLineArgs()[0];
var scriptDir = Path.GetDirectoryName(Path.GetFullPath(scriptPath));
// If running via dotnet run, the script path might be different
// Try to find the scripts directory
var currentDir = Directory.GetCurrentDirectory();
var scriptsDir = Path.Combine(currentDir, "scripts");
if (Directory.Exists(scriptsDir))
{
return currentDir;
}
// If we're in the scripts directory, go up one level
if (Path.GetFileName(currentDir) == "scripts")
{
return Directory.GetParent(currentDir)!.FullName;
}
// Fallback: assume we're in project root
return currentDir;
}
/// <summary>
/// Get the scripts directory
/// </summary>
public static string GetScriptsDirectory()
{
return Path.Combine(GetProjectRoot(), "scripts");
}
/// <summary>
/// Ensure a directory exists, creating it if necessary
/// </summary>
public static void EnsureDirectoryExists(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
/// <summary>
/// Run a command and return the exit code
/// </summary>
public static int RunCommand(string command, params string[] args)
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = string.Join(" ", args.Select(a => a.Contains(" ") ? $"\"{a}\"" : a)),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
}
};
process.OutputDataReceived += (sender, e) => { if (e.Data != null) Console.WriteLine(e.Data); };
process.ErrorDataReceived += (sender, e) => { if (e.Data != null) Console.Error.WriteLine(e.Data); };
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
return process.ExitCode;
}
/// <summary>
/// Run a command and return the exit code, with environment variables
/// </summary>
public static int RunCommandWithEnv(string command, Dictionary<string, string>? envVars, params string[] args)
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = string.Join(" ", args.Select(a => a.Contains(" ") ? $"\"{a}\"" : a)),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
}
};
if (envVars != null)
{
foreach (var kvp in envVars)
{
process.StartInfo.EnvironmentVariables[kvp.Key] = kvp.Value;
}
}
process.OutputDataReceived += (sender, e) => { if (e.Data != null) Console.WriteLine(e.Data); };
process.ErrorDataReceived += (sender, e) => { if (e.Data != null) Console.Error.WriteLine(e.Data); };
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
return process.ExitCode;
}
/// <summary>
/// Find an executable in the artifacts directory
/// </summary>
public static string? FindExecutable(string basePath, string rid, string exeName)
{
// Try direct path first
var directPath = Path.Combine(basePath, rid, exeName);
if (File.Exists(directPath))
{
return directPath;
}
// Try to find in subdirectories
if (Directory.Exists(basePath))
{
var files = Directory.GetFiles(basePath, exeName, SearchOption.AllDirectories)
.Where(f => f.Contains(rid))
.FirstOrDefault();
if (files != null)
{
return files;
}
}
return null;
}
/// <summary>
/// Get file size in human-readable format
/// </summary>
public static string GetFileSize(string filePath)
{
var fileInfo = new FileInfo(filePath);
var bytes = fileInfo.Length;
string[] sizes = { "B", "KB", "MB", "GB" };
double len = bytes;
int order = 0;
while (len >= 1024 && order < sizes.Length - 1)
{
order++;
len = len / 1024;
}
return $"{len:0.##} {sizes[order]}";
}
}