Search in sources :

Example 1 with AppleSimulator

use of com.facebook.buck.apple.simulator.AppleSimulator in project buck by facebook.

the class InstallCommand method installAppleBundleForSimulator.

private InstallResult installAppleBundleForSimulator(CommandRunnerParams params, AppleBundle appleBundle, SourcePathResolver pathResolver, ProjectFilesystem projectFilesystem, ProcessExecutor processExecutor) throws IOException, InterruptedException {
    // TODO(bhamiltoncx): This should be shared with the build and passed down.
    AppleConfig appleConfig = new AppleConfig(params.getBuckConfig());
    Optional<Path> xcodeDeveloperPath = appleConfig.getAppleDeveloperDirectorySupplier(processExecutor).get();
    if (!xcodeDeveloperPath.isPresent()) {
        params.getConsole().printBuildFailure(String.format("Cannot install %s (Xcode not found)", appleBundle.getFullyQualifiedName()));
        return FAILURE;
    }
    UnixUserIdFetcher userIdFetcher = new UnixUserIdFetcher();
    AppleCoreSimulatorServiceController appleCoreSimulatorServiceController = new AppleCoreSimulatorServiceController(processExecutor);
    Optional<Path> coreSimulatorServicePath = appleCoreSimulatorServiceController.getCoreSimulatorServicePath(userIdFetcher);
    boolean shouldWaitForSimulatorsToShutdown = false;
    if (!coreSimulatorServicePath.isPresent() || !coreSimulatorServicePath.get().toRealPath().startsWith(xcodeDeveloperPath.get().toRealPath())) {
        LOG.warn("Core simulator service path %s does not match developer directory %s, " + "killing all simulators.", coreSimulatorServicePath, xcodeDeveloperPath.get());
        if (!appleCoreSimulatorServiceController.killSimulatorProcesses()) {
            params.getConsole().printBuildFailure("Could not kill running simulator processes.");
            return FAILURE;
        }
        shouldWaitForSimulatorsToShutdown = true;
    }
    Path simctlPath = xcodeDeveloperPath.get().resolve("usr/bin/simctl");
    Optional<AppleSimulator> appleSimulator = getAppleSimulatorForBundle(appleBundle, processExecutor, simctlPath);
    if (!appleSimulator.isPresent()) {
        params.getConsole().printBuildFailure(String.format("Cannot install %s (no appropriate simulator found)", appleBundle.getFullyQualifiedName()));
        return FAILURE;
    }
    Path iosSimulatorPath = null;
    Path xcodeApplicationsPath = xcodeDeveloperPath.get().resolve("Applications");
    for (String simulatorApp : APPLE_SIMULATOR_APPS) {
        Path resolvedSimulatorPath = xcodeApplicationsPath.resolve(simulatorApp);
        if (projectFilesystem.isDirectory(resolvedSimulatorPath)) {
            iosSimulatorPath = resolvedSimulatorPath;
            break;
        }
    }
    if (iosSimulatorPath == null) {
        params.getConsole().printBuildFailure(String.format("Cannot install %s (could not find simulator under %s, checked %s)", appleBundle.getFullyQualifiedName(), xcodeApplicationsPath, APPLE_SIMULATOR_APPS));
        return FAILURE;
    }
    AppleSimulatorController appleSimulatorController = new AppleSimulatorController(processExecutor, simctlPath, iosSimulatorPath);
    if (!appleSimulatorController.canStartSimulator(appleSimulator.get().getUdid())) {
        LOG.warn("Cannot start simulator %s, killing simulators and trying again.");
        if (!appleCoreSimulatorServiceController.killSimulatorProcesses()) {
            params.getConsole().printBuildFailure("Could not kill running simulator processes.");
            return FAILURE;
        }
        shouldWaitForSimulatorsToShutdown = true;
        // Killing the simulator can cause the UDIDs to change, so we need to fetch them again.
        appleSimulator = getAppleSimulatorForBundle(appleBundle, processExecutor, simctlPath);
        if (!appleSimulator.isPresent()) {
            params.getConsole().printBuildFailure(String.format("Cannot install %s (no appropriate simulator found)", appleBundle.getFullyQualifiedName()));
            return FAILURE;
        }
    }
    long remainingMillis = APPLE_SIMULATOR_WAIT_MILLIS;
    if (shouldWaitForSimulatorsToShutdown) {
        Optional<Long> shutdownMillis = appleSimulatorController.waitForSimulatorsToShutdown(remainingMillis);
        if (!shutdownMillis.isPresent()) {
            params.getConsole().printBuildFailure(String.format("Cannot install %s (simulators did not shut down within %d ms).", appleBundle.getFullyQualifiedName(), APPLE_SIMULATOR_WAIT_MILLIS));
            return FAILURE;
        }
        LOG.debug("Simulators shut down in %d millis.", shutdownMillis.get());
        remainingMillis -= shutdownMillis.get();
    }
    LOG.debug("Starting up simulator %s", appleSimulator.get());
    Optional<Long> startMillis = appleSimulatorController.startSimulator(appleSimulator.get().getUdid(), remainingMillis);
    if (!startMillis.isPresent()) {
        params.getConsole().printBuildFailure(String.format("Cannot install %s (could not start simulator %s within %d ms)", appleBundle.getFullyQualifiedName(), appleSimulator.get().getName(), APPLE_SIMULATOR_WAIT_MILLIS));
        return FAILURE;
    }
    LOG.debug("Simulator started in %d ms. Installing Apple bundle %s in simulator %s", startMillis.get(), appleBundle, appleSimulator.get());
    if (!appleSimulatorController.installBundleInSimulator(appleSimulator.get().getUdid(), pathResolver.getAbsolutePath(Preconditions.checkNotNull(appleBundle.getSourcePathToOutput())))) {
        params.getConsole().printBuildFailure(String.format("Cannot install %s (could not install bundle %s in simulator %s)", appleBundle.getFullyQualifiedName(), pathResolver.getAbsolutePath(appleBundle.getSourcePathToOutput()), appleSimulator.get().getName()));
        return FAILURE;
    }
    if (run) {
        return launchAppleBundle(params, appleBundle, appleSimulatorController, projectFilesystem, appleSimulator.get());
    } else {
        params.getBuckEventBus().post(ConsoleEvent.info(params.getConsole().getAnsi().asHighlightedSuccessText("Successfully installed %s. (Use `buck install -r %s` to run.)"), getArguments().get(0), getArguments().get(0)));
        return InstallResult.builder().setExitCode(0).build();
    }
}
Also used : AppleConfig(com.facebook.buck.apple.AppleConfig) Path(java.nio.file.Path) SourcePath(com.facebook.buck.rules.SourcePath) AppleSimulator(com.facebook.buck.apple.simulator.AppleSimulator) UnixUserIdFetcher(com.facebook.buck.util.UnixUserIdFetcher) AppleSimulatorController(com.facebook.buck.apple.simulator.AppleSimulatorController) AppleCoreSimulatorServiceController(com.facebook.buck.apple.simulator.AppleCoreSimulatorServiceController)

