Skip to content

Commit

Permalink
fix(misc): various inference plugins caching should track changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed May 14, 2024
1 parent cfadd7d commit 8156a6b
Show file tree
Hide file tree
Showing 17 changed files with 287 additions and 349 deletions.
33 changes: 16 additions & 17 deletions packages/cypress/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,22 @@ export interface CypressPluginOptions {
}

const cachePath = join(projectGraphCacheDirectory, 'cypress.hash');
const targetsCache = existsSync(cachePath) ? readTargetsCache() : {};

const calculatedTargets: Record<string, CypressTargets> = {};
const targetsCache = readTargetsCache();

function readTargetsCache(): Record<string, CypressTargets> {
return readJsonFile(cachePath);
return existsSync(cachePath) ? readJsonFile(cachePath) : {};
}

function writeTargetsToCache(targets: Record<string, CypressTargets>) {
writeJsonFile(cachePath, targets);
function writeTargetsToCache() {
const oldCache = readTargetsCache();
writeJsonFile(cachePath, {
...oldCache,
targetsCache,
});
}

export const createDependencies: CreateDependencies = () => {
writeTargetsToCache(calculatedTargets);
writeTargetsToCache();
return [];
};

Expand All @@ -67,16 +69,13 @@ export const createNodes: CreateNodes<CypressPluginOptions> = [
getLockFileName(detectPackageManager(context.workspaceRoot)),
]);

const { targets, metadata } = targetsCache[hash]
? targetsCache[hash]
: await buildCypressTargets(
configFilePath,
projectRoot,
options,
context
);

calculatedTargets[hash] = { targets, metadata };
targetsCache[hash] ??= await buildCypressTargets(
configFilePath,
projectRoot,
options,
context
);
const { targets, metadata } = targetsCache[hash];

const project: Omit<ProjectConfiguration, 'root'> = {
projectType: 'application',
Expand Down
28 changes: 7 additions & 21 deletions packages/detox/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,21 @@ export interface DetoxPluginOptions {
}

const cachePath = join(projectGraphCacheDirectory, 'detox.hash');
const targetsCache = existsSync(cachePath) ? readTargetsCache() : {};

const calculatedTargets: Record<
string,
Record<string, TargetConfiguration>
> = {};
const targetsCache = readTargetsCache();

function readTargetsCache(): Record<
string,
Record<string, TargetConfiguration<DetoxPluginOptions>>
> {
return readJsonFile(cachePath);
return existsSync(cachePath) ? readJsonFile(cachePath) : {};
}

function writeTargetsToCache(
targets: Record<
string,
Record<string, TargetConfiguration<DetoxPluginOptions>>
>
) {
writeJsonFile(cachePath, targets);
function writeTargetsToCache() {
writeJsonFile(cachePath, targetsCache);
}

export const createDependencies: CreateDependencies = () => {
writeTargetsToCache(calculatedTargets);
writeTargetsToCache();
return [];
};

Expand All @@ -66,16 +56,12 @@ export const createNodes: CreateNodes<DetoxPluginOptions> = [
getLockFileName(detectPackageManager(context.workspaceRoot)),
]);

const targets = targetsCache[hash]
? targetsCache[hash]
: buildDetoxTargets(projectRoot, options, context);

calculatedTargets[hash] = targets;
targetsCache[hash] ??= buildDetoxTargets(projectRoot, options, context);

return {
projects: {
[projectRoot]: {
targets,
targets: targetsCache[hash],
},
},
};
Expand Down
32 changes: 11 additions & 21 deletions packages/expo/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,25 @@ export interface ExpoPluginOptions {
}

const cachePath = join(projectGraphCacheDirectory, 'expo.hash');
const targetsCache = existsSync(cachePath) ? readTargetsCache() : {};

const calculatedTargets: Record<
string,
Record<string, TargetConfiguration>
> = {};
const targetsCache = readTargetsCache();

function readTargetsCache(): Record<
string,
Record<string, TargetConfiguration<ExpoPluginOptions>>
> {
return readJsonFile(cachePath);
return existsSync(cachePath) ? readJsonFile(cachePath) : {};
}

function writeTargetsToCache(
targets: Record<
string,
Record<string, TargetConfiguration<ExpoPluginOptions>>
>
) {
writeJsonFile(cachePath, targets);
function writeTargetsToCache() {
const oldCache = readTargetsCache();
writeJsonFile(cachePath, {
...oldCache,
targetsCache,
});
}

export const createDependencies: CreateDependencies = () => {
writeTargetsToCache(calculatedTargets);
writeTargetsToCache();
return [];
};

Expand Down Expand Up @@ -82,16 +76,12 @@ export const createNodes: CreateNodes<ExpoPluginOptions> = [
getLockFileName(detectPackageManager(context.workspaceRoot)),
]);

const targets = targetsCache[hash]
? targetsCache[hash]
: buildExpoTargets(projectRoot, options, context);

calculatedTargets[hash] = targets;
targetsCache[hash] ??= buildExpoTargets(projectRoot, options, context);

return {
projects: {
[projectRoot]: {
targets,
targets: targetsCache[hash],
},
},
};
Expand Down
4 changes: 2 additions & 2 deletions packages/gradle/src/plugin/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
getGradleReport,
invalidateGradleReportCache,
} from '../utils/get-gradle-report';
import { calculatedTargets, writeTargetsToCache } from './nodes';
import { writeTargetsToCache } from './nodes';

