= Matt Raible's IntelliJ IDEA Live Templates
These are the https://www.jetbrains.com/help/idea/using-live-templates.html[IntelliJ IDEA Live Templates] that I've used in many demos and screencasts over the years.
I used IntelliJ's https://www.jetbrains.com/help/idea/sharing-live-templates.html[sharing live templates] feature, to create idea-settings.jar
. You should be able to import "Matt Raible's Shortcuts" using the following steps:
https://www.jetbrains.com/idea/download/[Download IntelliJ IDEA] today! It's a spectacular IDEA for Java, Kotlin, TypeScript, JavaScript, S/CSS, and HTML.
If you'd rather not import all of my templates, you can clone this project and open it in IntelliJ (with the Asciidoctor plugin installed). You should be able to edit this file and add the shortcuts below as live templates (Tools > Save as Live Template). Make sure to set the file type to match the language.
== Spring Boot
@javax.persistence.Entity class $entity$ {
@javax.persistence.Id
@javax.persistence.GeneratedValue
private java.lang.Long id;
private java.lang.String name;
public $entity$() {}
public $entity$(String name) {
this.name = name;
}
public java.lang.Long getId() {
return id;
}
public void setId(java.lang.Long id) {
this.id = id;
}
public java.lang.String getName() {
return name;
}
public void setName(java.lang.String name) {
this.name = name;
}
@java.lang.Override
public java.lang.String toString() {
return "$entity${" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
@lombok.Data @lombok.AllArgsConstructor @lombok.NoArgsConstructor @javax.persistence.Entity class $name$ {
public $name$(java.lang.String name) {
this.name = name;
}
@javax.persistence.Id
@javax.persistence.GeneratedValue
private java.lang.Long id;
private java.lang.String name;
@org.springframework.stereotype.Component class $entity$CommandLineRunner implements org.springframework.boot.CommandLineRunner {
private final $entity$Repository repository;
public $entity$CommandLineRunner($entity$Repository repository) {
this.repository = repository;
}
@java.lang.Override
public void run(java.lang.String... strings) throws java.lang.Exception {
repository.findAll().forEach(System.out::println);
}
@org.springframework.web.bind.annotation.RestController class $entity$Controller {
private $entity$Repository repository;
public $entity$Controller($entity$Repository repository) {
this.repository = repository;
}
@org.springframework.web.bind.annotation.GetMapping("/$uriMapping$")
java.util.Collection<$entity$> list() {
return repository.findAll();
}
@GetMapping("/good-beers")
public Collection
return repository.findAll().stream()
.filter(this::isGreat)
.collect(Collectors.toList());
}
private boolean isGreat(Beer beer) {
return !beer.getName().equals("Budweiser") &&
!beer.getName().equals("Coors Light") &&
!beer.getName().equals("PBR");
}
== Angular
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import 'rxjs/add/operator/map';
@Injectable() // http://tutorials.pluralsight.com/front-end-javascript/getting-started-with-angular-2-by-building-a-giphy-search-application export class GiphyService {
// Public beta key: https://github.com/Giphy/GiphyAPI#public-beta-key giphyApi = '//api.giphy.com/v1/gifs/search?api_key=dc6zaTOxFJmzC&limit=1&q=';
constructor(public http: HttpClient) { }
import { Injectable } from '@angular/core'; import * as OktaSignIn from '@okta/okta-signin-widget/dist/js/okta-sign-in.min.js' import { ReplaySubject } from 'rxjs/ReplaySubject'; import { Observable } from 'rxjs/Observable';
@Injectable() export class OktaAuthService {
signIn = new OktaSignIn({ baseUrl: 'https://$yourOktaDomain$', clientId: '$clientId$', authParams: { issuer: 'https://$yourOktaDomain$', responseType: ['id_token', 'token'], scopes: ['openid', 'email', 'profile'] } });
public user$: Observable
constructor() {
this.userSource = new ReplaySubject
isAuthenticated() { // Checks if there is a current accessToken in the TokenManger. return !!this.signIn.tokenManager.get('accessToken'); }
login() { // Launches the widget and stores the tokens. this.signIn.renderEl({el: '#okta-signin-container'}, response => { if (response.status === 'SUCCESS') { response.forEach(token => { if (token.idToken) { this.signIn.tokenManager.add('idToken', token); } if (token.accessToken) { this.signIn.tokenManager.add('accessToken', token); } this.userSource.next(this.idTokenAsUser); this.signIn.hide(); }); } else { console.error(response); } }); }
get idTokenAsUser() { const token = this.signIn.tokenManager.get('idToken'); return { name: token.claims.name, email: token.claims.email, username: token.claims.preferred_username } }
user;
constructor(public oktaService: OktaAuthService, private changeDetectorRef: ChangeDetectorRef) { }
ngOnInit() { // 1. for initial load and browser refresh if (this.oktaService.isAuthenticated()) { this.user = this.oktaService.idTokenAsUser; } else { this.oktaService.login(); }
// 2. register a listener for authentication and logout
this.oktaService.user$.subscribe(user => {
this.user = user;
if (!user) {
this.oktaService.login();
}
// Let Angular know that model changed.
// See https://github.com/okta/okta-signin-widget/issues/268 for more info.
this.changeDetectorRef.detectChanges();
});
<div *ngIf="user">
<button mat-raised-button (click)="oktaService.logout()">Logout
== Cloud Foundry
applications:
name: beer-server host: beer-server path: ./server/target/demo-0.0.1-SNAPSHOT.jar env : FORCE_HTTPS: true
sed -i -e "s|http://localhost:4200|https://beer-server.cfapps.io|g" $start/server/src/main/java/com/okta/developer/demo/DemoApplication.java
mvn clean package -f $start/server/pom.xml
cd $start/client rm -rf dist
sed -i -e "s|http://localhost:8080|https://beer-server.cfapps.io|g" $start/client/src/app/shared/beer/beer.service.ts
sed -i -e "s|http://localhost:4200|https://beer-client.cfapps.io|g" $start/client/src/app/shared/okta/okta.service.ts yarn && ng build -prod --aot touch dist/Staticfile
echo 'pushstate: enabled' > dist/Staticfile
cd $start cf push
set -e
CLOUD_DOMAIN=${DOMAIN:-run.pivotal.io} CLOUD_TARGET=api.${DOMAIN}
function login(){ cf api | grep ${CLOUD_TARGET} || cf api ${CLOUD_TARGET} --skip-ssl-validation cf apps | grep OK || cf login }
function app_domain(){
D=cf apps | grep $1 | tr -s ' ' | cut -d' ' -f 6 | cut -d, -f1
echo $D
}
function deploy_service(){
N=$1
D=app_domain $N
JSON='{"uri":"http://'$D'"}'
cf create-user-provided-service $N -p $JSON
}
cd dirname $0
r=pwd
echo $r
cf d -f react-client cf d -f good-beer-server
cf a
cd $r/server mvn clean package cf push -p target/*jar good-beer-server --no-start --random-route cf set-env good-beer-server FORCE_HTTPS true
serverUri=https://`app_domain good-beer-server`
cd $r/client rm -rf build
sed -i -e "s|http://localhost:8080|$serverUri|g" $r/client/src/BeerList.tsx yarn && yarn build cd build touch Staticfile echo 'pushstate: enabled' > Staticfile cf push react-client --no-start --random-route cf set-env react-client FORCE_HTTPS true cf start react-client
clientUri=https://`app_domain react-client`
sed -i -e "s|http://localhost:3000|$clientUri|g" $r/server/src/main/java/com/example/demo/DemoApplication.java
cd $r/server mvn package -DskipTests cf push -p target/*jar good-beer-server
git checkout $r/client git checkout $r/server rm $r/client/src/BeerList.tsx-e rm $r/server/src/main/java/com/example/demo/DemoApplication.java-e
== Heroku
set -e
cd dirname $0
r=pwd
echo $r
if [ -z "$(which heroku)" ]; then echo "You must install the Heroku CLI first!" echo "https://devcenter.heroku.com/articles/heroku-cli" exit 1 fi
if ! echo "$(heroku plugins)" | grep -q heroku-cli-deploy; then heroku plugins:install heroku-cli-deploy fi
if ! echo "$(git remote -v)" | grep -q good-beer-server-; then server_app=good-beer-server-$RANDOM heroku create -r server $server_app else server_app=$(heroku apps:info -r server --json | python -c 'import json,sys;print json.load(sys.stdin)["app"]["name"]') fi serverUri="https://$server_app.herokuapp.com"
if ! echo "$(git remote -v)" | grep -q react-client-; then client_app=react-client-$RANDOM heroku create -r client $client_app else client_app=$(heroku apps:info -r client --json | python -c 'import json,sys;print json.load(sys.stdin)["app"]["name"]') fi clientUri="https://$client_app.herokuapp.com"
sed -i -e "s|http://localhost:3000|$clientUri|g" $r/server/src/main/java/com/example/demo/DemoApplication.java
cd $r/server mvn clean package -DskipTests
heroku deploy:jar target/*jar -r server -o "--server.port=\$PORT" heroku config:set -r server FORCE_HTTPS="true"
cd $r/client rm -rf build
sed -i -e "s|http://localhost:8080|$serverUri|g" $r/client/src/BeerList.tsx yarn && yarn build cd build
cat << EOF > static.json { "https_only": true, "root": ".", "routes": { "/**": "index.html" } } EOF
rm -f ../dist.tgz tar -zcvf ../dist.tgz .
heroku static:deploy
source=$(curl -n -X POST https://api.heroku.com/apps/$client_app/sources -H 'Accept: application/vnd.heroku+json; version=3') get_url=$(echo "$source" | python -c 'import json,sys;print json.load(sys.stdin)["source_blob"]["get_url"]') put_url=$(echo "$source" | python -c 'import json,sys;print json.load(sys.stdin)["source_blob"]["put_url"]') curl "$put_url" -X PUT -H 'Content-Type:' --data-binary @../dist.tgz cat << EOF > build.json { "buildpacks": [{ "url": "https://github.com/heroku/heroku-buildpack-static" }], "source_blob": { "url" : "$get_url" } } EOF build_out=$(curl -n -s -X POST https://api.heroku.com/apps/$client_app/builds \ -d "$(cat build.json)" \ -H 'Accept: application/vnd.heroku+json; version=3' \ -H "Content-Type: application/json") output_stream_url=$(echo "$build_out" | python -c 'import json,sys;print json.load(sys.stdin)["output_stream_url"]') curl -s -L "$output_stream_url"
rm build.json rm ../dist.tgz
git checkout $r/client git checkout $r/server rm $r/client/src/BeerList.tsx-e rm $r/server/src/main/java/com/example/demo/DemoApplication.java-e
== JHipster
<div class="table-responsive" ngIf="entries"> <div infinite-scroll (scrolled)="loadPage(page + 1)" [infiniteScrollDisabled]="page >= links['last']" [infiniteScrollDistance]="0"> <div ngFor="let entry of entries; trackBy: trackId">
<small>Posted on {{entry.date | date: 'short'}} by {{entry.blog.user.login}}</small>
<div [innerHTML]="entry.content"></div>
<div class="btn-group mb-2 mt-1">
<button type="submit"
[routerLink]="['/entry', entry.id, 'edit']"
class="btn btn-primary btn-sm">
<fa-icon [icon]="'pencil-alt'"></fa-icon>
<span class="d-none d-md-inline" jhiTranslate="entity.action.edit">Edit</span>
</button>
<button type="submit"
[routerLink]="['/', { outlets: { popup: 'entry/'+ entry.id + '/delete'} }]"
replaceUrl="true"
queryParamsHandling="merge"
class="btn btn-danger btn-sm">
<fa-icon [icon]="'times'"></fa-icon>
<span class="d-none d-md-inline" jhiTranslate="entity.action.delete">Delete</span>
</button>
</div>
</div>
</div>
== React
const config = { issuer: 'https://$yourOktaDomain$/oauth2/default', redirectUri: window.location.origin + '/implicit/callback', clientId: '$clientId$' };
render() { return (
import * as React from 'react'; import './App.css'; import BeerList from './BeerList'; import { withAuth } from '@okta/okta-react'; import { Auth } from './App';
const logo = require('./logo.svg');
interface HomeProps { auth: Auth; }
interface HomeState { authenticated: boolean; }
export default withAuth(class Home extends React.Component<HomeProps, HomeState> { constructor(props: HomeProps) { super(props); this.state = {authenticated: false}; this.checkAuthentication = this.checkAuthentication.bind(this); this.checkAuthentication(); }
async checkAuthentication() { const isAuthenticated = await this.props.auth.isAuthenticated(); const {authenticated} = this.state; if (isAuthenticated !== authenticated) { this.setState({authenticated: isAuthenticated}); } }
componentDidUpdate() { this.checkAuthentication(); }
render() { const {authenticated} = this.state; let body = null; if (authenticated) { body = (
);
} else {
body = (
<div className="Buttons">
<button onClick={this.props.auth.login}>Login</button>
</div>
);
}
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo"/>
<h2>Welcome to React</h2>
</div>
{body}
</div>
);
async componentDidMount() { this.setState({isLoading: true});
== Contributing
Want to add more? Have you figured out how to import live templates? Send a pull request!