Skip to content

Commit 4ddd8e9

Browse files
committed
Vendor apache ant zip
1 parent 538d77d commit 4ddd8e9

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

build.mill

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ object Deps {
2323
val acyclic = ivy"com.lihaoyi:::acyclic:0.3.18"
2424
val jna = ivy"net.java.dev.jna:jna:5.15.0"
2525
val geny = ivy"com.lihaoyi::geny::1.1.1"
26-
val compress = ivy"org.apache.ant:ant:1.10.15"
26+
// val compress = ivy"org.apache.ant:ant:1.10.15"
2727
val sourcecode = ivy"com.lihaoyi::sourcecode::0.4.2"
2828
val utest = ivy"com.lihaoyi::utest::0.8.4"
2929
val expecty = ivy"com.eed3si9n.expecty::expecty::0.16.0"
@@ -113,7 +113,8 @@ trait OsLibModule
113113
}
114114

115115
trait OsModule extends OsLibModule { outer =>
116-
def ivyDeps = Agg(Deps.geny, Deps.compress)
116+
def ivyDeps = Agg(Deps.geny)
117+
// def ivyDeps = Agg(Deps.geny, Deps.compress)
117118
override def compileIvyDeps = T {
118119
val scalaReflectOpt = Option.when(!ZincWorkerUtil.isDottyOrScala3(scalaVersion()))(
119120
Deps.scalaReflect(scalaVersion())
@@ -139,6 +140,38 @@ trait OsModule extends OsLibModule { outer =>
139140

140141
def scalaDocOptions = super.scalaDocOptions() ++ conditionalScalaDocOptions()
141142

143+
def apacheAntZipSources: T[PathRef] = Task(persistent = true) {
144+
if (!_root_.os.exists(Task.dest / "src")) {
145+
_root_.os.remove.all(Task.dest / "unzipped")
146+
_root_.os.unzip.stream(
147+
requests.get.stream("https://repo1.maven.org/maven2/org/apache/ant/ant/1.10.15/ant-1.10.15-sources.jar"),
148+
Task.dest / "unzipped"
149+
)
150+
_root_.os.makeDir.all(Task.dest / "src/os")
151+
152+
val classes = _root_.os.walk.stream(Task.dest / "unzipped" / "org/apache/tools/zip")
153+
.map(_.baseName)
154+
val classRegex = classes.mkString("|")
155+
val prefix = "_Apache"
156+
157+
// Add "_Apache" prefix to all classes
158+
// Move from "package org.apache.tools.zip" to "package os"
159+
// Make all classes package private (private [os])
160+
_root_.os.walk.stream(Task.dest / "unzipped" / "org/apache/tools/zip")
161+
.filter(_.ext == "java")
162+
.foreach { p =>
163+
val content = _root_.os.read(p)
164+
.replaceAll(s"(?<![_a-zA-Z\\.])(${classRegex})(?![_a-zA-Z0-9])", prefix + "$1")
165+
.replaceAll(s"(?<=org.apache.tools.zip.)($classRegex)(?![_a-zA-Z0-9])", prefix + "$1")
166+
.replaceAll("org.apache.tools.zip", "os")
167+
.replaceAll("^public ", "")
168+
_root_.os.write(Task.dest / "src/os" / s"$prefix${p.last}", content)
169+
}
170+
}
171+
172+
PathRef(Task.dest / "src")
173+
}
174+
142175
def generatedSources = T {
143176
val conversions = for (i <- Range.inclusive(2, 22)) yield {
144177
val ts = Range.inclusive(1, i).map(n => s"T$n").mkString(", ")
@@ -162,7 +195,7 @@ trait OsModule extends OsLibModule { outer =>
162195
|""".stripMargin,
163196
createFolders = true
164197
)
165-
Seq(PathRef(T.dest))
198+
Seq(PathRef(T.dest), apacheAntZipSources())
166199
}
167200
}
168201

os/src/ZipOps.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package os
22

3-
import org.apache.tools.{zip => ant}
4-
53
import java.net.URI
64
import java.nio.file.{FileSystem, FileSystems, Files}
75
import java.nio.file.attribute.{BasicFileAttributeView, FileTime, PosixFilePermissions}
@@ -117,7 +115,7 @@ object zip {
117115
compressionLevel: Int,
118116
out: java.io.OutputStream
119117
): Unit = {
120-
val zipOut = new ant.ZipOutputStream(out)
118+
val zipOut = new _ApacheZipOutputStream(out)
121119
zipOut.setLevel(compressionLevel)
122120

123121
try {
@@ -157,9 +155,9 @@ object zip {
157155
file: os.Path,
158156
sub: os.SubPath,
159157
preserveMtimes: Boolean,
160-
zipOut: ant.ZipOutputStream
158+
zipOut: _ApacheZipOutputStream
161159
) = {
162-
val zipEntry = new ant.ZipEntry(sub.toString)
160+
val zipEntry = new _ApacheZipEntry(sub.toString)
163161

164162
val mtime = if (preserveMtimes) os.mtime(file) else 0
165163
zipEntry.setTime(mtime)
@@ -249,7 +247,7 @@ object unzip {
249247

250248
private lazy val S_IFMT: Int = java.lang.Integer.parseInt("0170000", 8)
251249
private def isSymLink(mode: Int): Boolean =
252-
(mode & S_IFMT) == ant.UnixStat.LINK_FLAG
250+
(mode & S_IFMT) == _ApacheUnixStat.LINK_FLAG
253251

254252
/**
255253
* Extract the given zip file into the destination directory
@@ -266,7 +264,7 @@ object unzip {
266264
): os.Path = {
267265
checker.value.onWrite(dest)
268266

269-
val zipFile = new ant.ZipFile(source.toIO)
267+
val zipFile = new _ApacheZipFile(source.toIO)
270268
val zipEntryInputStreams = zipFile.getEntries.asScala
271269
.filter(ze => os.zip.shouldInclude(ze.getName, excludePatterns, includePatterns))
272270
.map(ze => (ze, zipFile.getInputStream(ze)))

0 commit comments

Comments
 (0)