diff options
author | Christopher Vogt <oss.nsp@cvogt.org> | 2017-03-17 03:39:26 -0400 |
---|---|---|
committer | Christopher Vogt <oss.nsp@cvogt.org> | 2017-03-17 03:41:37 -0400 |
commit | 94500b34aef7744c7063bccfbeb4f0e2ebd76a26 (patch) | |
tree | 3022d0274105c6764c2022f0d4460278ae1ffa4b /stage2 | |
parent | 57e3ead5801ef3a88a3b40830b93657217b5a955 (diff) | |
download | cbt-94500b34aef7744c7063bccfbeb4f0e2ebd76a26.tar.gz cbt-94500b34aef7744c7063bccfbeb4f0e2ebd76a26.tar.bz2 cbt-94500b34aef7744c7063bccfbeb4f0e2ebd76a26.zip |
change and expose mechanism mapping from absolute to relative paths
to transfer files from different locations coherently into one location
Diffstat (limited to 'stage2')
-rw-r--r-- | stage2/Lib.scala | 54 | ||||
-rw-r--r-- | stage2/PackageJars.scala | 2 |
2 files changed, 33 insertions, 23 deletions
diff --git a/stage2/Lib.scala b/stage2/Lib.scala index d5a964a..1c34c9c 100644 --- a/stage2/Lib.scala +++ b/stage2/Lib.scala @@ -283,7 +283,35 @@ final class Lib(val logger: Logger) extends Stage1Lib(logger){ m } - def createJar( jarFile: File, files: Seq[File], baseDirectory: Option[File] = None, mainClass: Option[String] = None ): Option[File] = { + def autoRelative( files: Seq[File], collector: PartialFunction[(File,String), String] = { case (_,r) => r }): Seq[(File, String)] = { + val map = files.sorted.flatMap{ base => + val b = base.getCanonicalFile.string + if( base.isDirectory ){ + base.listRecursive.map{ f => + f -> f.getCanonicalFile.string.stripPrefix(b).stripPrefix(File.separator) + } + } else { + Seq( base -> base.getName ) + } + }.collect{ + case v@(file, _) if collector.isDefinedAt(v) => file -> collector(v) + } + val relatives = map.unzip._2 + val duplicateFiles = (relatives diff relatives.distinct).distinct + assert( + duplicateFiles.isEmpty, + { + val rs = relatives.toSet + "Conflicting:\n\n" + + map.filter(rs contains _._2).groupBy(_._2).mapValues(_.map(_._1).sorted).toSeq.sortBy(_._1).map{ + case (name, files) => s"$name:\n" ++ files.mkString("\n") + }.mkString("\n\n") + } + ) + map + } + + def createJar( jarFile: File, files: Seq[File], mainClass: Option[String] = None ): Option[File] = { deleteIfExists(jarFile.toPath) if( files.isEmpty ){ None @@ -293,32 +321,14 @@ final class Lib(val logger: Logger) extends Stage1Lib(logger){ val jar = new JarOutputStream(new FileOutputStream(jarFile), createManifest(mainClass)) try{ assert( files.forall(_.exists) ) - val names = for { - base <- files.map(realpath) - file <- base.listRecursive if file.isFile - } yield { - 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 ) + autoRelative(files).collect{ + case (file, relative) if file.isFile => + val entry = new JarEntry( relative ) entry.setTime(file.lastModified) jar.putNextEntry(entry) jar.write( readAllBytes( file.toPath ) ) jar.closeEntry - name } - - val duplicateFiles = (names diff names.distinct).distinct - assert( - duplicateFiles.isEmpty, - s"Conflicting file names when trying to create $jarFile: "++duplicateFiles.mkString(", ") - ) } finally { jar.close } diff --git a/stage2/PackageJars.scala b/stage2/PackageJars.scala index 910741d..221920b 100644 --- a/stage2/PackageJars.scala +++ b/stage2/PackageJars.scala @@ -18,7 +18,7 @@ trait PackageJars extends BaseBuild with ArtifactInfo{ def srcJar: Option[File] = taskCache[PackageJars]("srcJar").memoize{ lib.createJar( - jarTarget / jarFilePrefix++"-sources.jar", nonEmptySourceFiles, Some(projectDirectory) + jarTarget / jarFilePrefix++"-sources.jar", nonEmptySourceFiles ) } |