Search in sources :

Example 1 with AppleDeviceHelper

use of com.facebook.buck.apple.device.AppleDeviceHelper in project buck by facebook.

the class InstallCommand method installAppleBundleForDevice.

private InstallResult installAppleBundleForDevice(CommandRunnerParams params, AppleBundle appleBundle, ProjectFilesystem projectFilesystem, ProcessExecutor processExecutor, SourcePathResolver pathResolver) throws IOException, NoSuchBuildTargetException {
    // TODO(bhamiltoncx): This should be shared with the build and passed down.
    AppleConfig appleConfig = new AppleConfig(params.getBuckConfig());
    final Path helperPath;
    Optional<BuildTarget> helperTarget = appleConfig.getAppleDeviceHelperTarget();
    if (helperTarget.isPresent()) {
        BuildRuleResolver resolver = super.getBuild().getRuleResolver();
        BuildRule buildRule = resolver.requireRule(helperTarget.get());
        if (buildRule == null) {
            params.getConsole().printBuildFailure(String.format("Cannot install %s (could not resolve build rule for device helper target %s)", appleBundle.getFullyQualifiedName(), helperTarget.get().getBaseName()));
            return FAILURE;
        }
        SourcePath buildRuleOutputPath = buildRule.getSourcePathToOutput();
        if (buildRuleOutputPath == null) {
            params.getConsole().printBuildFailure(String.format("Cannot install %s (device helper target %s does not specify an output)", appleBundle.getFullyQualifiedName(), helperTarget.get().getBaseName()));
            return FAILURE;
        }
        helperPath = pathResolver.getAbsolutePath(buildRuleOutputPath);
    } else {
        Optional<Path> helperOverridePath = appleConfig.getAppleDeviceHelperAbsolutePath();
        if (helperOverridePath.isPresent()) {
            helperPath = helperOverridePath.get();
        } else {
            params.getConsole().printBuildFailure(String.format("Cannot install %s (could not find path to device install helper tool)", appleBundle.getFullyQualifiedName()));
            return FAILURE;
        }
    }
    AppleDeviceHelper helper = new AppleDeviceHelper(processExecutor, helperPath);
    ImmutableMap<String, String> connectedDevices = helper.getConnectedDevices();
    if (connectedDevices.size() == 0) {
        params.getConsole().printBuildFailure(String.format("Cannot install %s (no connected devices found)", appleBundle.getFullyQualifiedName()));
        return FAILURE;
    }
    String selectedUdid = null;
    if (targetDeviceOptions().getSerialNumber().isPresent()) {
        String udidPrefix = Assertions.assertNotNull(targetDeviceOptions().getSerialNumber().get()).toLowerCase();
        for (String udid : connectedDevices.keySet()) {
            if (udid.startsWith(udidPrefix)) {
                selectedUdid = udid;
                break;
            }
        }
        if (selectedUdid == null) {
            params.getConsole().printBuildFailure(String.format("Cannot install %s to the device %s (no connected devices with that UDID/prefix)", appleBundle.getFullyQualifiedName(), udidPrefix));
            return FAILURE;
        }
    } else {
        if (connectedDevices.size() > 1) {
            LOG.warn("More than one connected device found, and no device ID specified.  A device will be" + " arbitrarily picked.");
        }
        selectedUdid = connectedDevices.keySet().iterator().next();
    }
    LOG.info("Installing " + appleBundle.getFullyQualifiedName() + " to device " + selectedUdid + " (" + connectedDevices.get(selectedUdid) + ")");
    if (helper.installBundleOnDevice(selectedUdid, pathResolver.getAbsolutePath(Preconditions.checkNotNull(appleBundle.getSourcePathToOutput())))) {
        params.getConsole().printSuccess("Installed " + appleBundle.getFullyQualifiedName() + " to device " + selectedUdid + " (" + connectedDevices.get(selectedUdid) + ")");
        if (run) {
            Optional<String> appleBundleId;
            try (InputStream bundlePlistStream = projectFilesystem.getInputStreamForRelativePath(appleBundle.getInfoPlistPath())) {
                appleBundleId = AppleInfoPlistParsing.getBundleIdFromPlistStream(bundlePlistStream);
            }
            if (!appleBundleId.isPresent()) {
                params.getConsole().printBuildFailure(String.format("Cannot run %s (could not get bundle ID from %s)", appleBundle.getFullyQualifiedName(), appleBundle.getInfoPlistPath()));
                return FAILURE;
            }
            if (waitForDebugger) {
                LOG.warn(WAIT_FOR_DEBUGGER_LONG_ARG + " not yet implemented for devices.");
            }
            if (helper.runBundleOnDevice(selectedUdid, appleBundleId.get())) {
                return InstallResult.builder().setExitCode(0).build();
            } else {
                params.getConsole().printBuildFailure("Failed to run " + appleBundle.getFullyQualifiedName() + " on device " + selectedUdid + " (" + connectedDevices.get(selectedUdid) + ")");
                return FAILURE;
            }
        } else {
            return InstallResult.builder().setExitCode(0).build();
        }
    } else {
        params.getConsole().printBuildFailure("Failed to install " + appleBundle.getFullyQualifiedName() + " to device " + selectedUdid + " (" + connectedDevices.get(selectedUdid) + ")");
        return FAILURE;
    }
}
Also used : AppleConfig(com.facebook.buck.apple.AppleConfig) Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) InputStream(java.io.InputStream) AppleDeviceHelper(com.facebook.buck.apple.device.AppleDeviceHelper) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) SourcePath(com.facebook.buck.rules.SourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule)

Aggregations

AppleConfig (com.facebook.buck.apple.AppleConfig)1 AppleDeviceHelper (com.facebook.buck.apple.device.AppleDeviceHelper)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 BuildRule (com.facebook.buck.rules.BuildRule)1 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)1 SourcePath (com.facebook.buck.rules.SourcePath)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1