Skip to content
This repository has been archived by the owner on Nov 19, 2019. It is now read-only.

Commit

Permalink
Add infrastructure to generate global solution
Browse files Browse the repository at this point in the history
  • Loading branch information
matkoch committed Nov 9, 2019
1 parent cb82aae commit 515fd35
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
external

# Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
[Bb]in/
[Oo]bj/
Expand Down
41 changes: 41 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<!-- Thanks to Thomas Frenzel (https://github.com/dotnet/sdk/issues/1151#issuecomment-385133284) -->
<PropertyGroup>
<ReplacePackageReferences Condition="$(ReplacePackageReferences) == ''">true</ReplacePackageReferences>
<SolutionPath>$(MSBuildThisFileDirectory)\nuke.sln</SolutionPath>
</PropertyGroup>

<Choose>
<When Condition="$(ReplacePackageReferences) AND '$(SolutionPath)' != '' AND '$(SolutionPath)' != '*undefined*' AND Exists('$(SolutionPath)')">

<PropertyGroup>
<SolutionFileContent>$([System.IO.File]::ReadAllText($(SolutionPath)))</SolutionFileContent>
<SmartSolutionDir>$([System.IO.Path]::GetDirectoryName( $(SolutionPath) ))</SmartSolutionDir>
<RegexPattern>(?&lt;="[PackageName]", ")(.*)(?=", ")</RegexPattern>
</PropertyGroup>

<ItemGroup>
<!-- Keep the identity of the package reference -->
<SmartPackageReference Include="@(PackageReference)">
<PackageName>%(Identity)</PackageName>
<InSolution>$(SolutionFileContent.Contains('\%(Identity).csproj'))</InSolution>
</SmartPackageReference>

<!-- Filter them by mapping them to another itemGroup using the WithMetadataValue item function -->
<PackageInSolution Include="@(SmartPackageReference -> WithMetadataValue('InSolution', True) )">
<Pattern>$(RegexPattern.Replace('[PackageName]','%(PackageName)') )</Pattern>
<SmartPath>$([System.Text.RegularExpressions.Regex]::Match( '$(SolutionFileContent)', '%(Pattern)' ))</SmartPath>
</PackageInSolution>

<ProjectReference Include="@(PackageInSolution -> '$(SmartSolutionDir)\%(SmartPath)' )"/>

<!-- Remove the package references that are now referenced as projects -->
<PackageReference Remove="@(PackageInSolution -> '%(PackageName)' )"/>
</ItemGroup>

</When>
</Choose>

</Project>
80 changes: 80 additions & 0 deletions build/Build.GlobalSolution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2019 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System.Collections.Generic;
using System.IO;
using System.Linq;
using Nuke.Common;
using Nuke.Common.Git;
using Nuke.Common.IO;
using Nuke.Common.ProjectModel;
using Nuke.Common.Utilities.Collections;
using Octokit;
using static Nuke.Common.ControlFlow;
using static Nuke.Common.IO.PathConstruction;
using static Nuke.Common.IO.SerializationTasks;
using static Nuke.Common.ProjectModel.ProjectModelTasks;
using static Nuke.Common.Tools.Git.GitTasks;

partial class Build
{
[Parameter] readonly bool UseSSH;

AbsolutePath ExternalRepositoriesDirectory => RootDirectory / "external";
AbsolutePath ExternalRepositoriesFile => ExternalRepositoriesDirectory / "repositories.yml";

IEnumerable<GitRepository> ExternalRepositories =>
YamlDeserializeFromFile<string[]>(ExternalRepositoriesFile).Select(x => GitRepository.FromUrl(x));

Target CheckoutExternalRepositories => _ => _
.Executes(() =>
{
string GetDefaultBranch(GitRepository repository)
{
var client = new GitHubClient(new ProductHeaderValue(nameof(NukeBuild)));
var repo = client.Repository.Get(repository.GetGitHubOwner(), repository.GetGitHubName()).Result;
return repo.DefaultBranch;
}

foreach (var repository in ExternalRepositories)
{
var repositoryDirectory = ExternalRepositoriesDirectory / repository.GetGitHubName();
var origin = UseSSH ? repository.SshUrl : repository.HttpsUrl;

if (!Directory.Exists(repositoryDirectory))
Git($"clone {origin} {repositoryDirectory} --branch {GetDefaultBranch(repository)} --progress");
else
{
SuppressErrors(() => Git($"remote add origin {origin}", repositoryDirectory));
Git($"remote set-url origin {origin}", repositoryDirectory);
}
}
});

Target CreateGlobalSolution => _ => _
.DependsOn(CheckoutExternalRepositories)
.Executes(() =>
{
var global = CreateSolution();
global.Configurations = Solution.Configurations;

SolutionFolder GetParentFolder(PrimitiveProject solutionFolder) =>
global.AllSolutionFolders.FirstOrDefault(x => x.ProjectId == solutionFolder.SolutionFolder?.ProjectId);

void AddSolution(Solution solution, SolutionFolder folder = null)
{
solution.AllSolutionFolders.ForEach(x => global.AddSolutionFolder(x.Name, x.ProjectId, GetParentFolder(x) ?? folder));
solution.AllSolutionFolders.ForEach(x => global.GetSolutionFolder(x.ProjectId).Items = x.Items);
solution.AllProjects.ForEach(x => global.AddProject(x.Name, x.TypeId, x.Path, x.ProjectId, x.Configurations, GetParentFolder(x) ?? folder));
}

AddSolution(Solution);

ExternalRepositoriesDirectory.GlobFiles("*/*.sln")
.Select(x => ParseSolution(x))
.ForEach(x => AddSolution(x, global.AddSolutionFolder(x.Name)));

global.SaveAs(RootDirectory / "nuke-global.sln");
});
}
2 changes: 2 additions & 0 deletions build/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
</ItemDefinitionGroup>

<ItemGroup>
<PackageReference Include="Octokit" Version="0.36.0" />

<PackageDownload Include="GitVersion.Tool" Version="[5.1.1]" />
<PackageDownload Include="JetBrains.dotCover.CommandLineTools" Version="[2019.2.2]" />
<PackageDownload Include="JetBrains.ReSharper.CommandLineTools" Version="[2019.2.1]" />
Expand Down
12 changes: 12 additions & 0 deletions external/repositories.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- https://github.com/nuke-build/azure-keyvault.git
- https://github.com/nuke-build/docfx.git
- https://github.com/nuke-build/docker.git
- https://github.com/nuke-build/helm.git
- https://github.com/nuke-build/kubernetes.git
- https://github.com/nuke-build/nswag.git
- https://github.com/nuke-build/presentation.git
- https://github.com/nuke-build/resharper.git
- https://github.com/nuke-build/template.git
- https://github.com/nuke-build/visualstudio.git
- https://github.com/nuke-build/vscode.git
- https://github.com/nuke-build/web.git
4 changes: 4 additions & 0 deletions shell-completion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ Skip:
- Analysis
- Announce
- Changelog
- CheckoutExternalRepositories
- Clean
- Compile
- Coverage
- CreateGlobalSolution
- Generate
- Hotfix
- Install
Expand All @@ -44,9 +46,11 @@ Target:
- Analysis
- Announce
- Changelog
- CheckoutExternalRepositories
- Clean
- Compile
- Coverage
- CreateGlobalSolution
- Generate
- Hotfix
- Install
Expand Down

0 comments on commit 515fd35

Please sign in to comment.