-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOliveSolution.cs
More file actions
215 lines (176 loc) · 7.28 KB
/
Copy pathOliveSolution.cs
File metadata and controls
215 lines (176 loc) · 7.28 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using System.IO;
using System.Linq;
using Olive;
namespace MSharp.Build
{
class OliveSolution : Builder
{
public static DirectoryInfo Root, Lib;
readonly bool Publish;
public bool IsDotNetCore, IsWebForms;
static OliveSolution()
{
Root = Environment.CurrentDirectory.AsDirectory();
Lib = Root.CreateSubdirectory(Path.Combine("M#","lib"));
}
public OliveSolution(bool publish)
{
Console.WriteLine("Build started for: " + Root.FullName);
Console.WriteLine();
Publish = publish;
IsDotNetCore = IsProjectDotNetCore();
IsWebForms = Root.GetSubDirectory("Website").GetFiles("*.csproj").None();
if (IsDotNetCore)
{
Lib = Lib.GetDirectories()
.Where(x => x.Name.StartsWith("net") && x.Name.Any(c => c.IsDigit()))
.WithMax(x => x.Name.Where(c => c.IsDigit()).ToString("").To<int>())
?? throw new Exception("netcoreapp3.1 or net8.0 or net10.0 or similar folder is not found in " + Lib.FullName);
}
}
bool IsProjectDotNetCore()
{
return Lib.Parent.GetSubDirectory("Model").GetFile("#Model.csproj").ReadAllText()
.Contains("<TargetFramework>net");
}
protected override void AddTasks()
{
Add(() => BuildRuntimeConfigJson());
Add(() => RestoreNuget());
Add(() => BuildMSharpModel());
Add(() => MSharpGenerateModel());
Add(() => BuildAppDomain());
Add(() => BuildMSharpUI());
Add(() => MSharpGenerateUI());
Add(() => YarnInstall());
Add(() => TypescriptCompile());
Add(() => SassCompile());
Add(() => BuildAppWebsite());
}
void BuildRuntimeConfigJson()
{
var version = Lib.Name;
var runtime = version.SkipWhile(v => v.IsLetter()).ToString("");
var json = $@"{{
""runtimeOptions"":{{
""tfm"":""{version}"",
""framework"":{{
""name"":""Microsoft.NETCore.App"",
""version"":""{runtime}.0""
}}
}}
}}";
File.WriteAllText(Path.Combine(Lib.FullName, "MSharp.DSL.runtimeconfig.json"), json);
}
void RestoreNuget()
{
if (!IsDotNetCore)
Commands.FindExe("nuget").Execute("restore",
configuration: x => x.StartInfo.WorkingDirectory = Root.FullName);
}
void BuildMSharpModel() => DotnetBuild(Path.Combine("M#", "Model"));
void BuildAppDomain() => DotnetBuild("Domain");
void BuildMSharpUI() => DotnetBuild(Path.Combine("M#", "UI"));
void BuildAppWebsite()
{
if (IsWebForms)
{
RestorePackagesConfig("Website");
CopyDllsToWebsite();
}
else DotnetBuild("Website", $"publish -o ..{Path.DirectorySeparatorChar}publish".OnlyWhen(Publish));
}
void CopyDllsToWebsite()
{
var bin = "Website/bin".AsDirectory();
var dllPaths = from file in bin.GetFiles("*.refresh")
let source = Root.GetSubDirectory("Website").GetFile(file.ReadAllText())
let item = new
{
Source = source,
Destination = bin.GetFile(source.Name)
}
where item.Destination.Exists == false
select item;
dllPaths.Do(p => File.Copy(p.Source.FullName, p.Destination.FullName, true));
}
const string PACKAGES_DIRECTORY = "packages";
void RestorePackagesConfig(string folder)
{
var packages = Folder(folder).AsDirectory().GetFile("packages.config");
if (packages.Exists())
{
Commands.FindExe("nuget").Execute("restore " + folder + " -packagesdirectory " + Root.GetOrCreateSubDirectory(PACKAGES_DIRECTORY).FullName,
configuration: x => x.StartInfo.WorkingDirectory = Root.FullName);
}
}
FileInfo GetPackages(string folder) => Folder(folder).AsDirectory().GetFile("packages.config");
string GetProjectSolution() => Root.GetFiles("*.sln")[0].FullName;
void DotnetBuild(string folder, string command = null)
{
if (IsDotNetCore) DotnetCoreBuild(folder, command);
else
{
RestorePackagesConfig(folder);
var solution = GetProjectSolution();
var projName = folder;
var project = folder.AsDirectory().GetFiles("*.csproj")[0].FullName;
if (folder.StartsWith("M#")) projName = "#" + folder.Substring(3);
var dep = " /p:BuildProjectReferences=false".OnlyWhen(folder.StartsWith("M#"));
Commands.FindExe("msbuild").Execute($"\"{project}\" -v:m",
configuration: x => x.StartInfo.EnvironmentVariables.Add("MSHARP_BUILD", "FULL"));
}
}
void DotnetCoreBuild(string folder, string command = null)
{
if (command.IsEmpty()) command = $"build {folder.AsDirectory().GetFiles("*.csproj")[0].FullName} -v q";
var log = Commands.DotNet.Execute(command,
configuration: x =>
{
x.StartInfo.WorkingDirectory = Folder(folder);
x.StartInfo.EnvironmentVariables.Add("MSHARP_BUILD", "FULL");
});
Log(log);
}
void MSharpGenerateModel() => RunMSharpBuild("/build /model /no-domain");
void MSharpGenerateUI() => RunMSharpBuild("/build /ui");
void RunMSharpBuild(string command)
{
string log;
if (IsDotNetCore)
{
log = Commands.DotNet.Execute($"msharp.dsl.dll " + command,
configuration: x => x.StartInfo.WorkingDirectory = Lib.FullName);
}
else
{
log = Lib.GetFile("MSharp.dsl.exe").Execute(command, configuration: x => x.StartInfo.WorkingDirectory = Lib.FullName);
}
Log(log);
}
void YarnInstall()
{
var log = Commands.Yarn.Execute("install",
configuration: x => x.StartInfo.WorkingDirectory = Folder("Website"));
Log(log);
}
void TypescriptCompile()
{
var log = Commands.TypeScript.Execute("",
configuration: x => x.StartInfo.WorkingDirectory = Folder("Website"));
Log(log);
}
void SassCompile()
{
if (!IsDotNetCore) return;
var exe = Folder(Path.Combine("Website", "wwwroot", "Styles", "Build", "SassCompiler.exe")).AsFile();
var log = "SKIPPED! " + exe.FullName + " file does not exist";
var args = "\"" + Folder(Path.Combine("Website", "CompilerConfig.json")) + "\"";
if (exe.Exists())
log = exe.Execute(args);
Log(log);
}
string Folder(string relative) => Root.GetSubDirectory(relative).FullName;
}
}