Last active
November 29, 2017 13:41
-
-
Save RomanTsarou/5f11ca652bc982ba6a29 to your computer and use it in GitHub Desktop.
Use bitbucket as artifact repo for your gradle dependencies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
//add to build.gradle file in your app | |
apply from: 'https://gist.githubusercontent.com/RomanTsarou/5f11ca652bc982ba6a29/raw/e4a6a0e66797d4e00b3640d65cae453c35165136/bitbucket.gradle' | |
//add in dependencies block | |
dependencies { | |
//dont forgot log in | |
bitbucket.init(project,'your_bitbucket_login:your_bitbucket_password') | |
//example for .aar | |
bitbucket.compileFrom 'roooman30/mylog/raw/default/app/build/outputs/aar/app-debug.aar', 'MyLog.aar' | |
//example for .jar | |
bitbucket.compileFrom 'roooman30/generallibs/raw/default/commons-io-2.4.jar', 'commons-io-2.4.jar' | |
} | |
*/ | |
public class BitBucket { | |
private static final String FORMAT_URL = "https://bitbucket.org/api/1.0/repositories/%s"; | |
private Project project; | |
private Map<String, String> etags; | |
private File libsDir, etagsFile; | |
private String basicAuth; | |
final String TAG = "bitbucket: " | |
/** | |
* @param project | |
* @param auth ex: 'login:password' | |
*/ | |
void init(Project project, String auth) { | |
this.project = project; | |
if (auth != null) { | |
basicAuth = auth.bytes.encodeBase64().toString(); | |
} | |
libsDir = new File(project.getProjectDir(), "build/libs/"); | |
if (!libsDir.exists()) { | |
libsDir.mkdirs(); | |
} | |
etagsFile = new File(libsDir, "etags.txt"); | |
etags = readEtags(); | |
project.repositories { | |
flatDir { | |
dirs "build/libs" | |
} | |
} | |
} | |
/** | |
* @param value ex: 'roooman30/mylog/raw/default/app/build/outputs/aar/app-debug.aar' | |
*/ | |
void compileFrom(String path, lacalName) { | |
String urlPath = String.format(FORMAT_URL, path); | |
downloadAndCompile(urlPath, lacalName); | |
} | |
@Deprecated | |
void downloadAndCompile(String urlPath, String fileName) { | |
if (project == null) throw new IllegalStateException(TAG + "Must call before: bitbucket.init(project,'login:password')"); | |
HttpURLConnection connection = new URL(urlPath).openConnection(); | |
if (basicAuth != null) { | |
connection.setRequestProperty("Authorization", "Basic " + basicAuth); | |
} | |
File destFile = new File(libsDir, fileName); | |
String name = fileName.substring(0, fileName.lastIndexOf(".")); | |
String extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()); | |
if (!isNeedToDownload(connection, destFile, etags)) { | |
connection.disconnect(); | |
} else if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) { | |
downloadFile(connection, destFile); | |
etags.put(destFile.getAbsolutePath(), connection.getHeaderField("ETag")); | |
rewriteEtags(); | |
} else { | |
println TAG + "Unsuccess download " + fileName + ". Response code is " + connection.getResponseCode(); | |
return; | |
} | |
project.dependencies { | |
compile(name: name, ext: extension); | |
} | |
} | |
private boolean isNeedToDownload(HttpURLConnection connection, File destinationFile, Map<String, String> etags) { | |
boolean isNeedDownload = !destinationFile.exists() || !connection.getHeaderField("ETag").equals(etags.get(destinationFile.getAbsolutePath())); | |
println TAG + "isNeedDownload " + destinationFile.getName() + " = " + isNeedDownload | |
return isNeedDownload; | |
} | |
private void rewriteEtags() { | |
FileWriter fw = new FileWriter(etagsFile); | |
for (Map.Entry<String, String> e : etags.entrySet()) { | |
fw.write(e.key); | |
fw.write("="); | |
fw.write(e.value); | |
fw.write("\n"); | |
} | |
fw.close(); | |
} | |
private Map<String, String> readEtags() { | |
Map<String, String> etags = new HashMap<>(); | |
if (etagsFile.exists()) { | |
FileReader fr = new FileReader(etagsFile); | |
String line; | |
while ((line = fr.readLine()) != null) { | |
String[] values = line.split("="); | |
etags.put(values[0], values[1]); | |
} | |
fr.close(); | |
} | |
return etags; | |
} | |
private void downloadFile(HttpURLConnection connection, File destinationFile) { | |
BufferedInputStream bis = null; | |
FileOutputStream fos = null; | |
try { | |
bis = new BufferedInputStream(connection.getInputStream()); | |
fos = new FileOutputStream(destinationFile); | |
final byte[] data = new byte[1024]; | |
int count; | |
while ((count = bis.read(data, 0, 1024)) != -1) { | |
fos.write(data, 0, count); | |
} | |
fos.flush(); | |
println TAG + connection.getURL() + " downloaded to " + destinationFile.absolutePath; | |
} finally { | |
if (bis != null) { | |
bis.close(); | |
} | |
if (fos != null) { | |
fos.close(); | |
} | |
} | |
} | |
} | |
ext { | |
bitbucket = new BitBucket() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment