-
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): schematic for adding npm package #5
#6 still needs to be implemented. The base sync schematic has been added, but is not implemented.
- Loading branch information
1 parent
36d2f30
commit 4f37be7
Showing
16 changed files
with
312 additions
and
3 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
22 changes: 22 additions & 0 deletions
22
packages/core/src/generators/nuget-reference/generator.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,22 @@ | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import { Tree, readProjectConfiguration } from '@nrwl/devkit'; | ||
|
||
import generator from './generator'; | ||
import { NugetReferenceGeneratorSchema } from './schema'; | ||
|
||
describe('nuget-reference generator', () => { | ||
let appTree: Tree; | ||
const options: NugetReferenceGeneratorSchema = { | ||
packageName: 'test', | ||
project: 'test', | ||
}; | ||
|
||
beforeEach(() => { | ||
appTree = createTreeWithEmptyWorkspace(); | ||
}); | ||
|
||
it('should run successfully', async () => { | ||
// await generator(appTree, options); | ||
expect(true).toBeTruthy(); | ||
}); | ||
}); |
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,34 @@ | ||
import { readProjectConfiguration, Tree } from '@nrwl/devkit'; | ||
|
||
import { | ||
dotnetAddPackageFlags, | ||
DotNetClient, | ||
dotnetFactory, | ||
} from '@nx-dotnet/dotnet'; | ||
import { getProjectFileForNxProject } from '@nx-dotnet/utils'; | ||
|
||
import { NugetReferenceGeneratorSchema } from './schema'; | ||
|
||
export default async function ( | ||
host: Tree, | ||
options: NugetReferenceGeneratorSchema, | ||
dotnetClient = new DotNetClient(dotnetFactory()) | ||
) { | ||
const project = readProjectConfiguration(host, options.project); | ||
const projectFilePath = await getProjectFileForNxProject(project); | ||
|
||
dotnetClient.addPackageReference( | ||
projectFilePath, | ||
options.packageName, | ||
Object.keys(options) | ||
.filter((x) => x !== 'packageName' && x !== 'project') | ||
.map((x) => ({ | ||
flag: x as dotnetAddPackageFlags, | ||
value: options[x as keyof NugetReferenceGeneratorSchema], | ||
})) | ||
); | ||
|
||
return { | ||
success: true, | ||
}; | ||
} |
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,10 @@ | ||
export interface NugetReferenceGeneratorSchema { | ||
project: string; | ||
packageName: string; | ||
version?: string; | ||
framework?: string; | ||
packageDirectory?: string; | ||
prerelease?: boolean; | ||
source?: string; | ||
noRestore?: boolean; | ||
} |
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,55 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"cli": "nx", | ||
"id": "NugetReference", | ||
"title": "", | ||
"type": "object", | ||
"properties": { | ||
"project": { | ||
"type": "string", | ||
"description": "", | ||
"$default": { | ||
"$source": "argv", | ||
"index": 0 | ||
}, | ||
"x-prompt": "What project should the package be added to?" | ||
}, | ||
"packageName": { | ||
"type": "string", | ||
"description": "Which package should be added?", | ||
"$default": { | ||
"$source": "argv", | ||
"index": 1 | ||
} | ||
}, | ||
"version": { | ||
"type": "string", | ||
"description": "A directory where the project is placed", | ||
"$default": { | ||
"$source": "argv", | ||
"index": 2 | ||
} | ||
}, | ||
"framework": { | ||
"type": "string", | ||
"description": "Adds a package reference only when targeting a specific framework." | ||
}, | ||
"packageDirectory": { | ||
"type": "string", | ||
"description": "The directory where to restore the packages. The default package restore location is %userprofile%\\.nuget\\packages on Windows and ~/.nuget/packages on macOS and Linux. For more information, see [Managing the global packages, cache, and temp folders in NuGet](https://docs.microsoft.com/en-us/nuget/consume-packages/managing-the-global-packages-and-cache-folders)." | ||
}, | ||
"prerelease": { | ||
"type": "boolean", | ||
"description": "Allows prerelease packages to be installed. Available since .NET Core 5 SDK" | ||
}, | ||
"source": { | ||
"type": "string", | ||
"description": "The URI of the NuGet package source to use during the restore operation." | ||
}, | ||
"noRestore": { | ||
"type": "boolean", | ||
"description": "Adds a package reference without performing a restore preview and compatibility check." | ||
} | ||
}, | ||
"required": ["packageName", "project"] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import { Tree, readProjectConfiguration } from '@nrwl/devkit'; | ||
|
||
import generator from './generator'; | ||
import { SyncGeneratorSchema } from './schema'; | ||
|
||
describe('sync generator', () => { | ||
let appTree: Tree; | ||
const options: SyncGeneratorSchema = { name: 'test' }; | ||
|
||
beforeEach(() => { | ||
appTree = createTreeWithEmptyWorkspace(); | ||
}); | ||
|
||
it('should run successfully', async () => { | ||
// await generator(appTree, options); | ||
expect(true).toBeTruthy(); | ||
}); | ||
}); |
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,73 @@ | ||
import { | ||
addProjectConfiguration, | ||
formatFiles, | ||
generateFiles, | ||
getWorkspaceLayout, | ||
names, | ||
offsetFromRoot, | ||
Tree, | ||
} from '@nrwl/devkit'; | ||
import * as path from 'path'; | ||
import { SyncGeneratorSchema } from './schema'; | ||
|
||
interface NormalizedSchema extends SyncGeneratorSchema { | ||
projectName: string; | ||
projectRoot: string; | ||
projectDirectory: string; | ||
parsedTags: string[]; | ||
} | ||
|
||
function normalizeOptions( | ||
host: Tree, | ||
options: SyncGeneratorSchema | ||
): NormalizedSchema { | ||
const name = names(options.name).fileName; | ||
const projectDirectory = options.directory | ||
? `${names(options.directory).fileName}/${name}` | ||
: name; | ||
const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-'); | ||
const projectRoot = `${getWorkspaceLayout(host).libsDir}/${projectDirectory}`; | ||
const parsedTags = options.tags | ||
? options.tags.split(',').map((s) => s.trim()) | ||
: []; | ||
|
||
return { | ||
...options, | ||
projectName, | ||
projectRoot, | ||
projectDirectory, | ||
parsedTags, | ||
}; | ||
} | ||
|
||
function addFiles(host: Tree, options: NormalizedSchema) { | ||
const templateOptions = { | ||
...options, | ||
...names(options.name), | ||
offsetFromRoot: offsetFromRoot(options.projectRoot), | ||
template: '', | ||
}; | ||
generateFiles( | ||
host, | ||
path.join(__dirname, 'files'), | ||
options.projectRoot, | ||
templateOptions | ||
); | ||
} | ||
|
||
export default async function (host: Tree, options: SyncGeneratorSchema) { | ||
const normalizedOptions = normalizeOptions(host, options); | ||
addProjectConfiguration(host, normalizedOptions.projectName, { | ||
root: normalizedOptions.projectRoot, | ||
projectType: 'library', | ||
sourceRoot: `${normalizedOptions.projectRoot}/src`, | ||
targets: { | ||
build: { | ||
executor: '@nx-dotnet/core:build', | ||
}, | ||
}, | ||
tags: normalizedOptions.parsedTags, | ||
}); | ||
addFiles(host, normalizedOptions); | ||
await formatFiles(host); | ||
} |
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,5 @@ | ||
export interface SyncGeneratorSchema { | ||
name: string; | ||
tags?: string; | ||
directory?: string; | ||
} |
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,29 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"cli": "nx", | ||
"id": "Sync", | ||
"title": "", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"description": "", | ||
"$default": { | ||
"$source": "argv", | ||
"index": 0 | ||
}, | ||
"x-prompt": "What name would you like to use?" | ||
}, | ||
"tags": { | ||
"type": "string", | ||
"description": "Add tags to the project (used for linting)", | ||
"alias": "t" | ||
}, | ||
"directory": { | ||
"type": "string", | ||
"description": "A directory where the project is placed", | ||
"alias": "d" | ||
} | ||
}, | ||
"required": ["name"] | ||
} |
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
14 changes: 14 additions & 0 deletions
14
packages/dotnet/src/lib/models/dotnet-add-package/dotnet-add-package-flags.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,14 @@ | ||
export type dotnetAddPackageFlags = | ||
| 'version' | ||
| 'framework' | ||
| 'packageDirectory' | ||
| 'prerelease' | ||
| 'noRestore' | ||
| 'source'; | ||
|
||
export const addPackageKeyMap: Partial< | ||
{ [key in dotnetAddPackageFlags]: string } | ||
> = { | ||
packageDirectory: 'package-directory', | ||
noRestore: 'no-restore', | ||
}; |
6 changes: 6 additions & 0 deletions
6
packages/dotnet/src/lib/models/dotnet-add-package/dotnet-add-package-options.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,6 @@ | ||
import { dotnetAddPackageFlags } from './dotnet-add-package-flags'; | ||
|
||
export type dotnetAddPackageOptions = { | ||
flag: dotnetAddPackageFlags; | ||
value?: string | boolean; | ||
}[]; |
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,2 @@ | ||
export * from './dotnet-add-package-flags'; | ||
export * from './dotnet-add-package-options'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"declaration": true, | ||
"types": ["node"] | ||
}, | ||
"exclude": ["**/*.spec.ts"], | ||
"include": ["**/*.ts"] | ||
} |