Search in sources :

Example 1 with PowerShellInfo

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

the class PowerShellInfoProviderTest method testSelect64Over32.

/**
 * Both 64 bit and 32 bit PowerShells are installed
 * Any bitness is required
 * Any version is required
 * 64bit should be used
 */
@Test(dataProvider = "editionProvider")
public void testSelect64Over32(@NotNull final PowerShellEdition edition) {
    final Map<String, String> params = new HashMap<String, String>();
    mock32Bit("2.0", edition);
    mock64bit("2.0", edition);
    m.checking(new Expectations() {

        {
            allowing(myConfig).getConfigurationParameters();
            will(returnValue(params));
        }
    });
    final PowerShellInfo info = myProvider.selectTool(null, null, 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 2 with PowerShellInfo

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

the class BasePowerShellService method makeProgramCommandLine.

@NotNull
@Override
public ProgramCommandLine makeProgramCommandLine() throws RunBuildException {
    final PowerShellInfo info = selectTool();
    final BuildProgressLogger buildLogger = getBuild().getBuildLogger();
    final String psExecutable = info.getExecutablePath();
    final String workDir = getWorkingDirectory().getPath();
    final PowerShellExecutionMode mode = PowerShellExecutionMode.fromString(getRunnerParameters().get(RUNNER_EXECUTION_MODE));
    buildLogger.message("PowerShell Executable: " + psExecutable);
    buildLogger.message("Working directory: " + workDir);
    if (PowerShellExecutionMode.STDIN == mode) {
        return getStdInCommandLine(info, getEnv(info), workDir, generateCommand(info));
    } else if (PowerShellExecutionMode.PS1 == mode) {
        return getFileCommandLine(info, getEnv(info), workDir, generateArguments(info));
    } else {
        throw new RunBuildException("Could not select PowerShell tool for mode [" + mode + "]");
    }
}
Also used : BuildProgressLogger(jetbrains.buildServer.agent.BuildProgressLogger) PowerShellInfo(jetbrains.buildServer.powershell.agent.detect.PowerShellInfo) RunBuildException(jetbrains.buildServer.RunBuildException) PowerShellExecutionMode(jetbrains.buildServer.powershell.common.PowerShellExecutionMode) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PowerShellInfo

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

the class BasePowerShellService method selectTool.

private PowerShellInfo selectTool() throws RunBuildException {
    final BuildProgressLogger buildLogger = getBuild().getBuildLogger();
    PowerShellInfo result;
    if (getRunnerContext().isVirtualContext()) {
        if (SystemInfo.isWindows) {
            throw new RunBuildException("PowerShell is not supported on windows containers");
        }
        buildLogger.logMessage(internalize(createTextMessage("PowerShell is running in virtual agent context")));
        result = VirtualPowerShellSupport.getVirtualPowerShell();
    } else {
        buildLogger.logMessage(internalize(createTextMessage("PowerShell running in non-virtual agent context")));
        final PowerShellBitness bit = PowerShellBitness.fromString(getRunnerParameters().get(RUNNER_BITNESS));
        final String version = getRunnerParameters().get(RUNNER_MIN_VERSION);
        final PowerShellEdition edition = PowerShellEdition.fromString(getRunnerParameters().get(RUNNER_EDITION));
        result = myInfoProvider.selectTool(bit, version, edition);
        if (result == null) {
            throw new RunBuildException("Could not select PowerShell for given bitness " + (bit == null ? "<Auto>" : bit.getDisplayName() + " and version " + (version == null ? "<Any>" : version)));
        }
    }
    return result;
}
Also used : PowerShellEdition(jetbrains.buildServer.powershell.common.PowerShellEdition) BuildProgressLogger(jetbrains.buildServer.agent.BuildProgressLogger) PowerShellInfo(jetbrains.buildServer.powershell.agent.detect.PowerShellInfo) RunBuildException(jetbrains.buildServer.RunBuildException) PowerShellBitness(jetbrains.buildServer.powershell.common.PowerShellBitness)

Example 4 with PowerShellInfo

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

the class PowerShellInfoProvider method selectTool.

@Nullable
public PowerShellInfo selectTool(@Nullable final PowerShellBitness bit, @Nullable final String version, @Nullable final PowerShellEdition edition) {
    // filter by edition
    List<PowerShellInfo> availableShells = myHolder.getShells();
    if (edition != null) {
        availableShells = CollectionsUtil.filterCollection(availableShells, new Filter<PowerShellInfo>() {

            @Override
            public boolean accept(@NotNull PowerShellInfo data) {
                return edition.equals(data.getEdition());
            }
        });
    }
    // filter by version
    if (version != null) {
        availableShells = CollectionsUtil.filterCollection(availableShells, new Filter<PowerShellInfo>() {

            @Override
            public boolean accept(@NotNull PowerShellInfo data) {
                return VersionComparatorUtil.compare(data.getVersion(), version) >= 0;
            }
        });
    }
    // filter by bitness
    if (bit != null) {
        availableShells = CollectionsUtil.filterCollection(availableShells, new Filter<PowerShellInfo>() {

            @Override
            public boolean accept(@NotNull PowerShellInfo data) {
                return data.getBitness().equals(bit);
            }
        });
    }
    if (availableShells.isEmpty()) {
        return null;
    }
    if (availableShells.size() == 1) {
        return availableShells.get(0);
    }
    // prefer desktop over core
    if (edition == null) {
        Map<PowerShellEdition, List<PowerShellInfo>> byEdition = new HashMap<PowerShellEdition, List<PowerShellInfo>>();
        for (PowerShellInfo info : availableShells) {
            if (!byEdition.containsKey(info.getEdition())) {
                byEdition.put(info.getEdition(), new ArrayList<PowerShellInfo>());
            }
            byEdition.get(info.getEdition()).add(info);
        }
        if (byEdition.get(PowerShellEdition.DESKTOP) != null) {
            availableShells = byEdition.get(PowerShellEdition.DESKTOP);
        } else {
            availableShells = byEdition.get(PowerShellEdition.CORE);
        }
    }
    if (availableShells.size() == 1) {
        return availableShells.get(0);
    }
    // prefer 64bit over 32bit
    if (bit == null) {
        Map<PowerShellBitness, List<PowerShellInfo>> byBits = new HashMap<PowerShellBitness, List<PowerShellInfo>>();
        for (PowerShellInfo info : availableShells) {
            if (!byBits.containsKey(info.getBitness())) {
                byBits.put(info.getBitness(), new ArrayList<PowerShellInfo>());
            }
            byBits.get(info.getBitness()).add(info);
        }
        if (byBits.containsKey(PowerShellBitness.x64)) {
            availableShells = byBits.get(PowerShellBitness.x64);
        } else {
            availableShells = byBits.get(PowerShellBitness.x86);
        }
    }
    if (availableShells.size() == 1) {
        return availableShells.get(0);
    }
    // select max available version
    Collections.sort(availableShells, new Comparator<PowerShellInfo>() {

        @Override
        public int compare(PowerShellInfo info1, PowerShellInfo info2) {
            return VersionComparatorUtil.compare(info1.getVersion(), info2.getVersion());
        }
    });
    return availableShells.get(0);
}
Also used : PowerShellEdition(jetbrains.buildServer.powershell.common.PowerShellEdition) NotNull(org.jetbrains.annotations.NotNull) Filter(jetbrains.buildServer.util.filters.Filter) PowerShellInfo(jetbrains.buildServer.powershell.agent.detect.PowerShellInfo) PowerShellBitness(jetbrains.buildServer.powershell.common.PowerShellBitness) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with PowerShellInfo

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

the class PowerShellInfoProvider method registerDetectedPowerShells.

private void registerDetectedPowerShells(@NotNull final List<PowerShellDetector> detectors, @NotNull final DetectionContext detectionContext) {
    Map<String, PowerShellInfo> shells = new HashMap<String, PowerShellInfo>();
    for (PowerShellDetector detector : detectors) {
        for (Map.Entry<String, PowerShellInfo> entry : detector.findShells(detectionContext).entrySet()) {
            if (!shells.containsKey(entry.getKey())) {
                shells.put(entry.getKey(), entry.getValue());
                entry.getValue().saveInfo(myConfig);
                myHolder.addShellInfo(entry.getValue());
            }
        }
    }
    // provide parameters for agent compatibility filters
    if (!myHolder.getShells().isEmpty()) {
        provideMaxVersions();
        provideCompatibilityParams();
    }
}
Also used : PowerShellDetector(jetbrains.buildServer.powershell.agent.detect.PowerShellDetector) PowerShellInfo(jetbrains.buildServer.powershell.agent.detect.PowerShellInfo)

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