Skip to content

Commit

Permalink
feat(nx-quarkus): check if source is a valid quarkus project on linking
Browse files Browse the repository at this point in the history
  • Loading branch information
tinesoft committed Jun 24, 2022
1 parent f74e524 commit dc97a41
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
12 changes: 12 additions & 0 deletions packages/nx-quarkus/src/generators/link/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { Tree, readProjectConfiguration, addProjectConfiguration } from '@nrwl/d

import generator from './generator';
import { LinkGeneratorSchema } from './schema';
import { isQuarkusProject } from '../../utils/quarkus-utils';

jest.mock('../../utils/quarkus-utils');

describe('link generator', () => {
let tree: Tree;
Expand All @@ -20,11 +23,20 @@ describe('link generator', () => {
sourceRoot: `apps/${options.targetProjectName}/src`,
root: `apps/${options.targetProjectName}`,
});

jest.resetAllMocks();
});

it('should run successfully', async () => {

// mock the function that checks if the project is Quarkus project
(isQuarkusProject as jest.Mock).mockImplementation(() => true);
await generator(tree, options);
const targetProject = readProjectConfiguration(tree, options.targetProjectName);
expect(targetProject.implicitDependencies).toEqual([options.sourceProjectName]);
});

it('should fail if source project is not a Quarkus project', async () => {
await expect(generator(tree, options)).rejects.toThrow(`The source project (1st argument of this 'link' generator) must be a Quarkus project`) ;
})
});
6 changes: 5 additions & 1 deletion packages/nx-quarkus/src/generators/link/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import {
updateProjectConfiguration,
} from '@nrwl/devkit';
import { LinkGeneratorSchema } from './schema';
import { isQuarkusProject } from '../../utils/quarkus-utils';


export default async function (tree: Tree, options: LinkGeneratorSchema) {

readProjectConfiguration(tree, options.sourceProjectName);
const sourceProject = readProjectConfiguration(tree, options.sourceProjectName);
if(!isQuarkusProject(sourceProject)) {
throw new Error (`The source project (1st argument of this 'link' generator) must be a Quarkus project`);
}
const targetProject = readProjectConfiguration(tree, options.targetProjectName);

const targetProjectImplicitDependencies = targetProject.implicitDependencies || [];
Expand Down
20 changes: 3 additions & 17 deletions packages/nx-quarkus/src/project-graph.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
import {
ProjectGraph,
ProjectGraphProcessorContext,
ProjectConfiguration} from '@nrwl/devkit';
ProjectGraphProcessorContext} from '@nrwl/devkit';

import { getProjectGraph, NX_QUARKUS_PKG, getJvmPackageInfo } from '@nxrocks/common';
import { isQuarkusProject } from './utils/quarkus-utils';

import { isMavenProject, checkProjectBuildFileContains, isGradleProject, getProjectGraph, NX_QUARKUS_PKG, getJvmPackageInfo } from '@nxrocks/common';

function isQuarkusProject(project: ProjectConfiguration): boolean {

if(isMavenProject(project)) {
return checkProjectBuildFileContains(project, { fragments: ['<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>']}) ;
}

if(isGradleProject(project)) {
return checkProjectBuildFileContains(project, { fragments: ['implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")',
'implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}"))']}) ;
}

return false;
}

export function processProjectGraph(
graph: ProjectGraph,
Expand Down
19 changes: 18 additions & 1 deletion packages/nx-quarkus/src/utils/quarkus-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { NormalizedSchema } from '../generators/project/schema';
import { BuilderCommandAliasType, hasGradleProject, hasMavenProject, runBuilderCommand } from '@nxrocks/common';
import { isMavenProject, checkProjectBuildFileContains, isGradleProject, BuilderCommandAliasType, hasGradleProject, hasMavenProject, runBuilderCommand } from '@nxrocks/common';

import { MAVEN_BUILDER, GRADLE_BUILDER } from '../core/constants';
import { ProjectConfiguration } from '@nrwl/devkit';

const getBuilder = (cwd: string) => {
if (hasMavenProject(cwd)) return MAVEN_BUILDER;
Expand Down Expand Up @@ -35,3 +37,18 @@ export function buildQuarkusDownloadUrl(options: NormalizedSchema) {
return `${options.quarkusInitializerUrl}/d?${queryParams}`;
}


export function isQuarkusProject(project: ProjectConfiguration): boolean {

if(isMavenProject(project)) {
return checkProjectBuildFileContains(project, { fragments: ['<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>']}) ;
}

if(isGradleProject(project)) {
return checkProjectBuildFileContains(project, { fragments: ['implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")',
'implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}"))']}) ;
}

return false;
}

0 comments on commit dc97a41

Please sign in to comment.