Skip to content

Commit

Permalink
Get build running on Linux and MacOS (#3777)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Feb 5, 2024
1 parent 5e1a960 commit 0c9d87f
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ nswag.js text eol=lf
*.sass text
*.scm text
*.scss text
*.sh text
*.sh text eol=lf
*.sql text
*.styl text
*.tpl text
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,27 @@ jobs:
with:
name: NuGet Packages
path: artifacts/*.nupkg
ubuntu-latest:
name: ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: 'Run: Compile, Test, Pack, Publish'
run: ./build.cmd Compile Test Pack Publish
env:
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
MYGET_API_KEY: ${{ secrets.MYGET_API_KEY }}
CHOCO_API_KEY: ${{ secrets.CHOCO_API_KEY }}
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
macos-latest:
name: macos-latest
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: 'Run: Compile, Test, Pack, Publish'
run: ./build.cmd Compile Test Pack Publish
env:
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
MYGET_API_KEY: ${{ secrets.MYGET_API_KEY }}
CHOCO_API_KEY: ${{ secrets.CHOCO_API_KEY }}
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
37 changes: 34 additions & 3 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ jobs:
windows-latest:
name: windows-latest
runs-on: windows-latest
concurrency:
group: ${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.run_id }}
cancel-in-progress: true
steps:
- name: 'Allow long file path'
run: git config --system core.longpaths true
Expand All @@ -44,3 +41,37 @@ jobs:
- uses: actions/checkout@v3
- name: 'Run: Compile, Test, Pack'
run: ./build.cmd Compile Test Pack
- name: 'Publish: NSwag.zip'
uses: actions/upload-artifact@v3
with:
name: NSwag.zip
path: artifacts/NSwag.zip
- name: 'Publish: NSwag.Npm.zip'
uses: actions/upload-artifact@v3
with:
name: NSwag.Npm.zip
path: artifacts/NSwag.Npm.zip
- name: 'Publish: NSwagStudio.msi'
uses: actions/upload-artifact@v3
with:
name: NSwagStudio.msi
path: artifacts/NSwagStudio.msi
- name: 'Publish: NuGet Packages'
uses: actions/upload-artifact@v3
with:
name: NuGet Packages
path: artifacts/*.nupkg
ubuntu-latest:
name: ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: 'Run: Compile, Test, Pack'
run: ./build.cmd Compile Test Pack
macos-latest:
name: macos-latest
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: 'Run: Compile, Test, Pack'
run: ./build.cmd Compile Test Pack
43 changes: 24 additions & 19 deletions build/Build.CI.GitHubActions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Nuke.Common.CI.GitHubActions;
using Nuke.Common.CI.GitHubActions.Configuration;
Expand All @@ -7,28 +8,28 @@
[CustomGitHubActions(
"pr",
GitHubActionsImage.WindowsLatest,
// GitHubActionsImage.UbuntuLatest,
// GitHubActionsImage.MacOsLatest,
OnPullRequestBranches = new[] { "master", "main" },
OnPullRequestIncludePaths = new[] { "**/*.*" },
OnPullRequestExcludePaths = new[] { "**/*.md" },
PublishArtifacts = false,
InvokedTargets = new[] { nameof(Compile), nameof(Test), nameof(Pack) },
GitHubActionsImage.UbuntuLatest,
GitHubActionsImage.MacOsLatest,
OnPullRequestBranches = ["master", "main"],
OnPullRequestIncludePaths = ["**/*.*"],
OnPullRequestExcludePaths = ["**/*.md"],
PublishArtifacts = true,
InvokedTargets = [nameof(Compile), nameof(Test), nameof(Pack)],
CacheKeyFiles = new string[0],
JobConcurrencyCancelInProgress = true),
JobConcurrencyCancelInProgress = false),
]
[CustomGitHubActions(
"build",
GitHubActionsImage.WindowsLatest,
// GitHubActionsImage.UbuntuLatest,
// GitHubActionsImage.MacOsLatest,
OnPushBranches = new[] { "master", "main" },
OnPushTags = new[] { "v*.*.*" },
OnPushIncludePaths = new[] { "**/*.*" },
OnPushExcludePaths = new[] { "**/*.md" },
GitHubActionsImage.UbuntuLatest,
GitHubActionsImage.MacOsLatest,
OnPushBranches = ["master", "main"],
OnPushTags = ["v*.*.*"],
OnPushIncludePaths = ["**/*.*"],
OnPushExcludePaths = ["**/*.md"],
PublishArtifacts = true,
InvokedTargets = new[] { nameof(Compile), nameof(Test), nameof(Pack), nameof(Publish) },
ImportSecrets = new[] { "NUGET_API_KEY", "MYGET_API_KEY", "CHOCO_API_KEY", "NPM_AUTH_TOKEN" },
InvokedTargets = [nameof(Compile), nameof(Test), nameof(Pack), nameof(Publish)],
ImportSecrets = ["NUGET_API_KEY", "MYGET_API_KEY", "CHOCO_API_KEY", "NPM_AUTH_TOKEN"],
CacheKeyFiles = new string[0])
]
public partial class Build
Expand All @@ -55,11 +56,15 @@ protected override GitHubActionsJob GetJobs(GitHubActionsImage image, IReadOnlyC
}));
*/

