Skip to content

Commit

Permalink
Initial draft of the osstrich plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
swankjesse committed Oct 25, 2015
1 parent 5da43b3 commit 2ee8373
Show file tree
Hide file tree
Showing 8 changed files with 494 additions and 10 deletions.
30 changes: 21 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
*.class
.classpath
.project
.settings
eclipsebin

# Mobile Tools for Java (J2ME)
.mtj.tmp/
bin
gen
build
out
lib

# Package Files #
*.jar
*.war
*.ear
target
pom.xml.*
release.properties

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
.idea
*.iml
*.ipr
*.iws
classes

obj

.DS_Store
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# ossify
# osstrich
Maven plugin to help open source projects
111 changes: 111 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>

<groupId>com.squareup.osstrich</groupId>
<artifactId>osstrich</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<moshi.version>1.0.0</moshi.version>
<retrofit.version>2.0.0-beta2</retrofit.version>
</properties>

<scm>
<url>https://github.com/square/osstrich/</url>
<connection>scm:git:https://github.com/square/osstrich.git</connection>
<developerConnection>scm:git:git@github.com:square/osstrich.git</developerConnection>
<tag>HEAD</tag>
</scm>

<issueManagement>
<system>GitHub Issues</system>
<url>https://github.com/square/osstrich/issues</url>
</issueManagement>

<licenses>
<license>
<name>Apache 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>

<dependencies>
<dependency>
<groupId>com.squareup.retrofit</groupId>
<artifactId>retrofit</artifactId>
<version>${retrofit.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit</groupId>
<artifactId>converter-moshi</artifactId>
<version>${retrofit.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>${moshi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<configuration>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
<execution>
<id>help-goal</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

37 changes: 37 additions & 0 deletions src/main/java/com/squareup/osstrich/Artifact.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.osstrich;

import com.squareup.moshi.Json;
import java.util.Set;

/** An artifact search result. */
public final class Artifact {
@Json(name = "g") String groupId;
@Json(name = "a") String artifactId;
String latestVersion;
@Json(name = "p") String packaging;
long timestamp;
@Json(name = "ec") Set<String> extensions;

public boolean hasJavadoc() {
return extensions.contains("-javadoc.jar");
}

@Override public String toString() {
return String.format("%s:%s:%s", groupId, artifactId, latestVersion);
}
}
65 changes: 65 additions & 0 deletions src/main/java/com/squareup/osstrich/Cli.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.osstrich;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import okio.Buffer;
import okio.BufferedSource;
import okio.Okio;

/** Makes shelling out to the command line slightly neater. */
public final class Cli {
private final File directory;

public Cli() {
this.directory = null;
}

private Cli(File directory) {
this.directory = directory.getAbsoluteFile();
}

public Cli withCwd(File directory) {
return new Cli(directory);
}

public void exec(String... command) throws IOException {
ProcessBuilder processBuilder = new ProcessBuilder(command);

if (directory != null) {
processBuilder.directory(directory);
}

processBuilder.redirectErrorStream(true);
processBuilder.redirectOutput();
Process process = processBuilder.start();

Buffer buffer = new Buffer();
try (BufferedSource source = Okio.buffer(Okio.source(process.getInputStream()))) {
source.timeout().timeout(30, TimeUnit.SECONDS);
source.timeout().deadline(5, TimeUnit.MINUTES);
source.readAll(buffer);
} catch (IOException e) {
throw new IOException("Failed to execute " + Arrays.toString(command) + ":\n"
+ buffer.readUtf8());
} finally {
process.destroy();
}
}
}
119 changes: 119 additions & 0 deletions src/main/java/com/squareup/osstrich/JavadocPublisher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.osstrich;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import okio.BufferedSource;
import okio.Okio;
import okio.Sink;
import org.apache.maven.plugin.logging.Log;

/** Downloads Javadoc from Maven and uploads it to GitHub pages. */
public final class JavadocPublisher {
final MavenCentral mavenCentral;
final Cli cli;
final Log log;
final File directory;

public JavadocPublisher(MavenCentral mavenCentral, Cli cli, Log log, File directory) {
this.mavenCentral = mavenCentral;
this.cli = cli;
this.log = log;
this.directory = directory;
}

public int publishJavadoc(String repoUrl, String groupId) throws IOException {
initGitDirectory(repoUrl);

StringBuilder commitMessage = new StringBuilder();
commitMessage.append("Publish Javadoc\n"
+ "\n"
+ "Artifacts published:");

int artifactsPublished = 0;

List<Artifact> artifacts = mavenCentral.latestArtifacts(groupId);
log.info(String.format("Maven central returned %s artifacts", artifacts.size()));

for (Artifact artifact : artifacts) {
if (!artifact.hasJavadoc()) {
log.info(String.format("Skipping %s, artifact has no Javadoc", artifact));
continue;
}

File artifactDirectory = new File(
directory + "/" + artifact.latestVersion + "/" + artifact.artifactId);

if (artifactDirectory.exists()) {
log.info(String.format("Skipping %s, directory exists", artifactDirectory));
continue;
}

log.info(String.format("Downloading %s to %s", artifact, artifactDirectory));
downloadJavadoc(artifact, artifactDirectory);
gitAdd(artifactDirectory);

commitMessage.append("\n").append(artifact);
artifactsPublished++;
}

if (artifactsPublished > 0) {
gitCommitAndPush(commitMessage.toString());
}

return artifactsPublished;
}

public void initGitDirectory(String repoUrl) throws IOException {
log.info(String.format("Checking out %s to %s", repoUrl, directory));
cli.exec("rm", "-rf", directory.getAbsolutePath());
cli.exec("git", "clone",
"--single-branch",
"--branch", "gh-pages",
repoUrl,
directory.getAbsolutePath());
}

public void downloadJavadoc(Artifact artifact, File destination) throws IOException {
try (BufferedSource source = mavenCentral.downloadJavadocJar(artifact);
ZipInputStream zipIn = new ZipInputStream(source.inputStream())) {
for (ZipEntry entry; (entry = zipIn.getNextEntry()) != null; ) {
if (entry.isDirectory()) continue;

File file = new File(destination + "/" + entry.getName());
if (!file.getParentFile().mkdirs() && !file.getParentFile().isDirectory()) {
throw new IOException("failed to mkdirs for " + file);
}
try (Sink out = Okio.sink(file)) {
Okio.buffer(Okio.source(zipIn)).readAll(out);
}
}
}
}

private void gitAdd(File file) throws IOException {
cli.withCwd(directory).exec("git", "add", file.getAbsolutePath());
}

private void gitCommitAndPush(String message) throws IOException {
cli.withCwd(directory).exec("git", "commit", "-m", message);
cli.withCwd(directory).exec("git", "push", "origin", "gh-pages");
}
}
Loading

0 comments on commit 2ee8373

Please sign in to comment.