public Path writeScheme()

in src/com/facebook/buck/features/apple/project/SchemeGenerator.java [189:389]


  public Path writeScheme() throws IOException {
    Map<PBXTarget, XCScheme.BuildableReference> buildTargetToBuildableReferenceMap =
        new HashMap<>();

    for (PBXTarget target : Iterables.concat(orderedBuildTargets, orderedBuildTestTargets)) {
      String blueprintName = target.getProductName();
      if (blueprintName == null) {
        blueprintName = target.getName();
      }
      Path outputPath = outputDirectory.getParent();
      String buildableReferencePath;
      Path projectPath = Objects.requireNonNull(targetToProjectPathMap.get(target));
      if (outputPath == null) {
        // Root directory project
        buildableReferencePath = projectPath.toString();
      } else {
        buildableReferencePath = outputPath.relativize(projectPath).toString();
      }

      XCScheme.BuildableReference buildableReference =
          new XCScheme.BuildableReference(
              buildableReferencePath,
              Objects.requireNonNull(target.getGlobalID()),
              target.getProductReference() != null
                  ? target.getProductReference().getName()
                  : Objects.requireNonNull(target.getProductName()),
              blueprintName);
      buildTargetToBuildableReferenceMap.put(target, buildableReference);
    }

    Optional<XCScheme.BuildableReference> primaryBuildReference = Optional.empty();
    if (primaryTarget.isPresent()) {
      primaryBuildReference =
          Optional.ofNullable(buildTargetToBuildableReferenceMap.get(primaryTarget.get()));
    }

    XCScheme.BuildAction buildAction =
        new XCScheme.BuildAction(
            parallelizeBuild,
            additionalCommandsForSchemeAction(
                SchemeActionType.BUILD,
                XCScheme.AdditionalActions.PRE_SCHEME_ACTIONS,
                primaryBuildReference),
            additionalCommandsForSchemeAction(
                SchemeActionType.BUILD,
                XCScheme.AdditionalActions.POST_SCHEME_ACTIONS,
                primaryBuildReference));

    // For aesthetic reasons put all non-test build actions before all test build actions.
    for (PBXTarget target : orderedBuildTargets) {
      addBuildActionForBuildTarget(
          buildTargetToBuildableReferenceMap.get(target),
          XCScheme.BuildActionEntry.BuildFor.DEFAULT,
          buildAction);
    }

    for (PBXTarget target : orderedBuildTestTargets) {
      addBuildActionForBuildTarget(
          buildTargetToBuildableReferenceMap.get(target),
          XCScheme.BuildActionEntry.BuildFor.TEST_ONLY,
          buildAction);
    }

    ImmutableMap<SchemeActionType, ImmutableMap<String, String>> envVariables = ImmutableMap.of();
    Map<SchemeActionType, XCScheme.BuildableReference> envVariablesBasedOn = ImmutableMap.of();
    if (environmentVariables.isPresent()) {
      envVariables = environmentVariables.get();
      if (expandVariablesBasedOn.isPresent()) {
        envVariablesBasedOn = expandVariablesBasedOn.get().entrySet().stream()
          .collect(Collectors.toMap(Map.Entry::getKey, e -> buildTargetToBuildableReferenceMap.get(e.getValue())));
      }
    }

    ImmutableMap<SchemeActionType, ImmutableMap<String, String>> commandLineArgs = ImmutableMap.of();
    if (commandLineArguments.isPresent()) {
      commandLineArgs = commandLineArguments.get();
    }

    XCScheme.TestAction testAction =
        new XCScheme.TestAction(
            Objects.requireNonNull(actionConfigNames.get(SchemeActionType.TEST)),
            Optional.ofNullable(envVariables.get(SchemeActionType.TEST)),
            Optional.ofNullable(envVariablesBasedOn.get(SchemeActionType.TEST)),
            Optional.ofNullable(commandLineArgs.get(SchemeActionType.TEST)),
            additionalCommandsForSchemeAction(
                SchemeActionType.TEST, AdditionalActions.PRE_SCHEME_ACTIONS, primaryBuildReference),
            additionalCommandsForSchemeAction(
                SchemeActionType.TEST,
                AdditionalActions.POST_SCHEME_ACTIONS,
                primaryBuildReference),
            applicationLanguage,
            applicationRegion);

    for (PBXTarget target : orderedRunTestTargets) {
      XCScheme.BuildableReference buildableReference =
          buildTargetToBuildableReferenceMap.get(target);
      XCScheme.TestableReference testableReference =
          new XCScheme.TestableReference(buildableReference);
      testAction.addTestableReference(testableReference);
    }

    Optional<XCScheme.LaunchAction> launchAction = Optional.empty();
    Optional<XCScheme.ProfileAction> profileAction = Optional.empty();

    if (primaryTarget.isPresent()) {
      XCScheme.BuildableReference primaryBuildableReference =
          buildTargetToBuildableReferenceMap.get(primaryTarget.get());
      if (primaryBuildableReference != null) {
        launchAction =
            Optional.of(
                new XCScheme.LaunchAction(
                    primaryBuildableReference,
                    Objects.requireNonNull(actionConfigNames.get(SchemeActionType.LAUNCH)),
                    runnablePath,
                    remoteRunnablePath,
                    watchInterface,
                    launchStyle,
                    Optional.ofNullable(envVariables.get(SchemeActionType.LAUNCH)),
                    Optional.ofNullable(envVariablesBasedOn.get(SchemeActionType.LAUNCH)),
                    Optional.ofNullable(commandLineArgs.get(SchemeActionType.LAUNCH)),
                    additionalCommandsForSchemeAction(
                        SchemeActionType.LAUNCH,
                        AdditionalActions.PRE_SCHEME_ACTIONS,
                        primaryBuildReference),
                    additionalCommandsForSchemeAction(
                        SchemeActionType.LAUNCH,
                        AdditionalActions.POST_SCHEME_ACTIONS,
                        primaryBuildReference),
                    notificationPayloadFile,
                    applicationLanguage,
                    applicationRegion));

        profileAction =
            Optional.of(
                new XCScheme.ProfileAction(
                    primaryBuildableReference,
                    Objects.requireNonNull(actionConfigNames.get(SchemeActionType.PROFILE)),
                    Optional.ofNullable(envVariables.get(SchemeActionType.PROFILE)),
                    Optional.ofNullable(envVariablesBasedOn.get(SchemeActionType.PROFILE)),
                    Optional.ofNullable(commandLineArgs.get(SchemeActionType.PROFILE)),
                    additionalCommandsForSchemeAction(
                        SchemeActionType.PROFILE,
                        AdditionalActions.PRE_SCHEME_ACTIONS,
                        primaryBuildReference),
                    additionalCommandsForSchemeAction(
                        SchemeActionType.PROFILE,
                        AdditionalActions.POST_SCHEME_ACTIONS,
                        primaryBuildReference)));
      }
    }
    XCScheme.AnalyzeAction analyzeAction =
        new XCScheme.AnalyzeAction(
            Objects.requireNonNull(actionConfigNames.get(SchemeActionType.ANALYZE)),
            additionalCommandsForSchemeAction(
                SchemeActionType.ANALYZE,
                AdditionalActions.PRE_SCHEME_ACTIONS,
                primaryBuildReference),
            additionalCommandsForSchemeAction(
                SchemeActionType.ANALYZE,
                AdditionalActions.POST_SCHEME_ACTIONS,
                primaryBuildReference));

    XCScheme.ArchiveAction archiveAction =
        new XCScheme.ArchiveAction(
            Objects.requireNonNull(actionConfigNames.get(SchemeActionType.ARCHIVE)),
            additionalCommandsForSchemeAction(
                SchemeActionType.ARCHIVE,
                AdditionalActions.PRE_SCHEME_ACTIONS,
                primaryBuildReference),
            additionalCommandsForSchemeAction(
                SchemeActionType.ARCHIVE,
                AdditionalActions.POST_SCHEME_ACTIONS,
                primaryBuildReference));

    XCScheme scheme =
        new XCScheme(
            schemeName,
            wasCreatedForAppExtension,
            Optional.of(buildAction),
            Optional.of(testAction),
            launchAction,
            profileAction,
            Optional.of(analyzeAction),
            Optional.of(archiveAction));
    outputScheme = Optional.of(scheme);

    Path schemeDirectory = outputDirectory.resolve("xcshareddata/xcschemes");
    projectFilesystem.mkdirs(schemeDirectory);
    Path schemePath = schemeDirectory.resolve(schemeName + ".xcscheme");
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
      serializeScheme(scheme, outputStream);
      String contentsToWrite = outputStream.toString();
      if (MoreProjectFilesystems.fileContentsDiffer(
          new ByteArrayInputStream(contentsToWrite.getBytes(Charsets.UTF_8)),
          schemePath,
          projectFilesystem)) {
        projectFilesystem.writeContentsToPath(outputStream.toString(), schemePath);
      }
    }
    return schemePath;
  }