newSteps.Insert(0, new GitHubActionsUseGnuTarStep());
newSteps.Insert(0, new GitHubActionsConfigureLongPathsStep());
var onWindows = image.ToString().StartsWith("windows", StringComparison.OrdinalIgnoreCase);
if (onWindows)
{
newSteps.Insert(0, new GitHubActionsUseGnuTarStep());
newSteps.Insert(0, new GitHubActionsConfigureLongPathsStep());
}

// add artifacts manually as they would otherwise by hard to configure via attributes
if (PublishArtifacts)
if (PublishArtifacts && onWindows)
{
newSteps.Add(new GitHubActionsArtifactStep { Name = "NSwag.zip", Path = "artifacts/NSwag.zip" });
newSteps.Add(new GitHubActionsArtifactStep { Name = "NSwag.Npm.zip", Path = "artifacts/NSwag.Npm.zip" });
Expand Down
1 change: 1 addition & 0 deletions build/Build.Pack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public partial class Build
Target Pack => _ => _
.DependsOn(Compile)
.After(Test)
.OnlyWhenDynamic(() => IsRunningOnWindows)
.Executes(() =>
{
if (Configuration != Configuration.Release)
Expand Down
140 changes: 89 additions & 51 deletions build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public Build()
readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;

[Solution] readonly Solution Solution;

// the file we want to build, can be either full solution on Windows or a filtered one on other platforms
AbsolutePath SolutionFile;

[GitRepository] readonly GitRepository GitRepository;

AbsolutePath SourceDirectory => RootDirectory / "src";
Expand Down Expand Up @@ -85,6 +89,8 @@ string DetermineVersionPrefix()

protected override void OnBuildInitialized()
{
SolutionFile = IsRunningOnWindows ? Solution.Path : SourceDirectory / "NSwag.NoInstaller.slnf";

VersionPrefix = DetermineVersionPrefix();

var versionParts = VersionPrefix.Split('-');
Expand Down Expand Up @@ -122,6 +128,7 @@ protected override void OnBuildInitialized()

Target InstallDependencies => _ => _
.Before(Restore, Compile)
.OnlyWhenDynamic(() => !IsServerBuild)
.Executes(() =>
{
Chocolatey("install wixtoolset -y");
Expand All @@ -138,16 +145,8 @@ protected override void OnBuildInitialized()
.SetProcessWorkingDirectory(SourceDirectory / "NSwag.Npm")
);
MSBuild(x => x
.SetTargetPath(Solution)
.SetTargets("Restore")
.SetMaxCpuCount(Environment.ProcessorCount)
.SetNodeReuse(IsLocalBuild)
.SetVerbosity(MSBuildVerbosity.Minimal)
);
DotNetRestore(x => x
.SetProjectFile(Solution)
.SetProjectFile(SolutionFile)
.SetVerbosity(DotNetVerbosity.minimal)
);
});
Expand All @@ -162,48 +161,74 @@ protected override void OnBuildInitialized()
Serilog.Log.Information("Build and copy full .NET command line with configuration {Configuration}", Configuration);
// TODO: Fix build here
MSBuild(x => x
.SetProjectFile(GetProject("NSwagStudio"))
.SetTargets("Build")
.SetAssemblyVersion(VersionPrefix)
.SetFileVersion(VersionPrefix)
.SetInformationalVersion(VersionPrefix)
.SetConfiguration(Configuration)
.SetMaxCpuCount(Environment.ProcessorCount)
.SetNodeReuse(IsLocalBuild)
.SetVerbosity(MSBuildVerbosity.Minimal)
.SetProperty("Deterministic", IsServerBuild)
.SetProperty("ContinuousIntegrationBuild", IsServerBuild)
);
if (IsRunningOnWindows)
{
DotNetMSBuild(x => x
.SetTargetPath(GetProject("NSwagStudio"))
.SetAssemblyVersion(VersionPrefix)
.SetFileVersion(VersionPrefix)
.SetInformationalVersion(VersionPrefix)
.SetConfiguration(Configuration)
.SetMaxCpuCount(Environment.ProcessorCount)
.SetNodeReuse(IsLocalBuild)
.SetVerbosity(DotNetVerbosity.minimal)
.SetDeterministic(IsServerBuild)
.SetContinuousIntegrationBuild(IsServerBuild)
// ensure we don't generate too much output in CI run
// 0 Turns off emission of all warning messages
// 1 Displays severe warning messages
.SetWarningLevel(IsServerBuild ? 0 : 1)
);
MSBuild(x => x
.SetTargetPath(Solution)
.SetTargets("Build")
.SetAssemblyVersion(VersionPrefix)
.SetFileVersion(VersionPrefix)
.SetInformationalVersion(VersionPrefix)
.SetConfiguration(Configuration)
.SetMaxCpuCount(Environment.ProcessorCount)
.SetNodeReuse(IsLocalBuild)
.SetVerbosity(MSBuildVerbosity.Minimal)
.SetProperty("Deterministic", IsServerBuild)
.SetProperty("ContinuousIntegrationBuild", IsServerBuild)
);
MSBuild(x => x
.SetTargetPath(SolutionFile)
.SetAssemblyVersion(VersionPrefix)
.SetFileVersion(VersionPrefix)
.SetInformationalVersion(VersionPrefix)
.SetConfiguration(Configuration)
.SetMaxCpuCount(Environment.ProcessorCount)
.SetNodeReuse(IsLocalBuild)
.SetVerbosity(MSBuildVerbosity.Minimal)
.SetProperty("Deterministic", IsServerBuild)
.SetProperty("ContinuousIntegrationBuild", IsServerBuild)
// ensure we don't generate too much output in CI run
// 0 Turns off emission of all warning messages
// 1 Displays severe warning messages
.SetWarningLevel(IsServerBuild ? 0 : 1)
);
}
else
{
DotNetBuild(x => x
.SetProjectFile(SolutionFile)
.SetAssemblyVersion(VersionPrefix)
.SetFileVersion(VersionPrefix)
.SetInformationalVersion(VersionPrefix)
.SetConfiguration(Configuration)
.SetVerbosity(DotNetVerbosity.minimal)
.SetDeterministic(IsServerBuild)
.SetContinuousIntegrationBuild(IsServerBuild)
// ensure we don't generate too much output in CI run
// 0 Turns off emission of all warning messages
// 1 Displays severe warning messages
.SetWarningLevel(IsServerBuild ? 0 : 1)
);
}
// later steps need to have binaries in correct places
PublishAndCopyConsoleProjects();
});

Target Test => _ => _
.After(Compile)
.DependsOn(Compile)
.Executes(() =>
{
foreach (var project in Solution.AllProjects.Where(p => p.Name.EndsWith(".Tests")))
{
DotNetTest(x => x
.SetProjectFile(project)
.EnableNoRestore()
.EnableNoBuild()
.SetConfiguration(Configuration)
);
}
Expand All @@ -230,34 +255,47 @@ void PublishConsoleProject(Project project, string[] targetFrameworks)
.SetConfiguration(Configuration)
.SetDeterministic(IsServerBuild)
.SetContinuousIntegrationBuild(IsServerBuild)
// ensure we don't generate too much output in CI run
// 0 Turns off emission of all warning messages
// 1 Displays severe warning messages
.SetWarningLevel(IsServerBuild ? 0 : 1)
);
}
}

