Last active
March 15, 2019 09:15
-
-
Save ohze/5fcfb3c06f73a8f7c372bea6ffa57061 to your computer and use it in GitHub Desktop.
fetch and archive flutter-sources.jar for using in Android Studio / IntelliJ
This file contains 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
// This script will fetch java source files for <flutter.sdk>/bin/cache/artifacts/engine/android-arm/flutter.jar | |
// and pack into ~/flutter-sources.jar | |
// Run this script by [amm](http://ammonite.io/#ScalaScripts): | |
// + install amm: | |
// sudo sh -c '(echo "#!/usr/bin/env sh" && curl -L https://github.com/lihaoyi/Ammonite/releases/download/1.6.4/2.12-1.6.4) > /usr/local/bin/amm && chmod +x /usr/local/bin/amm' | |
// + run: | |
// amm flutter-sources.jar.sc | |
// Then you can Choose Sources for the flutter classes in IntelliJ/ Android Studio | |
// When flutter upgrade, you should re-run: amm flutter-sources.jar.sc | |
import java.io.Closeable | |
import java.net.URL | |
import java.nio.file.{Files, Paths} | |
import java.util.Properties | |
import scala.io.{BufferedSource, Source} | |
def use[A, B <: Closeable](r : B)(f : B => A) : A = try f(r) finally r.close() | |
def useFile[A](path: String)(f: BufferedSource => A): A = use(Source.fromFile(path, "UTF-8"))(f) | |
def parseProps(path: String): Properties = { | |
val props = new Properties() | |
useFile(path) { s => | |
props.load(s.reader()) | |
} | |
props | |
} | |
def getSdkPath(): String = { | |
def findFromProps(dir: String): String = { | |
val p = s"$dir/local.properties" | |
if (Files.notExists(Paths.get(p))) | |
return null | |
parseProps(p).getProperty("flutter.sdk") | |
} | |
var r = System.getenv("FLUTTER_ROOT") | |
if (r != null) return r | |
r = findFromProps(".") | |
if (r != null) return r | |
r = findFromProps("..") | |
if (r != null) return r | |
throw new RuntimeException( | |
"""Can't found flutter sdk path! | |
|You can define FLUTTER_ROOT or run this script from a folder that has local.properties | |
|with `flutter.sdk` property""".stripMargin) | |
} | |
val sdkPathStr = getSdkPath() | |
val engineVersion = useFile(s"$sdkPathStr/bin/internal/engine.version"){_.getLines().next()} | |
def githubUrl(path: String) = | |
s"https://raw.githubusercontent.com/flutter/engine/$engineVersion/shell/platform/android/$path" | |
def getFlutterSourcePaths() = { | |
val text = use(Source.fromURL(githubUrl("BUILD.gn")))(_.mkString) | |
val i = text.indexOf("java_files = [") + "java_files = [".length | |
val j = text.indexOf("]", i) | |
// "io/flutter/app/FlutterActivity.java", | |
// "io/flutter/app/FlutterActivityDelegate.java", | |
// ... | |
text. | |
substring(i, j). | |
split('\n'). | |
map(_. | |
trim(). | |
drop(1). //remove `"` | |
dropRight(2) //remove `",` | |
). | |
filter(_.nonEmpty) | |
} | |
import $ivy.`org.zeroturnaround:zt-zip:1.13` | |
import org.zeroturnaround.zip._ | |
import java.io.File | |
import java.util.zip.ZipEntry | |
class StreamSource(path: String) extends ZipEntrySource { | |
def getEntry = new ZipEntry(path) | |
def getInputStream = { | |
println(s"get $path..") | |
new URL(githubUrl(path)).openStream() | |
} | |
def getPath = path | |
} | |
val javaPaths = getFlutterSourcePaths() | |
val entries: Array[ZipEntrySource] = javaPaths.map(new StreamSource(_)) | |
ZipUtil.pack(entries, new File(System.getProperty("user.home"), "flutter-sources.jar")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment