Skip to content

Commit

Permalink
Merge pull request #1 from zplatform/zalo/ZPLA-1062-unittest
Browse files Browse the repository at this point in the history
[ZPLA-1062] Support schemes and tests for each target of a subproject in a workspace
  • Loading branch information
elhoangvu authored Feb 27, 2024
2 parents d1f2d06 + e5e7357 commit 0d8fed0
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,15 @@ private void addPhasesAndGroupsForSources(PBXNativeTarget target,
PBXGroup targetGroup,
ProjectFilesystem fileSystem) {
String sourcesDir = "Sources";
String mainTargetName = targetGroup.getName();
if (productType == ProductTypes.UNIT_TEST
|| productType == ProductTypes.UI_TEST) {
sourcesDir = "Tests";
mainTargetName = mainTargetName.replaceAll("Tests$", "");
}

PBXGroup sourcesGroup = targetGroup.getOrCreateChildGroupByName(sourcesDir);
Path originalPath = Paths.get(this.originalPath.toString(), targetGroup.getName(), sourcesDir);
Path originalPath = Paths.get(this.originalPath.toString(), mainTargetName, sourcesDir);

if (fileSystem.exists(originalPath)) {
Path relativePath = pathRelativizer.outputDirToRootRelative(originalPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,7 @@ && shouldEmbedSwiftRuntimeInBundleTarget(bundle)
extraSettingsBuilder
.put("TARGET_NAME", buildTargetName)
.put("SRCROOT", srcRoot);
if ((productType == ProductTypes.UI_TEST || productType == ProductTypes.UNIT_TEST) && isFocusedOnTarget) {
if (productType == ProductTypes.UI_TEST && isFocusedOnTarget) {
if (bundleLoaderNode.isPresent()) {
BuildTarget testTarget = bundleLoaderNode.get().getBuildTarget();
extraSettingsBuilder.put("TEST_TARGET_NAME", getXcodeTargetName(testTarget));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ default boolean shouldGenerateProjectSchemes() {
return false;
}

/** Create schemes for each target's contained build and test targets. */
@Value.Default
default boolean shouldGenerateTargetSchemes() {
return false;
}

/** Generate read-only project files */
@Value.Default
default boolean shouldGenerateReadOnlyFiles() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
Expand All @@ -91,6 +92,7 @@
import java.util.stream.Stream;
import com.facebook.buck.apple.AppleLibraryDescription;
import com.facebook.buck.apple.AppleLibraryDescriptionArg;
import com.facebook.buck.apple.AppleTestDescription;

public class WorkspaceAndProjectGenerator {
private static final Logger LOG = Logger.get(WorkspaceAndProjectGenerator.class);
Expand Down Expand Up @@ -325,6 +327,13 @@ public Path generateWorkspaceAndDependentProjects(
schemeUngroupedTestTargets);
}

if (projectGeneratorOptions.shouldGenerateTargetSchemes()) {
writeWorkspaceSchemesForTargets(
generatedProjectToPbxTargets,
targetToProjectPathMap
);
}

writeWorkspaceSchemes(
workspaceName,
outputDirectory,
Expand Down Expand Up @@ -457,14 +466,15 @@ private void generateProject(
BuildTarget buildTarget = targetNode.getBuildTarget();
projectCellToBuildTargetsBuilder.put(rootCell.getCell(buildTarget.getCell()), buildTarget);
Optional<String> groupName = Optional.empty();
if (targetNode.getDescription() instanceof AppleLibraryDescription) {
AppleLibraryDescriptionArg args = (AppleLibraryDescriptionArg)targetNode.getConstructorArg();
groupName = args.getGroupName();
}

if (targetNode.getDescription() instanceof AppleBundleDescription) {
AppleBundleDescriptionArg args = (AppleBundleDescriptionArg)targetNode.getConstructorArg();
groupName = args.getGroupName();
} else if (targetNode.getDescription() instanceof AppleLibraryDescription) {
AppleLibraryDescriptionArg args = (AppleLibraryDescriptionArg)targetNode.getConstructorArg();
groupName = args.getGroupName();
} else if (targetNode.getDescription() instanceof AppleTestDescription) {
AppleTestDescriptionArg args = (AppleTestDescriptionArg)targetNode.getConstructorArg();
groupName = args.getGroupName();
}

if (groupName.isPresent()) {
Expand Down Expand Up @@ -1219,6 +1229,45 @@ private void writeWorkspaceSchemesForProjects(
}
}

private void writeWorkspaceSchemesForTargets(
ImmutableSetMultimap<PBXProject, PBXTarget> generatedProjectToPbxTargets,
ImmutableMap<PBXTarget, Path> targetToProjectPathMap
) throws IOException {
for (PBXProject project : generatedProjectToPbxTargets.keys()) {
ImmutableSet<PBXTarget> projectTargets = generatedProjectToPbxTargets.get(project);
ImmutableSetMultimap.Builder<String, PBXTarget> targetMapBuilder = ImmutableSetMultimap.builder();
for (PBXTarget target : projectTargets) {
targetMapBuilder.put(target.getName(), target);
}

ImmutableSetMultimap<String, PBXTarget> targetMap = targetMapBuilder.build();
for (PBXTarget target : projectTargets) {
Path projectOutputDirectory = targetToProjectPathMap.get(target);
String schemeName = target.getName();
String testName = schemeName + "Tests";
ImmutableSet<PBXTarget> testTargets = targetMap.get(testName);

ImmutableSet<PBXTarget> orderedBuildTargets = ImmutableSet.of(target);
SchemeGenerator schemeGenerator =
buildSchemeGenerator(
targetToProjectPathMap,
projectOutputDirectory,
schemeName,
Optional.empty(),
Optional.empty(),
orderedBuildTargets,
testTargets,
testTargets,
Optional.empty(),
Optional.empty(),
Optional.empty());

schemeGenerator.writeScheme();
schemeGenerators.put(schemeName, schemeGenerator);
}
}
}

private void writeWorkspaceSchemes(
String workspaceName,
Path outputDirectory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public class XCodeProjectCommandHelper {
private final String modulesToFocusOn;
private final boolean combinedProject;
private final boolean createProjectSchemes;
private final boolean createTargetSchemes;
private final boolean dryRun;
private final boolean readOnly;
private final PathOutputPresenter outputPresenter;
Expand Down Expand Up @@ -179,6 +180,7 @@ public XCodeProjectCommandHelper(
String modulesToFocusOn,
boolean combinedProject,
boolean createProjectSchemes,
boolean createTargetSchemes,
boolean dryRun,
boolean readOnly,
PathOutputPresenter outputPresenter,
Expand Down Expand Up @@ -210,6 +212,7 @@ public XCodeProjectCommandHelper(
this.modulesToFocusOn = modulesToFocusOn;
this.combinedProject = combinedProject;
this.createProjectSchemes = createProjectSchemes;
this.createTargetSchemes = createTargetSchemes;
this.dryRun = dryRun;
this.readOnly = readOnly;
this.outputPresenter = outputPresenter;
Expand Down Expand Up @@ -379,6 +382,7 @@ private ExitCode runXcodeProjectGenerator(
.setShouldUseShortNamesForTargets(true)
.setShouldCreateDirectoryStructure(combinedProject)
.setShouldGenerateProjectSchemes(createProjectSchemes)
.setShouldGenerateTargetSchemes(createTargetSchemes)
.build();

LOG.debug("Xcode project generation: Generates workspaces for targets");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class XCodeProjectSubCommand extends ProjectSubCommand {

private static final boolean DEFAULT_READ_ONLY_VALUE = false;
private static final boolean DEFAULT_PROJECT_SCHEMES = false;
private static final boolean DEFAULT_TARGET_SCHEMES = false;
private static final boolean DEFAULT_ABSOLUTE_HEADER_MAP_PATHS = false;
private static final boolean DEFAULT_SHARED_LIBRARIES_AS_DYNAMIC_FRAMEWORKS = false;

Expand All @@ -52,6 +53,11 @@ public class XCodeProjectSubCommand extends ProjectSubCommand {
usage = "Generate an xcode scheme for each sub-project with its targets and tests.")
private boolean projectSchemes = false;

@Option(
name = "--target-schemes",
usage = "Generate an xcode scheme for each sub-target with its targets and tests.")
private boolean targetSchemes = false;

@Option(
name = "--focus",
usage =
Expand Down Expand Up @@ -139,6 +145,7 @@ public ExitCode run(
modulesToFocusOn,
combinedProject,
getProjectSchemes(params.getBuckConfig()),
getTargetSchemes(params.getBuckConfig()),
projectGeneratorParameters.isDryRun(),
getReadOnly(params.getBuckConfig()),
new PrintStreamPathOutputPresenter(
Expand Down Expand Up @@ -183,6 +190,7 @@ public ExitCode run(
projectGeneratorParameters.isWithoutDependenciesTests(),
modulesToFocusOn,
getProjectSchemes(params.getBuckConfig()),
getTargetSchemes(params.getBuckConfig()),
projectGeneratorParameters.isDryRun(),
getReadOnly(params.getBuckConfig()),
new PrintStreamPathOutputPresenter(
Expand Down Expand Up @@ -214,6 +222,12 @@ private boolean getProjectSchemes(BuckConfig buckConfig) {
|| buckConfig.getBooleanValue("project", "project_schemes", DEFAULT_PROJECT_SCHEMES);
}

private boolean getTargetSchemes(BuckConfig buckConfig) {
// command line arg takes precedence over buck config
return targetSchemes
|| buckConfig.getBooleanValue("project", "target_schemes", DEFAULT_TARGET_SCHEMES);
}

private boolean getReadOnly(BuckConfig buckConfig) {
if (readOnly) {
return readOnly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ default boolean shouldGenerateProjectSchemes() {
return false;
}

/** Create schemes for each target's contained build and test targets. */
@Value.Default
default boolean shouldGenerateTargetSchemes() {
return false;
}

/** Generate read-only project files */
@Value.Default
default boolean shouldGenerateReadOnlyFiles() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public class XCodeProjectCommandHelper {
private final boolean withoutTests;
private final boolean withoutDependenciesTests;
private final boolean createProjectSchemes;
private final boolean createTargetSchemes;
private final boolean dryRun;
private final boolean readOnly;
private final PathOutputPresenter outputPresenter;
Expand Down Expand Up @@ -178,6 +179,7 @@ public XCodeProjectCommandHelper(
boolean withoutDependenciesTests,
String focus,
boolean createProjectSchemes,
boolean createTargetSchemes,
boolean dryRun,
boolean readOnly,
PathOutputPresenter outputPresenter,
Expand Down Expand Up @@ -206,6 +208,7 @@ public XCodeProjectCommandHelper(
this.withoutTests = withoutTests;
this.withoutDependenciesTests = withoutDependenciesTests;
this.createProjectSchemes = createProjectSchemes;
this.createTargetSchemes = createTargetSchemes;
this.dryRun = dryRun;
this.readOnly = readOnly;
this.outputPresenter = outputPresenter;
Expand Down Expand Up @@ -406,6 +409,7 @@ private ExitCode runXcodeProjectGenerator(
appleConfig.shouldGenerateMissingUmbrellaHeaders())
.setShouldUseShortNamesForTargets(true)
.setShouldGenerateProjectSchemes(createProjectSchemes)
.setShouldGenerateTargetSchemes(createTargetSchemes)
.build();

LOG.debug("Xcode project generation: Generates workspaces for targets");
Expand Down

0 comments on commit 0d8fed0

Please sign in to comment.