Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect if dsc.exe is started from MSStore or Explorer and show a message and wait for key press #481

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ param(
$architecture = 'current',
[switch]$Clippy,
[switch]$SkipBuild,
[ValidateSet('msix','msixbundle','tgz','zip')]
[ValidateSet('msix','msix-private','msixbundle','tgz','zip')]
$packageType,
[switch]$Test,
[switch]$GetPackageVersion,
Expand Down Expand Up @@ -424,7 +424,7 @@ if ($packageType -eq 'msixbundle') {
$msixPath = Join-Path $PSScriptRoot 'bin' 'msix'
& $makeappx bundle /d $msixPath /p "$PSScriptRoot\bin\$packageName.msixbundle"
return
} elseif ($packageType -eq 'msix') {
} elseif ($packageType -eq 'msix' -or $packageType -eq 'msix-private') {
if (!$IsWindows) {
throw "MSIX is only supported on Windows"
}
Expand All @@ -433,21 +433,34 @@ if ($packageType -eq 'msixbundle') {
throw 'MSIX requires a specific architecture'
}

$isPrivate = $packageType -eq 'msix-private'

$makeappx = Find-MakeAppx
$makepri = Get-Item (Join-Path $makeappx.Directory "makepri.exe") -ErrorAction Stop
$displayName = "DesiredStateConfiguration"
$isPreview = $productVersion -like '*-*'
$productName = "DesiredStateConfiguration"
if ($isPreview) {
Write-Verbose -Verbose "Preview version detected"
$productName += "-Preview"
if ($isPrivate) {
$productName += "-Private"
}
else {
$productName += "-Preview"
}
# save preview number
$previewNumber = $productVersion -replace '.*?-[a-z]+\.([0-9]+)', '$1'
# remove label from version
$productVersion = $productVersion.Split('-')[0]
# replace revision number with preview number
$productVersion = $productVersion -replace '(\d+)$', "$previewNumber.0"
$displayName += "-Preview"

if ($isPrivate) {
$displayName += "-Private"
}
else {
$displayName += "-Preview"
}
}
Write-Verbose -Verbose "Product version is $productVersion"
$arch = if ($architecture -eq 'aarch64-pc-windows-msvc') { 'arm64' } else { 'x64' }
Expand Down Expand Up @@ -519,7 +532,7 @@ if ($packageType -eq 'msixbundle') {
throw "Failed to create msix package"
}

Write-Host -ForegroundColor Green "`nMSIX package is created at $packageName.msix"
Write-Host -ForegroundColor Green "`nMSIX package is created at $packageName"
} elseif ($packageType -eq 'zip') {
$zipTarget = Join-Path $PSScriptRoot 'bin' $architecture 'zip'
if (Test-Path $zipTarget) {
Expand Down
38 changes: 38 additions & 0 deletions dsc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ fn main() {
#[cfg(debug_assertions)]
check_debug();

#[cfg(windows)]
check_store();

if ctrlc::set_handler(ctrlc_handler).is_err() {
error!("Error: Failed to set Ctrl-C handler");
}
Expand Down Expand Up @@ -156,3 +159,38 @@ fn check_debug() {
}
}
}

// Check if the dsc binary parent process is WinStore.App or Exploerer.exe
#[cfg(windows)]
fn check_store() {
let message = r"
DSC.exe is a command-line tool and cannot be run directly from the Windows Store or Explorer.
Visit https://aka.ms/dscv3-docs for more information on how to use DSC.exe.

Press any key to close this window
";
let sys = System::new_with_specifics(RefreshKind::new().with_processes(ProcessRefreshKind::new()));
// get current process
let Ok(current_pid) = get_current_pid() else {
return;
};

// get parent process
let Some(current_process) = sys.process(current_pid) else {
return;
};
let Some(parent_process_pid) = current_process.parent() else {
return;
};
let Some(parent_process) = sys.process(parent_process_pid) else {
return;
};

// MS Store runs app using `sihost.exe`
if parent_process.name().to_lowercase() == "sihost.exe" || parent_process.name().to_lowercase() == "explorer.exe"{
eprintln!("{message}");
// wait for keypress
let _ = io::stdin().read(&mut [0u8]).unwrap();
exit(util::EXIT_INVALID_ARGS);
}
}
Loading