Skip to content

Commit

Permalink
Merge pull request #8 from Zastai/update-build-sdk-and-targets
Browse files Browse the repository at this point in the history
Update build SDK and targets
  • Loading branch information
Zastai committed Dec 5, 2023
2 parents 98d51f7 + fd57d80 commit adefa37
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 71 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Build

on:
push:
branches:
- 'main'
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
pull_request:
branches:
- 'main'

jobs:
build:
runs-on: ubuntu-latest
env:
dotnet-version: 8.0.x
strategy:
matrix:
configuration: ['Debug', 'Release']

steps:
- name: Check out the project
uses: actions/checkout@v4
- name: Set up .NET ${{env.dotnet-version}}
uses: actions/setup-dotnet@v4
id: setup
with:
dotnet-version: ${{env.dotnet-version}}
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: Create global.json to force use of .NET SDK ${{steps.setup.outputs.dotnet-version}}
run: echo '{"sdk":{"version":"${{steps.setup.outputs.dotnet-version}}"}}' > ./global.json
- name: Run build script (${{matrix.configuration}})
run: pwsh ./build-package.ps1 -WithBinLog -Configuration ${{matrix.configuration}}
- name: "Artifact: MSBuild Logs"
uses: actions/upload-artifact@v3
if: failure()
with:
name: MSBuild Logs (${{matrix.configuration}})
path: msbuild.*.binlog
- name: "Artifact: NuGet Packages"
uses: actions/upload-artifact@v3
with:
name: NuGet Packages (${{matrix.configuration}})
path: "output/package/${{matrix.configuration}}/*.*nupkg"
- name: Publish (NuGet - GitHub Packages)
if: matrix.configuration == 'Release' && startsWith(github.ref, 'refs/tags/v')
run: "dotnet nuget push output/package/${{matrix.configuration}}/*.*nupkg -s https://nuget.pkg.github.com/zastai/index.json -k ${{secrets.GITHUB_TOKEN}}"
- name: Publish (NuGet - nuget.org)
if: matrix.configuration == 'Release' && startsWith(github.ref, 'refs/tags/v')
run: "dotnet nuget push output/package/${{matrix.configuration}}/*.nupkg -s https://api.nuget.org/v3/index.json -k ${{secrets.NUGET_API_KEY}}"
2 changes: 0 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<!-- Package Versions -->
<ItemGroup>
<PackageVersion Include="JetBrains.Annotations" Version="2021.3.0" />
<PackageVersion Include="System.Memory" Version="4.5.4" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>

</Project>
3 changes: 1 addition & 2 deletions MetaBrainz.Common.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Support Files", "Support Files", "{96AE5FBC-DDA2-423D-8A21-FDC6A97015D4}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
appveyor.yml = appveyor.yml
build-package.ps1 = build-package.ps1
Directory.Packages.props = Directory.Packages.props
global.json = global.json
LICENSE.md = LICENSE.md
README.md = README.md
EndProjectSection
Expand All @@ -23,6 +21,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "GitHub", "GitHub", "{1E672E
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Actions", "Actions", "{23BF0B90-7393-4926-B495-32AAB375102C}"
ProjectSection(SolutionItems) = preProject
.github\workflows\build.yml = .github\workflows\build.yml
.github\workflows\release-drafter.yml = .github\workflows\release-drafter.yml
.github\workflows\update-labels.yml = .github\workflows\update-labels.yml
EndProjectSection
Expand Down
17 changes: 4 additions & 13 deletions MetaBrainz.Common/HttpUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,15 @@ public static string GetStringContent(HttpResponseMessage response)
CancellationToken cancellationToken = new()) {
var content = response.Content;
Debug.Print($"[{DateTime.UtcNow}] => RESPONSE ({content.Headers.ContentType}): {content.Headers.ContentLength} bytes");
#if NET
var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
await using var _ = stream.ConfigureAwait(false);
#elif NETSTANDARD2_1_OR_GREATER
var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
await using var _ = stream.ConfigureAwait(false);
#else
using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
#endif
#if !NET
if (stream is null) {
return "";
}
#endif
var characterSet = HttpUtils.GetContentEncoding(content.Headers);
using var sr = new StreamReader(stream, Encoding.GetEncoding(characterSet), false, 1024, true);
// This is not (yet?) cancelable
#if NET6_0
var text = await sr.ReadToEndAsync().ConfigureAwait(false);
#else
var text = await sr.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
#endif
Debug.Print($"[{DateTime.UtcNow}] => RESPONSE TEXT: {TextUtils.FormatMultiLine(text)}");
return text;
}
Expand Down
8 changes: 4 additions & 4 deletions MetaBrainz.Common/MetaBrainz.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="MetaBrainz.Build.Sdk">
<Project Sdk="MetaBrainz.Build.Sdk/3.0.0">

