diff options
author | Jan Christopher Vogt <oss.nsp@cvogt.org> | 2017-02-28 09:45:02 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-28 09:45:02 -0500 |
commit | 4cb8eb87c1b5979e614a8355215d1257d2867261 (patch) | |
tree | 15073f04de2e88dab1b1381a6259f9e047990cf4 | |
parent | 9f3c5f3a36ea70a8dec03b048cc41b6579eb6c4b (diff) | |
parent | 734d2f801a47f272d79b99531815e19704146b37 (diff) | |
download | cbt-4cb8eb87c1b5979e614a8355215d1257d2867261.tar.gz cbt-4cb8eb87c1b5979e614a8355215d1257d2867261.tar.bz2 cbt-4cb8eb87c1b5979e614a8355215d1257d2867261.zip |
Merge pull request #359 from cvogt/jar-refactor
Jar refactor
-rw-r--r-- | plugins/uber-jar/src/UberJar.scala | 2 | ||||
-rw-r--r-- | stage2/BasicBuild.scala | 8 | ||||
-rw-r--r-- | stage2/Lib.scala | 119 | ||||
-rw-r--r-- | stage2/PackageJars.scala | 16 | ||||
-rw-r--r-- | stage2/plugins/Dotty.scala | 2 | ||||
-rw-r--r-- | test/test.scala | 2 |
6 files changed, 72 insertions, 77 deletions
diff --git a/plugins/uber-jar/src/UberJar.scala b/plugins/uber-jar/src/UberJar.scala index 79b87a1..b1c613c 100644 --- a/plugins/uber-jar/src/UberJar.scala +++ b/plugins/uber-jar/src/UberJar.scala @@ -54,7 +54,7 @@ class UberJarLib(logger: Logger) { log("Writing jar file...") val uberJarPath = target.toPath.resolve(jarName) - val uberJar = lib.jarFile(uberJarPath.toFile, dirs :+ extractedJarsRoot, mainClass) getOrElse { + val uberJar = lib.createJar(uberJarPath.toFile, dirs :+ extractedJarsRoot, mainClass=mainClass) getOrElse { throw new Exception("Jar file wasn't created!") } log("Writing jar file - DONE") diff --git a/stage2/BasicBuild.scala b/stage2/BasicBuild.scala index 3c3cbec..36bb86f 100644 --- a/stage2/BasicBuild.scala +++ b/stage2/BasicBuild.scala @@ -153,6 +153,14 @@ trait BaseBuild extends BuildInterface with DependencyImplementation with Trigge ) } + def scaladoc = taskCache[BaseBuild]("scaladoc").memoize{ + lib.scaladoc( + context.cbtLastModified, + scalaVersion, sourceFiles, compileClasspath, docTarget, + scalacOptions, context.paths.mavenCache + ) + } + def cleanFiles: Seq[File] = Seq( target ) def clean: ExitCode = { diff --git a/stage2/Lib.scala b/stage2/Lib.scala index 33bfe1a..3f6a92e 100644 --- a/stage2/Lib.scala +++ b/stage2/Lib.scala @@ -56,44 +56,23 @@ final class Lib(val logger: Logger) extends Stage1Lib(logger){ } } - def srcJar( - sourceFiles: Seq[File], artifactId: String, scalaMajorVersion: String, version: String, jarTarget: File, - filter: File => Boolean, stripBase: File - ): Option[File] = { - lib.jarFile( - jarTarget ++ ("/"++artifactId++"_"++scalaMajorVersion++"-"++version++"-sources.jar"), - sourceFiles, filter=filter, stripBase=Some( stripBase ) - ) - } - - def jar(artifactId: String, scalaMajorVersion: String, version: String, files: Seq[File], jarTarget: File): Option[File] = { - lib.jarFile( - jarTarget ++ ("/"++artifactId++"_"++scalaMajorVersion++"-"++version++".jar"), - files - ) - } - - def docJar( + def scaladoc( cbtLastModified: Long, scalaVersion: String, sourceFiles: Seq[File], dependencyClasspath: ClassPath, - docTarget: File, - jarTarget: File, - artifactId: String, - scalaMajorVersion: String, - version: String, + scaladocTarget: File, compileArgs: Seq[String], mavenCache: File )(implicit transientCache: java.util.Map[AnyRef,AnyRef], classLoaderCache: ClassLoaderCache): Option[File] = { if(sourceFiles.isEmpty){ None } else { - docTarget.mkdirs + scaladocTarget.mkdirs val args = Seq( // FIXME: can we use compiler dependency here? "-cp", dependencyClasspath.string, // FIXME: does this break for builds that don't have scalac dependencies? - "-d", docTarget.toString + "-d", scaladocTarget.toString ) ++ compileArgs ++ sourceFiles.map(_.toString) logger.lib("creating docs for source files "+args.mkString(", ")) redirectOutToErr{ @@ -103,10 +82,7 @@ final class Lib(val logger: Logger) extends Stage1Lib(logger){ new ScalaDependencies(cbtLastModified,mavenCache,scalaVersion).classLoader ) } - lib.jarFile( - jarTarget ++ ("/"++artifactId++"_"++scalaMajorVersion++"-"++version++"-javadoc.jar"), - Vector(docTarget) - ) + Some(scaladocTarget) } } @@ -295,39 +271,44 @@ final class Lib(val logger: Logger) extends Stage1Lib(logger){ } yield file } - // FIXME: for some reason it includes full path in docs - def jarFile( - jarFile: File, files: Seq[File], mainClass: Option[String] = None, - filter: File => Boolean = _ => true, stripBase: Option[File] = None - ): Option[File] = { - val stripBaseCanonical = stripBase - Files.deleteIfExists(jarFile.toPath) + private def createManifest( mainClass: Option[String] ) = { + val m = new Manifest() + m.getMainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0") + m.getMainAttributes.putValue( "Created-By", "Scala CBT" ) + mainClass.foreach( + m.getMainAttributes.put(Attributes.Name.MAIN_CLASS, _) + ) + m + } + + def createJar( jarFile: File, files: Seq[File], baseDirectory: Option[File] = None, mainClass: Option[String] = None ): Option[File] = { + deleteIfExists(jarFile.toPath) if( files.isEmpty ){ None } else { jarFile.getParentFile.mkdirs logger.lib("Start packaging "++jarFile.string) - val manifest = new Manifest() - manifest.getMainAttributes.put( Attributes.Name.MANIFEST_VERSION, "1.0" ) - manifest.getMainAttributes.putValue( "Created-By", "Chris' Build Tool" ) - mainClass foreach { className => - manifest.getMainAttributes.put(Attributes.Name.MAIN_CLASS, className) - } - val jar = new JarOutputStream(new FileOutputStream(jarFile), manifest) + val jar = new JarOutputStream(new FileOutputStream(jarFile), createManifest(mainClass)) try{ + assert( files.forall(_.exists) ) val names = for { - base <- files.filter(_.exists).map(realpath) + base <- files.map(realpath) file <- base.listRecursive if file.isFile } yield { - val strip = Some( base ).filter(_.isDirectory) ++ stripBaseCanonical - val name = strip.foldLeft( file.getCanonicalPath )( - (f, prefix) => f.stripPrefix( prefix.getCanonicalPath ++ File.separator ) - ) + val name = { + Some(base).filter(_.isDirectory) orElse baseDirectory map (_.string) map ( + file.string stripPrefix _ stripPrefix File.separator + ) getOrElse ( + throw new Exception( + "Trying to add absolute path to jar: " + file + ) + ) + } val entry = new JarEntry( name ) entry.setTime(file.lastModified) jar.putNextEntry(entry) jar.write( readAllBytes( file.toPath ) ) - jar.closeEntry() + jar.closeEntry name } @@ -337,7 +318,7 @@ final class Lib(val logger: Logger) extends Stage1Lib(logger){ s"Conflicting file names when trying to create $jarFile: "++duplicateFiles.mkString(", ") ) } finally { - jar.close() + jar.close } logger.lib("Done packaging " ++ jarFile.toString) @@ -459,9 +440,11 @@ final class Lib(val logger: Logger) extends Stage1Lib(logger){ } } - def publishSigned( artifacts: Seq[File], url: URL, credentials: Option[String] = None ): Unit = { + def publishSigned( sourceFiles: Seq[File], artifacts: Seq[File], url: URL, credentials: Option[String] = None ): Unit = { // TODO: make concurrency configurable here - publish( artifacts ++ artifacts.map(sign), url, credentials ) + if(sourceFiles.nonEmpty){ + publish( artifacts ++ artifacts.map(sign), url, credentials ) + } } private def publish(artifacts: Seq[File], url: URL, credentials: Option[String]): Unit = { @@ -477,7 +460,7 @@ final class Lib(val logger: Logger) extends Stage1Lib(logger){ } def uploadAll(url: URL, nameAndContents: Seq[(String, Array[Byte])], credentials: Option[String] = None ): Unit = - nameAndContents.foreach { case (name, content) => upload(name, content, url, credentials ) } + nameAndContents.map{ case(name, content) => upload(name, content, url, credentials ) } def upload(fileName: String, fileContents: Array[Byte], baseUrl: URL, credentials: Option[String] = None): Unit = { import java.net._ @@ -485,19 +468,23 @@ final class Lib(val logger: Logger) extends Stage1Lib(logger){ val url = baseUrl ++ "/" ++ fileName System.err.println(blue("uploading ") ++ url.toString) val httpCon = Stage0Lib.openConnectionConsideringProxy(url) - httpCon.setDoOutput(true) - httpCon.setRequestMethod("PUT") - credentials.foreach( - c => { - val encoding = new sun.misc.BASE64Encoder().encode(c.getBytes) - httpCon.setRequestProperty("Authorization", "Basic " ++ encoding) - } - ) - httpCon.setRequestProperty("Content-Type", "application/binary") - httpCon.getOutputStream.write( - fileContents - ) - httpCon.getInputStream + try{ + httpCon.setDoOutput(true) + httpCon.setRequestMethod("PUT") + credentials.foreach( + c => { + val encoding = new sun.misc.BASE64Encoder().encode(c.getBytes) + httpCon.setRequestProperty("Authorization", "Basic " ++ encoding) + } + ) + httpCon.setRequestProperty("Content-Type", "application/binary") + httpCon.getOutputStream.write( + fileContents + ) + httpCon.getInputStream + } finally { + httpCon.disconnect + } } diff --git a/stage2/PackageJars.scala b/stage2/PackageJars.scala index 0e4a444..7b6e844 100644 --- a/stage2/PackageJars.scala +++ b/stage2/PackageJars.scala @@ -10,20 +10,20 @@ trait PackageJars extends BaseBuild with ArtifactInfo{ Seq(() => jar, () => docJar, () => srcJar) )( _() ).flatten + + def jarFilePrefix = artifactId++"_"++scalaMajorVersion++"-"++version + def jar: Option[File] = taskCache[PackageJars]("jar").memoize{ - lib.jar( artifactId, scalaMajorVersion, version, exportedClasspath.files, jarTarget ) + lib.createJar( jarTarget / jarFilePrefix++".jar", exportedClasspath.files ) } def srcJar: Option[File] = taskCache[PackageJars]("srcJar").memoize{ - lib.srcJar( sources, artifactId, scalaMajorVersion, version, scalaTarget, sourceFileFilter, projectDirectory ) + lib.createJar( + jarTarget / jarFilePrefix++"-sources.jar", sourceFiles, Some(projectDirectory) + ) } def docJar: Option[File] = taskCache[PackageJars]("docJar").memoize{ - lib.docJar( - context.cbtLastModified, - scalaVersion, sourceFiles, compileClasspath, docTarget, - jarTarget, artifactId, scalaMajorVersion, version, - scalacOptions, context.paths.mavenCache - ) + lib.createJar( jarTarget / jarFilePrefix++"-javadoc.jar", scaladoc.toSeq ) } } diff --git a/stage2/plugins/Dotty.scala b/stage2/plugins/Dotty.scala index c96dbcb..8ffcdb6 100644 --- a/stage2/plugins/Dotty.scala +++ b/stage2/plugins/Dotty.scala @@ -50,7 +50,7 @@ trait Dotty extends BaseBuild{ Some(min) } - def doc: ExitCode = + def dottydoc: ExitCode = dottyLib.doc( sourceFiles, compileClasspath, docTarget, dottyOptions ) diff --git a/test/test.scala b/test/test.scala index 3305926..f374217 100644 --- a/test/test.scala +++ b/test/test.scala @@ -214,7 +214,7 @@ object Main{ } else { compile("../examples/dotty-example") task("run","../examples/dotty-example") - task("doc","../examples/dotty-example") + task("dottydoc","../examples/dotty-example") } task("compile","../examples/scalajs-react-example/js") task("fullOpt.compile","../examples/scalajs-react-example/js") |