diff --git a/build.gradle b/build.gradle index 42e14ab..139ba90 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ plugins { def kotlinVersion = "1.3.50" group 'com.ivieleague' -version '0.0.5' +version '0.0.6' application { mainClassName = "com.ivieleague.skate.MainKt" diff --git a/buildHelper.kt b/buildHelper.kt index bf0eeb3..4a9c321 100644 --- a/buildHelper.kt +++ b/buildHelper.kt @@ -1,131 +1,132 @@ -@file:DependsOn("org.kohsuke:github-api:1.95") -@file:DependsOn("org.eclipse.jgit:org.eclipse.jgit:5.4.2.201908231537-r") - -import org.eclipse.jgit.api.CreateBranchCommand -import org.eclipse.jgit.api.Git -import org.eclipse.jgit.lib.Repository -import org.eclipse.jgit.storage.file.FileRepositoryBuilder -import org.eclipse.jgit.transport.CredentialsProvider -import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider -import org.kohsuke.github.GHIssueState -import org.kohsuke.github.GHRepository -import org.kohsuke.github.GitHub -import java.io.File -import java.text.DateFormat -import java.util.* -import java.util.concurrent.TimeUnit - -val github by lazy { GitHub.connect() } -fun git(base: File) = Git(FileRepositoryBuilder().setGitDir(base.resolve(".git")).build()) -val githubAuth by lazy { - val lines = File(System.getProperty("user.home")).resolve(".github").readLines() - lines.find { it.startsWith("oauth") }?.let { oauthLine -> - val oauth = oauthLine.substringAfter('=').trim() - return@lazy UsernamePasswordCredentialsProvider("token", oauth) - } - lines.find { it.startsWith("login") }?.let { loginLine -> - lines.find { it.startsWith("password") }?.let { passwordLine -> - val login = loginLine.substringAfter('=').trim() - val password = passwordLine.substringAfter('=').trim() - return@lazy UsernamePasswordCredentialsProvider(login, password) - } - } -} - -class GithubTasks(val git: Git, val remote: GHRepository) { - fun start(issueNumber: Int, basedOnBranch: String = remote.defaultBranch) { - val issue = remote.getIssue(issueNumber) - val branchName: String = - "issue-$issueNumber-${issue.title.map { if (it.isLetterOrDigit()) it.toLowerCase() else '-' }.joinToString("")}" - issue.assignTo(github.myself) - issue.comment("I have started working on this issue. The branch name will be '$branchName'.") - if (!git.status().call().isClean) { - git.stashCreate().call() - } - git.checkout().setName(basedOnBranch).call() - git.pull().setRemote("origin").setCredentialsProvider(githubAuth).setRemoteBranchName(basedOnBranch).call() - git.branchCreate().setName(branchName).call() - git.checkout().setName(branchName).call() - } - - fun pause(message: String) { - //Commit current stuff - git.commit().setMessage(message).setAuthor("Joseph Ivie", "josephivie@gmail.com").call() - - //Reset to master - git.checkout().setName(remote.defaultBranch).call() - git.pull().setRemote("origin").setCredentialsProvider(githubAuth).setRemoteBranchName(remote.defaultBranch) - .call() - } - - fun submit( - title: String, - message: String = "", - issueNumber: Int = git.repository.branch.substringAfter("issue-").takeWhile { it.isDigit() }.toInt() - ) { - //Push - git.add().addFilepattern(".").call() - git.commit().setMessage(message).setAuthor("Joseph Ivie", "josephivie@gmail.com").call() - git.push().setRemote("origin").setCredentialsProvider(githubAuth).call() - val pushedBranch = git.repository.branch - - //Reset to master - git.checkout().setName(remote.defaultBranch).call() - git.pull().setRemote("origin").setCredentialsProvider(githubAuth).setRemoteBranchName(remote.defaultBranch) - .call() - - //Create pull request - val p = remote.createPullRequest( - title, - pushedBranch, - remote.defaultBranch, - message + "\nThis branch handles issue #$issueNumber.}" - ) - - //Comment on issue - val issue = remote.getIssue(issueNumber) - issue.comment("This issue is solved by pull request #${p.number}.") - } -} - -val git = git(File("C:\\Users\\josep\\Projects\\standalone-kotlin-file")) -val repo = github.getRepository("UnknownJoe796/skate") -val tasks = GithubTasks(git, repo) - -fun release(versionString: String, updateInfo: String = "") { - - //Reset to Master - println("Resetting to Master") - git.checkout().setName(repo.defaultBranch).call() - git.pull().setRemote("origin").setCredentialsProvider(githubAuth).setRemoteBranchName(repo.defaultBranch).call() - - //Update build number - println("Updating build number to $versionString") - File("build.gradle").let { - it.writeText(it.readLines().map { - if (it.trim().startsWith("version")) { - "version '$versionString'" - } else it - }.joinToString("\n")) - } - - //Commit and push - println("Committing change") - git.add().addFilepattern(".").call() - git.commit().setMessage("Version bump to $versionString").setAuthor("Joseph Ivie", "josephivie@gmail.com").call() - git.push().setRemote("origin").setCredentialsProvider(githubAuth).call() - - //Build - println("Building") - val process = ProcessBuilder(".\\gradlew.bat", "distZip").inheritIO().start() - process.waitFor(60, TimeUnit.SECONDS) - assert(process.exitValue() == 0) - - //Push release - println("Pushing release") - repo.createRelease(versionString) - .name(versionString) - .body(updateInfo) - .create() - .uploadAsset(File("build/distributions/skate-${versionString}.zip"), "application/zip") -} \ No newline at end of file +@file:DependsOn("org.kohsuke:github-api:1.95") +@file:DependsOn("org.eclipse.jgit:org.eclipse.jgit:5.4.2.201908231537-r") + +import org.eclipse.jgit.api.CreateBranchCommand +import org.eclipse.jgit.api.Git +import org.eclipse.jgit.lib.Repository +import org.eclipse.jgit.storage.file.FileRepositoryBuilder +import org.eclipse.jgit.transport.CredentialsProvider +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider +import org.kohsuke.github.GHIssueState +import org.kohsuke.github.GHRepository +import org.kohsuke.github.GitHub +import java.io.File +import java.text.DateFormat +import java.util.* +import java.util.concurrent.TimeUnit + +val github by lazy { GitHub.connect() } +fun git(base: File) = Git(FileRepositoryBuilder().setGitDir(base.resolve(".git")).build()) +val githubAuth by lazy { + val lines = File(System.getProperty("user.home")).resolve(".github").readLines() + lines.find { it.startsWith("oauth") }?.let { oauthLine -> + val oauth = oauthLine.substringAfter('=').trim() + return@lazy UsernamePasswordCredentialsProvider("token", oauth) + } + lines.find { it.startsWith("login") }?.let { loginLine -> + lines.find { it.startsWith("password") }?.let { passwordLine -> + val login = loginLine.substringAfter('=').trim() + val password = passwordLine.substringAfter('=').trim() + return@lazy UsernamePasswordCredentialsProvider(login, password) + } + } +} + +class GithubTasks(val git: Git, val remote: GHRepository) { + fun start(issueNumber: Int, basedOnBranch: String = remote.defaultBranch) { + val issue = remote.getIssue(issueNumber) + val branchName: String = + "issue-$issueNumber-${issue.title.map { if (it.isLetterOrDigit()) it.toLowerCase() else '-' }.joinToString("")}" + issue.assignTo(github.myself) + issue.comment("I have started working on this issue. The branch name will be '$branchName'.") + if (!git.status().call().isClean) { + git.stashCreate().call() + } + git.checkout().setName(basedOnBranch).call() + git.pull().setRemote("origin").setCredentialsProvider(githubAuth).setRemoteBranchName(basedOnBranch).call() + git.branchCreate().setName(branchName).call() + git.checkout().setName(branchName).call() + } + + fun pause(message: String) { + //Commit current stuff + git.commit().setMessage(message).setAuthor("Joseph Ivie", "josephivie@gmail.com").call() + + //Reset to master + git.checkout().setName(remote.defaultBranch).call() + git.pull().setRemote("origin").setCredentialsProvider(githubAuth).setRemoteBranchName(remote.defaultBranch) + .call() + } + + fun submit( + title: String, + message: String = "", + issueNumber: Int = git.repository.branch.substringAfter("issue-").takeWhile { it.isDigit() }.toInt() + ) { + //Push + git.add().addFilepattern(".").call() + git.commit().setMessage(message).setAuthor("Joseph Ivie", "josephivie@gmail.com").call() + git.push().setRemote("origin").setCredentialsProvider(githubAuth).call() + val pushedBranch = git.repository.branch + + //Reset to master + git.checkout().setName(remote.defaultBranch).call() + git.pull().setRemote("origin").setCredentialsProvider(githubAuth).setRemoteBranchName(remote.defaultBranch) + .call() + + //Create pull request + val p = remote.createPullRequest( + title, + pushedBranch, + remote.defaultBranch, + message + "\nThis branch handles issue #$issueNumber.}" + ) + + //Comment on issue + val issue = remote.getIssue(issueNumber) + issue.comment("This issue is solved by pull request #${p.number}.") + } +} + +val git = git(File("./")) +val repo = github.getRepository("UnknownJoe796/skate") +val tasks = GithubTasks(git, repo) + +fun release(versionString: String, updateInfo: String = "") { + + //Reset to Master + println("Resetting to Master") + git.checkout().setName(repo.defaultBranch).call() + git.pull().setRemote("origin").setCredentialsProvider(githubAuth).setRemoteBranchName(repo.defaultBranch).call() + + //Update build number + println("Updating build number to $versionString") + File("build.gradle").let { + it.writeText(it.readLines().map { + if (it.trim().startsWith("version")) { + "version '$versionString'" + } else it + }.joinToString("\n")) + } + + //Commit and push + println("Committing change") + git.add().addFilepattern(".").call() + git.commit().setMessage("Version bump to $versionString").setAuthor("Joseph Ivie", "josephivie@gmail.com").call() + git.push().setRemote("origin").setCredentialsProvider(githubAuth).call() + + //Build + println("Building") + val exec = if (System.getProperty("os.name").toLowerCase().contains("win")) ".\\gradlew.bat" else "./gradlew" + val process = ProcessBuilder(".\\gradlew.bat", "distZip").inheritIO().start() + process.waitFor(60, TimeUnit.SECONDS) + assert(process.exitValue() == 0) + + //Push release + println("Pushing release") + repo.createRelease(versionString) + .name(versionString) + .body(updateInfo) + .create() + .uploadAsset(File("build/distributions/skate-${versionString}.zip"), "application/zip") +} diff --git a/src/main/kotlin/com/ivieleague/skate/File.createSymbolicLink.kt b/src/main/kotlin/com/ivieleague/skate/File.createSymbolicLink.kt index cc39244..3a4f664 100644 --- a/src/main/kotlin/com/ivieleague/skate/File.createSymbolicLink.kt +++ b/src/main/kotlin/com/ivieleague/skate/File.createSymbolicLink.kt @@ -1,30 +1,31 @@ -package com.ivieleague.skate - -import java.io.File -import java.nio.file.Files -import java.util.concurrent.TimeUnit - -fun File.createSymbolicLinkTo(other: File): Boolean { - return try { - Files.createSymbolicLink(this.toPath(), other.toPath()) - - true - } catch (e: Exception) { - if (System.getProperty("os.name").contains("win", true)) { - val batLoc = File(System.getProperty("user.home")).resolve(".temp/make_sym_link.bat").also { - if (!it.exists()) { - it.parentFile.mkdirs() - it.writeText("mklink %1 %2") - } - } - ProcessBuilder() - .command(batLoc.absolutePath, this.absolutePath, other.absolutePath) -// .inheritIO() - .start() - .waitFor(100L, TimeUnit.MILLISECONDS) - return true - } - - false - } -} \ No newline at end of file +package com.ivieleague.skate + +import java.io.File +import java.nio.file.Files +import java.util.concurrent.TimeUnit + +fun File.createSymbolicLinkTo(other: File): Boolean { + return try { + println("Linking $this <==> $other") + Files.createSymbolicLink(this.absoluteFile.toPath(), other.absoluteFile.toPath()) + + true + } catch (e: Exception) { + if (System.getProperty("os.name").contains("win", true)) { + val batLoc = File(System.getProperty("user.home")).resolve(".temp/make_sym_link.bat").also { + if (!it.exists()) { + it.parentFile.mkdirs() + it.writeText("mklink %1 %2") + } + } + ProcessBuilder() + .command(batLoc.absolutePath, this.absolutePath, other.absolutePath) +// .inheritIO() + .start() + .waitFor(100L, TimeUnit.MILLISECONDS) + return true + } + + false + } +} diff --git a/src/main/kotlin/com/ivieleague/skate/Maven.kt b/src/main/kotlin/com/ivieleague/skate/Maven.kt index 2dd487d..70540ca 100644 --- a/src/main/kotlin/com/ivieleague/skate/Maven.kt +++ b/src/main/kotlin/com/ivieleague/skate/Maven.kt @@ -1,173 +1,139 @@ -package com.ivieleague.skate - -import org.apache.maven.repository.internal.MavenRepositorySystemUtils -import org.eclipse.aether.RepositorySystem -import org.eclipse.aether.RepositorySystemSession -import org.eclipse.aether.artifact.Artifact -import org.eclipse.aether.artifact.DefaultArtifact -import org.eclipse.aether.collection.CollectRequest -import org.eclipse.aether.collection.CollectResult -import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory -import org.eclipse.aether.graph.Dependency -import org.eclipse.aether.graph.DependencyNode -import org.eclipse.aether.repository.LocalRepository -import org.eclipse.aether.repository.RemoteRepository -import org.eclipse.aether.resolution.ArtifactRequest -import org.eclipse.aether.resolution.VersionRangeRequest -import org.eclipse.aether.spi.connector.RepositoryConnectorFactory -import org.eclipse.aether.spi.connector.transport.TransporterFactory -import org.eclipse.aether.transport.file.FileTransporterFactory -import org.eclipse.aether.transport.http.HttpTransporterFactory -import java.io.File -import java.io.PrintStream - - -object Maven { - private val repositorySystem: RepositorySystem = run { - val locator = MavenRepositorySystemUtils.newServiceLocator() - locator.addService(RepositoryConnectorFactory::class.java, BasicRepositoryConnectorFactory::class.java) - locator.addService(TransporterFactory::class.java, FileTransporterFactory::class.java) - locator.addService(TransporterFactory::class.java, HttpTransporterFactory::class.java) - - locator.getService(RepositorySystem::class.java) - } - - private fun session(): RepositorySystemSession { - val session = MavenRepositorySystemUtils.newSession() - - val localRepo = LocalRepository(File(File(System.getProperty("user.home")), ".maven-cache")) - session.localRepositoryManager = repositorySystem.newLocalRepositoryManager(session, localRepo) - - return session - } - - fun resolveVersions( - repositories: List, - dependencies: List, - session: RepositorySystemSession = session(), - output: PrintStream = System.out - ): List { - return dependencies.map { - var versionString = it.artifact.version - if (versionString.contains('+')) { - val beforeParts = versionString.substringBefore('+').split('.').mapNotNull { it.toIntOrNull() } - val before = beforeParts.joinToString(".") - val afterParts = beforeParts.dropLast(1) + (beforeParts.last() + 1) - val after = afterParts.joinToString(".") - versionString = "[$before, $after)" - } - if (versionString.contains('(') || versionString.contains('[')) { - val withVersionString = it.artifact.setVersion(versionString) - val result = - repositorySystem.resolveVersionRange( - session, - VersionRangeRequest(withVersionString, repositories, null) - ) - Dependency( - it.artifact.setVersion(result.highestVersion.toString()), - it.scope, - it.optional, - it.exclusions - ) - } else it - } - } - - fun DependencyNode.allArtifacts(): Sequence = - (if (this.artifact != null) sequenceOf(this.artifact) else sequenceOf()) + this.children.asSequence().flatMap { it.allArtifacts() } - - fun libraries( - repositories: List, - dependencies: List, - output: PrintStream = System.out - ): List { - val session = session() - val versionedDependencies = resolveVersions(repositories, dependencies, session, output) - val dependencyResults: CollectResult = repositorySystem.collectDependencies( - session, - CollectRequest(versionedDependencies, null, repositories) - ) - - when (dependencyResults.exceptions.size) { - 0 -> { - } - 1 -> throw dependencyResults.exceptions.first() - else -> throw Exception("Several exceptions: ${dependencyResults.exceptions.joinToString("\n") { - it?.message ?: "?" - }}") - } - - return dependencyResults.root.allArtifacts() - .map { - output.println("Obtaining ${it.run { "$groupId:$artifactId:$version" }}") - Library( - name = it.run { "$groupId:$artifactId:$version" }, - default = repositorySystem.resolveArtifact( - session, - ArtifactRequest(it, repositories, null) - ).let { result -> - if (result.isResolved) - result.artifact.file - else - throw IllegalStateException("Could not resolve ${it.run { "$groupId:$artifactId:$version" }}: ${result.exceptions.joinToString { - it.message ?: "" - }}") - }, - javadoc = try { - repositorySystem.resolveArtifact( - session, - ArtifactRequest(it.javadoc(), repositories, null) - ).let { result -> - if (result.isResolved) - result.artifact.file - else - null - } - } catch (e: Exception) { - null - }, - sources = try { - repositorySystem.resolveArtifact( - session, - ArtifactRequest(it.sources(), repositories, null) - ).let { result -> - if (result.isResolved) - result.artifact.file - else - null - } - } catch (e: Exception) { - null - } - ) - } - .toList() - } - - val central = RemoteRepository.Builder("central", "default", "http://repo1.maven.org/maven2/").build() - val jcenter = RemoteRepository.Builder("jcenter", "default", "http://jcenter.bintray.com/").build() - val google = RemoteRepository.Builder("google", "default", "https://dl.google.com/dl/android/maven2/").build() - val local = RemoteRepository.Builder( - "local", - "default", - "file://" + File(File(System.getProperty("user.home")), ".m2").invariantSeparatorsPath - ).build() - val defaultRepositories = listOf(central, jcenter, google, local) - - const val kotlinStandardLibrary = "org.jetbrains.kotlin:kotlin-stdlib:1.3.+" - - fun compile(stringAddress: String) = Dependency(DefaultArtifact(stringAddress), "compile") -} - -fun Artifact.javadoc() = DefaultArtifact(this.groupId, this.artifactId, "javadoc", "jar", this.version) -fun Artifact.sources() = DefaultArtifact(this.groupId, this.artifactId, "sources", "jar", this.version) - -fun main() { - Maven.libraries( - repositories = Maven.defaultRepositories, - dependencies = listOf(Maven.compile("org.jetbrains.kotlin:kotlin-compiler-embeddable:jar:1.3.+")) - ).let { - println("Dependencies: ") - println(it.joinToString("\n")) - } -} \ No newline at end of file +package com.ivieleague.skate + +import org.apache.maven.repository.internal.MavenRepositorySystemUtils +import org.eclipse.aether.RepositorySystem +import org.eclipse.aether.RepositorySystemSession +import org.eclipse.aether.artifact.Artifact +import org.eclipse.aether.artifact.DefaultArtifact +import org.eclipse.aether.collection.CollectRequest +import org.eclipse.aether.collection.CollectResult +import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory +import org.eclipse.aether.graph.Dependency +import org.eclipse.aether.graph.DependencyNode +import org.eclipse.aether.repository.LocalRepository +import org.eclipse.aether.repository.RemoteRepository +import org.eclipse.aether.resolution.ArtifactRequest +import org.eclipse.aether.spi.connector.RepositoryConnectorFactory +import org.eclipse.aether.spi.connector.transport.TransporterFactory +import org.eclipse.aether.transport.file.FileTransporterFactory +import org.eclipse.aether.transport.http.HttpTransporterFactory +import java.io.File +import java.io.PrintStream + + +object Maven { + private val repositorySystem: RepositorySystem = run { + val locator = MavenRepositorySystemUtils.newServiceLocator() + locator.addService(RepositoryConnectorFactory::class.java, BasicRepositoryConnectorFactory::class.java) + locator.addService(TransporterFactory::class.java, FileTransporterFactory::class.java) + locator.addService(TransporterFactory::class.java, HttpTransporterFactory::class.java) + + locator.getService(RepositorySystem::class.java) + } + + private fun session(): RepositorySystemSession { + val session = MavenRepositorySystemUtils.newSession() + + val localRepo = LocalRepository(File(File(System.getProperty("user.home")), ".maven-cache")) + session.localRepositoryManager = repositorySystem.newLocalRepositoryManager(session, localRepo) + + return session + } + + fun DependencyNode.allArtifacts(): Sequence = + (if (this.artifact != null) sequenceOf(this.artifact) else sequenceOf()) + this.children.asSequence().flatMap { it.allArtifacts() } + + fun libraries( + repositories: List, + dependencies: List, + output: PrintStream = System.out + ): List { + val session = session() + val dependencyResults: CollectResult = repositorySystem.collectDependencies( + session, + CollectRequest(dependencies, null, repositories) + ) + + when (dependencyResults.exceptions.size) { + 0 -> { + } + 1 -> throw dependencyResults.exceptions.first() + else -> throw Exception("Several exceptions: ${dependencyResults.exceptions.joinToString("\n") { + it?.message ?: "?" + }}") + } + + return dependencyResults.root.allArtifacts() + .map { + output.println("Obtaining ${it.run { "$groupId:$artifactId:$version" }}") + Library( + name = it.run { "$groupId:$artifactId:$version" }, + default = repositorySystem.resolveArtifact( + session, + ArtifactRequest(it, repositories, null) + ).let { result -> + if (result.isResolved) + result.artifact.file + else + throw IllegalStateException("Could not resolve ${it.run { "$groupId:$artifactId:$version" }}: ${result.exceptions.joinToString { + it.message ?: "" + }}") + }, + javadoc = try { + repositorySystem.resolveArtifact( + session, + ArtifactRequest(it.javadoc(), repositories, null) + ).let { result -> + if (result.isResolved) + result.artifact.file + else + null + } + } catch (e: Exception) { + null + }, + sources = try { + repositorySystem.resolveArtifact( + session, + ArtifactRequest(it.sources(), repositories, null) + ).let { result -> + if (result.isResolved) + result.artifact.file + else + null + } + } catch (e: Exception) { + null + } + ) + } + .toList() + } + + val central = RemoteRepository.Builder("central", "default", "http://repo1.maven.org/maven2/").build() + val jcenter = RemoteRepository.Builder("jcenter", "default", "http://jcenter.bintray.com/").build() + val google = RemoteRepository.Builder("google", "default", "https://dl.google.com/dl/android/maven2/").build() + val local = RemoteRepository.Builder( + "local", + "default", + "file://" + File(File(System.getProperty("user.home")), ".m2").invariantSeparatorsPath + ).build() + val defaultRepositories = listOf(central, jcenter, google, local) + + const val kotlinStandardLibrary = "org.jetbrains.kotlin:kotlin-stdlib:1.3.50" + + fun compile(stringAddress: String) = Dependency(DefaultArtifact(stringAddress), "compile") +} + +fun Artifact.javadoc() = DefaultArtifact(this.groupId, this.artifactId, "javadoc", "jar", this.version) +fun Artifact.sources() = DefaultArtifact(this.groupId, this.artifactId, "sources", "jar", this.version) + +fun main() { + Maven.libraries( + repositories = Maven.defaultRepositories, + dependencies = listOf(Maven.compile("org.jetbrains.kotlin:kotlin-compiler-embeddable:jar:1.3.+")) + ).let { + println("Dependencies: ") + println(it.joinToString("\n")) + } +}