Search in sources :

Example 6 with PowerShellInfo

use of jetbrains.buildServer.powershell.agent.detect.PowerShellInfo in project teamcity-powershell by JetBrains.

the class CommandLinePowerShellDetector method findShells.

@NotNull
@Override
public Map<String, PowerShellInfo> findShells(@NotNull DetectionContext detectionContext) {
    LOG.info("Detecting PowerShell using CommandLinePowerShellDetector");
    // group by home
    Map<String, PowerShellInfo> shells = new HashMap<String, PowerShellInfo>();
    // determine paths
    if (LOG.isDebugEnabled()) {
        if (!detectionContext.getSearchPaths().isEmpty()) {
            LOG.debug("Detection paths were overridden by [teamcity.powershell.detector.search.paths] property.");
        }
    }
    final List<String> pathsToCheck = !detectionContext.getSearchPaths().isEmpty() ? detectionContext.getSearchPaths() : (SystemInfo.isWindows ? getWindowsPaths() : PATHS);
    final List<String> executablesToCheck = SystemInfo.isWindows ? EXECUTABLES_WIN : EXECUTABLES;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Will be detecting powershell at: " + Arrays.toString(pathsToCheck.toArray()));
    }
    File script = null;
    try {
        script = prepareDetectionScript();
        if (script != null) {
            final String scriptPath = script.getAbsolutePath();
            if (LOG.isDebugEnabled()) {
                LOG.info("Detection script path is: " + scriptPath);
            }
            for (String path : pathsToCheck) {
                for (String executable : executablesToCheck) {
                    final PowerShellInfo detected = doDetect(path, executable, scriptPath);
                    if (detected != null) {
                        shells.put(detected.getHome().getAbsolutePath(), detected);
                    }
                }
            }
        }
        return shells;
    } finally {
        if (script != null) {
            FileUtil.delete(script);
        }
    }
}
Also used : PowerShellInfo(jetbrains.buildServer.powershell.agent.detect.PowerShellInfo) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with PowerShellInfo

use of jetbrains.buildServer.powershell.agent.detect.PowerShellInfo in project teamcity-powershell by JetBrains.

the class CommandLinePowerShellDetector method doDetect.

@Nullable
private PowerShellInfo doDetect(@NotNull final String homePath, @NotNull final String executable, @NotNull final String scriptPath) {
    PowerShellInfo result = null;
    final File exeFile = new File(homePath, executable);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Searching for PowerShell at " + exeFile.getAbsolutePath());
    }
    if (exeFile.isFile()) {
        String executablePath = exeFile.getAbsolutePath();
        try {
            final List<String> outputLines = myRunner.runDetectionScript(executablePath, scriptPath);
            if (outputLines.size() == 3) {
                final PowerShellEdition edition = PowerShellEdition.fromString(outputLines.get(1));
                if (edition != null) {
                    result = new PowerShellInfo(Boolean.valueOf(outputLines.get(2)) ? PowerShellBitness.x64 : PowerShellBitness.x86, exeFile.getParentFile(), outputLines.get(0), edition, executable);
                } else {
                    LOG.warn("Failed to determine PowerShell edition for [" + executablePath + "]");
                    LOG.debug(StringUtil.join("\n", outputLines));
                }
            } else {
                LOG.warn("Failed to parse output from PowerShell executable [" + executablePath + "]");
                LOG.debug(StringUtil.join("\n", outputLines));
            }
        } catch (ExecutionException e) {
            LOG.warnAndDebugDetails("Failed to run PowerShell detection script [" + scriptPath + "] with executable [" + executablePath + "]", e);
        }
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("PowerShell at " + exeFile.getAbsolutePath() + " was not found");
        }
    }
    return result;
}
Also used : PowerShellEdition(jetbrains.buildServer.powershell.common.PowerShellEdition) PowerShellInfo(jetbrains.buildServer.powershell.agent.detect.PowerShellInfo) ExecutionException(com.intellij.execution.ExecutionException) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with PowerShellInfo

use of jetbrains.buildServer.powershell.agent.detect.PowerShellInfo in project teamcity-powershell by JetBrains.

the class RegistryPowerShellDetector method findShells.

