-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): support for nx incremental builds
Signed-off-by: AgentEnder <craigorycoppola@gmail.com>
- Loading branch information
1 parent
ed5aed0
commit 6739a6b
Showing
8 changed files
with
256 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/core/src/migrations/1.8.0/remove-output-option.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { | ||
addProjectConfiguration, | ||
readProjectConfiguration, | ||
stripIndents, | ||
Tree, | ||
} from '@nrwl/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import update from './remove-output-option'; | ||
|
||
import * as utils from '@nx-dotnet/utils'; | ||
import { NXDOTNET_TAG } from '@nx-dotnet/utils'; | ||
|
||
jest.mock('@nx-dotnet/utils', () => ({ | ||
...(jest.requireActual('@nx-dotnet/utils') as typeof utils), | ||
getProjectFileForNxProject: () => | ||
Promise.resolve('apps/my-app/my-app.csproj'), | ||
})); | ||
|
||
describe('remove-output-option', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(2); | ||
}); | ||
|
||
it('should not update projects where output != OutputPath', async () => { | ||
tree.write( | ||
'/apps/my-app/my-app.csproj', | ||
stripIndents`<Root> | ||
<PropertyGroup> | ||
<OutputPath>./dist/apps/my-app</OutputPath> | ||
</PropertyGroup> | ||
</Root>`, | ||
); | ||
|
||
addProjectConfiguration(tree, 'my-app', { | ||
root: 'apps/my-app', | ||
targets: { | ||
build: { | ||
executor: '@nx-dotnet/core:build', | ||
options: { | ||
output: 'dist/apps/my-app', | ||
}, | ||
}, | ||
}, | ||
tags: [NXDOTNET_TAG], | ||
}); | ||
|
||
await expect(update(tree)).resolves.not.toThrow(); | ||
|
||
const projectConfiguration = readProjectConfiguration(tree, 'my-app'); | ||
expect(projectConfiguration.targets?.build?.options?.output).toEqual( | ||
'dist/apps/my-app', | ||
); | ||
}); | ||
|
||
it('should update projects where output == OutputPath', async () => { | ||
tree.write( | ||
'/apps/my-app/my-app.csproj', | ||
stripIndents`<Root> | ||
<PropertyGroup> | ||
<OutputPath>../../dist/apps/my-app</OutputPath> | ||
</PropertyGroup> | ||
</Root>`, | ||
); | ||
|
||
addProjectConfiguration(tree, 'my-app', { | ||
root: 'apps/my-app', | ||
targets: { | ||
build: { | ||
executor: '@nx-dotnet/core:build', | ||
outputs: ['{options.output}'], | ||
options: { | ||
output: 'dist/apps/my-app', | ||
}, | ||
}, | ||
}, | ||
tags: [NXDOTNET_TAG], | ||
}); | ||
|
||
await expect(update(tree)).resolves.not.toThrow(); | ||
|
||
const projectConfiguration = readProjectConfiguration(tree, 'my-app'); | ||
expect( | ||
projectConfiguration.targets?.build?.options?.output, | ||
).toBeUndefined(); | ||
expect(projectConfiguration.targets?.build?.outputs).toEqual([ | ||
'dist/apps/my-app', | ||
]); | ||
}); | ||
|
||
it('should not update projects where OutputPath is not set', async () => { | ||
tree.write( | ||
'/apps/my-app/my-app.csproj', | ||
stripIndents`<Root> | ||
<PropertyGroup> | ||
</PropertyGroup> | ||
</Root>`, | ||
); | ||
|
||
addProjectConfiguration(tree, 'my-app', { | ||
root: 'apps/my-app', | ||
targets: { | ||
build: { | ||
executor: '@nx-dotnet/core:build', | ||
options: { | ||
output: 'dist/apps/my-app', | ||
}, | ||
}, | ||
}, | ||
tags: [NXDOTNET_TAG], | ||
}); | ||
|
||
await expect(update(tree)).resolves.not.toThrow(); | ||
|
||
const projectConfiguration = readProjectConfiguration(tree, 'my-app'); | ||
expect(projectConfiguration.targets?.build?.options?.output).toEqual( | ||
'dist/apps/my-app', | ||
); | ||
}); | ||
}); |
79 changes: 79 additions & 0 deletions
79
packages/core/src/migrations/1.8.0/remove-output-option.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { | ||
joinPathFragments, | ||
logger, | ||
normalizePath, | ||
TargetConfiguration, | ||
Tree, | ||
updateProjectConfiguration, | ||
} from '@nrwl/devkit'; | ||
import { | ||
getNxDotnetProjects, | ||
getProjectFileForNxProject, | ||
} from '@nx-dotnet/utils'; | ||
import { basename, relative, resolve } from 'path'; | ||
import { XmlDocument } from 'xmldoc'; | ||
import { BuildExecutorConfiguration } from '../../models'; | ||
|
||
export default async function update(host: Tree) { | ||
const projects = getNxDotnetProjects(host); | ||
for (const [name, projectConfiguration] of projects.entries()) { | ||
const projectFile = await getProjectFileForNxProject(projectConfiguration); | ||
if (projectFile) { | ||
const xml = new XmlDocument((host.read(projectFile) ?? '').toString()); | ||
const outputPath = xml | ||
.childNamed('PropertyGroup') | ||
?.childNamed('OutputPath'); | ||
|
||
if (!outputPath) { | ||
logger.warn( | ||
`Skipping ${name} because it does not have OutputPath set in ${basename( | ||
projectFile, | ||
)}`, | ||
); | ||
continue; | ||
} | ||
|
||
let xmlOutputPath = outputPath.val; | ||
xmlOutputPath = normalizePath( | ||
resolve(host.root, projectConfiguration.root, xmlOutputPath), | ||
); | ||
|
||
const buildTarget = Object.entries( | ||
(projectConfiguration.targets ??= {}), | ||
).find( | ||
([, configuration]) => | ||
configuration.executor === '@nx-dotnet/core:build', | ||
); | ||
|
||
if (buildTarget) { | ||
const [target, { options }] = buildTarget as [ | ||
string, | ||
BuildExecutorConfiguration, | ||
]; | ||
const outputPath = normalizePath(resolve(host.root, options.output)); | ||
if (outputPath !== xmlOutputPath) { | ||
logger.info( | ||
`Skipping ${name} since .csproj OutputPath is set differently from --output parameter`, | ||
); | ||
logger.info(`- .csproj OutputPath: ${xmlOutputPath}`); | ||
logger.info(`- project.json output: ${outputPath}`); | ||
continue; | ||
} else { | ||
const t: BuildExecutorConfiguration = projectConfiguration.targets[ | ||
target | ||
] as BuildExecutorConfiguration; | ||
const output = t.options.output; | ||
const outputs = t.outputs || []; | ||
delete t.options.output; | ||
t.options['noIncremental'] = 'true'; | ||
projectConfiguration.targets[target].outputs = outputs.filter( | ||
(x) => x !== '{options.output}', | ||
); | ||
(projectConfiguration.targets[target].outputs || []).push(output); | ||
} | ||
updateProjectConfiguration(host, name, projectConfiguration); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters