ZmtDochostPlugin.groovy 3.26 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
        apply plugin: 'org.hidetake.ssh'
16 17 18

        task('determineCredentials') {
            description = 'Ask user to enter credentials.'
19
            ext.host = 'dochost'
20
            ext.wwwPath = '/var/www/dochost/'
21
            onlyIf { !credentials.dochostUser || !credentials.dochostPw }
22
        }
23 24 25 26 27 28 29 30 31
        
        remotes {
            dochost {
                host = determineCredentials.host
                knownHosts = allowAnyHosts
                user = credentials.dochostUser
                password = credentials.dochostPw
            }
        }
32 33 34 35 36 37

        determineCredentials.doLast {
            def console = System.console()
            // we can access console: ask for username / password
            if (console) {
                console.printf("\nEnter username: ")
38
                credentials.dochostUser = console.readLine()
39 40
                console.printf("\nEnter password: ")
                // readPassword() returns char array, wrap it into string
41
                credentials.dochostPw = new String(console.readPassword())
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
            }
            // 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: {
63 64
                                credentials.dochostUser = userInput.text
                                credentials.dochostPw = pwInput.text
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
                                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)
    }
}