Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local JAR Support #20

Merged
merged 5 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ build/
.classpath
.settings/
bin/

# IntelliJ
.idea/
out/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.1.2] - 2021-05-26
chris-serra marked this conversation as resolved.
Show resolved Hide resolved
### Added
- Support for local JAR file ([#20](https://github.com/diffplug/blowdryer/pull/20))

## [1.1.1] - 2021-02-12
### Fixed
- Occasionally a file would be deleted from temp storage while a long-lived gradle daemon kept it in cache ([#11](https://github.com/diffplug/blowdryer/pull/18)).
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ output = [
[![Maven central](https://img.shields.io/badge/mavencentral-available-blue.svg)](https://search.maven.org/classic/#search%7Cgav%7C1%7Cg%3A%22com.diffplug%22%20AND%20a%3A%22blowdryer%22)
[![License Apache 2.0](https://img.shields.io/badge/license-apache--2.0-blue.svg)](https://tldrlegal.com/license/apache-license-2.0-(apache-2.0))

[![Changelog](https://img.shields.io/badge/changelog-1.1.1-brightgreen.svg)](CHANGELOG.md)
[![Javadoc](https://img.shields.io/badge/javadoc-yes-brightgreen.svg)](https://javadoc.io/doc/com.diffplug/blowdryer/1.1.1/index.html)
[![Changelog](https://img.shields.io/badge/changelog-1.1.2-brightgreen.svg)](CHANGELOG.md)
[![Javadoc](https://img.shields.io/badge/javadoc-yes-brightgreen.svg)](https://javadoc.io/doc/com.diffplug/blowdryer/1.1.2/index.html)
[![Live chat](https://img.shields.io/badge/gitter-chat-brightgreen.svg)](https://gitter.im/diffplug/blowdryer)
[![JitCI](https://jitci.com/gh/diffplug/blowdryer/svg)](https://jitci.com/gh/diffplug/blowdryer)
<!---freshmark /shields -->
Expand Down Expand Up @@ -47,7 +47,7 @@ Then, in the `settings.gradle` for the project that you want to suck these into,

```gradle
plugins {
id 'com.diffplug.blowdryerSetup' version '1.1.1'
id 'com.diffplug.blowdryerSetup' version '1.1.2'
}

blowdryerSetup {
Expand Down Expand Up @@ -156,8 +156,8 @@ static String 干.proj(Project proj, String String key, String description)
static <T> T 干.proj(Project proj, Class<T> clazz, String String key, String description)
```

- [javadoc `BlowdryerSetup`](https://javadoc.io/static/com.diffplug/blowdryer/1.1.1/com/diffplug/blowdryer/BlowdryerSetup.html)
- [javadoc `Blowdryer`](https://javadoc.io/static/com.diffplug/blowdryer/1.1.1/com/diffplug/blowdryer/Blowdryer.html)
- [javadoc `BlowdryerSetup`](https://javadoc.io/static/com.diffplug/blowdryer/1.1.2/com/diffplug/blowdryer/BlowdryerSetup.html)
- [javadoc `Blowdryer`](https://javadoc.io/static/com.diffplug/blowdryer/1.1.2/com/diffplug/blowdryer/Blowdryer.html)

If you do `apply plugin: 'com.diffplug.blowdryer'` then every project gets an extension object ([code](https://github.com/diffplug/blowdryer/blob/master/src/main/java/com/diffplug/blowdryer/BlowdryerPlugin.java)) where the project field has been filled in for you, which is why we don't pass it explicitly in the examples before this section. If you don't apply the plugin, you can still call these static methods and pass `project` explicitly for the `proj()` methods.

Expand All @@ -168,7 +168,7 @@ The Gradle Kotlin DSL doesn't play well with the name-based extension object tha
```kotlin
// settings.gradle.kts
plugins {
id("com.diffplug.blowdryerSetup") version "1.1.1"
id("com.diffplug.blowdryerSetup") version "1.1.2"
}
import com.diffplug.blowdryer.BlowdryerSetup
import com.diffplug.blowdryer.BlowdryerSetup.GitAnchorType
Expand All @@ -191,9 +191,9 @@ somePlugin {

### Setup with something besides GitHub

[`Blowdryer.immutableUrl`](https://javadoc.io/static/com.diffplug/blowdryer/1.1.1/com/diffplug/blowdryer/Blowdryer.html#immutableUrl-java.lang.String-) returns a `File` containing the downloaded content of the given URL. It's on you to guarantee that the content of that URL is immutable.
[`Blowdryer.immutableUrl`](https://javadoc.io/static/com.diffplug/blowdryer/1.1.2/com/diffplug/blowdryer/Blowdryer.html#immutableUrl-java.lang.String-) returns a `File` containing the downloaded content of the given URL. It's on you to guarantee that the content of that URL is immutable.

When you setup the Blowdryer plugin in your `settings.gradle`, you're telling Blowdryer what URL scheme to use when resolving a call to [`Blowdryer.file`](https://javadoc.io/static/com.diffplug/blowdryer/1.1.1/com/diffplug/blowdryer/Blowdryer.html#file-java.lang.String-), for example:
When you setup the Blowdryer plugin in your `settings.gradle`, you're telling Blowdryer what URL scheme to use when resolving a call to [`Blowdryer.file`](https://javadoc.io/static/com.diffplug/blowdryer/1.1.2/com/diffplug/blowdryer/Blowdryer.html#file-java.lang.String-), for example:

```java
//blowdryer {
Expand Down Expand Up @@ -238,7 +238,7 @@ repositories {
mavenCentral()
}
dependencies {
implementation 'com.diffplug:blowdryer:1.1.1'
implementation 'com.diffplug:blowdryer:1.1.2'
}

// settings.gradle
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id 'com.diffplug.spotless-changelog'
id 'com.gradle.plugin-publish'
id 'com.jfrog.bintray'
id "com.palantir.idea-test-fix" version "0.1.0" // Added to run tests successfully in IntelliJ
}

apply from: 干.file('base/changelog.gradle')
Expand Down
39 changes: 36 additions & 3 deletions src/main/java/com/diffplug/blowdryer/Blowdryer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
import com.diffplug.common.hash.Hashing;
import com.diffplug.common.io.Files;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Base64;
Expand All @@ -34,6 +37,8 @@
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
Expand Down Expand Up @@ -111,7 +116,7 @@ public static File immutableUrl(String url) {
urlToContent.put(url, dataFile);
return dataFile;
}
} catch (IOException e) {
} catch (IOException | URISyntaxException e) {
throw Errors.asRuntime(e);
}
}
Expand All @@ -131,7 +136,35 @@ private static Map<String, String> loadPropertyFile(File file) throws IOExceptio

private static final String PROP_URL = "url";

private static void download(String url, File dst) throws IOException {
private static void download(String url, File dst) throws IOException, URISyntaxException {
if (url != null && url.startsWith("file:///")) {
chris-serra marked this conversation as resolved.
Show resolved Hide resolved
downloadLocal(url, dst);
} else {
downloadRemote(url, dst);
}
}

private static void downloadLocal(String url, File dst) throws IOException, URISyntaxException {

String[] splitUrl = url.split("!/");
if (splitUrl.length != 2) {
throw new IllegalArgumentException("Expected a file URL in the format: file:///path-to-dependency.jar!/path-to-file.ext");
}

String jarPath = splitUrl[0];
String filename = splitUrl[1];

URI jarPathUri = new URI(jarPath);
try (ZipFile jar = new ZipFile(new File(jarPathUri))) {
ZipEntry foundEntry = jar.stream()
.filter(s -> s.getName().equals(filename)).findAny()
.orElseThrow(() -> new FileNotFoundException("Could not find '" + filename + "' in '" + jarPath + "'"));

java.nio.file.Files.copy(jar.getInputStream(foundEntry), dst.toPath());
}
}

private static void downloadRemote(String url, File dst) throws IOException {
OkHttpClient client = new OkHttpClient.Builder().build();
Request.Builder req = new Request.Builder().url(url);
authPlugin.addAuthToken(url, req);
Expand All @@ -154,7 +187,7 @@ private static void download(String url, File dst) throws IOException {

/** Returns either the filename safe URL, or (first40)--(Base64 filenamesafe)(last40). */
static String filenameSafe(String url) {
String allSafeCharacters = url.replaceAll("[^a-zA-Z0-9-+_\\.]", "-");
String allSafeCharacters = url.replaceAll("[^a-zA-Z0-9-+_.]", "-");
nedtwigg marked this conversation as resolved.
Show resolved Hide resolved
String noDuplicateDash = allSafeCharacters.replaceAll("-+", "-");
if (noDuplicateDash.length() <= MAX_FILE_LENGTH) {
return noDuplicateDash;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/diffplug/blowdryer/BlowdryerSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ private GitLab setGlobals() {
}
}

/**
* Uses the provided {@code jarFilePath} to extract a file resource.
* @param jarFilePath Absolute path to JAR on the file system.
*/
public void localJar(String jarFilePath) {
chris-serra marked this conversation as resolved.
Show resolved Hide resolved
Blowdryer.setResourcePluginNull();
File localJar = new File(jarFilePath);//findLocalJar(dependency);
String rootUrl = "file:///" + localJar.getAbsolutePath() + "!/";
Blowdryer.setResourcePlugin(resource -> rootUrl + resource);
}

@NotNull
private String getFullResourcePath(String resource) {
return (repoSubfolder.isEmpty() ? "" : repoSubfolder + "/") + resource;
Expand Down
Loading