Skip to content

Commit

Permalink
Execute powershell from file
Browse files Browse the repository at this point in the history
  • Loading branch information
eray-felek-sonarsource committed Dec 10, 2024
1 parent e893c4d commit ae21f1d
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ qa_task:
# echo ${CIRRUS_WORKING_DIR}
# echo ls ${CIRRUS_WORKING_DIR}/staged-plugin
mkdir -p its/build/idea-sandbox/config-uiTest/
powershell -command "Get-Process | Select-Object Id, ProcessName, MainWindowTitle | Where-Object { $_.MainWindowTitle -ne '' }"
gradle :its:runIdeForUiTests --stacktrace -i -PijVersion=${IDEA_VERSION} -PslPluginDirectory=${CIRRUS_WORKING_DIR}/staged-plugin > ${CIRRUS_WORKING_DIR}/runIdeGradle.log &
wait_ide_script: |
echo "Wait for IDE to start"
Expand All @@ -255,13 +254,16 @@ qa_task:
run_its_script: |
echo "Run ITs on ${IDEA_VERSION}"
source .cirrus/use-gradle-wrapper.sh
# powershell -ExecutionPolicy Bypass -File ".cirrus/foreground.ps1"
gradle :its:check --stacktrace -i -PijVersion=${IDEA_VERSION} -PslPluginDirectory=${CIRRUS_WORKING_DIR}/staged-plugin
<<: *CLEANUP_GRADLE_CACHE_SCRIPT
always:
display_log_script:
- cat ${CIRRUS_WORKING_DIR}/runIdeGradle.log
log_artifacts:
path: "its/build/idea-sandbox/system/log"
image_artifacts:
path: "${CIRRUS_WORKING_DIR}/image_${IDEA_VERSION}.png"
on_failure:
reports_artifacts:
path: "**/reports/**/*"
Expand Down
67 changes: 67 additions & 0 deletions .cirrus/foreground.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class CustomUser32 {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder lpString, int nMaxCount);
public static string GetActiveWindowTitle() {
IntPtr handle = GetForegroundWindow();
System.Text.StringBuilder sb = new System.Text.StringBuilder(256);
if (GetWindowText(handle, sb, sb.Capacity) > 0) {
return sb.ToString();
}
return null;
}
}
"@

function Print-AllProcessesWithMainWindowTitle {
$processes = Get-Process | Select-Object Id, ProcessName, MainWindowTitle

# Write the output to the console
$processes | ForEach-Object { Write-Output "Id: $_.Id, ProcessName: $_.ProcessName, MainWindowTitle: $_.MainWindowTitle" }
}

function Bring-WindowToForegroundByTitle {
param (
[string]$windowTitle,
[int]$maxRetries = 15,
[int]$retryDelay = 1000
)

Write-Output "Starting to try bring window to foreground"

for ($i = 0; $i -lt $maxRetries; $i++) {
$process = Get-Process | Where-Object { $_.MainWindowTitle -eq $windowTitle } | Select-Object -First 1

if ($null -ne $process) {
if ([CustomUser32]::SetForegroundWindow($process.MainWindowHandle)) {
Write-Output "Brought window with title '$windowTitle' to the foreground."
return
} else {
Start-Sleep -Milliseconds $retryDelay
}
} else {
Write-Output "No window with the title '$windowTitle' found."
return
}
}

Write-Output "Failed to bring window with title '$windowTitle' to the foreground after $maxRetries attempts."
}

Write-Output "Starting to print process"

# Print all processes with main window titles
Print-AllProcessesWithMainWindowTitle

# Example call to bring the window with title "Welcome to IntelliJ IDEA" to the foreground
Bring-WindowToForegroundByTitle -windowTitle "Welcome to IntelliJ IDEA"
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
*/
package org.sonarlint.intellij.its.tests

