This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schematics): Add example component generation.
- Loading branch information
1 parent
e97879b
commit 138cb87
Showing
13 changed files
with
769 additions
and
0 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
21 changes: 21 additions & 0 deletions
21
libs/workspace/src/schematics/dt-component-example/README.md
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,21 @@ | ||
# Shipped schematics | ||
|
||
This schematics generates the component for barista examples. The only needed | ||
parameters are the name of the component and the example. | ||
|
||
The schematics is compatible with previously added components, so if the | ||
component already has example, a new example for that component will be | ||
generated. | ||
|
||
Note: the compatibility mentioned above can be only guaranteed if the original | ||
example component was generated with this schematics, and is not altered with | ||
hand. | ||
|
||
## Usage | ||
|
||
- Please run: `ng build workspace` and after that, | ||
`ng g ./dist/libs/workspace:dt-example --name={name of your example} --component={name of your component}` | ||
|
||
### Testing | ||
|
||
The schematics can be tested with the `ng test workspace` command. |
143 changes: 143 additions & 0 deletions
143
libs/workspace/src/schematics/dt-component-example/__snapshots__/index.spec.ts.snap
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,143 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Add Example Component should change files appropriately 1`] = ` | ||
"/** | ||
* @license | ||
* Copyright 2020 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the \\"License\\"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an \\"AS IS\\" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { NgModule } from '@angular/core'; | ||
import { DtTestComponentModule } from '@dynatrace/barista-components/test-component'; | ||
import { DtExampleSimpleTestComponent } from './test-component-simple-example/test-component-simple-example'; | ||
import { DtExampleComplexTestComponent } from './test-component-complex-example/test-component-complex-example'; | ||
export const DT_TEST_COMPONENT_EXAMPLES = [ | ||
DtExampleSimpleTestComponent, | ||
DtExampleComplexTestComponent, | ||
]; | ||
@NgModule({ | ||
imports: [DtTestComponentModule], | ||
declarations: [...DT_TEST_COMPONENT_EXAMPLES], | ||
entryComponents: [...DT_TEST_COMPONENT_EXAMPLES], | ||
}) | ||
export class DtExamplesTestComponentModule {} | ||
" | ||
`; | ||
|
||
exports[`Add Example Component should change files appropriately 2`] = ` | ||
"/** | ||
* @license | ||
* Copyright 2020 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the \\"License\\"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an \\"AS IS\\" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
export * from './test-component-examples.module'; | ||
export * from './test-component-simple-example/test-component-simple-example'; | ||
export * from './test-component-complex-example/test-component-complex-example'; | ||
" | ||
`; | ||
|
||
exports[`Add Example Component should generate files with the appropriate content 1`] = ` | ||
"/** | ||
* @license | ||
* Copyright 2020 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the \\"License\\"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an \\"AS IS\\" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
export * from './test-component-examples.module'; | ||
export * from './test-component-simple-example/test-component-simple-example'; | ||
" | ||
`; | ||
|
||
exports[`Add Example Component should generate files with the appropriate content 2`] = ` | ||
"/** | ||
* @license | ||
* Copyright 2020 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the \\"License\\"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an \\"AS IS\\" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { NgModule } from '@angular/core'; | ||
import { DtTestComponentModule } from '@dynatrace/barista-components/test-component'; | ||
import { DtExampleSimpleTestComponent } from './test-component-simple-example/test-component-simple-example'; | ||
export const DT_TEST_COMPONENT_EXAMPLES = [DtExampleSimpleTestComponent]; | ||
@NgModule({ | ||
imports: [DtTestComponentModule], | ||
declarations: [...DT_TEST_COMPONENT_EXAMPLES], | ||
entryComponents: [...DT_TEST_COMPONENT_EXAMPLES], | ||
}) | ||
export class DtExamplesTestComponentModule {} | ||
" | ||
`; | ||
|
||
exports[`Add Example Component should generate files with the appropriate content 3`] = ` | ||
"<dt-test-component></dt-test-component> | ||
" | ||
`; | ||
|
||
exports[`Add Example Component should generate files with the appropriate content 4`] = ` | ||
"/** | ||
* @license | ||
* Copyright 2020 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the \\"License\\"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an \\"AS IS\\" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { Component } from '@angular/core'; | ||
@Component({ | ||
selector: 'dt-example-simple-test-component', | ||
templateUrl: './test-component-simple-example.html', | ||
}) | ||
export class DtExampleSimpleTestComponent {} | ||
" | ||
`; |
1 change: 1 addition & 0 deletions
1
...matics/dt-component-example/files/__component@dasherize__-__name@dasherize__-example.html
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 @@ | ||
<<%= componentSelector %>></<%= componentSelector %>> |
23 changes: 23 additions & 0 deletions
23
...hematics/dt-component-example/files/__component@dasherize__-__name@dasherize__-example.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,23 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: '<%= selector %>', | ||
templateUrl: './<%= exampleComponent.template %>', | ||
}) | ||
export class <%= exampleComponent.component %> {} |
18 changes: 18 additions & 0 deletions
18
libs/workspace/src/schematics/dt-component-example/fixtures/index.ts.fixture
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,18 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export * from './test-component-examples.module'; | ||
export * from './test-component-simple-example/test-component-simple-example'; |
27 changes: 27 additions & 0 deletions
27
...ce/src/schematics/dt-component-example/fixtures/test-component-examples.module.ts.fixture
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,27 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { NgModule } from '@angular/core'; | ||
import { DtTestComponentModule } from '@dynatrace/barista-components/test-component'; | ||
import { DtExampleSimpleTestComponent } from './test-component-simple-example/test-component-simple-example'; | ||
|
||
export const DT_TEST_COMPONENT_EXAMPLES = [DtExampleSimpleTestComponent]; | ||
|
||
@NgModule({ | ||
imports: [DtTestComponentModule], | ||
declarations: [...DT_TEST_COMPONENT_EXAMPLES], | ||
entryComponents: [...DT_TEST_COMPONENT_EXAMPLES], | ||
}) | ||
export class DtExamplesTestComponentModule {} |
145 changes: 145 additions & 0 deletions
145
libs/workspace/src/schematics/dt-component-example/index.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,145 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Tree } from '@angular-devkit/schematics'; | ||
import { | ||
SchematicTestRunner, | ||
UnitTestTree, | ||
} from '@angular-devkit/schematics/testing'; | ||
import { | ||
Schema as ApplicationOptions, | ||
Style, | ||
} from '@schematics/angular/application/schema'; | ||
import { Schema as WorkspaceOptions } from '@schematics/angular/workspace/schema'; | ||
import { join } from 'path'; | ||
import { addFixtureToTree, getNewFiles } from '../utils/test-utils'; | ||
|
||
const fixturesFolder = join(__dirname, './fixtures'); | ||
|
||
describe('Add Example Component', () => { | ||
const workspaceOptions: WorkspaceOptions = { | ||
name: 'workspace', | ||
newProjectRoot: 'apps', | ||
version: '8.0.0', | ||
}; | ||
|
||
const appOptions: ApplicationOptions = { | ||
name: 'myapp', | ||
inlineStyle: false, | ||
inlineTemplate: false, | ||
routing: false, | ||
style: Style.Scss, | ||
skipTests: true, | ||
skipPackageJson: true, | ||
}; | ||
|
||
let initialTree: UnitTestTree; | ||
let schematicRunner: SchematicTestRunner; | ||
let initialFiles: string[]; | ||
|
||
beforeEach(async () => { | ||
schematicRunner = new SchematicTestRunner( | ||
'@dynatrace/barista-components/schematics', | ||
join(__dirname, '../../../collection.json'), | ||
); | ||
|
||
initialTree = await schematicRunner | ||
.runExternalSchematicAsync( | ||
'@schematics/angular', | ||
'workspace', | ||
workspaceOptions, | ||
Tree.empty(), | ||
) | ||
.toPromise(); | ||
|
||
initialTree = await schematicRunner | ||
.runExternalSchematicAsync( | ||
'@schematics/angular', | ||
'application', | ||
appOptions, | ||
initialTree, | ||
) | ||
.toPromise(); | ||
|
||
initialFiles = initialTree.files; | ||
}); | ||
|
||
it('should add the necessary files', async () => { | ||
const result = await schematicRunner | ||
.runSchematicAsync( | ||
'dt-example', | ||
{ name: 'simple', component: 'TestComponent' }, | ||
initialTree, | ||
) | ||
.toPromise(); | ||
|
||
const newFiles = getNewFiles(result.files, initialFiles); | ||
|
||
expect(newFiles).toEqual([ | ||
'/libs/examples/src/test-component/index.ts', | ||
'/libs/examples/src/test-component/test-component-examples.module.ts', | ||
'/libs/examples/src/test-component/test-component-simple-example/test-component-simple-example.html', | ||
'/libs/examples/src/test-component/test-component-simple-example/test-component-simple-example.ts', | ||
]); | ||
}); | ||
|
||
it('should generate files with the appropriate content', async () => { | ||
const result = await schematicRunner | ||
.runSchematicAsync( | ||
'dt-example', | ||
{ name: 'simple', component: 'TestComponent' }, | ||
initialTree, | ||
) | ||
.toPromise(); | ||
|
||
const newFiles = getNewFiles(result.files, initialFiles); | ||
newFiles.map((file) => expect(result.readContent(file)).toMatchSnapshot()); | ||
}); | ||
|
||
it('should change files appropriately', async () => { | ||
await addFixtureToTree( | ||
initialTree, | ||
'test-component-examples.module.ts.fixture', | ||
'/libs/examples/src/test-component/test-component-examples.module.ts', | ||
fixturesFolder, | ||
); | ||
|
||
await addFixtureToTree( | ||
initialTree, | ||
'index.ts.fixture', | ||
'/libs/examples/src/test-component/index.ts', | ||
fixturesFolder, | ||
); | ||
|
||
const result = await schematicRunner | ||
.runSchematicAsync( | ||
'dt-example', | ||
{ name: 'complex', component: 'TestComponent' }, | ||
initialTree, | ||
) | ||
.toPromise(); | ||
|
||
expect( | ||
result.readContent( | ||
'/libs/examples/src/test-component/test-component-examples.module.ts', | ||
), | ||
).toMatchSnapshot(); | ||
|
||
expect( | ||
result.readContent('/libs/examples/src/test-component/index.ts'), | ||
).toMatchSnapshot(); | ||
}); | ||
}); |
Oops, something went wrong.