AvaloniaEdit/build.cake

374 строки
13 KiB
C#

/////////////////////////////////////////////////////////////////////
// ADDINS
/////////////////////////////////////////////////////////////////////
#addin "nuget:?package=Polly&version=5.3.1"
#addin "nuget:?package=NuGet.Core&version=2.14.0"
//////////////////////////////////////////////////////////////////////
// TOOLS
//////////////////////////////////////////////////////////////////////
#tool "nuget:?package=NuGet.CommandLine&version=4.3.0"
///////////////////////////////////////////////////////////////////////////////
// USINGS
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Polly;
using NuGet;
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var platform = Argument("platform", "AnyCPU");
var configuration = Argument("configuration", "Release");
///////////////////////////////////////////////////////////////////////////////
// CONFIGURATION
///////////////////////////////////////////////////////////////////////////////
var MainRepo = "AvaloniaUI/AvaloniaEdit";
var MasterBranch = "master";
var ReleasePlatform = "Any CPU";
var ReleaseConfiguration = "Release";
///////////////////////////////////////////////////////////////////////////////
// PARAMETERS
///////////////////////////////////////////////////////////////////////////////
var isPlatformAnyCPU = StringComparer.OrdinalIgnoreCase.Equals(platform, "Any CPU");
var isPlatformX86 = StringComparer.OrdinalIgnoreCase.Equals(platform, "x86");
var isPlatformX64 = StringComparer.OrdinalIgnoreCase.Equals(platform, "x64");
var isLocalBuild = BuildSystem.IsLocalBuild;
var isRunningOnUnix = IsRunningOnUnix();
var isRunningOnWindows = IsRunningOnWindows();
var isRunningOnAppVeyor = BuildSystem.AppVeyor.IsRunningOnAppVeyor;
var isPullRequest = BuildSystem.AppVeyor.Environment.PullRequest.IsPullRequest;
var isMainRepo = StringComparer.OrdinalIgnoreCase.Equals(MainRepo, BuildSystem.AppVeyor.Environment.Repository.Name);
var isMasterBranch = StringComparer.OrdinalIgnoreCase.Equals(MasterBranch, BuildSystem.AppVeyor.Environment.Repository.Branch);
var isTagged = BuildSystem.AppVeyor.Environment.Repository.Tag.IsTag
&& !string.IsNullOrWhiteSpace(BuildSystem.AppVeyor.Environment.Repository.Tag.Name);
var isReleasable = StringComparer.OrdinalIgnoreCase.Equals(ReleasePlatform, platform)
&& StringComparer.OrdinalIgnoreCase.Equals(ReleaseConfiguration, configuration);
var isMyGetRelease = !isTagged && isReleasable;
var isNuGetRelease = isTagged && isReleasable;
///////////////////////////////////////////////////////////////////////////////
// VERSION
///////////////////////////////////////////////////////////////////////////////
var version = "0.2.0"; //XmlPeek("./src/AvaloniaEdit/AvaloniaEdit.csproj", "//*[local-name()='Version']/text()");
if (isRunningOnAppVeyor)
{
if (isTagged)
{
// Use Tag Name as version
version = BuildSystem.AppVeyor.Environment.Repository.Tag.Name;
}
else
{
// Use AssemblyVersion with Build as version
version += "-build" + EnvironmentVariable("APPVEYOR_BUILD_NUMBER") + "-alpha";
}
}
var editbin = @"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.12.25827\bin\HostX86\x86\editbin.exe";
///////////////////////////////////////////////////////////////////////////////
// DIRECTORIES
///////////////////////////////////////////////////////////////////////////////
var artifactsDir = (DirectoryPath)Directory("./artifacts");
var zipRootDir = artifactsDir.Combine("zip");
var nugetRoot = artifactsDir.Combine("nuget");
var fileZipSuffix = ".zip";
var buildDirs = GetDirectories("./src/**/bin/**") +
GetDirectories("./src/**/obj/**") +
GetDirectories("./artifacts/**/zip/**");
var netCoreAppsRoot= "./src/";
var netCoreApps = new string[] { "AvaloniaEdit.Demo" };
var netCoreProjects = netCoreApps.Select(name =>
new {
Path = string.Format("{0}/{1}", netCoreAppsRoot, name),
Name = name,
Framework = XmlPeek(string.Format("{0}/{1}/{1}.csproj", netCoreAppsRoot, name), "//*[local-name()='TargetFramework']/text()"),
Runtimes = XmlPeek(string.Format("{0}/{1}/{1}.csproj", netCoreAppsRoot, name), "//*[local-name()='RuntimeIdentifiers']/text()").Split(';')
}).ToList();
///////////////////////////////////////////////////////////////////////////////
// NUGET NUSPECS
///////////////////////////////////////////////////////////////////////////////
// Key: Package Id
// Value is Tuple where Item1: Package Version, Item2: The *.csproj/*.props file path.
var packageVersions = new Dictionary<string, IList<Tuple<string,string>>>();
System.IO.Directory.EnumerateFiles(((DirectoryPath)Directory("./src")).FullPath, "*.csproj", SearchOption.AllDirectories)
.ToList()
.ForEach(fileName => {
var xdoc = XDocument.Load(fileName);
foreach (var reference in xdoc.Descendants().Where(x => x.Name.LocalName == "PackageReference"))
{
var name = reference.Attribute("Include").Value;
var versionAttribute = reference.Attribute("Version");
var packageVersion = versionAttribute != null
? versionAttribute.Value
: reference.Elements().First(x=>x.Name.LocalName == "Version").Value;
IList<Tuple<string, string>> versions;
packageVersions.TryGetValue(name, out versions);
if (versions == null)
{
versions = new List<Tuple<string, string>>();
packageVersions[name] = versions;
}
versions.Add(Tuple.Create(packageVersion, fileName));
}
});
Information("Checking installed NuGet package dependencies versions:");
packageVersions.ToList().ForEach(package =>
{
var packageVersion = package.Value.First().Item1;
bool isValidVersion = package.Value.All(x => x.Item1 == packageVersion);
if (!isValidVersion)
{
Information("Error: package {0} has multiple versions installed:", package.Key);
foreach (var v in package.Value)
{
Information("{0}, file: {1}", v.Item1, v.Item2);
}
throw new Exception("Detected multiple NuGet package version installed for different projects.");
}
});
Information("Setting NuGet package dependencies versions:");
var AvaloniaVersion = "0.9.0-preview1";
Information("Package: Avalonia, version: {0}", AvaloniaVersion);
var nuspecNuGetBehaviors = new NuGetPackSettings()
{
Id = "Avalonia.AvaloniaEdit",
Version = version,
Authors = new [] { "Avalonia Team" },
Owners = new [] { "Avalonia" },
LicenseUrl = new Uri("http://opensource.org/licenses/MIT"),
ProjectUrl = new Uri("https://github.com/AvaloniaUI/AvaloniaEdit/"),
RequireLicenseAcceptance = false,
Symbols = false,
NoPackageAnalysis = true,
Description = "A port of AvalonEdit to the AvaloniaUI Framework.",
Copyright = "Copyright 2019",
Tags = new [] { "Avalonia", "AvalonEdit", "TextEditor", "AvaloniaEdit", "Control" },
Dependencies = new []
{
new NuSpecDependency { Id = "Avalonia", Version = AvaloniaVersion }
},
Files = new []
{
new NuSpecContent { Source = "src/AvaloniaEdit/bin/" + configuration + "/netstandard2.0/AvaloniaEdit.dll", Target = "lib/netstandard2.0" },
},
BasePath = Directory("./"),
OutputDirectory = nugetRoot
};
var nuspecNuGetSettings = new List<NuGetPackSettings>();
nuspecNuGetSettings.Add(nuspecNuGetBehaviors);
var nugetPackages = nuspecNuGetSettings.Select(nuspec => {
return nuspec.OutputDirectory.CombineWithFilePath(string.Concat(nuspec.Id, ".", nuspec.Version, ".nupkg"));
}).ToArray();
///////////////////////////////////////////////////////////////////////////////
// INFORMATION
///////////////////////////////////////////////////////////////////////////////
Information("Building version {0} of AvaloniaEdit ({1}, {2}, {3}) using version {4} of Cake.",
version,
platform,
configuration,
target,
typeof(ICakeContext).Assembly.GetName().Version.ToString());
if (isRunningOnAppVeyor)
{
Information("Repository Name: " + BuildSystem.AppVeyor.Environment.Repository.Name);
Information("Repository Branch: " + BuildSystem.AppVeyor.Environment.Repository.Branch);
}
Information("Target: " + target);
Information("Platform: " + platform);
Information("Configuration: " + configuration);
Information("IsLocalBuild: " + isLocalBuild);
Information("IsRunningOnUnix: " + isRunningOnUnix);
Information("IsRunningOnWindows: " + isRunningOnWindows);
Information("IsRunningOnAppVeyor: " + isRunningOnAppVeyor);
Information("IsPullRequest: " + isPullRequest);
Information("IsMainRepo: " + isMainRepo);
Information("IsMasterBranch: " + isMasterBranch);
Information("IsTagged: " + isTagged);
Information("IsReleasable: " + isReleasable);
Information("IsMyGetRelease: " + isMyGetRelease);
Information("IsNuGetRelease: " + isNuGetRelease);
///////////////////////////////////////////////////////////////////////////////
// TASKS
///////////////////////////////////////////////////////////////////////////////
Task("Clean")
.Does(()=>{
CleanDirectories(buildDirs);
CleanDirectory(nugetRoot);
CleanDirectory(zipRootDir);
});
Task("Restore-NetCore")
.IsDependentOn("Clean")
.Does(() =>
{
foreach (var project in netCoreProjects)
{
DotNetCoreRestore(project.Path);
}
});
Task("Build-NetCore")
.IsDependentOn("Restore-NetCore")
.Does(() =>
{
foreach (var project in netCoreProjects)
{
Information("Building: {0}", project.Name);
var settings = new DotNetCoreBuildSettings {
Configuration = configuration
};
if (!IsRunningOnWindows())
{
settings.Framework = "netcoreapp2.1";
}
DotNetCoreBuild(project.Path, settings);
}
});
Task("Create-NuGet-Packages")
.IsDependentOn("Build-NetCore")
.Does(() =>
{
foreach(var nuspec in nuspecNuGetSettings)
{
NuGetPack(nuspec);
}
});
Task("Publish-MyGet")
.IsDependentOn("Create-NuGet-Packages")
.WithCriteria(() => !isLocalBuild)
.WithCriteria(() => !isPullRequest)
.WithCriteria(() => isMainRepo)
.WithCriteria(() => isMasterBranch)
.WithCriteria(()=> isRunningOnAppVeyor)
.WithCriteria(()=> isTagged)
.Does(() =>
{
var apiKey = EnvironmentVariable("MYGET_API_KEY");
if(string.IsNullOrEmpty(apiKey))
{
throw new InvalidOperationException("Could not resolve MyGet API key.");
}
var apiUrl = EnvironmentVariable("MYGET_API_URL");
if(string.IsNullOrEmpty(apiUrl))
{
throw new InvalidOperationException("Could not resolve MyGet API url.");
}
foreach(var nupkg in nugetPackages)
{
NuGetPush(nupkg, new NuGetPushSettings {
Source = apiUrl,
ApiKey = apiKey
});
}
})
.OnError(exception =>
{
Information("Publish-MyGet Task failed, but continuing with next Task...");
});
Task("Publish-NetCore")
.IsDependentOn("Restore-NetCore")
.WithCriteria(()=>isMainRepo && isMasterBranch && isTagged)
.Does(() =>
{
foreach (var project in netCoreProjects)
{
foreach(var runtime in project.Runtimes)
{
var outputDir = zipRootDir.Combine(project.Name + "-" + runtime);
Information("Publishing: {0}, runtime: {1}", project.Name, runtime);
DotNetCorePublish(project.Path, new DotNetCorePublishSettings {
Framework = "netcoreapp3.0",
Configuration = configuration,
Runtime = runtime,
OutputDirectory = outputDir.FullPath
});
if (IsRunningOnWindows() && (runtime == "win7-x86" || runtime == "win7-x64"))
{
Information("Patching executable subsystem for: {0}, runtime: {1}", project.Name, runtime);
var targetExe = outputDir.CombineWithFilePath(project.Name + ".exe");
var exitCodeWithArgument = StartProcess(editbin, new ProcessSettings {
Arguments = "/subsystem:windows " + targetExe.FullPath
});
Information("The editbin command exit code: {0}", exitCodeWithArgument);
}
}
}
});
Task("Zip-NetCore")
.IsDependentOn("Publish-NetCore")
.WithCriteria(()=>isMainRepo && isMasterBranch)
.Does(() =>
{
foreach (var project in netCoreProjects)
{
foreach(var runtime in project.Runtimes)
{
var outputDir = zipRootDir.Combine(project.Name + "-" + runtime);
Zip(outputDir.FullPath, zipRootDir.CombineWithFilePath(project.Name + "-" + runtime + fileZipSuffix),
GetFiles(outputDir.FullPath + "/*.*"));
}
}
});
Task("Default")
.IsDependentOn("Restore-NetCore")
.IsDependentOn("Build-NetCore")
.IsDependentOn("Create-NuGet-Packages")
.IsDependentOn("Publish-MyGet")
.IsDependentOn("Publish-NetCore")
.IsDependentOn("Zip-NetCore");
RunTarget(target);