PublishConsoleProject(consoleX86Project, new[] { "net462" });
PublishConsoleProject(consoleProject, new[] { "net462" });
PublishConsoleProject(consoleCoreProject, new[] { "net6.0", "net7.0", "net8.0" });
if (IsRunningOnWindows)
{
PublishConsoleProject(consoleX86Project, ["net462"]);
PublishConsoleProject(consoleProject, ["net462"]);
}
PublishConsoleProject(consoleCoreProject, ["net6.0", "net7.0", "net8.0"]);

void CopyConsoleBinaries(AbsolutePath target)
{
// take just exe from X86 as other files are shared with console project
var consoleX86Directory = ArtifactsDirectory / "publish" / consoleX86Project.Name / Configuration;
CopyFileToDirectory(consoleX86Directory / "NSwag.x86.exe", target / "Win");
CopyFileToDirectory(consoleX86Directory / "NSwag.x86.exe.config", target / "Win");
var configuration = Configuration.ToString().ToLowerInvariant();

CopyDirectoryRecursively(ArtifactsDirectory / "publish" / consoleProject.Name / Configuration, target / "Win", DirectoryExistsPolicy.Merge);
if (IsRunningOnWindows)
{
var consoleX86Directory = ArtifactsDirectory / "publish" / consoleX86Project.Name / configuration;
CopyFileToDirectory(consoleX86Directory / "NSwag.x86.exe", target / "Win");
CopyFileToDirectory(consoleX86Directory / "NSwag.x86.exe.config", target / "Win");

CopyDirectoryRecursively(ArtifactsDirectory / "publish" / consoleCoreProject.Name / (Configuration + "_net6.0"), target / "Net60");
CopyDirectoryRecursively(ArtifactsDirectory / "publish" / consoleCoreProject.Name / (Configuration + "_net7.0"), target / "Net70");
CopyDirectoryRecursively(ArtifactsDirectory / "publish" / consoleCoreProject.Name / (Configuration + "_net8.0"), target / "Net80");
}
CopyDirectoryRecursively(ArtifactsDirectory / "publish" / consoleProject.Name / configuration, target / "Win", DirectoryExistsPolicy.Merge);
}

