-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nx-flutter): add support for Nx 's dependency graph generation
Closes #28
- Loading branch information
Showing
12 changed files
with
182 additions
and
20 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
9 changes: 9 additions & 0 deletions
9
packages/nx-flutter/src/generators/project/lib/add-plugin-to-nx-json.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,9 @@ | ||
import { Tree, readJson, writeJson} from '@nrwl/devkit'; | ||
|
||
export function addPluginToNxJson(tree:Tree){ | ||
const nxJson = readJson(tree, 'nx.json'); | ||
nxJson.plugins = (nxJson.plugins || []); | ||
nxJson.plugins.push('@nxrocks/nx-flutter'); | ||
|
||
writeJson(tree,'nx.json', nxJson); | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { generateFlutterProject } from './generate-project'; | ||
export { promptAdditionalOptions } from './prompt-options'; | ||
export { addPluginToNxJson } from './add-plugin-to-nx-json' | ||
export { normalizeOptions } from './normalize-options'; | ||
export { promptAdditionalOptions } from './prompt-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { projectGenerator } from './generators/project/generator'; | ||
export { projectGenerator } from './generators/project/generator'; | ||
export { processProjectGraph } from './project-graph'; |
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,85 @@ | ||
import { | ||
logger, | ||
ProjectGraph, | ||
ProjectGraphBuilder, | ||
ProjectGraphProcessorContext, | ||
DependencyType, | ||
ProjectConfiguration, | ||
WorkspaceJsonConfiguration | ||
} from '@nrwl/devkit'; | ||
|
||
import * as path from 'path'; | ||
|
||
import { fileExists } from '@nrwl/workspace/src/utils/fileutils'; | ||
import { appRootPath } from '@nrwl/workspace/src/utils/app-root'; | ||
import { getPackageInfo, PackageInfo } from './utils/deps-utils'; | ||
interface WorkspacePackageInfoConfiguration { | ||
projects: { | ||
[projectName: string]: PackageInfo; | ||
}; | ||
|
||
packages: { | ||
[packageId: string]: string; | ||
} | ||
} | ||
|
||
function isFlutterProject(project: ProjectConfiguration): boolean { | ||
|
||
return fileExists(path.join(appRootPath, project.root, 'pubspec.yaml')); | ||
} | ||
|
||
function getPackageInfosForNxFlutterProjects(workspace: WorkspaceJsonConfiguration): WorkspacePackageInfoConfiguration { | ||
const workspacePackageInfo = { | ||
projects: {}, | ||
packages: {} | ||
}; | ||
|
||
Object.entries(workspace.projects).filter(([, project]) => isFlutterProject(project)) | ||
.forEach(([projectName, project]) => { | ||
try { | ||
const pkgInfo = getPackageInfo(path.join(appRootPath, project.root)); | ||
|
||
workspacePackageInfo.projects[projectName] = pkgInfo; | ||
workspacePackageInfo.packages[pkgInfo.packageId] = projectName; | ||
} | ||
catch { | ||
logger.warn(`[nx-flutter]: Failed to get package info for project '${projectName}'`); | ||
} | ||
}); | ||
|
||
return workspacePackageInfo; | ||
} | ||
|
||
function addDependenciesForProject(rootProjectName: string, rootPkgInfo: PackageInfo, builder: ProjectGraphBuilder, workspace: WorkspacePackageInfoConfiguration): void { | ||
|
||
logger.debug(`[nx-flutter]: Adding dependencies for project '${rootProjectName}'...`) | ||
|
||
rootPkgInfo.dependencies.forEach(depPkgInfo => { | ||
const depProjectName = workspace.packages[depPkgInfo.packageId]; | ||
|
||
if (depProjectName) { | ||
builder.addDependency( | ||
DependencyType.static, | ||
rootProjectName, | ||
depProjectName | ||
); | ||
} | ||
}); | ||
} | ||
|
||
export function processProjectGraph( | ||
graph: ProjectGraph, | ||
context: ProjectGraphProcessorContext | ||
): ProjectGraph { | ||
const builder = new ProjectGraphBuilder(graph); | ||
|
||
logger.debug('[nx-flutter]: Looking Flutter related projects inside the workspace...'); | ||
|
||
const workspace = getPackageInfosForNxFlutterProjects(context.workspace); | ||
|
||
Object.entries(workspace.projects).forEach(([projectName, pkgInfo]) => { | ||
addDependenciesForProject(projectName, pkgInfo, builder, workspace); | ||
}); | ||
|
||
return builder.getProjectGraph(); | ||
} |
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,41 @@ | ||
import { load } from 'js-yaml'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
|
||
import { fileExists } from '@nrwl/workspace/src/utils/fileutils'; | ||
|
||
interface Pubspec { | ||
name: string; | ||
version: string; | ||
description: string; | ||
homepage?: string | ||
documentation?: string | ||
environment?: Record<string, string> | ||
dependencies?: Record<string, string>, | ||
dev_dependencies?: Record<string, string> | ||
} | ||
export interface PackageInfo { | ||
packageId: string; | ||
dependencies?: PackageInfo[]; | ||
} | ||
|
||
export function getPackageInfo(projectRoot: string): PackageInfo { | ||
|
||
if (fileExists(path.join(projectRoot, 'pubspec.yaml'))) { //flutterproject | ||
|
||
const pubspec = load(fs.readFileSync(path.join(projectRoot, 'pubspec.yaml'), 'utf8')) as Pubspec; | ||
|
||
const dependencies: PackageInfo[] = []; | ||
|
||
Object.keys(Object.assign({}, pubspec.dependencies, pubspec.dev_dependencies )).forEach(depId => { | ||
dependencies.push({packageId: depId}); | ||
}); | ||
|
||
return { packageId: pubspec.name, dependencies: dependencies}; | ||
} | ||
|
||
throw new Error( | ||
`Cannot inspect dependencies of Flutter projet at: '${projectRoot}'.\n` + | ||
`No 'pubspec.yaml' was found.` | ||
); | ||
} |
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