Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(generate): implement UI for ng generate #75

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f595733
feat(generate): implement UI for ng generate
sumitparakh Nov 26, 2018
352c61d
Merge branch 'master' of https://github.com/sumitparakh/klingon into …
sumitparakh Nov 26, 2018
d49d41d
feat(generate): implement UI for ng generate
sumitparakh Nov 27, 2018
23a8891
feat(generate): implement UI for ng generate
sumitparakh Nov 28, 2018
ee095b2
Merge branch 'master' of https://github.com/sumitparakh/klingon into …
sumitparakh Nov 28, 2018
bc51c4c
feat(generate): generate multiple components
sumitparakh Dec 9, 2018
603a5fa
Merge branch 'master' of https://github.com/sumitparakh/klingon into …
sumitparakh Dec 9, 2018
4a3af52
feat(generate): some refactoring
sumitparakh Dec 9, 2018
36d943d
Merge branch 'master' of https://github.com/sumitparakh/klingon into …
sumitparakh Dec 14, 2018
ae6b980
feat(generate): do not enable generate tab while running in dry mode
sumitparakh Dec 14, 2018
f793bd7
Merge branch 'master' of https://github.com/angular-klingon/klingon i…
sumitparakh Dec 14, 2018
68f2fb8
feat(generate): classes generate support added
sumitparakh Dec 16, 2018
f453263
feat(generate): generate feature added for directives
sumitparakh Dec 17, 2018
de08554
feat(ui): generate feature for enum added
sumitparakh Dec 17, 2018
df0b77c
feat(ui): interface generate support added
sumitparakh Dec 17, 2018
faa18c1
feat(ui): module generate feature added
sumitparakh Dec 17, 2018
dcec136
feat(ui): pipe and service generate feature done
sumitparakh Dec 17, 2018
096b361
feat(generate): ng components
sumitparakh Feb 2, 2019
d22f41d
Merge branch 'master' of https://github.com/angular-klingon/klingon i…
sumitparakh Feb 2, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
selector: 'app-drop-down',
template: `
<mat-accordion>
<mat-expansion-panel [expanded]="open" (opened)="open=true" (closed)="open=false">
<mat-expansion-panel [expanded]="open" [disabled]="disabled" (opened)="open=true" (closed)="open=false">
<mat-expansion-panel-header>
<mat-panel-title>
<ng-content select="mat-icon"></ng-content>
Expand All @@ -17,6 +17,7 @@ import {
<mat-panel-description *ngIf="!open">
<ng-content select=".sub-title"></ng-content>
</mat-panel-description>
<ng-content select=".action"></ng-content>
</mat-expansion-panel-header>
<ng-content select=".content"></ng-content>
</mat-expansion-panel>
Expand Down Expand Up @@ -53,6 +54,9 @@ import {
margin: 3px 0px 0px 0px;
font-size: 1em;
}
::ng-deep .component-title {
margin: 9px 0px 0px 0px;
}
::ng-deep .sub-title {
padding: 6px;
margin: 0;
Expand All @@ -70,9 +74,13 @@ import {
]
})
export class DropDownComponent implements OnInit {

@Input()
open: boolean = false;

@Input()
disabled: boolean = false;

ngOnInit() {
}

Expand Down
3 changes: 2 additions & 1 deletion packages/klingon-ui/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
<ng-template mat-tab-label>New</ng-template>
<app-cli-create #appCli (onStdOut)="onSuccess($event)" (onStdErr)="onError($event)"></app-cli-create>
</mat-tab>
<mat-tab [disabled]="true">
<mat-tab [disabled]="!klingon.generate">
<ng-template mat-tab-label>Generate</ng-template>
<app-cli-generate #appGenerate (onStdOut)="onSuccess($event)" (onStdErr)="onError($event)"></app-cli-generate>
</mat-tab>
<mat-tab>
<ng-template mat-tab-label>Serve</ng-template>
Expand Down
42 changes: 37 additions & 5 deletions packages/klingon-ui/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Component, ViewChild } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed unused imports.

import { MatSnackBar } from '@angular/material/snack-bar';
import { OnInit } from '@angular/core/src/metadata/lifecycle_hooks';
import { CliCreateComponent } from './cli/create/create.component';
import { CliService, CommandResult } from './cli/cli.service';
import { Subject } from 'rxjs';
import { flatMap } from 'rxjs/operators';
import { CliGenerateComponent } from './cli/generate/generate.component';

@Component({
selector: 'app-snack-bar-error',
Expand All @@ -26,7 +29,7 @@ import { CliCreateComponent } from './cli/create/create.component';
template:
'<mat-icon>error</mat-icon><span>An error has occured. Check the logs tab.</span>'
})
export class SnackBarErrorComponent {}
export class SnackBarErrorComponent { }

@Component({
selector: 'app-snack-bar-success',
Expand All @@ -50,7 +53,7 @@ export class SnackBarErrorComponent {}
template:
'<mat-icon>verified_user</mat-icon><span>Command executed successfully.</span>'
})
export class SnackBarSuccessComponent {}
export class SnackBarSuccessComponent { }

@Component({
selector: 'app-root',
Expand All @@ -60,22 +63,51 @@ export class SnackBarSuccessComponent {}
export class AppComponent implements OnInit {
selectedIndex = 0;

// Enable/Disable generate tab
klingon = { generate: false };

@ViewChild('appCli') appCli: CliCreateComponent;
@ViewChild('appGenerate') appGenerate: CliGenerateComponent;

constructor(
public snackBarError: MatSnackBar,
public snackBarSuccess: MatSnackBar
) {}
public snackBarSuccess: MatSnackBar,
private cliService: CliService
) { }

ngOnInit() {
this.selectedIndex = parseInt(
localStorage.getItem('ui.selectedIndex') || '0',
10
);
localStorage.setItem('ui.selectedIndex', `${this.selectedIndex}`);

this.subscribeToNgCreate();
}

subscribeToNgCreate() {
this.cliService.onNgCreate.asObservable()
.pipe(flatMap((data: Subject<CommandResult>) => data))
.subscribe((response: any) => {
/**
* exit event of ng command returns exit code (0/1). So if it returns 0, means ng command executed successfully. Only then
* we enable generate tab.
*/
if (response.exit === 0) {
manekinekko marked this conversation as resolved.
Show resolved Hide resolved
this.selectedIndex = 1;
this.klingon.generate = true;
}
});
}

onNgCreateEvent(response: any) {
}

storeIndex(index: number) {
// Set app-name from localStorage if generate tab is selected.
if (index === 1) {
this.appGenerate.form.patchValue({ 'app-name': localStorage.getItem('ui.appName') });
}
localStorage.setItem('ui.selectedIndex', `${index}`);
}

Expand Down
6 changes: 5 additions & 1 deletion packages/klingon-ui/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { LogComponent } from './_shared/log/log.component';
import { MatExpansionModule } from '@angular/material/expansion';
import { ServiceWorkerModule } from '@angular/service-worker';
import { environment } from '../environments/environment';
import { CliGenerateComponent } from './cli/generate/generate.component';
import { ComponentComponent } from './cli/generate/component/component.component';

@NgModule({
declarations: [
Expand All @@ -36,7 +38,9 @@ import { environment } from '../environments/environment';
CliTestComponent,
LogComponent,
SnackBarSuccessComponent,
SnackBarErrorComponent
SnackBarErrorComponent,
CliGenerateComponent,
ComponentComponent
],
entryComponents: [SnackBarSuccessComponent, SnackBarErrorComponent],
imports: [
Expand Down
24 changes: 23 additions & 1 deletion packages/klingon-ui/src/app/cli/cli.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Injectable, Output, EventEmitter } from '@angular/core';
import { Subject } from 'rxjs';
import { environment } from '../../environments/environment';

Expand All @@ -14,6 +14,8 @@ export class CliService {
ws: WebSocket;
isConnectionOn;

@Output() onNgCreate: EventEmitter<Subject<CommandResult>> = new EventEmitter<Subject<CommandResult>>();

constructor() {
this.response$ = new Subject();
this.ws = new WebSocket(`ws://` + environment.host + `:` + environment.port + `/cli`);
Expand All @@ -40,6 +42,7 @@ export class CliService {
key !== 'app-name' &&
key !== 'root-dir' &&
key !== 'dir' &&
key !== 'component-name' &&
key !== 'app'
)
.map(key => `--${key}=${values[key]}`)
Expand All @@ -57,10 +60,29 @@ export class CliService {
runNgCommand(stdin, dir = undefined) {
if (this.isConnectionOn) {
this._send(stdin, dir);
this.detectEvents(stdin, this.response$);
}
return this.response$;
}

/**
* Detect ng operations such as new, serve, build, generate etc.
* @param stdin string
*/
private detectEvents(stdin: string, response: Subject<CommandResult>) {
if (!stdin) {
return;
}

const commandArray = stdin.split(' ');

if (commandArray.length > 1) {
if (commandArray[0] === 'new') {
this.onNgCreate.emit(response);
}
}
}

_send(stdin, dir) {
console.log(stdin);

Expand Down
4 changes: 2 additions & 2 deletions packages/klingon-ui/src/app/cli/create/create.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { FlagsComponent } from './../flags/flags.component';
import { CliService } from './../cli.service';
import { Validators } from '@angular/forms';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed unused imports

import { FormControl } from '@angular/forms';
import { FormGroup } from '@angular/forms';
import { Component, OnInit } from '@angular/core';
import { ImportService } from '../../_shared/utilities/import.service';
Expand Down Expand Up @@ -40,7 +38,9 @@ export class CliCreateComponent extends FlagsComponent implements OnInit {
const rootDir = this.form.value['root-dir'];
localStorage.setItem('ui.lastUsedRootDirectory', rootDir || '');

// save application name to local storage to reuse it in generate tab
const appName = this.form.value['directory'] || this.form.value['app-name'];
localStorage.setItem('ui.appName', appName);

this.isWorking = true;
this.cli
Expand Down
26 changes: 24 additions & 2 deletions packages/klingon-ui/src/app/cli/flags/flags.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Validators } from '@angular/forms';
import { Validators, FormArray } from '@angular/forms';
import { FormControl } from '@angular/forms';
import { FormGroup } from '@angular/forms';
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
Expand All @@ -14,7 +14,8 @@ export class FlagsComponent implements OnInit {
CREATE: 0,
SERVE: 1,
BUILD: 2,
TEST: 3
TEST: 3,
GENERATE: 4
};

@Output()
Expand Down Expand Up @@ -49,6 +50,8 @@ export class FlagsComponent implements OnInit {
'ui.lastUsedRootDirectory'
);

const appName = localStorage.getItem('ui.appName');

if (flag === FlagsComponent.Flags.CREATE) {
return new FormGroup({
'app-name': new FormControl('', Validators.required),
Expand Down Expand Up @@ -138,6 +141,25 @@ export class FlagsComponent implements OnInit {
sourcemap: new FormControl(true),
watch: new FormControl(false)
});
} else if (flag === FlagsComponent.Flags.GENERATE) {
return new FormGroup({
'app-name': new FormControl(appName, Validators.required),
'root-dir': new FormControl(lastUsedRootDirectory),
components: new FormArray([]),
serviceworkers: new FormArray([]),
applications: new FormArray([]),
classes: new FormArray([]),
directives: new FormArray([]),
enums: new FormArray([]),
guards: new FormArray([]),
interfaces: new FormArray([]),
modules: new FormArray([]),
pipes: new FormArray([]),
services: new FormArray([]),
universals: new FormArray([]),
appshells: new FormArray([]),
libraries: new FormArray([])
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<p *ngIf="form.get('components').controls.length === 0">
Please add component to continue
</p>
<app-drop-down *ngFor="let component of form.get('components').controls; let i = index">
<h3 class="title component-title">
<span>
{{ component.value['component-name'] ? component.value['component-name'] : 'Component '+(i+1) }}
</span>
</h3>
<h6 class="sub-title">Application Component</h6>
<main class="content" [formGroup]="component">

<div class="component-form">
<p>
<mat-form-field>
<input formControlName="component-name" matInput placeholder="Name of the component to create in the app"
required>
<mat-hint align="start">Name of the component to create in the app</mat-hint>
</mat-form-field>
</p>
<p>
<mat-form-field>
<mat-select formControlName="change-detection" placeholder="Specify change detection strategy for component (-c)">
<mat-option *ngFor="let value of changeDetection" [value]="value">
{{value}}
</mat-option>
</mat-select>
<mat-hint align="start">Specify change detection strategy for component</mat-hint>
</mat-form-field>
</p>
<p>
<mat-form-field>
<mat-select formControlName="styleext" placeholder="The file extension to be used for style files.">
<mat-option *ngFor="let style of styleExt" [value]="style">
{{ style }}
</mat-option>
</mat-select>
<mat-hint align="start">The file extension to be used for style files.</mat-hint>
</mat-form-field>
</p>
<p>
<mat-form-field>
<input formControlName="module" matInput placeholder="Allows specification of the declaring module. (-m)">
<mat-hint align="start">Allows specification of the declaring module. (-m)</mat-hint>
</mat-form-field>
</p>
<p>
<mat-form-field>
<input formControlName="prefix" matInput placeholder="The prefix to apply to generated selectors. (-p)">
<mat-hint align="start">The prefix to apply to generated selectors. (-p)</mat-hint>
</mat-form-field>
</p>
<p>
<mat-form-field>
<input formControlName="project" matInput placeholder="The name of the project">
<mat-hint align="start">The name of the project</mat-hint>
</mat-form-field>
</p>
<p>
<mat-form-field>
<input formControlName="selector" matInput placeholder="Selector">
<mat-hint align="start">The selector to use for the component.</mat-hint>
</mat-form-field>
</p>
<p>
<mat-list>
<mat-list-item>
<mat-checkbox formControlName="dryRun">Run through without making any changes. (-d)</mat-checkbox>
</mat-list-item>
<mat-list-item>
<mat-checkbox formControlName="entry-component">Entry component</mat-checkbox>
</mat-list-item>
<mat-list-item>
<mat-checkbox formControlName="export">Specifies if declaring module exports the component.</mat-checkbox>
</mat-list-item>
<mat-list-item>
<mat-checkbox formControlName="flat">Flag to indicate if a dir is created.</mat-checkbox>
</mat-list-item>
<mat-list-item>
<mat-checkbox formControlName="force">Forces overwriting of files. (-f)</mat-checkbox>
</mat-list-item>
<mat-list-item>
<mat-checkbox formControlName="inline-style">Specifies if the style will be in the ts file. (-s)</mat-checkbox>
</mat-list-item>
<mat-list-item>
<mat-checkbox formControlName="inline-template">Specifies if the template will be in the ts file. (-t)</mat-checkbox>
</mat-list-item>
<mat-list-item>
<mat-checkbox formControlName="lint-fix">Specifies whether to apply lint fixes after generating the
component.</mat-checkbox>
</mat-list-item>
<mat-list-item>
<mat-checkbox formControlName="skip-import">Flag to skip the module import.</mat-checkbox>
</mat-list-item>
<mat-list-item>
<mat-checkbox formControlName="spec">Specifies if a spec file is generated.</mat-checkbox>
</mat-list-item>
</mat-list>
</p>
</div>
</main>
<div class="action">
<button mat-icon-button type="button" (click)="removeComponent(i);">
<mat-icon aria-label="Example icon-button with a heart icon">remove_circle</mat-icon>
</button>
</div>

</app-drop-down>
<mat-action-row>
<button mat-mini-fab color="primary" type="button" (click)="addNewComponent($event)">
<i class="material-icons action">
add
</i>
</button>
</mat-action-row>
Loading