Files
speckle-sharp-connectors/Build/Program.cs
T
Adam Hathcock 438df1240f Update SDK and add Clean Locks (#35)
* Update SDK dlls

* add clean locks file

* fix build

* Fix clean
2024-07-16 17:12:52 +00:00

261 lines
6.4 KiB
C#

using System.IO.Compression;
using System.Net;
using System.Runtime.InteropServices;
using Build;
using GlobExpressions;
using static Bullseye.Targets;
using static SimpleExec.Command;
const string CLEAN = "clean";
const string RESTORE = "restore";
const string BUILD = "build";
const string TEST = "test";
const string TEST_ONLY = "test-only";
const string FORMAT = "format";
const string ZIP = "zip";
const string VERSION = "version";
const string RESTORE_TOOLS = "restore-tools";
const string BUILD_SERVER_VERSION = "build-server-version";
const string LOCAL_SDK = "add-local-sdk";
const string CLEAN_LOCKS = "clean-locks";
//need to pass arguments
/*var arguments = new List<string>();
if (args.Length > 1)
{
arguments = args.ToList();
args = new[] { arguments.First() };
//arguments = arguments.Skip(1).ToList();
}*/
Target(
CLEAN_LOCKS,
() =>
{
foreach (var f in Glob.Files(".", "**/*.lock.json"))
{
Console.WriteLine("Found and will delete: " + f);
File.Delete(f);
}
}
);
Target(
CLEAN,
ForEach("**/output"),
dir =>
{
IEnumerable<string> GetDirectories(string d)
{
return Glob.Directories(".", d);
}
void RemoveDirectory(string d)
{
if (Directory.Exists(d))
{
Console.WriteLine(d);
Directory.Delete(d, true);
}
}
foreach (var d in GetDirectories(dir))
{
RemoveDirectory(d);
}
}
);
Target(
VERSION,
async () =>
{
var (output, _) = await ReadAsync("dotnet", "minver -v w").ConfigureAwait(false);
output = output.Trim();
Console.WriteLine($"Version: {output}");
Run("echo", $"\"version={output}\" >> $GITHUB_OUTPUT");
}
);
Target(
RESTORE_TOOLS,
() =>
{
Run("dotnet", "tool restore");
}
);
Target(
FORMAT,
DependsOn(RESTORE_TOOLS),
() =>
{
Run("dotnet", "csharpier --check .");
}
);
Target(
RESTORE,
DependsOn(FORMAT),
Consts.Solutions,
s =>
{
Run("dotnet", $"restore {s} --locked-mode");
}
);
Target(
BUILD_SERVER_VERSION,
DependsOn(RESTORE_TOOLS),
() =>
{
Run("dotnet", "tool run dotnet-gitversion /output json /output buildserver");
}
);
Target(
BUILD,
DependsOn(RESTORE),
Consts.Solutions,
s =>
{
var version = Environment.GetEnvironmentVariable("GitVersion_FullSemVer") ?? "3.0.0-localBuild";
var fileVersion = Environment.GetEnvironmentVariable("GitVersion_AssemblySemFileVer") ?? "3.0.0.0";
Console.WriteLine($"Version: {version} & {fileVersion}");
Run("dotnet", $"build {s} -c Release --no-restore -p:Version={version} -p:FileVersion={fileVersion} -v:m");
}
);
Target(
TEST,
DependsOn(BUILD),
Glob.Files(".", "**/*.Tests.csproj"),
file =>
{
Run("dotnet", $"test {file} -c Release --no-build --no-restore --verbosity=normal");
}
);
Target(
TEST_ONLY,
DependsOn(FORMAT),
Glob.Files(".", "**/*.Tests.csproj"),
file =>
{
Run("dotnet", $"restore {file} --locked-mode");
Run(
"dotnet",
$"test {file} -c Release --no-restore --verbosity=normal /p:AltCover=true /p:AltCoverAttributeFilter=ExcludeFromCodeCoverage"
);
}
);
Target(
ZIP,
DependsOn(TEST),
Consts.InstallerManifests,
x =>
{
var outputDir = Path.Combine(".", "output");
var slugDir = Path.Combine(outputDir, x.HostAppSlug);
Directory.CreateDirectory(outputDir);
Directory.CreateDirectory(slugDir);
foreach (var asset in x.Projects)
{
var fullPath = Path.Combine(".", asset.ProjectPath, "bin", "Release", asset.TargetName);
if (!Directory.Exists(fullPath))
{
throw new InvalidOperationException("Could not find: " + fullPath);
}
var assetName = Path.GetFileName(asset.ProjectPath);
var connectorDir = Path.Combine(slugDir, assetName);
Directory.CreateDirectory(connectorDir);
foreach (var directory in Directory.EnumerateDirectories(fullPath, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(directory.Replace(fullPath, connectorDir));
}
foreach (var file in Directory.EnumerateFiles(fullPath, "*", SearchOption.AllDirectories))
{
Console.WriteLine(file);
File.Copy(file, file.Replace(fullPath, connectorDir), true);
}
}
var outputPath = Path.Combine(outputDir, $"{x.HostAppSlug}.zip");
File.Delete(outputPath);
Console.WriteLine($"Zipping: '{slugDir}' to '{outputPath}'");
ZipFile.CreateFromDirectory(slugDir, outputPath);
// Directory.Delete(slugDir, true);
}
);
Target(
LOCAL_SDK,
async () =>
{
var path = Environment.CurrentDirectory;
var gitRoot = Directory.GetParent(path)?.FullName ?? "..";
var sdkRepo = Path.Combine(gitRoot, "speckle-sharp-sdk");
var output = Path.Combine(sdkRepo, "output");
var localFeed = Path.Combine(gitRoot, "speckle-local-feed");
Console.WriteLine($"Creating/cleaning output from SDK at: {output}");
if (Directory.Exists(localFeed))
{
Directory.Delete(localFeed, true);
}
Directory.CreateDirectory(localFeed);
Console.WriteLine($"Creating/cleaning local repo at: {localFeed}");
if (Directory.Exists(localFeed))
{
Directory.Delete(localFeed, true);
}
Directory.CreateDirectory(localFeed);
var nugetCli = Path.Combine(localFeed, "nuget.exe");
if (File.Exists(nugetCli))
{
Console.WriteLine($"Updating nuget: {nugetCli}");
await RunAsync(nugetCli, "update -self");
}
else
{
Console.WriteLine($"Downloading nuget: {nugetCli}");
using var client = new HttpClient();
await using var s = await client.GetStreamAsync("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe");
await using var fs = new FileStream(nugetCli, FileMode.OpenOrCreate);
await s.CopyToAsync(fs);
}
var nugets = Glob.Files(output, "*.nupkg").ToList();
if (!nugets.Any())
{
Console.WriteLine($"No nugets found in: {output}");
}
foreach (var nuget in nugets)
{
await RunAsync(nugetCli, $"add {Path.Combine(output, nuget)} -source {localFeed}");
}
Console.WriteLine($"Adding local source: {localFeed}");
await RunAsync(
"dotnet",
$"nuget add source {localFeed} --name \"Speckle SDK Local\"",
handleExitCode: i =>
{
if (i != 0)
{
Console.WriteLine($"Adding nuget local feed failed: {i}");
}
return true;
}
);
}
);
Target("default", DependsOn(FORMAT, ZIP), () => Console.WriteLine("Done!"));
await RunTargetsAndExitAsync(args).ConfigureAwait(true);