Serilog.Log.Information("Copy published Console for NSwagStudio");
CopyDirectoryRecursively(ArtifactsDirectory / "publish" / consoleCoreProject.Name / (configuration + "_net6.0"), target / "Net60");
CopyDirectoryRecursively(ArtifactsDirectory / "publish" / consoleCoreProject.Name / (configuration + "_net7.0"), target / "Net70");
CopyDirectoryRecursively(ArtifactsDirectory / "publish" / consoleCoreProject.Name / (configuration + "_net8.0"), target / "Net80");
}

CopyConsoleBinaries(target: NSwagStudioBinaries);
if (IsRunningOnWindows)
{
Serilog.Log.Information("Copy published Console for NSwagStudio");
CopyConsoleBinaries(target: NSwagStudioBinaries);
}

Serilog.Log.Information("Copy published Console for NPM");

CopyConsoleBinaries(target: SourceDirectory / "NSwag.Npm" / "bin" / "binaries");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Linq;
using System.Threading.Tasks;
using NConsole;
using NJsonSchema.Infrastructure;

#pragma warning disable 1591

Expand All @@ -28,7 +27,8 @@ public class ExecuteDocumentCommand : IConsoleCommand

public async Task<object> RunAsync(CommandLineProcessor processor, IConsoleHost host)
{
if (!string.IsNullOrEmpty(Input) && !Input.StartsWith("/") && !Input.StartsWith("-"))
// input can be nix-like file path starting with /
if (!string.IsNullOrEmpty(Input) && (!Input.StartsWith("/") || File.Exists(Input) || Input.EndsWith("nswag.json")) && !Input.StartsWith("-"))
{
await ExecuteDocumentAsync(host, Input);
}
Expand Down
Loading

0 comments on commit 0c9d87f

Please sign in to comment.