ZmtDochostPlugin.groovy 2.97 KB
Newer Older
1 2 3 4 5 6
package de.zmt.gradle;

import org.gradle.api.*
import groovy.swing.*

/*
7
 * Plugin for getting user / password for dochost server.
8 9 10 11
 * <p>
 * With this plugin a task 'determineCredentials' is added to the project
 * that asks the user to enter login credentials.
 */
12
class ZmtDochostPlugin implements Plugin<Project> {
13
    private static final def CONFIGURE = {
14
        apply plugin: 'nu.studer.credentials'
15 16 17

        task('determineCredentials') {
            description = 'Ask user to enter credentials.'
18
            ext.host = 'dochost'
19
            ext.wwwPath = '/var/www/dochost/'
20
            onlyIf { !credentials.dochostUser || !credentials.dochostPw }
21 22 23 24 25 26 27
        }

        determineCredentials.doLast {
            def console = System.console()
            // we can access console: ask for username / password
            if (console) {
                console.printf("\nEnter username: ")
28
                credentials.dochostUser = console.readLine()
29 30
                console.printf("\nEnter password: ")
                // readPassword() returns char array, wrap it into string
31
                credentials.dochostPw = new String(console.readPassword())
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
            }
            // no console: create dialog and get it from there
            else {
                // https://www.timroes.de/2014/01/19/using-password-prompts-with-gradle-build-files/
                new SwingBuilder().edt {
                    dialog(
                        modal: true, // Otherwise the build will continue running before you closed the dialog
                        title: 'Connecting...', // Dialog title
                        alwaysOnTop: true, // pretty much what the name says
                        resizable: false, // Don't allow the user to resize the dialog
                        locationRelativeTo: null, // Place dialog in center of the screen
                        pack: true, // We need to pack the dialog (so it will take the size of it's children)
                        show: true // Let's show it
                    ) {
                        vbox {
                            label(text: "username: ")
                            userInput = textField(text: System.getProperty("user.name"))
                            userInput.selectAll()
                            label(text: "password: ")
                            pwInput = passwordField()
                            button(defaultButton: true, text: 'OK', actionPerformed: {
53 54
                                credentials.dochostUser = userInput.text
                                credentials.dochostPw = pwInput.text
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
                                dispose() // Close dialog
                            })
                        }
                    }
                }
            }
            
            println "Use task 'addCredentials' with keys 'dochostUser' and 'dochostPw' if you want to store credentials permanently."
        }
    }

    @Override
    void apply(Project project) {
        project.configure(project, CONFIGURE)
    }
}