export const createDependencies: CreateDependencies = async (
_,
Expand Down Expand Up @@ -58,7 +58,7 @@ export const createDependencies: CreateDependencies = async (
gradleDependenciesEnd.name
);

writeTargetsToCache(calculatedTargets);
writeTargetsToCache();
if (dependencies.length) {
invalidateGradleReportCache();
}
Expand Down
171 changes: 81 additions & 90 deletions packages/gradle/src/plugin/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,26 @@ export interface GradlePluginOptions {
}

const cachePath = join(projectGraphCacheDirectory, 'gradle.hash');
const targetsCache = existsSync(cachePath) ? readTargetsCache() : {};

export const calculatedTargets: Record<
export const targetsCache = readTargetsCache();
type GradleTargets = Record<
string,
{
name: string;
targets: Record<string, TargetConfiguration>;
metadata: ProjectConfiguration['metadata'];
}
> = {};
>;

function readTargetsCache(): Record<
string,
{
name: string;
targets: Record<string, TargetConfiguration>;
metadata: ProjectConfiguration['metadata'];
}
> {
return readJsonFile(cachePath);
function readTargetsCache(): GradleTargets {
return existsSync(cachePath) ? readJsonFile(cachePath) : {};
}

export function writeTargetsToCache(
targets: Record<
string,
{
name: string;
targets: Record<string, TargetConfiguration>;
metadata: ProjectConfiguration['metadata'];
}
>
) {
writeJsonFile(cachePath, targets);
export function writeTargetsToCache() {
const oldCache = readTargetsCache();
writeJsonFile(cachePath, {
...oldCache,
...targetsCache,
});
}

export const createNodes: CreateNodes<GradlePluginOptions> = [
Expand All @@ -83,75 +70,79 @@ export const createNodes: CreateNodes<GradlePluginOptions> = [
options ?? {},
context
);
if (targetsCache[hash]) {
calculatedTargets[hash] = targetsCache[hash];
return {
projects: {
[projectRoot]: targetsCache[hash],
},
};
targetsCache[hash] ??= createGradleProject(
gradleFilePath,
options,
context
);
return {
projects: {
[projectRoot]: targetsCache[hash],
},
};
},
];

function createGradleProject(
gradleFilePath: string,
options: GradlePluginOptions | undefined,
context: CreateNodesContext
) {
try {
const {
gradleProjectToTasksTypeMap,
gradleFileToOutputDirsMap,
gradleFileToGradleProjectMap,
gradleProjectToProjectName,
} = getGradleReport();

const gradleProject = gradleFileToGradleProjectMap.get(
gradleFilePath
) as string;
const projectName = gradleProjectToProjectName.get(gradleProject);
if (!projectName) {
return;
}

try {
const {
gradleProjectToTasksTypeMap,
gradleFileToOutputDirsMap,
gradleFileToGradleProjectMap,
gradleProjectToProjectName,
} = getGradleReport();

const gradleProject = gradleFileToGradleProjectMap.get(
gradleFilePath
) as string;
const projectName = gradleProjectToProjectName.get(gradleProject);
if (!projectName) {
return;
}

const tasksTypeMap = gradleProjectToTasksTypeMap.get(
gradleProject
) as Map<string, string>;
let tasks: GradleTask[] = [];
for (let [taskName, taskType] of tasksTypeMap.entries()) {
tasks.push({
type: taskType,
name: taskName,
});
}

const outputDirs = gradleFileToOutputDirsMap.get(gradleFilePath) as Map<
string,
string
>;

const { targets, targetGroups } = createGradleTargets(
tasks,
options,
context,
outputDirs,
gradleProject
);
const project = {
name: projectName,
targets,
metadata: {
targetGroups,
technologies: ['gradle'],
},
};
calculatedTargets[hash] = project;

return {
projects: {
[projectRoot]: project,
},
};
} catch (e) {
console.error(e);
return {};
const tasksTypeMap = gradleProjectToTasksTypeMap.get(gradleProject) as Map<
string,
string
>;
let tasks: GradleTask[] = [];
for (let [taskName, taskType] of tasksTypeMap.entries()) {
tasks.push({
type: taskType,
name: taskName,
});
}
},
];

const outputDirs = gradleFileToOutputDirsMap.get(gradleFilePath) as Map<
string,
string
>;

const { targets, targetGroups } = createGradleTargets(
tasks,
options,
context,
outputDirs,
gradleProject
);
const project = {
name: projectName,
targets,
metadata: {
targetGroups,
technologies: ['gradle'],
},
};

return project;
} catch (e) {
console.error(e);
return undefined;
}
}

function createGradleTargets(
tasks: GradleTask[],
Expand Down
2 changes: 0 additions & 2 deletions packages/jest/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ describe('@nx/jest/plugin', () => {
});
});

test('foo', () => {});

afterEach(() => {
jest.resetModules();
process.chdir(cwd);
Expand Down
Loading

0 comments on commit 8156a6b

Please sign in to comment.