Skip to content
Draft
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
74 changes: 73 additions & 1 deletion packages/build-tools/src/android/gradle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function runGradleCommand(
await fs.chmod(path.join(androidDir, 'gradlew'), 0o755);
const verboseFlag = ctx.env['EAS_VERBOSE'] === '1' ? '--info' : '';

const spawnPromise = spawn('bash', ['-c', `./gradlew ${gradleCommand} ${verboseFlag}`], {
const spawnPromise = spawn('bash', ['-c', `./gradlew ${gradleCommand} --profile ${verboseFlag}`], {
cwd: androidDir,
logger,
lineTransformer: (line?: string) => {
Expand Down Expand Up @@ -103,6 +103,78 @@ function resolveVersionOverridesEnvs(ctx: BuildContext<Job>): Env {
return extraEnvs;
}

export interface GradleProfileTask {
path: string;
durationMs: number;
result: string;
}

export async function parseGradleProfile(androidDir: string): Promise<GradleProfileTask[] | null> {
const profileDir = path.join(androidDir, 'build', 'reports', 'profile');
if (!(await fs.pathExists(profileDir))) {
return null;
}

const files = await fs.readdir(profileDir);
const profileFile = files.filter((f) => f.endsWith('.html')).sort().pop();
if (!profileFile) {
return null;
}

const html = await fs.readFile(path.join(profileDir, profileFile), 'utf8');

const tab4Match = html.match(/id="tab4"[\s\S]*?<\/table>/);
if (!tab4Match) {
return null;
}

const taskSection = tab4Match[0];
const tasks: GradleProfileTask[] = [];
const rowRegex = /<tr>\s*<td class="indentPath">(.*?)<\/td>\s*<td class="numeric">(.*?)<\/td>\s*<td>(.*?)<\/td>\s*<\/tr>/g;

let match;
while ((match = rowRegex.exec(taskSection)) !== null) {
tasks.push({
path: match[1],
durationMs: parseDurationToMs(match[2]),
result: match[3] || 'executed',
});
}

return tasks;
}

function parseDurationToMs(duration: string): number {
let totalMs = 0;

const daysMatch = duration.match(/(\d+)d/);
if (daysMatch) {
totalMs += parseInt(daysMatch[1], 10) * 86400000;
}

const hoursMatch = duration.match(/(\d+)h/);
if (hoursMatch) {
totalMs += parseInt(hoursMatch[1], 10) * 3600000;
}

const minsMatch = duration.match(/(\d+)m(?!\s*s)/);
if (minsMatch) {
totalMs += parseInt(minsMatch[1], 10) * 60000;
}

const secsMatch = duration.match(/([\d.]+)s$/);
if (secsMatch) {
totalMs += Math.round(parseFloat(secsMatch[1]) * 1000);
}

const msMatch = duration.match(/([\d.]+)ms$/);
if (msMatch) {
totalMs += Math.round(parseFloat(msMatch[1]));
}

return totalMs;
}

export function resolveGradleCommand(job: Android.Job): string {
if (job.gradleCommand) {
return job.gradleCommand;
Expand Down
3 changes: 3 additions & 0 deletions packages/build-tools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ export { findAndUploadXcodeBuildLogsAsync } from './ios/xcodeBuildLogs';

export { Hook, runHookIfPresent } from './utils/hooks';

export { parseGradleProfile } from './android/gradle';
export type { GradleProfileTask } from './android/gradle';

export * from './generic';
40 changes: 40 additions & 0 deletions packages/worker/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
BuildContext,
Hook,
findAndUploadXcodeBuildLogsAsync,
parseGradleProfile,
runHookIfPresent,
} from '@expo/build-tools';
import {
Expand Down Expand Up @@ -360,6 +361,45 @@ export default class BuildService {
}

await this.finishError(err, maybeArtifacts);
} finally {
if (job.platform === Platform.ANDROID) {
await this.maybeUploadGradleProfile(job);
}
}
}

private async maybeUploadGradleProfile(job: Job): Promise<void> {
try {
const robotAccessToken = job.secrets?.robotAccessToken;
if (!robotAccessToken || !this.buildContext) {
return;
}

const androidDir = path.join(
this.buildContext.getReactNativeProjectDirectory(),
'android'
);
const tasks = await parseGradleProfile(androidDir);
if (!tasks || tasks.length === 0) {
return;
}

await turtleFetch(
new URL('turtle-builds/gradle-profile', config.wwwApiV2BaseUrl).toString(),
'POST',
{
json: {
buildId: this.buildId,
tasks,
},
headers: {
Authorization: `Bearer ${robotAccessToken}`,
},
shouldThrowOnNotOk: false,
}
);
} catch (err: any) {
logger.debug({ err }, 'Failed to upload Gradle profile');
}
}

Expand Down
Loading