Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

[config-plugins] fix detecting application target #3215

Merged
merged 1 commit into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions packages/config-plugins/src/ios/Target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ interface SchemeXML {
Scheme?: {
BuildAction?: {
BuildActionEntries?: {
BuildActionEntry?: {
BuildableReference?: {
$?: {
BlueprintName?: string;
};
}[];
}[];
BuildActionEntry?: BuildActionEntryType[];
}[];
}[];
};
}

interface BuildActionEntryType {
BuildableReference?: {
$?: {
BlueprintName?: string;
BuildableName?: string;
};
}[];
}

export async function getApplicationTargetForSchemeAsync(
projectRoot: string,
scheme: string
Expand All @@ -31,12 +34,23 @@ export async function getApplicationTargetForSchemeAsync(
}

const schemeXML = ((await readXMLAsync({ path: schemePath })) as unknown) as SchemeXML;
const buildActionEntry =
schemeXML.Scheme?.BuildAction?.[0]?.BuildActionEntries?.[0]?.BuildActionEntry;
const targetName =
schemeXML.Scheme?.BuildAction?.[0]?.BuildActionEntries?.[0]?.BuildActionEntry?.[0]
?.BuildableReference?.[0]?.['$']?.BlueprintName;
buildActionEntry?.length === 1
? getBlueprintName(buildActionEntry[0])
: getBlueprintName(
buildActionEntry?.find(entry => {
return entry.BuildableReference?.[0]?.['$']?.BuildableName?.endsWith('.app');
})
);
if (!targetName) {
const schemeRelativePath = path.relative(projectRoot, schemePath);
throw new Error(`${schemeRelativePath} seems to be corrupted`);
}
return targetName;
}

function getBlueprintName(entry?: BuildActionEntryType): string | undefined {
return entry?.BuildableReference?.[0]?.['$']?.BlueprintName;
}
72 changes: 51 additions & 21 deletions packages/config-plugins/src/ios/__tests__/Target-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,60 @@ const fsReal = jest.requireActual('fs') as typeof fs;
jest.mock('fs');

describe(getApplicationTargetForSchemeAsync, () => {
beforeAll(async () => {
vol.fromJSON(
{
'ios/testproject.xcodeproj/xcshareddata/xcschemes/testproject.xcscheme': fsReal.readFileSync(
path.join(__dirname, 'fixtures/testproject.xcscheme'),
'utf-8'
),
},
'/app'
);
});
describe('single build action entry', () => {
beforeAll(async () => {
vol.fromJSON(
{
'ios/testproject.xcodeproj/xcshareddata/xcschemes/testproject.xcscheme': fsReal.readFileSync(
path.join(__dirname, 'fixtures/testproject.xcscheme'),
'utf-8'
),
},
'/app'
);
});

afterAll(() => {
vol.reset();
});
afterAll(() => {
vol.reset();
});

it('returns the target name for existing scheme', async () => {
const target = await getApplicationTargetForSchemeAsync('/app', 'testproject');
expect(target).toBe('testproject');
});

it('returns the target name for existing scheme', async () => {
const target = await getApplicationTargetForSchemeAsync('/app', 'testproject');
expect(target).toBe('testproject');
it('throws if the scheme does not exist', async () => {
await expect(() =>
getApplicationTargetForSchemeAsync('/app', 'nonexistentscheme')
).rejects.toThrow(/does not exist/);
});
});
describe('multiple build action entries', () => {
beforeAll(async () => {
vol.fromJSON(
{
'ios/testproject.xcodeproj/xcshareddata/xcschemes/testproject.xcscheme': fsReal.readFileSync(
path.join(__dirname, 'fixtures/testproject-2.xcscheme'),
'utf-8'
),
},
'/app'
);
});

afterAll(() => {
vol.reset();
});

it('returns the target name for existing scheme', async () => {
const target = await getApplicationTargetForSchemeAsync('/app', 'testproject');
expect(target).toBe('testproject');
});

it('throws if the scheme does not exist', async () => {
await expect(() =>
getApplicationTargetForSchemeAsync('/app', 'nonexistentscheme')
).rejects.toThrow(/does not exist/);
it('throws if the scheme does not exist', async () => {
await expect(() =>
getApplicationTargetForSchemeAsync('/app', 'nonexistentscheme')
).rejects.toThrow(/does not exist/);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1220"
version = "1.3">
<BuildAction
parallelizeBuildables = "NO"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "82CBBA2D1A601D0E00E9B172"
BuildableName = "libReact.a"
BlueprintName = "React"
ReferencedContainer = "container:../node_modules/react-native/React/React.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "13B07F861A680F5B00A75B9A"
BuildableName = "testproject.app"
BlueprintName = "testproject"
ReferencedContainer = "container:testproject.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "00E356ED1AD99517003FC87E"
BuildableName = "testprojectTests.xctest"
BlueprintName = "testprojectTests"
ReferencedContainer = "container:testproject.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "13B07F861A680F5B00A75B9A"
BuildableName = "testproject.app"
BlueprintName = "testproject"
ReferencedContainer = "container:testproject.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "00E356ED1AD99517003FC87E"
BuildableName = "testprojectTests.xctest"
BlueprintName = "testprojectTests"
ReferencedContainer = "container:testproject.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "13B07F861A680F5B00A75B9A"
BuildableName = "testproject.app"
BlueprintName = "testproject"
ReferencedContainer = "container:testproject.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "13B07F861A680F5B00A75B9A"
BuildableName = "testproject.app"
BlueprintName = "testproject"
ReferencedContainer = "container:testproject.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>