@NotNull
@Override
public Map<String, PowerShellInfo> findShells(@NotNull DetectionContext detectionContext) {
    Map<String, PowerShellInfo> result = new HashMap<String, PowerShellInfo>();
    LOG.info("Detecting PowerShell using RegistryPowerShellDetector");
    if (!SystemInfo.isWindows) {
        LOG.info("RegistryPowerShellDetector is only available on Windows");
        return Collections.emptyMap();
    }
    for (PowerShellBitness bitness : PowerShellBitness.values()) {
        final PowerShellRegistry reg = new PowerShellRegistry(bitness.toBitness(), myAccessor);
        if (!reg.isPowerShellInstalled()) {
            LOG.debug("Powershell for " + bitness + " was not found.");
            continue;
        }
        final String ver = reg.getInstalledVersion();
        final File home = reg.getPowerShellHome();
        if (ver == null || home == null) {
            LOG.debug("Found powershell: " + bitness + " " + ver + " " + home);
            continue;
        }
        final PowerShellInfo info = new PowerShellInfo(bitness, home, ver, PowerShellEdition.DESKTOP, "powershell.exe");
        LOG.info("Found: " + info);
        result.put(info.getHome().getAbsolutePath(), info);
    }
    return result;
}
Also used : PowerShellInfo(jetbrains.buildServer.powershell.agent.detect.PowerShellInfo) PowerShellBitness(jetbrains.buildServer.powershell.common.PowerShellBitness) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with PowerShellInfo

use of jetbrains.buildServer.powershell.agent.detect.PowerShellInfo in project teamcity-powershell by JetBrains.

the class PowerShellInfoProviderTest method testSelectExact.

@Test(dataProvider = "editionProvider")
public void testSelectExact(@NotNull final PowerShellEdition edition) {
    final Map<String, String> params = new HashMap<String, String>();
    mock32Bit("5.0", edition);
    mock64bit("6.0", edition);
    m.checking(new Expectations() {

        {
            allowing(myConfig).getConfigurationParameters();
            will(returnValue(params));
        }
    });
    final PowerShellInfo info = myProvider.selectTool(PowerShellBitness.x64, "6.0", null);
    assertNotNull(info);
    assertEquals(PowerShellBitness.x64, info.getBitness());
}
Also used : Expectations(org.jmock.Expectations) HashMap(java.util.HashMap) PowerShellInfo(jetbrains.buildServer.powershell.agent.detect.PowerShellInfo) Test(org.testng.annotations.Test)

Example 10 with PowerShellInfo

use of jetbrains.buildServer.powershell.agent.detect.PowerShellInfo in project teamcity-powershell by JetBrains.

the class PowerShellInfoProviderTest method testFilterByEdition.

@Test
public void testFilterByEdition() {
    final Map<String, String> params = new HashMap<String, String>();
    mock32Bit("5.0", PowerShellEdition.DESKTOP);
    mock64bit("6.0", PowerShellEdition.CORE);
    m.checking(new Expectations() {

        {
            allowing(myConfig).getConfigurationParameters();
            will(returnValue(params));
        }
    });
    final PowerShellInfo infoCore = myProvider.selectTool(null, null, PowerShellEdition.CORE);
    assertNotNull(infoCore);
    assertEquals(PowerShellBitness.x64, infoCore.getBitness());
    assertEquals("6.0", infoCore.getVersion());
    assertEquals(PowerShellEdition.CORE, infoCore.getEdition());
    final PowerShellInfo infoDesktop = myProvider.selectTool(null, null, PowerShellEdition.DESKTOP);
    assertNotNull(infoDesktop);
    assertEquals(PowerShellBitness.x86, infoDesktop.getBitness());
    assertEquals("5.0", infoDesktop.getVersion());
    assertEquals(PowerShellEdition.DESKTOP, infoDesktop.getEdition());
}
Also used : Expectations(org.jmock.Expectations) HashMap(java.util.HashMap) PowerShellInfo(jetbrains.buildServer.powershell.agent.detect.PowerShellInfo) Test(org.testng.annotations.Test)

Aggregations

PowerShellInfo (jetbrains.buildServer.powershell.agent.detect.PowerShellInfo)13 Test (org.testng.annotations.Test)6 HashMap (java.util.HashMap)5 Expectations (org.jmock.Expectations)5 NotNull (org.jetbrains.annotations.NotNull)4 File (java.io.File)3 PowerShellBitness (jetbrains.buildServer.powershell.common.PowerShellBitness)3 PowerShellEdition (jetbrains.buildServer.powershell.common.PowerShellEdition)3 RunBuildException (jetbrains.buildServer.RunBuildException)2 BuildProgressLogger (jetbrains.buildServer.agent.BuildProgressLogger)2 Nullable (org.jetbrains.annotations.Nullable)2 ExecutionException (com.intellij.execution.ExecutionException)1 PowerShellDetector (jetbrains.buildServer.powershell.agent.detect.PowerShellDetector)1 PowerShellExecutionMode (jetbrains.buildServer.powershell.common.PowerShellExecutionMode)1 Filter (jetbrains.buildServer.util.filters.Filter)1