Ref file from GitHub?


#1

I have a little app that references mail.scala, which is on GitHub at https://gist.github.com/mariussoutier/3436111. Starting out, I just downloaded the file and included it in my project. Ideally, I’d rather have my project reference the GitHub URL. Is that possible?

TIA


#2

It’s possible but it comes at a cost. Without adding the file to your repository, you may not be able to compile a local copy. You may not be online, GitHub might not be available, the gist may be removed, the owner may change the file name, etc. In short, it means that your build may not be repeatable so it’s not advisable to do this.

If you understand the risks and still think that it’s worth the cost then you can adapt the following code to match your needs:

val downloadableSources = settingKey[Seq[URI]]("source files to be downloaded")
val downloadableSourceDirectory = settingKey[File]("directory to contain downloaded sources")
downloadableSources := Seq.empty
Compile / downloadableSourceDirectory := (Compile / managedSourceDirectories).value.head

def download(uris: Seq[URI], targetDir: File):  Seq[File] = {
  targetDir.mkdirs()
  uris.map { uri =>
    val fileName = uri.toString.split('/').last
    val targetFile = targetDir.toPath.resolve(fileName)
    io.Using.urlInputStream(uri.toURL) { stream =>
      java.nio.file.Files.write(targetFile, IO.readBytes(stream))
    }.toFile
  }
}

Compile / downloadableSources ++= Seq(
  "https://gist.githubusercontent.com/mariussoutier/3436111/raw/58775ff80f7825426294507ab965b3ea1abeeaaf/Mail.scala",
  "https://gist.githubusercontent.com/mariussoutier/3436111/raw/58775ff80f7825426294507ab965b3ea1abeeaaf/UseMail.scala"
).map(new URI(_))

Compile / managedSources ++=
  download(uris = (Compile/downloadableSources).value,
           targetDir = (Compile/managedSourceDirectories).value.head
  )

#3

Perhaps a better way is to add the gist as a git submodule to your repository. You can read about that here and here.


#4

I was thinking of something like sbt or Gradle that would have a local copy that would just sync with the source repo.

I’ll check out your gist links.

Thanks