Skip to content

Commit

Permalink
Add workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Guy Fankam committed Jul 22, 2024
1 parent 27d489d commit 3715a59
Show file tree
Hide file tree
Showing 98 changed files with 9,212 additions and 249 deletions.
3 changes: 2 additions & 1 deletion tools/code/aspire/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ private static void Main(string[] args)
{
var builder = DistributedApplication.CreateBuilder(args);

builder.AddProject<integration_tests>("integration-tests");
//builder.AddProject<extractor>("extractor");
builder.AddProject<publisher>("publisher");

builder.Build().Run();
}
Expand Down
2 changes: 2 additions & 0 deletions tools/code/aspire/aspire.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\extractor\extractor.csproj" />
<ProjectReference Include="..\integration.tests\integration.tests.csproj" />
<ProjectReference Include="..\publisher\publisher.csproj" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions tools/code/common.tests/Workspace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using CsCheck;
using LanguageExt;
using System.Collections.Frozen;
using System.Linq;

namespace common.tests;

public sealed record WorkspaceModel
{
public required WorkspaceName Name { get; init; }
public required string DisplayName { get; init; }
public Option<string> Description { get; init; }

public static Gen<WorkspaceModel> Generate() =>
from name in GenerateName()
from displayName in GenerateDisplayName()
from description in GenerateDescription().OptionOf()
select new WorkspaceModel
{
Name = name,
DisplayName = displayName,
Description = description
};

public static Gen<WorkspaceName> GenerateName() =>
from name in Generator.AlphaNumericStringBetween(10, 20)
select WorkspaceName.From(name);

public static Gen<string> GenerateDisplayName() =>
Generator.AlphaNumericStringBetween(10, 20);

public static Gen<string> GenerateDescription() =>
from lorem in Generator.Lorem
select lorem.Paragraph();

public static Gen<FrozenSet<WorkspaceModel>> GenerateSet() =>
Generate().FrozenSetOf(x => x.Name, 0, 10)
.DistinctBy(x => x.DisplayName);
}
2 changes: 1 addition & 1 deletion tools/code/common.tests/common.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bogus" Version="35.5.1" />
<PackageReference Include="Bogus" Version="35.6.0" />
<PackageReference Include="CsCheck" Version="3.2.2" />
</ItemGroup>

Expand Down
18 changes: 0 additions & 18 deletions tools/code/common/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -730,24 +730,6 @@ public static async ValueTask<ApiDto> ReadDto(this ApiInformationFile file, Canc
public static async ValueTask WriteSpecification(this ApiSpecificationFile file, BinaryData contents, CancellationToken cancellationToken) =>
await file.ToFileInfo().OverwriteWithBinaryData(contents, cancellationToken);

public static FileInfo ToFileInfo(this ApiSpecificationFile file) =>
file switch
{
GraphQlSpecificationFile graphQl => graphQl.ToFileInfo(),
WadlSpecificationFile wadl => wadl.ToFileInfo(),
WsdlSpecificationFile wsdl => wsdl.ToFileInfo(),
OpenApiSpecificationFile openApi => openApi switch
{
YamlOpenApiSpecificationFile yaml => yaml.ToFileInfo(),
JsonOpenApiSpecificationFile json => json.ToFileInfo(),
_ => throw new NotSupportedException()
},
_ => throw new NotSupportedException()
};

public static async ValueTask<BinaryData> ReadContents(this ApiSpecificationFile file, CancellationToken cancellationToken) =>
await file.ToFileInfo().ReadAsBinaryData(cancellationToken);

public static Option<VersionSetName> TryGetVersionSetName(ApiDto dto) =>
from versionSetId in Prelude.Optional(dto.Properties.ApiVersionSetId)
from versionSetNameString in versionSetId.Split('/')
Expand Down
5 changes: 1 addition & 4 deletions tools/code/common/ApiSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -93,7 +92,6 @@ public static async ValueTask<Option<ApiSpecificationFile>> TryParse(FileInfo? f
select specificationFile as ApiSpecificationFile;

return await ImmutableArray.Create(tryParseGraphQl, tryParseWadl, tryParseWsdl, tryParseOpenApi)
.ToAsyncEnumerable()
.Pick(async (f, cancellationToken) => await f(), cancellationToken);
}
}
Expand All @@ -116,7 +114,7 @@ public static Option<GraphQlSpecificationFile> TryParse(FileInfo? file, Manageme
: Option<GraphQlSpecificationFile>.None;
}