import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
import java.nio.file.Paths
import javax.imageio.ImageIO
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.EnabledIf
Expand Down Expand Up @@ -59,6 +64,12 @@ class StandaloneIdeaTests : BaseUiTest() {

@Test
fun should_exclude_file_and_analyze_file_and_no_issues_found() = uiTest {
remoteRobot.getScreenshot()
val screenshot = remoteRobot.getScreenshot()
val cirrusWorkingDir = System.getenv("CIRRUS_WORKING_DIR")
val ideaVersion = System.getenv("IDEA_VERSION")
val outputFile = File("$cirrusWorkingDir/image_$ideaVersion.jpg")
ImageIO.write(screenshot, "png", outputFile)
openExistingProject("sample-java-issues")
excludeFile("src/main/java/foo/Foo.java")
openFile("src/main/java/foo/Foo.java", "Foo.java")
Expand All @@ -72,4 +83,32 @@ class StandaloneIdeaTests : BaseUiTest() {
openFile("templates/memory_limit_pod2.yml", "memory_limit_pod2.yml")
verifyCurrentFileTabContainsMessages("Bind this resource's automounted service account to RBAC or disable automounting.")
}

fun executePS() {
try {
// Specify the PowerShell script to execute, located in the same directory as this Java file
val scriptPath = Paths.get(System.getProperty("user.dir"), "src", "test", "kotlin", "org", "sonarlint", "intellij", "its", "tests", "foreground.ps1").toString()
//val scriptPath = Paths.get(System.getProperty("user.dir"), "foreground.ps1").toString()

// Create a ProcessBuilder to execute the PowerShell script
val processBuilder = ProcessBuilder("powershell.exe", "-File", scriptPath)
processBuilder.redirectErrorStream(true)

// Start the process
val process = processBuilder.start()

// Read the output from the PowerShell script
val reader = BufferedReader(InputStreamReader(process.inputStream))
var line: String?
while ((reader.readLine().also { line = it }) != null) {
println(line)
}

// Wait for the process to complete and get the exit value
val exitCode = process.waitFor()
println("Exited with code: $exitCode")
} catch (e: Exception) {
e.printStackTrace()
}
}
}
128 changes: 128 additions & 0 deletions its/src/test/kotlin/org/sonarlint/intellij/its/tests/foreground.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class CustomUser32 {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder lpString, int nMaxCount);
public static string GetActiveWindowTitle() {
IntPtr handle = GetForegroundWindow();
System.Text.StringBuilder sb = new System.Text.StringBuilder(256);
if (GetWindowText(handle, sb, sb.Capacity) > 0) {
return sb.ToString();
}
return null;
}
}
"@

function Print-AllProcessesWithMainWindowTitle {
$processes = Get-Process | Select-Object Id, ProcessName, MainWindowTitle

# Write the output to the console
$processes | ForEach-Object { Write-Output "Id: $_.Id, ProcessName: $_.ProcessName, MainWindowTitle: $_.MainWindowTitle" }
}

function Bring-WindowToForegroundByTitle {
param (
[string]$windowTitle,
[int]$maxRetries = 15,
[int]$retryDelay = 1000
)

Write-Output "Starting to try bring window to foreground"

for ($i = 0; $i -lt $maxRetries; $i++) {
$process = Get-Process | Where-Object { $_.MainWindowTitle -eq $windowTitle } | Select-Object -First 1

if ($null -ne $process) {
if ([CustomUser32]::SetForegroundWindow($process.MainWindowHandle)) {
Write-Output "Brought window with title '$windowTitle' to the foreground."
return
} else {
Start-Sleep -Milliseconds $retryDelay
}
} else {
Write-Output "No window with the title '$windowTitle' found."
return
}
}

Write-Output "Failed to bring window with title '$windowTitle' to the foreground after $maxRetries attempts."
}

function Get-FocusedProcess {
Add-Type @"
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
public class User32 {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
}
"@

# Initialize the processId variable
[uint32]$processId = 0

$hWnd = [User32]::GetForegroundWindow()
$null = [User32]::GetWindowThreadProcessId($hWnd, [ref]$processId)
$process = Get-Process -Id $processId
return $process
}


function Set-ForegroundWindow {
param (
[int]$hWnd
)
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Eray32 {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"@
[Eray32]::SetForegroundWindow([IntPtr]::new($hWnd))
}

function Bring-JavaProcessToForeground {
$javaProcesses = Get-Process -Name java
foreach ($process in $javaProcesses) {
$hWnd = $process.MainWindowHandle
if ($hWnd -ne [IntPtr]::Zero) {
Set-ForegroundWindow -hWnd $hWnd
Write-Output "Brought Java process with PID $($process.Id) to the foreground."
return
}
}
Write-Output "No Java process with a main window found."
}

# Example usage
Bring-JavaProcessToForeground

# Example usage
$focusedProcess = Get-FocusedProcess
Write-Output "Focused Process ID: $($focusedProcess.Id)"
Write-Output "Focused Process Name: $($focusedProcess.ProcessName)"


Write-Output "Starting to print process"

# Print all processes with main window titles
Print-AllProcessesWithMainWindowTitle

# Example call to bring the window with title "Welcome to IntelliJ IDEA" to the foreground
Bring-WindowToForegroundByTitle -windowTitle "Welcome to IntelliJ IDEA"

0 comments on commit ae21f1d

Please sign in to comment.