Skip to content

Commit d0b6932

Browse files
authored
Merge pull request #93 from microsoft/alzollin/caller
Properly handling caller and other env variables.
2 parents ea0cc48 + beb1370 commit d0b6932

File tree

8 files changed

+47
-14
lines changed

8 files changed

+47
-14
lines changed

src/winapp-CLI/WinApp.Cli.Tests/WinappDirectoryServiceTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ public void SetCacheDirectoryForTesting_CanBeChangedMultipleTimes()
7676
public void GetGlobalWinappDirectory_WithEnvironmentVariable_ReturnsEnvironmentVariablePath()
7777
{
7878
// Store original environment variable value to restore later
79-
var originalValue = Environment.GetEnvironmentVariable("WINAPP_CACHE_DIRECTORY");
79+
var originalValue = Environment.GetEnvironmentVariable("WINAPP_CLI_CACHE_DIRECTORY");
8080

8181
// Arrange - Create a test directory for environment variable
8282
var envTestDirectory = _tempDirectory.CreateSubdirectory("env-test-winapp");
8383

8484
try
8585
{
8686
// Set environment variable
87-
Environment.SetEnvironmentVariable("WINAPP_CACHE_DIRECTORY", envTestDirectory.FullName);
87+
Environment.SetEnvironmentVariable("WINAPP_CLI_CACHE_DIRECTORY", envTestDirectory.FullName);
8888

8989
// Act - Create fresh instance to test environment variable behavior
9090
var directoryService = GetRequiredService<IWinappDirectoryService>();
@@ -96,7 +96,7 @@ public void GetGlobalWinappDirectory_WithEnvironmentVariable_ReturnsEnvironmentV
9696
finally
9797
{
9898
// Cleanup - Restore original environment variable value
99-
Environment.SetEnvironmentVariable("WINAPP_CACHE_DIRECTORY", originalValue);
99+
Environment.SetEnvironmentVariable("WINAPP_CLI_CACHE_DIRECTORY", originalValue);
100100
}
101101
}
102102

@@ -105,7 +105,7 @@ public void GetGlobalWinappDirectory_WithEnvironmentVariable_ReturnsEnvironmentV
105105
public void SetCacheDirectoryForTesting_TakesPrecedenceOverEnvironmentVariable()
106106
{
107107
// Store original environment variable value to restore later
108-
var originalValue = Environment.GetEnvironmentVariable("WINAPP_CACHE_DIRECTORY");
108+
var originalValue = Environment.GetEnvironmentVariable("WINAPP_CLI_CACHE_DIRECTORY");
109109

110110
// Arrange - Create test directories
111111
var envTestDirectory = _tempDirectory.CreateSubdirectory("env-winapp");
@@ -114,7 +114,7 @@ public void SetCacheDirectoryForTesting_TakesPrecedenceOverEnvironmentVariable()
114114
try
115115
{
116116
// Set environment variable
117-
Environment.SetEnvironmentVariable("WINAPP_CACHE_DIRECTORY", envTestDirectory.FullName);
117+
Environment.SetEnvironmentVariable("WINAPP_CLI_CACHE_DIRECTORY", envTestDirectory.FullName);
118118

119119
// Act - Create instance and set override (should take precedence)
120120
var directoryService = GetRequiredService<IWinappDirectoryService>();
@@ -127,7 +127,7 @@ public void SetCacheDirectoryForTesting_TakesPrecedenceOverEnvironmentVariable()
127127
finally
128128
{
129129
// Cleanup - Restore original environment variable value
130-
Environment.SetEnvironmentVariable("WINAPP_CACHE_DIRECTORY", originalValue);
130+
Environment.SetEnvironmentVariable("WINAPP_CLI_CACHE_DIRECTORY", originalValue);
131131
}
132132
}
133133

src/winapp-CLI/WinApp.Cli/Services/WinappDirectoryService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public DirectoryInfo GetGlobalWinappDirectory()
2828
}
2929

3030
// Allow override via environment variable (useful for CI/CD)
31-
var cacheDirectory = Environment.GetEnvironmentVariable("WINAPP_CACHE_DIRECTORY");
31+
var cacheDirectory = Environment.GetEnvironmentVariable("WINAPP_CLI_CACHE_DIRECTORY");
3232
if (!string.IsNullOrEmpty(cacheDirectory))
3333
{
3434
return new DirectoryInfo(cacheDirectory);

src/winapp-CLI/WinApp.Cli/Telemetry/Events/EventBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public abstract PartA_PrivTags PartA_PrivTags
3333

3434
public bool CI { get; } = CIEnvironmentDetectorForTelemetry.IsCIEnvironment();
3535

36+
public string? Caller { get; set; } = Environment.GetEnvironmentVariable("WINAPP_CLI_CALLER");
37+
3638
/// <summary>
3739
/// Replaces all the strings in this event that may contain PII using the provided function.
3840
/// </summary>

src/winapp-CLI/WinApp.Cli/Telemetry/Telemetry.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ internal Telemetry()
103103
/// Gets a value indicating whether telemetry is on
104104
/// For future use if we add a registry key or some other setting to check if telemetry is turned on.
105105
/// </summary>
106-
public bool IsTelemetryOn { get; } = true;
106+
public bool IsTelemetryOn { get; } = Environment.GetEnvironmentVariable("WINAPP_CLI_TELEMETRY_OPTOUT") != "1";
107107

108108
/// <summary>
109109
/// Gets or sets a value indicating whether diagnostic telemetry is on.
@@ -213,6 +213,7 @@ public void LogCritical(string eventName, bool isError = false, Guid? relatedAct
213213
where T : EventBase
214214
{
215215
data.ReplaceSensitiveStrings(this.ReplaceSensitiveStrings);
216+
data.Caller = data.Caller != null ? this.ReplaceSensitiveStrings(data.Caller) : null;
216217
this.LogInternal(eventName, level, data, relatedActivityId, isError: false);
217218
}
218219

@@ -228,6 +229,7 @@ public void LogCritical(string eventName, bool isError = false, Guid? relatedAct
228229
where T : EventBase
229230
{
230231
data.ReplaceSensitiveStrings(this.ReplaceSensitiveStrings);
232+
data.Caller = data.Caller != null ? this.ReplaceSensitiveStrings(data.Caller) : null;
231233
this.LogInternal(eventName, level, data, relatedActivityId, isError: true);
232234
}
233235

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"winapp-GUI (Package)": {
4+
"commandName": "MsixPackage"
5+
},
6+
"winapp-GUI (Unpackaged)": {
7+
"commandName": "Project"
8+
}
9+
}
10+
}

src/winapp-GUI/winapp-GUI/Services/CliService.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public async Task<int> RunCliStandardizedAsync(string title, string arguments, s
4646
Arguments = arguments,
4747
WorkingDirectory = workingDir,
4848
CreateNoWindow = true,
49-
WindowStyle = ProcessWindowStyle.Hidden
49+
WindowStyle = ProcessWindowStyle.Hidden,
50+
EnvironmentVariables =
51+
{
52+
["WINAPP_CLI_CALLER"] = "winapp-GUI"
53+
}
5054
};
5155
if (runAsAdmin)
5256
{

src/winapp-npm/cli.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { generateAddonFiles } = require('./addon-utils');
44
const { generateCsAddonFiles } = require('./cs-addon-utils');
55
const { addElectronDebugIdentity } = require('./msix-utils');
6-
const { getWinappCliPath, callWinappCli } = require('./winapp-cli-utils');
6+
const { getWinappCliPath, callWinappCli, WINAPP_CLI_CALLER_VALUE } = require('./winapp-cli-utils');
77
const { spawn } = require('child_process');
88
const path = require('path');
99
const fs = require('fs');
@@ -81,7 +81,11 @@ async function showCombinedHelp() {
8181
await new Promise((resolve, reject) => {
8282
const child = spawn(winappCliPath, ['--help'], {
8383
stdio: 'inherit',
84-
shell: false
84+
shell: false,
85+
env: {
86+
...process.env,
87+
WINAPP_CLI_CALLER: WINAPP_CLI_CALLER_VALUE
88+
}
8589
});
8690

8791
child.on('close', (code) => {
@@ -133,7 +137,11 @@ async function showVersion() {
133137
await new Promise((resolve, reject) => {
134138
const child = spawn(winappCliPath, ['--version'], {
135139
stdio: 'inherit',
136-
shell: false
140+
shell: false,
141+
env: {
142+
...process.env,
143+
WINAPP_CLI_CALLER: WINAPP_CLI_CALLER_VALUE
144+
}
137145
});
138146

139147
child.on('close', (code) => {

src/winapp-npm/winapp-cli-utils.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const path = require('path');
33
const os = require('os');
44
const { spawn } = require('child_process');
55

6+
const WINAPP_CLI_CALLER_VALUE = 'nodejs-package';
7+
68
/**
79
* Helper function to get the path to the winapp-cli executable
810
*/
@@ -38,7 +40,11 @@ async function callWinappCli(args, options = {}) {
3840
const child = spawn(winappCliPath, args, {
3941
stdio: verbose ? 'inherit' : 'pipe',
4042
cwd: process.cwd(),
41-
shell: false
43+
shell: false,
44+
env: {
45+
...process.env,
46+
WINAPP_CLI_CALLER: WINAPP_CLI_CALLER_VALUE
47+
}
4248
});
4349

4450
// Only capture stderr when not using inherit mode (needed for error messages)
@@ -78,5 +84,6 @@ async function callWinappCli(args, options = {}) {
7884

7985
module.exports = {
8086
getWinappCliPath: getWinappCliPath,
81-
callWinappCli: callWinappCli
87+
callWinappCli: callWinappCli,
88+
WINAPP_CLI_CALLER_VALUE: WINAPP_CLI_CALLER_VALUE
8289
};

0 commit comments

Comments
 (0)