Example 2 with AppleSimulator

use of com.facebook.buck.apple.simulator.AppleSimulator in project buck by facebook.

the class InstallCommand method getAppleSimulatorForBundle.

private Optional<AppleSimulator> getAppleSimulatorForBundle(AppleBundle appleBundle, ProcessExecutor processExecutor, Path simctlPath) throws IOException, InterruptedException {
    LOG.debug("Choosing simulator for %s", appleBundle);
    Optional<AppleSimulator> simulatorByUdid = Optional.empty();
    Optional<AppleSimulator> simulatorByName = Optional.empty();
    Optional<AppleSimulator> defaultSimulator = Optional.empty();
    boolean wantUdid = deviceOptions.getSerialNumber().isPresent();
    boolean wantName = deviceOptions.getSimulatorName().isPresent();
    for (AppleSimulator simulator : AppleSimulatorDiscovery.discoverAppleSimulators(processExecutor, simctlPath)) {
        if (wantUdid && deviceOptions.getSerialNumber().get().toLowerCase(Locale.US).equals(simulator.getUdid().toLowerCase(Locale.US))) {
            LOG.debug("Got UDID match (%s): %s", deviceOptions.getSerialNumber().get(), simulator);
            simulatorByUdid = Optional.of(simulator);
            // We shouldn't need to keep looking.
            break;
        } else if (wantName && deviceOptions.getSimulatorName().get().toLowerCase(Locale.US).equals(simulator.getName().toLowerCase(Locale.US))) {
            LOG.debug("Got name match (%s): %s", simulator.getName(), simulator);
            simulatorByName = Optional.of(simulator);
        // We assume the simulators are sorted by OS version, so we'll keep
        // looking for a more recent simulator with this name.
        } else if (simulator.getName().equals(DEFAULT_APPLE_SIMULATOR_NAME)) {
            LOG.debug("Got default match (%s): %s", DEFAULT_APPLE_SIMULATOR_NAME, simulator);
            defaultSimulator = Optional.of(simulator);
        }
    }
    if (wantUdid) {
        if (simulatorByUdid.isPresent()) {
            return simulatorByUdid;
        } else {
            LOG.warn("Asked to find simulator with UDID %s, but couldn't find one.", deviceOptions.getSerialNumber().get());
            return Optional.empty();
        }
    } else if (wantName) {
        if (simulatorByName.isPresent()) {
            return simulatorByName;
        } else {
            LOG.warn("Asked to find simulator with name %s, but couldn't find one.", deviceOptions.getSimulatorName().get());
            return Optional.empty();
        }
    } else {
        return defaultSimulator;
    }
}
Also used : AppleSimulator(com.facebook.buck.apple.simulator.AppleSimulator)

Aggregations

AppleSimulator (com.facebook.buck.apple.simulator.AppleSimulator)2 AppleConfig (com.facebook.buck.apple.AppleConfig)1 AppleCoreSimulatorServiceController (com.facebook.buck.apple.simulator.AppleCoreSimulatorServiceController)1 AppleSimulatorController (com.facebook.buck.apple.simulator.AppleSimulatorController)1 SourcePath (com.facebook.buck.rules.SourcePath)1 UnixUserIdFetcher (com.facebook.buck.util.UnixUserIdFetcher)1 Path (java.nio.file.Path)1