Skip to content

Commit ee88b0d

Browse files
committed
Add synchronous methods to the Setup class
1 parent c59e098 commit ee88b0d

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/Cmdlets/Install-Release.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class InstallReleaseCommand: PSCmdlet {
3030
/// </summary>
3131
protected override void ProcessRecord() {
3232
var release = ParameterSetName == nameof(InputObject) ? InputObject : Release.Find(Constraint);
33-
if (release?.Exists ?? false) WriteObject(new Setup(release).Install(OptionalTasks).GetAwaiter().GetResult());
33+
if (release?.Exists ?? false) WriteObject(new Setup(release).Install(OptionalTasks));
3434
else {
3535
var exception = new InvalidOperationException("No release matches the specified version constraint.");
3636
WriteError(new ErrorRecord(exception, "ReleaseNotFound", ErrorCategory.ObjectNotFound, null));

src/Setup.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@ public class Setup(Release release) {
1515
/// The release to download and install.
1616
/// </summary>
1717
public Release Release => release;
18+
19+
/// <summary>
20+
/// Downloads and extracts the ZIP archive of Apache Ant.
21+
/// </summary>
22+
/// <param name="optionalTasks">Value indicating whether to fetch the Ant optional tasks.</param>
23+
/// <returns>The path to the extracted directory.</returns>
24+
public string Download(bool optionalTasks = false) => DownloadAsync(optionalTasks, CancellationToken.None).GetAwaiter().GetResult();
1825

1926
/// <summary>
2027
/// Downloads and extracts the ZIP archive of Apache Ant.
2128
/// </summary>
2229
/// <param name="optionalTasks">Value indicating whether to fetch the Ant optional tasks.</param>
2330
/// <param name="cancellationToken">The token to cancel the operation.</param>
2431
/// <returns>The path to the extracted directory.</returns>
25-
public async Task<string> Download(bool optionalTasks = false, CancellationToken cancellationToken = default) {
32+
public async Task<string> DownloadAsync(bool optionalTasks = false, CancellationToken cancellationToken = default) {
2633
using var httpClient = new HttpClient();
2734
var version = GetType().Assembly.GetName().Version!;
2835
httpClient.DefaultRequestHeaders.Add("User-Agent", $".NET/{Environment.Version.ToString(3)} | SetupAnt/{version.ToString(3)}");
@@ -39,15 +46,22 @@ public async Task<string> Download(bool optionalTasks = false, CancellationToken
3946
if (optionalTasks) await FetchOptionalTasks(antHome);
4047
return antHome;
4148
}
49+
50+
/// <summary>
51+
/// Installs Apache Ant, after downloading it.
52+
/// </summary>
53+
/// <param name="optionalTasks">Value indicating whether to fetch the Ant optional tasks.</param>
54+
/// <returns>The path to the installation directory.</returns>
55+
public string Install(bool optionalTasks = false) => InstallAsync(optionalTasks, CancellationToken.None).GetAwaiter().GetResult();
4256

4357
/// <summary>
4458
/// Installs Apache Ant, after downloading it.
4559
/// </summary>
4660
/// <param name="optionalTasks">Value indicating whether to fetch the Ant optional tasks.</param>
4761
/// <param name="cancellationToken">The token to cancel the operation.</param>
4862
/// <returns>The path to the installation directory.</returns>
49-
public async Task<string> Install(bool optionalTasks = false, CancellationToken cancellationToken = default) {
50-
var antHome = await Download(optionalTasks, cancellationToken);
63+
public async Task<string> InstallAsync(bool optionalTasks = false, CancellationToken cancellationToken = default) {
64+
var antHome = await DownloadAsync(optionalTasks, cancellationToken);
5165

5266
var binFolder = Path.Join(antHome, "bin");
5367
Environment.SetEnvironmentVariable("PATH", $"{Environment.GetEnvironmentVariable("PATH")}{Path.PathSeparator}{binFolder}");

test/Setup.Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void ClassInitialize(TestContext testContext) {
1616

1717
[TestMethod]
1818
public async Task Download() {
19-
var path = await new Setup(Release.Latest).Download(optionalTasks: true, testContext.CancellationToken);
19+
var path = await new Setup(Release.Latest).DownloadAsync(optionalTasks: true, testContext.CancellationToken);
2020
IsTrue(File.Exists(Path.Join(path, "bin", OperatingSystem.IsWindows() ? "ant.cmd" : "ant")));
2121

2222
var jars = Directory.EnumerateFiles(Path.Join(path, "lib"), "*.jar");
@@ -25,7 +25,7 @@ public async Task Download() {
2525

2626
[TestMethod]
2727
public async Task Install() {
28-
var path = await new Setup(Release.Latest).Install(optionalTasks: false, testContext.CancellationToken);
28+
var path = await new Setup(Release.Latest).InstallAsync(optionalTasks: false, testContext.CancellationToken);
2929
AreEqual(path, Environment.GetEnvironmentVariable("ANT_HOME"));
3030
Contains(path, Environment.GetEnvironmentVariable("PATH")!);
3131
}

0 commit comments

Comments
 (0)