public sealed record class WadlSpecificationFile : ApiSpecificationFile
public sealed record WadlSpecificationFile : ApiSpecificationFile
{
public override ApiSpecification Specification { get; } = new ApiSpecification.Wadl();

Expand Down Expand Up @@ -180,7 +178,6 @@ await TryParse(file,
select json as OpenApiSpecificationFile;

return await ImmutableArray.Create(tryParseYaml, tryParseJson)
.ToAsyncEnumerable()
.Pick(async (f, cancellationToken) => await f(), cancellationToken);
}
}
Expand Down
17 changes: 9 additions & 8 deletions tools/code/common/ApiTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,21 @@ public static async ValueTask PutDto(this ApiTagUri uri, ApiTagDto dto, HttpPipe
public static IEnumerable<ApiTagInformationFile> ListInformationFiles(ApiName apiName, ManagementServiceDirectory serviceDirectory) =>
ListApiTagsDirectories(apiName, serviceDirectory)
.SelectMany(ListApiTagDirectories)
.Select(directory => ApiTagInformationFile.From(directory.Name, apiName, serviceDirectory));
.Select(directory => ApiTagInformationFile.From(directory.Name, apiName, serviceDirectory))
.Where(informationFile => informationFile.ToFileInfo().Exists());

private static IEnumerable<ApiTagsDirectory> ListApiTagsDirectories(ApiName apiName, ManagementServiceDirectory serviceDirectory) =>
ApiDirectory.From(apiName, serviceDirectory)
.ToDirectoryInfo()
.ListDirectories("*")
.Where(ApiTagsDirectory.IsDirectoryNameValid)
.Select(_ => ApiTagsDirectory.From(apiName, serviceDirectory));
.ToDirectoryInfo()
.ListDirectories("*")
.Where(ApiTagsDirectory.IsDirectoryNameValid)
.Select(_ => ApiTagsDirectory.From(apiName, serviceDirectory));

private static IEnumerable<ApiTagDirectory> ListApiTagDirectories(ApiTagsDirectory apiTagsDirectory) =>
apiTagsDirectory.ToDirectoryInfo()
.ListDirectories("*")
.Choose(directory => from name in ApiTagDirectory.TryParseApiTagName(directory)
select new ApiTagDirectory { Name = name, Parent = apiTagsDirectory });
.ListDirectories("*")
.Choose(directory => from name in ApiTagDirectory.TryParseApiTagName(directory)
select new ApiTagDirectory { Name = name, Parent = apiTagsDirectory });

public static async ValueTask WriteDto(this ApiTagInformationFile file, ApiTagDto dto, CancellationToken cancellationToken)
{
Expand Down
77 changes: 43 additions & 34 deletions tools/code/common/Backend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading;
Expand All @@ -27,7 +26,8 @@ public sealed record BackendsUri : ResourceUri

private static string PathSegment { get; } = "backends";

protected override Uri Value => ServiceUri.ToUri().AppendPathSegment(PathSegment).ToUri();
protected override Uri Value =>
ServiceUri.ToUri().AppendPathSegment(PathSegment).ToUri();

public static BackendsUri From(ManagementServiceUri serviceUri) =>
new() { ServiceUri = serviceUri };
Expand All @@ -36,9 +36,11 @@ public static BackendsUri From(ManagementServiceUri serviceUri) =>
public sealed record BackendUri : ResourceUri
{
public required BackendsUri Parent { get; init; }

public required BackendName Name { get; init; }

protected override Uri Value => Parent.ToUri().AppendPathSegment(Name.ToString()).ToUri();
protected override Uri Value =>
Parent.ToUri().AppendPathSegment(Name.ToString()).ToUri();

public static BackendUri From(BackendName name, ManagementServiceUri serviceUri) =>
new()
Expand All @@ -61,9 +63,8 @@ public static BackendsDirectory From(ManagementServiceDirectory serviceDirectory
new() { ServiceDirectory = serviceDirectory };

public static Option<BackendsDirectory> TryParse(DirectoryInfo? directory, ManagementServiceDirectory serviceDirectory) =>
directory is not null &&
directory.Name == Name &&
directory.Parent?.FullName == serviceDirectory.ToDirectoryInfo().FullName
directory?.Name == Name &&
directory?.Parent?.FullName == serviceDirectory.ToDirectoryInfo().FullName
? new BackendsDirectory { ServiceDirectory = serviceDirectory }
: Option<BackendsDirectory>.None;
}
Expand All @@ -75,7 +76,7 @@ public sealed record BackendDirectory : ResourceDirectory
public required BackendName Name { get; init; }

protected override DirectoryInfo Value =>
Parent.ToDirectoryInfo().GetChildDirectory(Name.ToString());
Parent.ToDirectoryInfo().GetChildDirectory(Name.Value);

public static BackendDirectory From(BackendName name, ManagementServiceDirectory serviceDirectory) =>
new()
Expand All @@ -86,35 +87,37 @@ public static BackendDirectory From(BackendName name, ManagementServiceDirectory

public static Option<BackendDirectory> TryParse(DirectoryInfo? directory, ManagementServiceDirectory serviceDirectory) =>
from parent in BackendsDirectory.TryParse(directory?.Parent, serviceDirectory)
let name = BackendName.From(directory!.Name)
select new BackendDirectory
{
Parent = parent,
Name = BackendName.From(directory!.Name)
Name = name
};
}

public sealed record BackendInformationFile : ResourceFile
{
public required BackendDirectory Parent { get; init; }
private static string Name { get; } = "backendInformation.json";

public static string Name { get; } = "backendInformation.json";

protected override FileInfo Value =>
Parent.ToDirectoryInfo().GetChildFile(Name);

public static BackendInformationFile From(BackendName name, ManagementServiceDirectory serviceDirectory) =>
new()
{
Parent = new BackendDirectory
{
Parent = BackendsDirectory.From(serviceDirectory),
Name = name
}
Parent = BackendDirectory.From(name, serviceDirectory)
};

public static Option<BackendInformationFile> TryParse(FileInfo? file, ManagementServiceDirectory serviceDirectory) =>
file is not null && file.Name == Name
file is not null &&
file.Name == Name
? from parent in BackendDirectory.TryParse(file.Directory, serviceDirectory)
select new BackendInformationFile { Parent = parent }
select new BackendInformationFile
{
Parent = parent
}
: Option<BackendInformationFile>.None;
}

Expand Down Expand Up @@ -277,23 +280,25 @@ public static class BackendModule
{
public static async ValueTask DeleteAll(this BackendsUri uri, HttpPipeline pipeline, CancellationToken cancellationToken) =>
await uri.ListNames(pipeline, cancellationToken)
.IterParallel(async name => await BackendUri.From(name, uri.ServiceUri)
.Delete(pipeline, cancellationToken),
cancellationToken);
.IterParallel(async name =>
{
var backendUri = new BackendUri { Parent = uri, Name = name };
await backendUri.Delete(pipeline, cancellationToken);
}, cancellationToken);

public static IAsyncEnumerable<BackendName> ListNames(this BackendsUri uri, HttpPipeline pipeline, CancellationToken cancellationToken) =>
pipeline.ListJsonObjects(uri.ToUri(), cancellationToken)
.Select(jsonObject => jsonObject.GetStringProperty("name"))
.Select(BackendName.From);

public static IAsyncEnumerable<(BackendName Name, BackendDto Dto)> List(this BackendsUri backendsUri, HttpPipeline pipeline, CancellationToken cancellationToken) =>
backendsUri.ListNames(pipeline, cancellationToken)
.SelectAwait(async name =>
{
var uri = new BackendUri { Parent = backendsUri, Name = name };
var dto = await uri.GetDto(pipeline, cancellationToken);
return (name, dto);
});
public static IAsyncEnumerable<(BackendName Name, BackendDto Dto)> List(this BackendsUri uri, HttpPipeline pipeline, CancellationToken cancellationToken) =>
uri.ListNames(pipeline, cancellationToken)
.SelectAwait(async name =>
{
var backendUri = new BackendUri { Parent = uri, Name = name };
var dto = await backendUri.GetDto(pipeline, cancellationToken);
return (name, dto);
});

public static async ValueTask<BackendDto> GetDto(this BackendUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
Expand All @@ -314,16 +319,20 @@ public static IEnumerable<BackendDirectory> ListDirectories(ManagementServiceDir
{
var backendsDirectory = BackendsDirectory.From(serviceDirectory);

return backendsDirectory.ToDirectoryInfo()
.ListDirectories("*")
.Select(directoryInfo => BackendName.From(directoryInfo.Name))
.Select(name => new BackendDirectory { Parent = backendsDirectory, Name = name });
return from backendsDirectoryInfo in backendsDirectory.ToDirectoryInfo().ListDirectories("*")
let name = BackendName.From(backendsDirectoryInfo.Name)
select new BackendDirectory
{
Parent = backendsDirectory,
Name = name
};
}

public static IEnumerable<BackendInformationFile> ListInformationFiles(ManagementServiceDirectory serviceDirectory) =>
ListDirectories(serviceDirectory)
.Select(directory => new BackendInformationFile { Parent = directory })
.Where(informationFile => informationFile.ToFileInfo().Exists());
from backendDirectory in ListDirectories(serviceDirectory)
let informationFile = new BackendInformationFile { Parent = backendDirectory }
where informationFile.ToFileInfo().Exists()
select informationFile;

public static async ValueTask WriteDto(this BackendInformationFile file, BackendDto dto, CancellationToken cancellationToken)
{
Expand Down
19 changes: 19 additions & 0 deletions tools/code/common/Enumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ public static IEnumerable<T2> Choose<T, T2>(this IEnumerable<T> enumerable, Func
return EnumerableMExtensions.Choose(enumerableM, f);
}

/// <summary>
/// Applies <paramref name="f"/> to each element of <paramref name="enumerable"/> and returns the first Option of <typeparamref name="T2"/>
/// that is Some. If all options are None, returns a None.
/// </summary>
public static async ValueTask<Option<T2>> Pick<T, T2>(this IEnumerable<T> enumerable, Func<T, CancellationToken, ValueTask<Option<T2>>> f, CancellationToken cancellationToken)
{
foreach (var item in enumerable)
{
var option = await f(item, cancellationToken);

if (option.IsSome)
{
return option;
}
}

return Option<T2>.None;
}

/// <summary>
/// Returns the first item in the enumerable. If the enumerable is empty, returns <seealso cref="Option.None"/>.
/// </summary>
Expand Down
7 changes: 0 additions & 7 deletions tools/code/common/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading;
Expand All @@ -17,12 +16,6 @@ public sealed record LoggerName : ResourceName
{
private LoggerName(string value) : base(value) { }

/// <summary>
/// Logger names with revisions have the format 'loggerName;revision'
/// </summary>
public LoggerName ToNonRevisionedName() =>
new(Value.Split(';').First());

public static LoggerName From(string value) => new(value);
}

Expand Down
1 change: 0 additions & 1 deletion tools/code/common/NamedValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
Expand Down
1 change: 0 additions & 1 deletion tools/code/common/PolicyFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
Expand Down
1 change: 0 additions & 1 deletion tools/code/common/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
Expand Down
Loading

0 comments on commit 3715a59

Please sign in to comment.