Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 61baa48

Browse files
authored
fix(vsts): Update build script to accept command line args (#166)
1 parent 9246956 commit 61baa48

File tree

20 files changed

+105
-37
lines changed

20 files changed

+105
-37
lines changed

build.ps1

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,16 @@ Function BuildProject($path, $message) {
6868
}
6969
}
7070

71-
Function RunApp($path, $message, $args) {
71+
Function RunApp($path, $message, $params) {
7272

73-
$label = "RUN: --- $message $configuration ($args)---"
73+
$label = "RUN: --- $message $configuration ($params)---"
7474

7575
Write-Host
7676
Write-Host -ForegroundColor Cyan $label
7777
cd (Join-Path $rootDir $path)
7878

79-
& dotnet run $args
79+
$runCommand = "dotnet run -- $params"
80+
Invoke-Expression $runCommand
8081

8182
if ($LASTEXITCODE -ne 0) {
8283
throw "Tests failed: $label"
@@ -103,26 +104,34 @@ try {
103104

104105
if ($run)
105106
{
106-
# Run cleanup first so the samples don't get overloaded with old device instances
107+
$sampleRunningTime = 60
108+
109+
# Run cleanup first so the samples don't get overloaded with old devices
107110
RunApp provisioning\Samples\service\CleanupEnrollmentsSample "Provisioning\Service\CleanupEnrollmentsSample"
108111
RunApp iot-hub\Samples\service\CleanUpDevicesSample "IoTHub\Service\CleanUpDevicesSample"
109112

110-
RunApp iot-hub\Samples\device\FileUploadSample "IoTHub\Device\FileUploadSample"
111-
RunApp iot-hub\Samples\device\KeysRolloverSample "IoTHub\Device\KeysRolloverSample"
112-
RunApp iot-hub\Samples\device\MessageSample "IoTHub\Device\MessageSample"
113-
RunApp iot-hub\Samples\device\MethodSample "IoTHub\Device\MethodSample"
114-
RunApp iot-hub\Samples\device\TwinSample "IoTHub\Device\TwinSample"
113+
RunApp iot-hub\Samples\device\DeviceReconnectionSample "IoTHub\Device\DeviceReconnectionSample" "-p ""$env:IOTHUB_DEVICE_CONN_STRING"" -r $sampleRunningTime"
114+
RunApp iot-hub\Samples\device\FileUploadSample "IoTHub\Device\FileUploadSample" "-p ""$env:IOTHUB_DEVICE_CONN_STRING"""
115+
RunApp iot-hub\Samples\device\MessageReceiveSample "IoTHub\Device\MessageReceiveSample" "-p ""$env:IOTHUB_DEVICE_CONN_STRING"""
116+
RunApp iot-hub\Samples\device\MethodSample "IoTHub\Device\MethodSample" "-p ""$env:IOTHUB_DEVICE_CONN_STRING"""
117+
RunApp iot-hub\Samples\device\TwinSample "IoTHub\Device\TwinSample" "-p ""$env:IOTHUB_DEVICE_CONN_STRING"""
118+
119+
$pnpDeviceSecurityType = "connectionString"
120+
RunApp iot-hub\Samples\device\PnpDeviceSamples\TemperatureController "IoTHub\Device\PnpDeviceSamples\TemperatureController" "-s $pnpDeviceSecurityType -p ""$env:PNP_TC_DEVICE_CONN_STRING"" -r $sampleRunningTime"
121+
RunApp iot-hub\Samples\device\PnpDeviceSamples\Thermostat "IoTHub\Device\PnpDeviceSamples\Thermostat" "-s $pnpDeviceSecurityType -p ""$env:PNP_THERMOSTAT_DEVICE_CONN_STRING"" -r $sampleRunningTime"
122+
# DeviceStreaming sample is not added since it is not available in all regions.
115123

116-
RunApp iot-hub\Samples\module\MessageSample "IoTHub\Module\MessageSample"
117-
RunApp iot-hub\Samples\module\TwinSample "IoTHub\Module\TwinSample"
124+
RunApp iot-hub\Samples\module\ModuleSample "IoTHub\Module\ModuleSample" "-p ""$env:IOTHUB_MODULE_CONN_STRING"" -r $sampleRunningTime"
118125

119126
RunApp iot-hub\Samples\service\AutomaticDeviceManagementSample "IoTHub\Service\AutomaticDeviceManagementSample"
127+
RunApp iot-hub\Samples\service\EdgeDeploymentSample "IoTHub\Service\EdgeDeploymentSample"
120128
RunApp iot-hub\Samples\service\JobsSample "IoTHub\Service\JobsSample"
121129
RunApp iot-hub\Samples\service\RegistryManagerSample "IoTHub\Service\RegistryManagerSample"
122130

123131
$deviceId = ($Env:IOTHUB_DEVICE_CONN_STRING.Split(';') | where {$_ -like "DeviceId*"}).Split("=")[1]
124-
Write-Warning $deviceId
125-
RunApp iot-hub\Samples\service\ServiceClientSample "IoTHub\Service\ServiceClientSample" - $deviceId
132+
Write-Warning "Using device $deviceId for the ServiceClientSample."
133+
RunApp iot-hub\Samples\service\ServiceClientSample "IoTHub\Service\ServiceClientSample" "-c ""$env:IOTHUB_CONN_STRING_CSHARP"" -d $deviceId -r $sampleRunningTime"
134+
# DigitalTwinClientSamples and PnpServiceSamples are not added here since they require the device counterparts to be running as well.
126135

127136
# TODO #11: Modify Provisioning\device samples to run unattended.
128137

iot-hub/Samples/device/DeviceReconnectionSample/DeviceReconnectionSample.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public DeviceReconnectionSample(List<string> deviceConnectionStrings, TransportT
5353

5454
private bool IsDeviceConnected => s_connectionStatus == ConnectionStatus.Connected;
5555

56-
public async Task RunSampleAsync()
56+
public async Task RunSampleAsync(TimeSpan sampleRunningTime)
5757
{
58-
using var cts = new CancellationTokenSource();
58+
using var cts = new CancellationTokenSource(sampleRunningTime);
5959
Console.CancelKeyPress += (sender, eventArgs) =>
6060
{
6161
eventArgs.Cancel = true;
@@ -117,7 +117,7 @@ private async Task InitializeAndOpenClientAsync()
117117
// It is not good practice to have async void methods, however, DeviceClient.SetConnectionStatusChangesHandler() event handler signature has a void return type.
118118
// As a result, any operation within this block will be executed unmonitored on another thread.
119119
// To prevent multi-threaded synchronization issues, the async method InitializeClientAsync being called in here first grabs a lock
120-
// before attempting to initailize or dispose the device client instance.
120+
// before attempting to initialize or dispose the device client instance.
121121
private async void ConnectionStatusChangeHandler(ConnectionStatus status, ConnectionStatusChangeReason reason)
122122
{
123123
_logger.LogDebug($"Connection status changed: status={status}, reason={reason}");

iot-hub/Samples/device/DeviceReconnectionSample/Parameters.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ internal class Parameters
3030
HelpText = "The transport to use to communicate with the IoT Hub. Possible values include Mqtt, Mqtt_WebSocket_Only, Mqtt_Tcp_Only, Amqp, Amqp_WebSocket_Only, Amqp_Tcp_only, and Http1.")]
3131
public TransportType TransportType { get; set; }
3232

33+
[Option(
34+
'r',
35+
"Application running time (in seconds)",
36+
Required = false,
37+
HelpText = "The running time for this console application. Leave it unassigned to run the application until it is explicitly canceled using Control+C.")]
38+
public double? ApplicationRunningTime { get; set; }
39+
3340
public List<string> GetConnectionStrings()
3441
{
3542
var cs = new List<string>(2)

iot-hub/Samples/device/DeviceReconnectionSample/Program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Microsoft.Azure.Devices.Logging;
66
using Microsoft.Extensions.Logging;
77
using System;
8-
using System.Diagnostics.Tracing;
8+
using System.Threading;
99
using System.Threading.Tasks;
1010

1111
namespace Microsoft.Azure.Devices.Client.Samples
@@ -46,8 +46,12 @@ public static async Task<int> Main(string[] args)
4646
_ = new ConsoleEventListener(SdkEventProviderPrefix, logger);
4747

4848
// Run the sample
49+
var runningTime = parameters.ApplicationRunningTime != null
50+
? TimeSpan.FromSeconds((double)parameters.ApplicationRunningTime)
51+
: Timeout.InfiniteTimeSpan;
52+
4953
var sample = new DeviceReconnectionSample(parameters.GetConnectionStrings(), parameters.TransportType, logger);
50-
await sample.RunSampleAsync();
54+
await sample.RunSampleAsync(runningTime);
5155

5256
logger.LogInformation("Done.");
5357
return 0;

iot-hub/Samples/device/FileUploadSample/FileUploadSample.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
@@ -7,8 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="CommandLineParser" Version="2.8.0" />
11-
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.31.0" />
10+
<PackageReference Include="CommandLineParser" Version="2.8.0" />
11+
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.1" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

iot-hub/Samples/device/MessageReceiveSample/MessageReceiveSample.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ private async Task ReceiveC2dMessagesPollingAndComplete(TimeSpan timeout)
7171

7272
await _deviceClient.CompleteAsync(receivedMessage);
7373
Console.WriteLine($"{DateTime.Now}> Completed C2D message with Id={receivedMessage.MessageId}.");
74-
75-
receivedMessage.Dispose();
7674
}
7775

7876
sw.Stop();

iot-hub/Samples/device/PnpDeviceSamples/TemperatureController/Parameter.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ internal class Parameters
5555
"\nDefaults to environment variable \"IOTHUB_DEVICE_DPS_DEVICE_KEY\".")]
5656
public string DeviceSymmetricKey { get; set; } = Environment.GetEnvironmentVariable("IOTHUB_DEVICE_DPS_DEVICE_KEY");
5757

58+
[Option(
59+
'r',
60+
"Application running time (in seconds)",
61+
Required = false,
62+
HelpText = "The running time for this console application. Leave it unassigned to run the application until it is explicitly canceled using Control+C.")]
63+
public double? ApplicationRunningTime { get; set; }
64+
5865
public bool Validate(ILogger logger)
5966
{
6067
if (string.IsNullOrWhiteSpace(DeviceSecurityType))

iot-hub/Samples/device/PnpDeviceSamples/TemperatureController/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ public static async Task Main(string[] args)
4343
throw new ArgumentException("Required parameters are not set. Please recheck required variables by using \"--help\"");
4444
}
4545

46+
var runningTime = parameters.ApplicationRunningTime != null
47+
? TimeSpan.FromSeconds((double)parameters.ApplicationRunningTime)
48+
: Timeout.InfiniteTimeSpan;
49+
4650
s_logger.LogInformation("Press Control+C to quit the sample.");
47-
using var cts = new CancellationTokenSource();
51+
using var cts = new CancellationTokenSource(runningTime);
4852
Console.CancelKeyPress += (sender, eventArgs) =>
4953
{
5054
eventArgs.Cancel = true;

iot-hub/Samples/device/PnpDeviceSamples/Thermostat/Parameter.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ internal class Parameters
5555
"\nDefaults to environment variable \"IOTHUB_DEVICE_DPS_DEVICE_KEY\".")]
5656
public string DeviceSymmetricKey { get; set; } = Environment.GetEnvironmentVariable("IOTHUB_DEVICE_DPS_DEVICE_KEY");
5757

58+
[Option(
59+
'r',
60+
"Application running time (in seconds)",
61+
Required = false,
62+
HelpText = "The running time for this console application. Leave it unassigned to run the application until it is explicitly canceled using Control+C.")]
63+
public double? ApplicationRunningTime { get; set; }
64+
5865
public bool Validate(ILogger logger)
5966
{
6067
if (string.IsNullOrWhiteSpace(DeviceSecurityType))

iot-hub/Samples/device/PnpDeviceSamples/Thermostat/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ public static async Task Main(string[] args)
3939
throw new ArgumentException("Required parameters are not set. Please recheck required variables by using \"--help\"");
4040
}
4141

42+
var runningTime = parameters.ApplicationRunningTime != null
43+
? TimeSpan.FromSeconds((double)parameters.ApplicationRunningTime)
44+
: Timeout.InfiniteTimeSpan;
45+
4246
s_logger.LogInformation("Press Control+C to quit the sample.");
43-
using var cts = new CancellationTokenSource();
47+
using var cts = new CancellationTokenSource(runningTime);
4448
Console.CancelKeyPress += (sender, eventArgs) =>
4549
{
4650
eventArgs.Cancel = true;

0 commit comments

Comments
 (0)