-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlob.scala
More file actions
919 lines (869 loc) · 36.7 KB
/
Glob.scala
File metadata and controls
919 lines (869 loc) · 36.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
/*
* sbt IO
* Copyright Scala Center, Lightbend, and Mark Harrah
*
* Licensed under Apache License 2.0
* SPDX-License-Identifier: Apache-2.0
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package sbt.nio.file
import java.io.File
import java.nio.file.{ FileSystems, Path, Paths }
import java.util
import sbt.io._
import sbt.nio.file.PathFilter.PathFilterExtensions
import sbt.nio.file.RelativeGlob.{ PathComponent, SingleComponentMatcher }
import scala.annotation.tailrec
import scala.collection.JavaConverters._
import scala.util.Properties
import scala.util.matching.Regex
/**
* Represents a query for a path on the file system. Instances of `Glob` can be combined together
* to build a more complicated query. Similar to the path name components of a `Path`, a `Glob` has
* components and matching is done from left to right component-wise. Each component is matched
* using a
* [[https://docs.oracle.com/javase/8/docs/api/java/nio/file/FileSystem.html#getPathMatcher-java.lang.String-
* PathMatcher]].
* For example,
* {{{
* val baseGlob = Glob("/foo/bar")
* baseGlob.matches(Paths.get("/foo/bar")) // true
* baseGlob.matches(Paths.get("/foo")) // false
* baseGlob.matches(Paths.get("/foo/bar/baz")) // false
*
* val children = baseGlob / AnyPath
* children.matches(Paths.get("/foo/bar")) // false
* children.matches(Paths.get("/foo/bar/baz")) // true
* children.matches(Paths.get("/foo/bar/baz/buzz")) false
*
* val secondGenerationChildren = children / AnyPath
* secondGeneration.matches(Paths.get("/foo/bar/baz")) // false
* secondGeneration.matches(Paths.get("/foo/bar/baz/buzz")) // true
* secondGeneration.matches(Paths.get("/foo/bar/baz/buzz/fizz")) // false
* }}}
* where [[AnyPath]] matches any path name.
*
* Globs can be recursive and are constructed using the [[RecursiveGlob]]:
* {{{
* val baseGlob = Glob("/foo/bar")
* val allDescendants = baseGlob / RecursiveGlob
* allDescendants.matches(Paths.get("/foo/bar")) // false
* allDescendants.matches(Paths.get("/foo/bar/baz/buzz.txt")) // true
* }}}
*
* They can also filter on the basis file names:
* {{{
* val baseGlob = Glob("/foo/bar")
* val allScalaSources = baseGlob / RecursiveGlob / "*.scala"
* allScalaSources.matches(Paths.get("/foo/bar/Foo.scala")) // true
* allScalaSources.matches(Paths.get("/foo/bar/baz/fizz/buzz/Bar.scala")) // true
* allScalaSources.matches(Paths.get("/foo/bar/baz/fizz/buzz/Buzz.java")) // false
*
* val allScalaAndJavaSources = baseGlob / RecursiveGlob / "*.{java,scala}"
* allScalaAndJavaSources.matches(Paths.get("/foo/bar/baz/fizz/buzz/Bar.scala")) // true
* allScalaAndJavaSources.matches(Paths.get("/foo/bar/baz/fizz/buzz/Buzz.java")) // true
* }}}
*/
sealed trait Glob {
/**
* Indicates whether a path matches the pattern specified by this [[Glob]].
*
* @param path the path to match
* @return true it the path matches.
*/
def matches(path: Path): Boolean
}
object Glob {
private def comp[T](left: T, right: T)(implicit ordering: Ordering[T]): Int =
ordering.compare(left, right)
/**
* Construct a single file [[Glob]]. The resulting [[Glob]] will only match the input file.
* @param file the single path to match.
* @return a Glob whose [[Glob.matches]] method only accepts the input path.
*/
def apply(file: File): Glob = if (file.isAbsolute) Root(file.toPath) else apply(file.toString)
/**
* Construct a Glob with a file prefix and relativeGlob glob string. For example:
* {{{
* val glob = Glob(file("foo/bar"), "*.txt") // matches all txt files in foo/bar
* val recursiveGlob = Glob(file("foo/bar"), "**<code>/</code>*.txt") // recursively matches all text files in foo/bar
* }}}
* @param file the prefix of the glob pattern. This can be an absolute or relativeGlob file.
* @param relative the relativeGlob glob string
* @return a Glob that matches the file prefix with
*/
def apply(file: File, relative: String): Glob =
Glob(file, RelativeGlob.parse(relative, isRegex = false))
/**
* Construct a [[Glob]] with a file prefix and a [[RelativeGlob]] suffix. In order for a path to
* match the output [[Glob]], it must start with `file` and its relative path with respect to
* `file` must match the `relativeGlob`. For example:
* {{{
* val txtFiles = Glob(file("foo/bar"), * / "*.txt") // matches all text files in any subdirectory of foo/bar
* val allChildren = Glob(file("/foo/bar"), **) // matches all descendants of /foo/bar
* val scalaSources = Glob(file("src/main/scala", ** / "*.scala")) // matches scala source file
* }}}
*
* @param file the prefix of the glob pattern. This can be an absolute or relativeGlob file.
* @param relativeGlob the [[RelativeGlob]] to append to the file prefix
* @return a Glob that matches a path with the same prefix as `file` and whose suffix matches
* `relativeGlob`.
*/
def apply(file: File, relativeGlob: RelativeGlob): Glob = Glob(file.toPath) / relativeGlob
/**
* Appends a relative glob specified by a regex pattern to an existing [[Glob]]. For example:
* {{{
* val scalaSources = Glob(file("foo/bar"), "^[^.].*\\.scala".r
* scalaSources.matches(Paths.get("foo/bar/Foo.scala")) // true
* scalaSources.matches(Paths.get("foo/bar/.hidden.scala")) // false
* }}}
*
* Note that on Windows, `\` will be treated as a regex escape character but `/` will be treated
* as a path separator.
*
* @param file the file prefix of the pattern
* @param regex the pattern to apply
* @return a Glob with the new [[RelativeGlob]] appended to the query.
*/
def apply(file: File, regex: Regex): Glob =
Glob(file, RelativeGlob.parse(regex.regex, isRegex = true))
/**
* Construct a single path Glob. The resulting glob
* @param path the single path to match.
* @return a Glob whose [[Glob.matches]] method only accepts the input path.
*/
def apply(path: Path): Glob = if (path.isAbsolute) Root(path) else apply(path.toString)
/**
* Construct a Glob with a path prefix and a `RelativeGlob`. For example:
* {{{
* val txtFiles = Glob(Paths.get("foo/bar"), * / "*.txt") // matches all text files in any subdirectory of foo/bar
* val allChildren = Glob(Paths.get("/foo/bar"), **) // matches all descendants of /foo/bar
* val scalaSources = Glob(Paths.get("src/main/scala", ** / "*.scala")) // matches scala source file
* }}}
* @param path the prefix of the glob pattern. This can be an absolute or relativeGlob file.
* @param relativeGlob the [[RelativeGlob]] to append to the file prefix
* @return a Glob that matches the file prefix with
*/
def apply(path: Path, relativeGlob: RelativeGlob): Glob = Glob(path) / relativeGlob
/**
* Construct a Glob with a file prefix and relativeGlob glob string. For example:
* {{{
* val glob = Glob(file("foo/bar"), "*.txt") // matches all text files in foo/bar
* }}}
* @param path the prefix of the glob pattern. This can be an absolute or relativeGlob file.
* @param relative the relativeGlob glob string
* @return a Glob that matches the file prefix with
*/
def apply(path: Path, relative: String): Glob = Glob(path) / relative
/**
* Appends a relative glob specified by a regex pattern to an existing [[Glob]]. For example:
* {{{
* val scalaSources = Glob(file("foo/bar"), "^[^.].*\\.scala".r
* scalaSources.matches(Paths.get("foo/bar/Foo.scala")) // true
* scalaSources.matches(Paths.get("foo/bar/.hidden.scala")) // false
* }}}
*
* Note that on Windows, `\` will be treated as a regex escape character but `/` will be treated
* as a path separator.
*
* @param path the file path prefix of the pattern
* @param regex the pattern to apply
* @return a Glob with the new [[RelativeGlob]] appended to the query.
*/
def apply(path: Path, regex: Regex): Glob =
Glob(path, RelativeGlob.parse(regex.regex, isRegex = true))
/**
* Construct a glob from a string. It can represent a full path. Any `/` characters in a pathname
* are automatically converted to `\\` on windows.
* {{{
* val absoluteGlob = Glob("/foo/bar<code>/</code>**<code>/</code>*.txt")
* val relativeGlob = Glob("*<code>/</code>*.txt")
* }}}
* @param glob the string to convert to a glob
* @return a parsed `Glob` for the input. May throw if the input cannot be parsed into a `Glob`.
*/
def apply(glob: String): Glob = apply(glob, isRegex = false)
private[sbt] def apply(glob: String, isRegex: Boolean): Glob = {
val parts = splitter(glob, isRegex)
@tailrec def fullGlob(path: Path, rest: List[String]): Glob = {
rest match {
case component :: tail if !hasMeta(component) => fullGlob(path.resolve(component), tail)
case Nil => Root(path)
case _ => Pattern(path, RelativeGlob(rest*))
}
}
def addRegexPrefix(parts: List[String]): List[String] =
if (isRegex) parts.map(p => s"regex:$p") else parts
parts match {
case h :: rest if !hasMeta(h) =>
val base = if (isWin) {
if (h.endsWith(":")) Paths.get(h + File.separator) else Paths.get(h)
} else {
if (h.isEmpty) Paths.get("/") else Paths.get(h)
}
base match {
case p if p.isAbsolute => fullGlob(p, addRegexPrefix(rest))
case _ => RelativeGlob(addRegexPrefix(parts)*)
}
case _ => RelativeGlob(addRegexPrefix(parts)*)
}
}
/**
* Provides a default ordering for glob. Globs with a path prefix precede relative globs and are
* sorted by their path prefix.
*/
implicit object ordering extends Ordering[Glob] {
override def compare(left: Glob, right: Glob): Int = left match {
case Pattern(leftRoot, leftRelative) =>
right match {
case Pattern(rightRoot, rightRelative) =>
leftRoot.compareTo(rightRoot) match {
case 0 => compare(leftRelative, rightRelative)
case i => i
}
case Root(rightRoot) =>
leftRoot.compareTo(rightRoot) match {
case 0 => -1
case i => i
}
case FullFileGlob(base, _, _) =>
leftRoot.compareTo(base) match {
case 0 => -1
case i => i
}
case _: RelativeGlob => -1
}
case l: RelativeGlob =>
right match {
case r: RelativeGlob => comp(l.matchers, r.matchers)
case _: Pattern => 1
case _: Root => 1
case _: FullFileGlob => 1
}
case Root(leftRoot) =>
right match {
case Root(rightRoot) => leftRoot.compareTo(rightRoot)
case FullFileGlob(leftBase, _, _) =>
leftRoot.compareTo(leftBase) match {
case 0 => -1
case i => i
}
case _ => -compare(right, left)
}
case FullFileGlob(leftBase, _, _) =>
right match {
case FullFileGlob(rightBase, _, _) => leftBase.compareTo(rightBase)
case _ => -compare(right, left)
}
}
}
private object Root {
implicit val ordering: Ordering[Root] = Ordering.by(_.root)
}
private[file] case object Empty extends RelativeGlob {
override private[file] def matchers: List[RelativeGlob.Matcher] = Nil
override def matches(path: Path): Boolean = false
}
private[file] final case class Root(root: Path) extends Glob {
require(root.isAbsolute, s"Tried to construct absolute glob from relative path $root")
override def matches(path: Path): Boolean = root == path
override def toString: String = root.toString
override def equals(o: Any): Boolean = o match {
case that: Root => this.root == that.root
case _ => false
}
override def hashCode: Int = root.hashCode
}
private[sbt] final case class FullFileGlob(base: Path, recursive: Boolean, filter: FileFilter)
extends Glob {
override def matches(path: Path): Boolean = {
path.startsWith(base) && {
if (recursive) filter.accept(path.toFile)
else path.getParent == base && filter.accept(path.toFile)
}
}
}
private[nio] object Pattern {
implicit val ordering: Ordering[Pattern] = Ordering.by(p => (p.root, p.relative))
}
private[nio] final case class Pattern(root: Path, relative: RelativeGlob) extends Glob {
override def matches(path: Path): Boolean =
path.startsWith(root) && ((path != root) && relative.matches(root.relativize(path)))
override def equals(o: Any): Boolean = o match {
case that: Pattern =>
this.root == that.root && this.relative == that.relative
case _ => false
}
override def hashCode: Int = (root.hashCode * 31) ^ relative.hashCode
override def toString: String = s"$root${File.separatorChar}$relative"
}
private[sbt] sealed trait RelativeGlobViewOption
private[sbt] object RelativeGlobViewOption {
private[sbt] val propName = "sbt.io.implicit.relative.glob.conversion"
implicit val default: RelativeGlobViewOption = System.getProperty(propName) match {
case "allow" => Ignore
case "error" => Error
case "warn" | null => Warn
case p =>
val message = s"Unrecognized option $p passed in for $propName. " +
"Valid values are: {'allow', 'warn', 'error'}. Setting default to: 'warn'."
System.err.println(message)
Warn
}
private[sbt] case object Warn extends RelativeGlobViewOption
private[sbt] case object Error extends RelativeGlobViewOption
private[sbt] case object Ignore extends RelativeGlobViewOption
}
private def errorMessage(relative: Glob, warn: Boolean): String = {
val prefix = if (warn) "Warning" else "Error"
val action =
if (warn) "To disable this warning, "
else "To allow implicit conversions using the current jvm working directory, "
s"$prefix: Tried to extract the base path for relative glob $relative. $action" +
s"re-run the program with java option, -D${RelativeGlobViewOption.propName}=allow"
}
/**
* Provides extension methods for [[Glob]].
* @param glob the [[Glob]] to augment.
*/
implicit class GlobOps(val glob: Glob) extends AnyVal {
private[sbt] def descendentMatches(path: Path): Boolean = glob match {
case Pattern(p, relative) if path.startsWith(p) => relative.matches(p.getFileName)
case g => g.matches(path)
}
private[sbt] def toFileFilter: FileFilter = pathname => glob.matches(pathname.toPath)
private[sbt] def toAbsolutePath(path: Path)(implicit option: RelativeGlobViewOption): Path = {
import RelativeGlobViewOption._
if (!path.isAbsolute) {
option match {
case Error => throw new IllegalArgumentException(errorMessage(glob, warn = false))
case Ignore => path.toAbsolutePath
case Warn =>
System.err.println(errorMessage(glob, warn = true))
path.toAbsolutePath
}
} else path
}
private[sbt] def fileTreeViewListParameters(implicit
option: RelativeGlobViewOption
): (Path, Int, Glob) = {
val b = base
val r = range._2
val g = glob match {
case r: RelativeGlob =>
r.tail match {
case Nil => Root(b)
case t => Pattern(b, RelativeGlob(t))
}
case _ => glob
}
(b, r, g)
}
private[sbt] def base(implicit option: RelativeGlobViewOption): Path =
toAbsolutePath(glob match {
case Pattern(root, r) => r.prefix.map(root.resolve).getOrElse(root)
case Root(root) => root
case r: RelativeGlob => r.prefix.getOrElse(Paths.get(""))
case FullFileGlob(base, _, _) => base
})
private[sbt] def range: (Int, Int) = glob match {
case Pattern(_, relative: RelativeGlob) => RelativeGlob.range(relative)
case Root(_) => (0, 0)
case relative: RelativeGlob => RelativeGlob.range(relative)
case FullFileGlob(_, recursive, _) => if (recursive) (1, Int.MaxValue) else (1, 1)
}
/**
* Appends a relative glob path to an existing [[Glob]]. For example:
* {{{
* val glob = Glob("foo/bar")
* val scalaSources = glob / "**<code>/</code>*.scala"
* scalaSources.matches(Paths.get("foo/bar/Foo.scala")) // true
* scalaSources.matches(Paths.get("foo/bar/baz/fizz/buzz/Bar.scala")) // true
* scalaSources.matches(Paths.get("foo/bar/baz/fizz/buzz/Baz.java")) // false
* }}}
*
* @param relativeGlob the string that will be parsed into a [[RelativeGlob]] and appended to
* the base glob
* @return a Glob with the new [[RelativeGlob]] appended to the query.
*/
def /(relativeGlob: String): Glob = /(RelativeGlob.parse(relativeGlob, isRegex = false))
/**
* Appends a relative glob specified by a regex pattern to an existing [[Glob]]. For example:
* {{{
* val glob = Glob("foo/bar")
* val scalaSources = glob / "^[^.].*\\.scala".r
* scalaSources.matches(Paths.get("foo/bar/Foo.scala")) // true
* scalaSources.matches(Paths.get("foo/bar/.hidden.scala")) // false
* }}}
*
* Note that it is not possible to specify a recursive glob this way because `**` is not valid
* in regex. To make a recursive glob, combine with [[RecursiveGlob]]:
* {{{
* val scalaSources = Glob("foo/bar") / ** / "^[^.].*\\.scala".r
* scalaSources.matches(Paths.get("foo/bar/baz/fizz/buzz/Foo.scala")) // true
* scalaSources.matches(Paths.get("foo/bar/baz/fizz/buzz/.hidden.scala")) // false
* }}}
*
* On Windows, `\` will be treated as a regex escape character but `/` will be treated
* as a path separator.
*
* @param regex the pattern to apply
* @return a Glob with the new [[RelativeGlob]] appended to the query.
*/
def /(regex: Regex): Glob = /(RelativeGlob.parse(regex.regex, isRegex = true))
/**
* Appends a relative glob path to an existing [[Glob]]. For example:
* {{{
* val glob = Glob("foo/bar")
* val scalaSources = glob / "**<code>/</code>*.scala"
* scalaSources.matches(Paths.get("foo/bar/Foo.scala")) // true
* scalaSources.matches(Paths.get("foo/bar/baz/fizz/buzz/Bar.scala")) // true
* scalaSources.matches(Paths.get("foo/bar/baz/fizz/buzz/Baz.java")) // false
* }}}
*
* @param relativeGlob the [[RelativeGlob]] to be appended to the query
* @return a Glob with the new [[RelativeGlob]] appended to the query.
*/
def /(relativeGlob: RelativeGlob): Glob = glob match {
case Pattern(root, relative) => Pattern(root, relative / relativeGlob)
case Root(path) =>
val newRoot = relativeGlob.prefix.map(path.resolve).getOrElse(path)
relativeGlob.tail match {
case Nil => Root(newRoot)
case t => Pattern(newRoot, RelativeGlob(t))
}
case f: FullFileGlob =>
throw new IllegalArgumentException(s"Can't call / on legacy glob $f")
case r: RelativeGlob => r / relativeGlob
}
}
/**
* Provides extension methods to [[Glob]] that allows it to create combined
* [[sbt.nio.file.PathFilter]] instances with `&&` and `||`.
* @param glob the Glob to extend
*/
implicit class PathFilterOps(val glob: Glob) extends AnyVal with PathFilterExtensions {
def &&(other: PathFilter): PathFilter = (glob: PathFilter) && other
def ||(other: PathFilter): PathFilter = (glob: PathFilter) || other
def unary_! : PathFilter = !(glob: PathFilter)
}
/**
* Converts a string to a [[Glob]].
* @param glob the string to convert
* @return the converted [[Glob]].
*/
implicit def stringToGlob(glob: String): Glob = Glob(glob)
/**
* Provides extension methods to `java.nio.file.Path`.
* @param path the `Path` instance to extend.
*/
final class PathOps(val path: Path) extends AnyVal {
/**
* Converts the path to a single path glob
* @return the single path [[Glob]].
*/
def toGlob: Glob =
if (path.isAbsolute) Root(path)
else {
path.iterator.asScala.toList match {
case Nil => Empty
case h :: Nil if h.getFileName.toString.isEmpty => Empty
case parts => RelativeGlob(parts.map(p => PathComponent(p.getFileName.toString)))
}
}
/**
* Appends a path component to a path.
* @param component the component to append
* @return the appended path.
*/
def /(component: String): Path = path.resolve(component)
}
/**
* Provides extension methods to `java.nio.file.Path`
* @param file the `File` instance to extend.
*/
final class FileOps(val file: File) extends AnyVal {
/**
* Converts the file to a single path glob.
* @return the single path [[Glob]].
*/
def toGlob: Glob = new PathOps(file.toPath).toGlob
}
private val windowsEscapable = "(){}"
private val allMeta = "*{([?"
private[file] val hasMeta: String => Boolean = _.exists(allMeta.contains(_))
private[file] val isWin = Properties.isWin
private val splitter: (String, Boolean) => List[String] = {
if (Glob.isWin) { (glob, isRegex) =>
{
val stringBuilder = new StringBuilder(glob.length)
val components = new util.ArrayList[String]
val array = glob.toCharArray
val separator = File.separatorChar
@tailrec def fillComponents(index: Int): Unit = index match {
case i if i < array.length =>
array(i) match {
case `separator` if !isRegex =>
val nextIndex = i + 1
if (nextIndex < array.length) {
array(nextIndex) match {
case c if windowsEscapable.contains(c) =>
stringBuilder.append(separator).append(c)
case c =>
components.add(stringBuilder.toString)
stringBuilder.clear()
stringBuilder.append(c)
}
fillComponents(nextIndex + 1)
} else {
components.add(stringBuilder.toString)
()
}
case '/' =>
components.add(stringBuilder.toString)
stringBuilder.clear()
fillComponents(i + 1)
case c =>
stringBuilder.append(c)
fillComponents(i + 1)
}
case _ =>
components.add(stringBuilder.toString)
()
}
fillComponents(index = 0)
components.asScala.toList
}
} else { (glob, _) =>
glob.split(File.separatorChar).toList
}
}
}
/**
* A specialization of [[Glob]] that applies only to relative paths. [[RelativeGlob]] instances
* can always be combined with the `/` operator whereas general `Glob` instances cannot because they
* may correspond to an absolute file path:
* {{{
* val combined = Glob("/foo/bar") / RelativeGlob("
* }}}
*/
sealed trait RelativeGlob extends Glob {
private[file] def matchers: List[RelativeGlob.Matcher]
private[file] def prefix: Option[Path] = matchers.takeWhile(_.isInstanceOf[PathComponent]) match {
case Nil => None
case (h: PathComponent) :: (t: List[PathComponent] @unchecked) =>
Some(Paths.get(h.glob, t.map(_.glob)*))
case _ => None
}
private[file] def tail: List[RelativeGlob.Matcher] =
matchers.dropWhile(_.isInstanceOf[PathComponent])
/**
* Appends additional components to this [[RelativeGlob]]:
* {{{
* val glob = Glob("foo/bar")
* val scalaSources = glob / "**<code>/</code>*.scala"
* scalaSources.matches(Paths.get("foo/bar/Foo.scala")) // true
* scalaSources.matches(Paths.get("foo/bar/baz/fizz/buzz/Bar.scala")) // true
* scalaSources.matches(Paths.get("foo/bar/baz/fizz/buzz/Baz.java")) // false
* }}}
*
* @param relativeGlob the additional query string to parse and append
* @return a Glob with the new [[RelativeGlob]] appended to the query.
*/
def /(relativeGlob: String): RelativeGlob = /(RelativeGlob.parse(relativeGlob, isRegex = false))
/**
* Appends an additional [[RelativeGlob]] to this [[RelativeGlob]]:
* {{{
* val glob = Glob("foo/bar")
* val scalaSources = glob / ** / "*.scala"
* scalaSources.matches(Paths.get("foo/bar/Foo.scala")) // true
* scalaSources.matches(Paths.get("foo/bar/baz/fizz/buzz/Bar.scala")) // true
* scalaSources.matches(Paths.get("foo/bar/baz/fizz/buzz/Baz.java")) // false
* }}}
*
* @param relativeGlob the additional query string to parse and append
* @return a Glob with the new [[RelativeGlob]] appended to the query.
*/
def /(relativeGlob: RelativeGlob): RelativeGlob =
RelativeGlob(this.matchers ::: relativeGlob.matchers)
/**
* Appends a relative glob specified by a regex pattern to an existing [[Glob]]. For example:
* {{{
* val glob = Glob("foo/bar")
* val scalaSources = glob / "^[^.].*\\.scala".r
* scalaSources.matches(Paths.get("foo/bar/Foo.scala")) // true
* scalaSources.matches(Paths.get("foo/bar/.hidden.scala")) // false
* }}}
*
* Note that it is not possible to specify a recursive glob this way because `**` is not valid
* in regex. To make a recursive glob, combine with [[RecursiveGlob]]:
* {{{
* val scalaSources = Glob("foo/bar") / ** / "^[^.].*\\.scala".r
* scalaSources.matches(Paths.get("foo/bar/baz/fizz/buzz/Foo.scala")) // true
* scalaSources.matches(Paths.get("foo/bar/baz/fizz/buzz/.hidden.scala")) // false
* }}}
*
* On Windows, `\` will be treated as a regex escape character but `/` will be treated
* as a path separator.
*
* @param regex the pattern to apply
* @return a Glob with the new [[RelativeGlob]] appended to the query.
*/
def /(regex: Regex): RelativeGlob = /(RelativeGlob.parse(regex.regex, isRegex = true))
}
/**
* A [[RelativeGlob]] that matches any path. Can be combined with other [[Glob]] instances to build
* recursive queries:
* {{{
* val scalaSources = Glob("/foo/bar") / RecursiveGlob / "*.scala"
* scalaSources.matches(Paths.get("/foo/bar/Foo.scala")) // true
* scalaSources.matches(Paths.get("/foo/bar/baz/fizz/buzz/Bar.scala")) // true
* }}}
*/
case object RecursiveGlob extends SingleComponentMatcher with RelativeGlob {
override private[file] def glob: String = "**"
def matches(path: Path): Boolean = true
}
/**
* A [[RelativeGlob]] that matches any single path name.
* {{{
* AnyPath.matches(Paths.get("foo")) // true
* AnyPath.matches(Paths.get("foo/bar")) // false
* Glob("foo/bar", AnyPath).matches(Paths.get("foo/bar")) // false
* Glob("foo/bar", AnyPath).matches(Paths.get("foo/bar/baz")) // true
* Glob("foo/bar", AnyPath).matches(Paths.get("foo/bar/baz/buzz")) // false
* }}}
*
* It can be used to specify the minimum depth of a query:
* {{{
* val childScalaSources = Glob("/foo/bar") / AnyPath / RecursiveGlob / "*.scala"
* childScalaSources.matches(Paths.get("/foo/bar/Foo.scala")) // false
* childScalaSources.matches(Paths.get("/foo/bar/baz/Bar.scala")) // true
* childScalaSources.matches(Paths.get("/foo/bar/baz/fizz/buzz/Bar.scala")) // true
* }}}
*/
case object AnyPath extends SingleComponentMatcher with RelativeGlob {
override private[file] def glob: String = "*"
override def matches(path: Path): Boolean = path.getNameCount == 1
}
object RelativeGlob {
/**
* Aliases [[RecursiveGlob]].
*/
val ** = RecursiveGlob
/**
* Aliases [[AnyPath]].
*/
val * = AnyPath
private[file] def parse(glob: String, isRegex: Boolean): RelativeGlob =
Glob(glob, isRegex) match {
case r: RelativeGlob => r
case _ =>
val msg = s"Couldn't create relativeGlob glob from absolute glob: $glob"
throw new IllegalArgumentException(msg)
}
private[sbt] def apply(matchers: String*): RelativeGlob =
new RelativeGlobImpl(matchers.view.filterNot(_ == ".").map(Matcher.apply).toList)
private[sbt] def apply(matchers: List[Matcher]): RelativeGlob = new RelativeGlobImpl(matchers)
implicit val ordering: Ordering[RelativeGlob] = Ordering.by(_.matchers)
private[file] def range(relative: RelativeGlob): (Int, Int) = {
val res = relative.matchers.foldLeft((0, 0)) {
case ((0, 0), RecursiveGlob) => (1, Int.MaxValue)
case (r @ (_, Int.MaxValue), RecursiveGlob) => r
case ((min, Int.MaxValue), _) => (min + 1, Int.MaxValue)
case ((min, max), _) => (min + 1, max + 1)
}
res
}
private final class RelativeGlobImpl(val matchers: List[Matcher]) extends RelativeGlob {
override def matches(path: Path): Boolean = {
val count = path.getNameCount
@tailrec def impl(currentIndex: Int, matchers: List[Matcher]): Boolean = matchers match {
case RecursiveGlob :: Nil => count > 0
case RecursiveGlob :: matchersTail =>
currentIndex match {
case i if i < count => recursiveMatches(matchersTail, i)
case _ => false
}
case m :: Nil if currentIndex == count - 1 => m.matches(path.getFileName)
case m :: matchersTail =>
currentIndex match {
case i if i < count && m.matches(path.getName(i)) => impl(i + 1, matchersTail)
case _ => false
}
case Nil => currentIndex == count
}
def recursiveMatches(remaining: List[Matcher], currentIndex: Int): Boolean = {
remaining match {
case Nil => true
case nameMatcher :: Nil => nameMatcher.matches(path.getFileName)
case _ =>
@tailrec def recursiveImpl(index: Int): Boolean = index match {
case i if i < count => impl(i, remaining) || recursiveImpl(i + 1)
case _ => false
}
recursiveImpl(currentIndex)
}
}
impl(currentIndex = 0, matchers)
}
override def hashCode: Int = matchers.hashCode
override def toString: String = matchers.mkString(File.separator)
override def equals(o: Any): Boolean = o match {
case that: RelativeGlob => this.matchers == that.matchers
case _ => false
}
}
private[sbt] sealed trait Matcher extends RelativeGlob
private[sbt] object Matcher {
private[sbt] implicit object ordering extends Ordering[Matcher] {
override def compare(x: Matcher, y: Matcher): Int = x match {
case RecursiveGlob =>
y match {
case RecursiveGlob => 0
case _ => -1
}
case AnyPath =>
y match {
case AnyPath => 0
case RecursiveGlob => 1
case _ => -1
}
case spm: SingleComponentMatcher =>
y match {
case that: SingleComponentMatcher => spm.glob.compareTo(that.glob)
case _ => -1
}
case _: FunctionNameFilter =>
y match {
case _: FunctionNameFilter => 0
case _: NotMatcher | _: AndMatcher | _: OrMatcher => -1
case _ => 1
}
case _: NotMatcher =>
y match {
case _: NotMatcher | _: AndMatcher | _: OrMatcher => 0
case _ => 1
}
case _: AndMatcher =>
y match {
case _: NotMatcher | _: AndMatcher | _: OrMatcher => 0
case _ => 1
}
case _: OrMatcher =>
y match {
case _: NotMatcher | _: AndMatcher | _: OrMatcher => 0
case _ => 1
}
}
}
private[sbt] implicit val listOrdering: Ordering[List[Matcher]] = new Ordering[List[Matcher]] {
override def compare(left: List[Matcher], right: List[Matcher]): Int = {
val leftIt = left.iterator
val rightIt = right.iterator
while (leftIt.hasNext && rightIt.hasNext) {
val res = ordering.compare(leftIt.next(), rightIt.next())
if (res != 0) return res
}
Ordering.Boolean.compare(leftIt.hasNext, rightIt.hasNext)
}
}
private[sbt] def and(left: Matcher, right: Matcher): Matcher = {
if (left == NoPath || right == NoPath) NoPath
else if (left == AnyPath) right
else if (right == AnyPath) left
else AndMatcher(left, right)
}
private[sbt] def or(left: Matcher, right: Matcher): Matcher = OrMatcher(left, right)
private[sbt] def not(matcher: Matcher): Matcher = matcher match {
case NoPath => AnyPath
case AnyPath => NoPath
case m => NotMatcher(m)
}
private[sbt] def apply(glob: String): Matcher = glob match {
case "**" => RecursiveGlob
case "*" => AnyPath
case g if !g.startsWith("regex:") && !Glob.hasMeta(g) => PathComponent(g)
case g => new GlobMatcher(g)
}
private[sbt] def apply(f: String => Boolean): Matcher = FunctionNameFilter(f)
}
private[sbt] case object NoPath extends SingleComponentMatcher with RelativeGlob {
override def glob: String = "<null>"
override def matches(path: Path): Boolean = false
}
private[file] sealed trait SingleComponentMatcher extends Matcher {
private[file] def glob: String
override private[file] final val matchers: List[Matcher] = this :: Nil
override def toString: String = glob
}
private[file] object PathComponent {
def apply(component: String): PathComponent = new PathComponent(component)
def unapply(glob: Glob): Option[String] = glob match {
case p: PathComponent => Some(p.glob)
case _ => None
}
}
private[file] final case class NotMatcher(matcher: Matcher) extends Matcher {
override private[sbt] def matchers: List[Matcher] = this :: Nil
/**
* Indicates whether a path matches the pattern specified by this [[Glob]].
*
* @param path the path to match
* @return true it the path matches.
*/
override def matches(path: Path): Boolean = !matcher.matches(path)
override def toString: String = s"!$matcher"
}
private[file] final case class OrMatcher(left: Matcher, right: Matcher) extends Matcher {
override private[sbt] def matchers: List[Matcher] = this :: Nil
/**
* Indicates whether a path matches the pattern specified by this [[Glob]].
*
* @param path the path to match
* @return true it the path matches.
*/
override def matches(path: Path): Boolean = left.matches(path) || right.matches(path)
override def toString: String = s"($left || $right)"
}
private[file] final case class AndMatcher(left: Matcher, right: Matcher) extends Matcher {
override private[sbt] def matchers: List[Matcher] = this :: Nil
/**
* Indicates whether a path matches the pattern specified by this [[Glob]].
*
* @param path the path to match
* @return true it the path matches.
*/
override def matches(path: Path): Boolean = left.matches(path) && right.matches(path)
override def toString: String = s"($left && $right)"
}
private[file] final case class FunctionNameFilter(f: String => Boolean) extends Matcher {
override private[sbt] def matchers: List[Matcher] = this :: Nil
override def matches(path: Path): Boolean = f(path.getFileName.toString)
}
private[file] final class PathComponent private (override val glob: String)
extends SingleComponentMatcher {
override def matches(path: Path): Boolean =
path.getNameCount == 1 && path.getFileName.toString == glob
override def equals(o: Any): Boolean = o match {
case that: PathComponent => this.glob == that.glob
case _ => false
}
override def hashCode: Int = glob.hashCode
}
private final class GlobMatcher(override val glob: String) extends SingleComponentMatcher {
private val (prefixString, pattern) = glob.indexOf(":") match {
case -1 => ("glob", glob)
case i => (glob.substring(0, i), glob.substring(i + 1))
}
private val matcher = FileSystems.getDefault.getPathMatcher(s"$prefixString:$pattern")
override def matches(path: Path): Boolean = matcher.matches(path)
override def equals(o: Any): Boolean = o match {
case that: GlobMatcher => this.glob == that.glob
case _ => false
}
override def hashCode(): Int = glob.hashCode
}
}