11package mill .scalalib
22import mill .*
33import mill .define .BuildCtx
4+ import mill .scalalib .api .JvmWorkerUtil
45
56/**
67 * Mix this in to any [[ScalaModule ]] to provide a [[unidocSite ]] target that
78 * can be used to build a unified scaladoc site for this module and all of
89 * its transitive dependencies
910 */
1011trait UnidocModule extends ScalaModule {
12+
13+ /** The URL of the source code of this module. */
1114 def unidocSourceUrl : T [Option [String ]] = None
1215
16+ /** Passed as `-doc-version` to scaladoc. */
1317 def unidocVersion : T [Option [String ]] = None
1418
1519 def unidocCompileClasspath = Task {
1620 Seq (compile().classes) ++ Task .traverse(moduleDeps)(_.compileClasspath)().flatten
1721 }
1822
23+ /**
24+ * Which module dependencies to include in the scaladoc site.
25+ *
26+ * By default, all transitive module dependencies are included.
27+ */
28+ def unidocModuleDeps : Seq [JavaModule ] = transitiveModuleDeps
29+
1930 def unidocSourceFiles = Task {
20- allSourceFiles() ++ Task .traverse(moduleDeps)(_.allSourceFiles)().flatten
31+ if (JvmWorkerUtil .isScala3(scalaVersion())) {
32+ // On Scala 3 scaladoc only accepts .tasty files and .jar files
33+ Task .traverse(unidocModuleDeps)(_.compile)().map(_.classes)
34+ .filter(pr => os.exists(pr.path))
35+ .flatMap(pr => os.walk(pr.path))
36+ .filter(path => path.ext == " tasty" || path.ext == " jar" )
37+ .map(PathRef (_))
38+ } else
39+ Task .traverse(unidocModuleDeps)(_.allSourceFiles)().flatten
2140 }
2241
2342 /** The title of the scaladoc site. */
24- def unidocDocumentTitle : T [String ] = Task { " Mill " }
43+ def unidocDocumentTitle : T [String ]
2544
2645 /** Extra options passed to scaladoc. */
2746 def unidocOptions : T [Seq [String ]] = Task { Seq .empty[String ] }
2847
2948 /**
30- * @param local whether to use 'file://' as the `-doc-source-url`.
49+ * @param local whether to use 'file://' as the `-doc-source-url`/`-source-links` .
3150 */
3251 def unidocCommon (local : Boolean ) = Task .Anon {
52+ val scalaVersion0 = scalaVersion()
53+ val onScala3 = JvmWorkerUtil .isScala3(scalaVersion0)
3354
55+ val scalaOrganization0 = scalaOrganization()
56+ val scalaDocClasspath0 = scalaDocClasspath()
57+ val scalacPluginClasspath0 = scalacPluginClasspath()
3458 val unidocSourceFiles0 = unidocSourceFiles()
3559
3660 Task .log.info(s " Staging scaladoc for ${unidocSourceFiles0.length} files " )
3761
3862 // the details of the options and jvmWorker call are significantly
39- // different between scala-2 scaladoc and scala-3 scaladoc
40- // below is for scala-2 variant
63+ // different between scala-2 scaladoc and scala-3 scaladoc, so make sure to
64+ // use the correct options for the correct version
4165 val options : Seq [String ] = Seq (
4266 " -doc-title" ,
4367 unidocDocumentTitle(),
@@ -48,18 +72,35 @@ trait UnidocModule extends ScalaModule {
4872 ) ++
4973 unidocVersion().toSeq.flatMap(Seq (" -doc-version" , _)) ++
5074 unidocSourceUrl().toSeq.flatMap { url =>
75+ val sourceLinksOption = if (onScala3) " -source-links" else " -doc-source-url"
76+
5177 if (local) Seq (
52- " -doc-source-url" ,
53- " file://€{FILE_PATH}.scala"
54- )
55- else Seq (
56- " -doc-source-url" ,
57- url + " €{FILE_PATH}.scala" ,
58- " -sourcepath" ,
59- BuildCtx .workspaceRoot.toString
78+ sourceLinksOption,
79+ " file://€{FILE_PATH_EXT}"
6080 )
81+ else {
82+ val workspaceRoot = BuildCtx .workspaceRoot
83+ Seq (
84+ sourceLinksOption,
85+ // Relative path to the workspace
86+ if (onScala3) s " $workspaceRoot= $url€{FILE_PATH_EXT} " else s " $url€{FILE_PATH_EXT} " ,
87+ " -sourcepath" ,
88+ workspaceRoot.toString
89+ )
90+ }
6191 } ++ unidocOptions()
6292
93+ Task .log.info(
94+ s """ |Running Unidoc with:
95+ | scalaVersion: ${scalaVersion0}
96+ | scalaOrganization: ${scalaOrganization0}
97+ | options: $options
98+ | scalaDocClasspath: ${scalaDocClasspath0.map(_.path)}
99+ | scalacPluginClasspath: ${scalacPluginClasspath0.map(_.path)}
100+ | unidocSourceFiles: ${unidocSourceFiles0.map(_.path)}
101+ | """ .stripMargin
102+ )
103+
63104 jvmWorker().worker().docJar(
64105 scalaVersion(),
65106 scalaOrganization(),
0 commit comments