-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpublish.csx
173 lines (148 loc) · 5.38 KB
/
publish.csx
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
#r "nuget: System.Diagnostics.Process, 4.3.0"
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.IO.Compression;
// Define output path for published applications
var output = "artifacts/Publish/";
var platforms = new List<string>(){"win-x64", "linux-x64", "osx-x64", "osx-arm64"};
void PublishTools()
{
var name = "DxFeed.Graal.Net.Tools";
var framework = "net8.0";
var project = FindProject(name);
PublishProjects(new[] { project }, name, framework, true);
}
void PublishSamples()
{
var name = "Samples";
var framework = "net6.0";
var projects = FindAllProjectInDirectory("samples");
PublishProjects(projects, name, framework, false);
}
void PublishProjects(IEnumerable<string> projects, string name, string framework, bool isSingleFile)
{
foreach (var project in projects)
{
foreach (var platform in platforms)
{
PublishScd(project, framework, platform, $"{output}/{name}/{platform}", isSingleFile);
}
}
// Sign the macOS binaries if they exist.
Codesign($"{output}/{name}/osx-x64");
Codesign($"{output}/{name}/osx-arm64");
// Zip the output directory.
ZipDirectory($"{output}/{name}", name, GetVersion());
}
// Function to publish self-contained deployment (SCD).
// Publishes the specified .NET project for the given framework and platform.
void PublishScd(string project, string framework, string platform, string output, bool isSingleFile)
{
ExecuteCommand("dotnet", $"publish" +
$" {project}" +
$" --framework {framework}" +
$" -r {platform}" +
$" -o {output}" +
$" -p:PublishSingleFile={isSingleFile}" +
$" -p:SolutionDir={GetSolutionFolder()}" +
" -p:IncludeNativeLibrariesForSelfExtract=true" +
" -p:AllowedReferenceRelatedFileExtensions=none" +
" -p:DebugType=None" +
" -p:DebugSymbols=false" +
" --self-contained true" +
" -c Release");
}
bool CommandExists(string command)
{
try
{
var process = Process.Start(new ProcessStartInfo
{
FileName = "sh",
Arguments = $"-c \"command -v {command}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
});
process.WaitForExit();
return process.ExitCode == 0;
}
catch
{
return false;
}
}
void ExecuteCommand(string command, string arguments)
{
try
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
process.OutputDataReceived += (sender, e) => { if (e.Data != null) Console.WriteLine(e.Data); };
process.ErrorDataReceived += (sender, e) => { if (e.Data != null) Console.WriteLine(e.Data); };
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
if (process.ExitCode != 0)
{
Console.WriteLine($"Command failed: {command} {arguments}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to execute command: {command} {arguments}. Error: {ex.Message}");
}
}
// Function to sign macOS applications
// path: Path to the application to be signed
void Codesign(string path)
{
// Check if codesign is available before attempting to sign
if (CommandExists("codesign"))
{
foreach (var file in Directory.GetFiles(path))
{
ExecuteCommand("codesign", $"-f -s - {file}");
}
}
}
void ZipDirectory(string path, string name, string version)
{
// Iterate through each directory in the base directory.
foreach (var dir in Directory.GetDirectories(path))
{
string dirName = Path.GetFileName(dir); // Get the directory name.
string zipFileName = $"{name}-{dirName}-{version}.zip"; // Construct the zip file name.
string zipFilePath = Path.Combine(path, zipFileName); // Full path to the zip file.
// Compress the directory into a zip file.
if (File.Exists(zipFilePath))
{
File.Delete(zipFilePath); // Delete the existing zip file if it exists.
}
ZipFile.CreateFromDirectory(dir, zipFilePath, CompressionLevel.Optimal, false);
Console.WriteLine($"Created zip file: {zipFilePath}");
}
}
string FindProject(string name) =>
Directory.EnumerateFiles($"{GetSolutionFolder()}", $"{name}.csproj", SearchOption.AllDirectories).First();
List<string> FindAllProjectInDirectory(string path) =>
Directory.EnumerateFiles($"{GetSolutionFolder()}/{path}", "*.csproj", SearchOption.AllDirectories).ToList();
string GetVersion() =>
File.ReadLines($"{GetSolutionFolder()}/version.txt").First().Trim();
string GetSolutionFolder([CallerFilePath] string path = null) =>
Path.GetDirectoryName(path);
PublishSamples();
PublishTools();