private InstallResult installAppleBundleForDevice()

in src/com/facebook/buck/cli/InstallCommand.java [622:792]


  private InstallResult installAppleBundleForDevice(
      CommandRunnerParams params,
      AppleBundle appleBundle,
      ProjectFilesystem projectFilesystem,
      ProcessExecutor processExecutor,
      SourcePathResolverAdapter pathResolver)
      throws IOException {
    AppleConfig appleConfig = params.getBuckConfig().getView(AppleConfig.class);

    Path helperPath;
    Optional<BuildTarget> helperTarget =
        appleConfig.getAppleDeviceHelperTarget(params.getTargetConfiguration());
    if (helperTarget.isPresent()) {
      ActionGraphBuilder graphBuilder = getBuild().getGraphBuilder();
      BuildRule buildRule = graphBuilder.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.isEmpty()) {
      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.info(
            "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(
            Objects.requireNonNull(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(
                  appleBundle.getInfoPlistPath(), 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 ImmutableInstallResult.of(ExitCode.SUCCESS, Optional.empty());
        } else {
          params
              .getConsole()
              .printBuildFailure(
                  "Failed to run "
                      + appleBundle.getFullyQualifiedName()
                      + " on device "
                      + selectedUdid
                      + " ("
                      + connectedDevices.get(selectedUdid)
                      + ")");
          return FAILURE;
        }
      } else {
        return ImmutableInstallResult.of(ExitCode.SUCCESS, Optional.empty());
      }
    } else {
      params
          .getConsole()
          .printBuildFailure(
              "Failed to install "
                  + appleBundle.getFullyQualifiedName()
                  + " to device "
                  + selectedUdid
                  + " ("
                  + connectedDevices.get(selectedUdid)
                  + ")");
      return FAILURE;
    }
  }