|
| 1 | +import org.gradle.logging.StyledTextOutput; |
| 2 | +import org.gradle.logging.StyledTextOutputFactory; |
| 3 | +import static org.gradle.logging.StyledTextOutput.Style; |
| 4 | + |
| 5 | +apply plugin: Build |
| 6 | + |
| 7 | +class Build implements Plugin<Project> { |
| 8 | + def project |
| 9 | + def teamNumber |
| 10 | + |
| 11 | + void apply(Project project) { |
| 12 | + this.project = project |
| 13 | + System.setProperty('org.gradle.color.header', 'CYAN') |
| 14 | + |
| 15 | + project.repositories.add(project.repositories.mavenCentral()) |
| 16 | + |
| 17 | + project.getConfigurations().maybeCreate('compile') |
| 18 | + |
| 19 | + def sshAntTask = project.getConfigurations().maybeCreate('sshAntTask') |
| 20 | + project.dependencies.add(sshAntTask.name, 'org.apache.ant:ant-jsch:1.7.1') |
| 21 | + project.dependencies.add(sshAntTask.name, 'jsch:jsch:0.1.29') |
| 22 | + |
| 23 | + project.ant.taskdef(name: 'scp', |
| 24 | + classname: 'org.apache.tools.ant.taskdefs.optional.ssh.Scp', |
| 25 | + classpath: sshAntTask.asPath) |
| 26 | + |
| 27 | + def dl = project.task('download') << { |
| 28 | + String baseURL = "http://dev.sourcecoded.info/jaci/maven/jaci/openrio/toast/Toast"; |
| 29 | + download("caches/toast", "${baseURL}/maven-metadata.xml", "toast-metadata.xml") |
| 30 | + def latest = getLatest() |
| 31 | + println "\tLatest Toast version: ${latest}" |
| 32 | + download("caches/toast", "${baseURL}/${latest}/Toast-${latest}.jar", "toast.jar") |
| 33 | + println "\tToast Download Successful!" |
| 34 | + } |
| 35 | + |
| 36 | + def dp = project.task('deploy') << { |
| 37 | + if (teamNumber == null) teamNumber = project.team |
| 38 | + File file = new File("caches/toast/toast.jar") |
| 39 | + if (!file.exists()) |
| 40 | + throw new Exception("Could not deploy (file not found). Have you tried running 'gradlew download' first?") |
| 41 | + try { |
| 42 | + deploy(rioHost(project), file) |
| 43 | + } catch (Exception e) { |
| 44 | + println "\tCould not deploy to Hostname, attempting IP...." |
| 45 | + deploy(rioIP(project), file) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + project.task('combo') << { |
| 50 | + def out = services.get(StyledTextOutputFactory).create("Toast") |
| 51 | + out.withStyle(Style.Header).println "Connect to the internet and hit 'enter' to download Toast!" |
| 52 | + System.console().readLine() |
| 53 | + dl.execute() |
| 54 | + out.withStyle(Style.Header).println "Please enter your Team Number and hit 'enter'" |
| 55 | + teamNumber = System.console().readLine("\tTeam Number: ") |
| 56 | + out.withStyle(Style.Header).println "Connect to the same network as the RoboRIO and hit 'enter' to deploy Toast!" |
| 57 | + System.console().readLine() |
| 58 | + dp.execute() |
| 59 | + out.withStyle(Style.Header).println "Toast has been deployed to your RoboRIO successfully! Hit 'enter' to continue!" |
| 60 | + System.console().readLine() |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + String getLatest() { |
| 65 | + def xml = new XmlSlurper().parse("caches/toast/toast-metadata.xml").versioning.versions.depthFirst() |
| 66 | + return (xml.toList() as Set).last() |
| 67 | + } |
| 68 | + |
| 69 | + void download(String dest, String from, String name) { |
| 70 | + File output = new File(dest, name) |
| 71 | + File f = new File(dest) |
| 72 | + f.mkdirs() |
| 73 | + def file = new FileOutputStream(output) |
| 74 | + def out = new BufferedOutputStream(file) |
| 75 | + HttpURLConnection httpcon = new URL(from).openConnection() |
| 76 | + httpcon.addRequestProperty("User-Agent", "Toast-Deployment") |
| 77 | + out << httpcon.getInputStream() |
| 78 | + out.close() |
| 79 | + } |
| 80 | + |
| 81 | + void deploy(String host, File source) { |
| 82 | + println "\tAttempting to send new code to RoboRIO... ${host}" |
| 83 | + |
| 84 | + project.ant.scp(file: "${source.getAbsolutePath()}", |
| 85 | + todir:"lvuser@${host}:FRCUserProgram.jar", |
| 86 | + password:"", |
| 87 | + port:22, |
| 88 | + trust:true) |
| 89 | + |
| 90 | + println "\tDeploy Successful!" |
| 91 | + } |
| 92 | + |
| 93 | + String rioHost(Project project) { |
| 94 | + return "roboRIO-${teamNumber}.local" |
| 95 | + } |
| 96 | + |
| 97 | + String rioIP(Project project) { |
| 98 | + String team = teamNumber |
| 99 | + int length = team.length(); |
| 100 | + if (length < 4) |
| 101 | + for (int i = 0; i < 4 - length; i++) |
| 102 | + team = "0" + team; |
| 103 | + |
| 104 | + return "10." + team.substring(0, 2) + "." + team.substring(2, 4) + ".20" |
| 105 | + } |
| 106 | +} |
0 commit comments