<PropertyGroup>
<Authors>Zastai</Authors>
<Title>Common Utilities</Title>
<Description>This package provides general utility classes used by other MetaBrainz packages.</Description>
<PackageCopyrightYears>2022</PackageCopyrightYears>
<PackageCopyrightOwners>Tim Van Holder</PackageCopyrightOwners>
<PackageCopyrightYears>2022, 2023</PackageCopyrightYears>
<PackageRepositoryName>MetaBrainz.Common</PackageRepositoryName>
<PackageTags>MetaBrainz</PackageTags>
<Version>1.0.1-pre</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" IncludeAssets="compile" PrivateAssets="all" />
<PackageReference Include="System.Memory" />
<PackageReference Include="System.Net.Http" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion MetaBrainz.Common/RateLimitInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace MetaBrainz.Common;
if (text is null) {
return null;
}
return long.TryParse(text, out var value) ? UnixTime.Convert(value) : null;
return long.TryParse(text, out var value) ? DateTimeOffset.FromUnixTimeSeconds(value) : null;
}

}
9 changes: 2 additions & 7 deletions MetaBrainz.Common/TextUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ public static class TextUtils {
/// <summary>Decodes all the bytes in the specified span as a string, using the UTF-8 character set.</summary>
/// <param name="bytes">A read-only byte span to decode to a Unicode string.</param>
/// <returns>A string that contains the decoded bytes from the provided read-only span.</returns>
public static string DecodeUtf8(ReadOnlySpan<byte> bytes) {
#if NETFRAMEWORK || NETSTANDARD2_0 // No Span-based API
return Encoding.UTF8.GetString(bytes.ToArray());
#else
return Encoding.UTF8.GetString(bytes);
#endif
}
[Obsolete("Call Encoding.UTF8.GetString() instead.")]
public static string DecodeUtf8(ReadOnlySpan<byte> bytes) => Encoding.UTF8.GetString(bytes);

/// <summary>Formats a string, including extra handling if it's multiline.</summary>
/// <param name="text">The string to format. Trailing line breaks are discarded.</param>
Expand Down
36 changes: 6 additions & 30 deletions MetaBrainz.Common/UnixTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,35 @@ namespace MetaBrainz.Common;

/// <summary>Utility class for working with Unix time values (seconds since 1970-01-01T00:00:00).</summary>
[PublicAPI]
[Obsolete($"Use {nameof(DateTimeOffset)} instead.")]
public static class UnixTime {

#if NET || NETSTANDARD2_1_OR_GREATER // Unix Time support available

/// <summary>The epoch for Unix time values (1970-01-01T00:00:00Z).</summary>
[Obsolete($"Use {nameof(DateTimeOffset)}.{nameof(DateTimeOffset.UnixEpoch)} instead.")]
public static readonly DateTimeOffset Epoch = DateTimeOffset.UnixEpoch;

/// <summary>Computes the Unix time value corresponding to the specified date/time.</summary>
/// <param name="value">The date/time to convert to a Unix time value.</param>
/// <returns>The corresponding Unix time value.</returns>
[Obsolete($"Use {nameof(DateTimeOffset)}.{nameof(DateTimeOffset.ToUnixTimeSeconds)} instead.")]
public static long Convert(DateTimeOffset value) => value.ToUnixTimeSeconds();

/// <summary>Computes the Unix time value corresponding to the specified date/time.</summary>
/// <param name="value">The date/time to convert to a Unix time value.</param>
/// <returns>The corresponding Unix time value.</returns>
[Obsolete($"Use {nameof(DateTimeOffset)}.{nameof(DateTimeOffset.ToUnixTimeSeconds)} instead.")]
public static long? Convert(DateTimeOffset? value) => value?.ToUnixTimeSeconds();

/// <summary>Computes the date/time corresponding to the specified Unix time value.</summary>
/// <param name="value">The Unix time value to convert to a date/time.</param>
/// <returns>The corresponding date/time.</returns>
[Obsolete($"Use {nameof(DateTimeOffset)}.{nameof(DateTimeOffset.FromUnixTimeSeconds)} instead.")]
public static DateTimeOffset Convert(long value) => DateTimeOffset.FromUnixTimeSeconds(value);

/// <summary>Computes the date/time corresponding to the specified Unix time value.</summary>
/// <param name="value">The Unix time value to convert to a date/time.</param>
/// <returns>The corresponding date/time.</returns>
[Obsolete($"Use {nameof(DateTimeOffset)}.{nameof(DateTimeOffset.FromUnixTimeSeconds)} instead.")]
public static DateTimeOffset? Convert(long? value) => value.HasValue ? DateTimeOffset.FromUnixTimeSeconds(value.Value) : null;

#else

/// <summary>The epoch for Unix time values (1970-01-01T00:00:00Z).</summary>
public static readonly DateTimeOffset Epoch = new(621355968000000000L, TimeSpan.Zero);

/// <summary>Computes the Unix time value corresponding to the specified date/time.</summary>
/// <param name="value">The date/time to convert to a Unix time value.</param>
/// <returns>The corresponding Unix time value.</returns>
public static long Convert(DateTimeOffset value) => (long) (value - UnixTime.Epoch).TotalSeconds;

/// <summary>Computes the Unix time value corresponding to the specified date/time.</summary>
/// <param name="value">The date/time to convert to a Unix time value.</param>
/// <returns>The corresponding Unix time value.</returns>
public static long? Convert(DateTimeOffset? value)
=> value.HasValue ? (long?) (value.Value - UnixTime.Epoch).TotalSeconds : null;

/// <summary>Computes the date/time corresponding to the specified Unix time value.</summary>
/// <param name="value">The Unix time value to convert to a date/time.</param>
/// <returns>The corresponding date/time.</returns>
public static DateTimeOffset Convert(long value) => UnixTime.Epoch.AddSeconds(value);

/// <summary>Computes the date/time corresponding to the specified Unix time value.</summary>
/// <param name="value">The Unix time value to convert to a date/time.</param>
/// <returns>The corresponding date/time.</returns>
public static DateTimeOffset? Convert(long? value) => value.HasValue ? UnixTime.Epoch.AddSeconds(value.Value) : null;

#endif

}
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# MetaBrainz.Common [![Build Status](https://img.shields.io/appveyor/build/zastai/metabrainz-common)](https://ci.appveyor.com/project/Zastai/metabrainz-common) [![NuGet Version](https://img.shields.io/nuget/v/MetaBrainz.Common)](https://www.nuget.org/packages/MetaBrainz.Common)
# MetaBrainz.Common [![Build Status][CI-S]][CI-L] [![NuGet Package Version][NuGet-S]][NuGet-L]

General helper classes, for use by the other `MetaBrainz.*` packages.

[CI-S]: https://github.com/Zastai/MetaBrainz.Common/actions/workflows/build.yml/badge.svg
[CI-L]: https://github.com/Zastai/MetaBrainz.Common/actions/workflows/build.yml

[NuGet-S]: https://img.shields.io/nuget/v/MetaBrainz.Common
[NuGet-L]: https://www.nuget.org/packages/MetaBrainz.Common
6 changes: 0 additions & 6 deletions appveyor.yml

This file was deleted.

5 changes: 0 additions & 5 deletions global.json

This file was deleted.

0 comments on commit adefa37

Please sign in to comment.