Skip to content

Commit

Permalink
fix(core): move generator not updating paths correctly (#767)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Leigh <chris.leigh@securitas.com>
  • Loading branch information
Tungsten78 and Tungsten78 authored Oct 6, 2023
1 parent 72b7c15 commit 6398a06
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
47 changes: 37 additions & 10 deletions packages/core/src/generators/move/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
addProjectConfiguration,
joinPathFragments,
names,
offsetFromRoot,
ProjectConfiguration,
} from '@nrwl/devkit';
import { uniq } from '@nrwl/nx-plugin/testing';

Expand All @@ -29,8 +31,8 @@ describe('move generator', () => {
expect(tree.exists(`apps/${destination}/readme.md`)).toBeTruthy();
});

it('should move simple projects down a directory', async () => {
const { project } = makeSimpleProject(tree, 'app', 'apps/libs/test');
it('should move simple projects up a directory', async () => {
const { project } = makeSimpleProject(tree, 'app', 'test');
const destination = uniq('app');
await generator(tree, { projectName: project, destination });
const config = readProjectConfiguration(tree, destination);
Expand All @@ -39,8 +41,8 @@ describe('move generator', () => {
expect(tree.exists(`apps/libs/test/readme.md`)).toBeFalsy();
});

it('should move simple projects up a directory', async () => {
const { project } = makeSimpleProject(tree, 'app', 'apps/test');
it('should move simple projects down a directory', async () => {
const { project } = makeSimpleProject(tree, 'app', 'test');
const destination = joinPathFragments('test', 'nested', uniq('app'));
await generator(tree, { projectName: project, destination });
const config = readProjectConfiguration(
Expand All @@ -53,15 +55,17 @@ describe('move generator', () => {
});

it('should update references in .csproj files', async () => {
const { project, root } = makeSimpleProject(tree, 'app', 'apps/test');
const { project, root } = makeSimpleProject(tree, 'app', 'test');
const csProjPath = 'apps/other/Other.csproj';
tree.write(
csProjPath,
`<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="../test/${names(project).className}.csproj" />
<ProjectReference Include="../test/${project}/${
names(project).className
}.csproj" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>test_lib2</RootNamespace>
Expand All @@ -86,15 +90,38 @@ describe('move generator', () => {
expect(updatedCsProj).not.toContain(project);
expect(updatedCsProj).toContain(basename(destination));
});

it('should update paths in project configuration', async () => {
const { project, root: source } = makeSimpleProject(tree, 'app', 'a/b');
const destination = joinPathFragments('a', 'b', 'c', uniq('app'));
await generator(tree, { projectName: project, destination });
const config: ProjectConfiguration & { $schema?: string } =
readProjectConfiguration(tree, destination.replace(/[\\|/]/g, '-'));
expect(config).toBeDefined();
expect(JSON.stringify(config)).not.toContain(source);
expect(JSON.stringify(config)).toContain(destination);
expect(config.root.endsWith(destination)).toBeTruthy();
expect(config.sourceRoot?.startsWith(config.root)).toBeTruthy();
const relativeToRoot = offsetFromRoot(config.root);
expect(config.$schema).toMatch(
new RegExp(`^${joinPathFragments(relativeToRoot, 'node_modules')}.*`),
);
});
});

function makeSimpleProject(tree: Tree, type: 'app' | 'lib', path?: string) {
const project = uniq(type);
const root = path ? path.replaceAll('{n}', project) : `${type}s/${project}`;
const root = joinPathFragments(`${type}s`, path ?? '', project);
addProjectConfiguration(tree, project, {
root: root,
root,
sourceRoot: joinPathFragments(root, 'src'),
projectType: type === 'app' ? 'application' : 'library',
targets: { 'my-target': { executor: 'nx:noop' } },
targets: {
'my-target': {
executor: 'nx:noop',
outputs: [`{workspaceRoot}/dist/${root}`],
},
},
});
tree.write(joinPathFragments(root, 'readme.md'), 'contents');
return { project, root };
Expand Down
22 changes: 19 additions & 3 deletions packages/core/src/generators/move/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
joinPathFragments,
names,
normalizePath,
offsetFromRoot,
ProjectConfiguration,
readProjectConfiguration,
removeProjectConfiguration,
Expand Down Expand Up @@ -70,17 +71,32 @@ export default async function (tree: Tree, options: MoveGeneratorSchema) {
addProjectConfiguration(
tree,
options.projectName,
transformConfiguration(config, normalizedOptions),
transformConfiguration(tree, config, normalizedOptions),
);
updateXmlReferences(tree, normalizedOptions);
await formatFiles(tree);
}

function transformConfiguration(
tree: Tree,
config: ProjectConfiguration,
options: NormalizedSchema,
) {
return updateReferencesInObject(config, options);
const copy = updateReferencesInObject(config, options);
updateSchemaPath(tree, copy, config.root);
return copy;
}

function updateSchemaPath(
tree: Tree,
config: ProjectConfiguration & { $schema?: string },
projectRoot: string,
) {
const relativeToRoot = offsetFromRoot(projectRoot);
config.$schema = config.$schema?.replace(
/^.*\/node_modules/,
joinPathFragments(relativeToRoot, 'node_modules'),
);
}

function updateReferencesInObject<
Expand All @@ -94,7 +110,7 @@ function updateReferencesInObject<
for (const key in object) {
if (typeof object[key] === 'string') {
newValue[key] = (object[key] as string).replace(
options.currentProject,
options.currentRoot,
options.destinationRoot,
);
} else if (typeof object[key] === 'object') {
Expand Down

0 comments on commit 6398a06

Please sign in to comment.