-
-
Notifications
You must be signed in to change notification settings - Fork 399
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
fix #233 add append and exclude rules to assembly #309
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8dc9def
fix #233 add append and exclude rules to assembly
rockjam 31949a6
handle existing files and concatenation when file already exists in a…
rockjam 976f31f
add assembly tests for append rules
rockjam 3bb201f
tests for append patterns
rockjam 59a61d6
tests for exclude patterns
rockjam 335d110
make append algorithm use single map with fold over classpathIterator
rockjam 1ea2206
move assembly rules logic to method
rockjam d1ab971
move grouping method to Assembly object, make assemblyRules Seq[_] ra…
rockjam f1f79e5
add test cases for when there are no rules
rockjam 435ac29
keep default parameter in createAssembly not to break CI
rockjam d731f15
add one more reference.conf entry to tests
rockjam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package mill.modules | ||
|
||
import java.io.InputStream | ||
import java.util.jar.JarFile | ||
import java.util.regex.Pattern | ||
|
||
import ammonite.ops._ | ||
import geny.Generator | ||
import mill.Agg | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
object Assembly { | ||
|
||
val defaultRules: Seq[Rule] = Seq( | ||
Rule.Append("reference.conf"), | ||
Rule.Exclude(JarFile.MANIFEST_NAME), | ||
Rule.ExcludePattern(".*\\.[sS][fF]"), | ||
Rule.ExcludePattern(".*\\.[dD][sS][aA]"), | ||
Rule.ExcludePattern(".*\\.[rR][sS][aA]") | ||
) | ||
|
||
sealed trait Rule extends Product with Serializable | ||
object Rule { | ||
case class Append(path: String) extends Rule | ||
|
||
object AppendPattern { | ||
def apply(pattern: String): AppendPattern = AppendPattern(Pattern.compile(pattern)) | ||
} | ||
case class AppendPattern(pattern: Pattern) extends Rule | ||
|
||
case class Exclude(path: String) extends Rule | ||
|
||
object ExcludePattern { | ||
def apply(pattern: String): ExcludePattern = ExcludePattern(Pattern.compile(pattern)) | ||
} | ||
case class ExcludePattern(pattern: Pattern) extends Rule | ||
} | ||
|
||
def groupAssemblyEntries(inputPaths: Agg[Path], assemblyRules: Seq[Assembly.Rule]): Map[String, GroupedEntry] = { | ||
val rulesMap = assemblyRules.collect { | ||
case r@Rule.Append(path) => path -> r | ||
case r@Rule.Exclude(path) => path -> r | ||
}.toMap | ||
|
||
val appendPatterns = assemblyRules.collect { | ||
case Rule.AppendPattern(pattern) => pattern.asPredicate().test(_) | ||
} | ||
|
||
val excludePatterns = assemblyRules.collect { | ||
case Rule.ExcludePattern(pattern) => pattern.asPredicate().test(_) | ||
} | ||
|
||
classpathIterator(inputPaths).foldLeft(Map.empty[String, GroupedEntry]) { | ||
case (entries, entry) => | ||
val mapping = entry.mapping | ||
|
||
rulesMap.get(mapping) match { | ||
case Some(_: Assembly.Rule.Exclude) => | ||
entries | ||
case Some(_: Assembly.Rule.Append) => | ||
val newEntry = entries.getOrElse(mapping, AppendEntry.empty).append(entry) | ||
entries + (mapping -> newEntry) | ||
|
||
case _ if excludePatterns.exists(_(mapping)) => | ||
entries | ||
case _ if appendPatterns.exists(_(mapping)) => | ||
val newEntry = entries.getOrElse(mapping, AppendEntry.empty).append(entry) | ||
entries + (mapping -> newEntry) | ||
|
||
case _ if !entries.contains(mapping) => | ||
entries + (mapping -> WriteOnceEntry(entry)) | ||
case _ => | ||
entries | ||
} | ||
} | ||
} | ||
|
||
private def classpathIterator(inputPaths: Agg[Path]): Generator[AssemblyEntry] = { | ||
Generator.from(inputPaths) | ||
.filter(exists) | ||
.flatMap { | ||
p => | ||
if (p.isFile) { | ||
val jf = new JarFile(p.toIO) | ||
Generator.from( | ||
for(entry <- jf.entries().asScala if !entry.isDirectory) | ||
yield JarFileEntry(entry.getName, () => jf.getInputStream(entry)) | ||
) | ||
} | ||
else { | ||
ls.rec.iter(p) | ||
.filter(_.isFile) | ||
.map(sub => PathEntry(sub.relativeTo(p).toString, sub)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private[modules] sealed trait GroupedEntry { | ||
def append(entry: AssemblyEntry): GroupedEntry | ||
} | ||
|
||
private[modules] object AppendEntry { | ||
val empty: AppendEntry = AppendEntry(Nil) | ||
} | ||
|
||
private[modules] case class AppendEntry(entries: List[AssemblyEntry]) extends GroupedEntry { | ||
def append(entry: AssemblyEntry): GroupedEntry = copy(entries = entry :: this.entries) | ||
} | ||
|
||
private[modules] case class WriteOnceEntry(entry: AssemblyEntry) extends GroupedEntry { | ||
def append(entry: AssemblyEntry): GroupedEntry = this | ||
} | ||
|
||
private[this] sealed trait AssemblyEntry { | ||
def mapping: String | ||
def inputStream: InputStream | ||
} | ||
|
||
private[this] case class PathEntry(mapping: String, path: Path) extends AssemblyEntry { | ||
def inputStream: InputStream = read.getInputStream(path) | ||
} | ||
|
||
private[this] case class JarFileEntry(mapping: String, getIs: () => InputStream) extends AssemblyEntry { | ||
def inputStream: InputStream = getIs() | ||
} |
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 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
4 changes: 4 additions & 0 deletions
4
scalalib/test/resources/hello-world-multi/core/resources/reference.conf
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
############################## | ||
# Core Reference Config File # | ||
############################## | ||
bar.baz=hello |
5 changes: 5 additions & 0 deletions
5
scalalib/test/resources/hello-world-multi/core/src/Main.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
object Main extends App { | ||
val person = Person.fromString("rockjam:25") | ||
println(s"hello ${person.name}, your age is: ${person.age}") | ||
} | ||
|
4 changes: 4 additions & 0 deletions
4
scalalib/test/resources/hello-world-multi/model/resources/reference.conf
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
############################### | ||
# Model Reference Config File # | ||
############################### | ||
foo.bar=2 |
8 changes: 8 additions & 0 deletions
8
scalalib/test/resources/hello-world-multi/model/src/Person.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
object Person { | ||
def fromString(s: String): Person = { | ||
val Array(name, age) = s.split(":") | ||
Person(name, age.toInt) | ||
} | ||
} | ||
case class Person(name: String, age: Int) | ||
|
4 changes: 4 additions & 0 deletions
4
scalalib/test/resources/hello-world/core/resources/reference.conf
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
######################################## | ||
# My application Reference Config File # | ||
######################################## | ||
akka.http.client.user-agent-header="hello-world-client" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we maintain this behavior in the new default
assemblyRules
? IIRC we added this because these files were causing problems for someone (there's an issue)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I added it in default rules actually, right here: https://github.com/lihaoyi/mill/pull/309/files#diff-8987430aa3e4d7f9373fa4b3c3d5f2d0R13