Skip to content

Commit

Permalink
Merge pull request #1560 from wleczny/change-settings-filename
Browse files Browse the repository at this point in the history
Change project settings filename
  • Loading branch information
wleczny authored Nov 10, 2022
2 parents f470e88 + 9a32078 commit bf5ebc2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions modules/build/src/main/scala/scala/build/CrossSources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ object CrossSources {
lazy val subPath = sourcePath.subRelativeTo(dir)
if (os.isDir(sourcePath))
Right(Inputs.singleFilesFromDirectory(Inputs.Directory(sourcePath), enableMarkdown))
else if (sourcePath == os.sub / "project.settings.scala")
Right(Seq(Inputs.SettingsScalaFile(dir, subPath)))
else if (sourcePath == os.sub / "project.scala")
Right(Seq(Inputs.ProjectScalaFile(dir, subPath)))
else if (sourcePath.ext == "scala") Right(Seq(Inputs.SourceScalaFile(dir, subPath)))
else if (sourcePath.ext == "sc") Right(Seq(Inputs.Script(dir, subPath)))
else if (sourcePath.ext == "java") Right(Seq(Inputs.JavaFile(dir, subPath)))
Expand Down
26 changes: 13 additions & 13 deletions modules/build/src/main/scala/scala/build/Inputs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ object Inputs {
}
final case class SourceScalaFile(base: os.Path, subPath: os.SubPath)
extends OnDisk with SourceFile with ScalaFile
final case class SettingsScalaFile(base: os.Path, subPath: os.SubPath)
final case class ProjectScalaFile(base: os.Path, subPath: os.SubPath)
extends OnDisk with SourceFile with ScalaFile
final case class JavaFile(base: os.Path, subPath: os.SubPath)
extends OnDisk with SourceFile with Compiled {
Expand Down Expand Up @@ -237,8 +237,8 @@ object Inputs {
.collect {
case p if p.last.endsWith(".java") =>
Inputs.JavaFile(d.path, p.subRelativeTo(d.path))
case p if p.last == "project.settings.scala" =>
Inputs.SettingsScalaFile(d.path, p.subRelativeTo(d.path))
case p if p.last == "project.scala" =>
Inputs.ProjectScalaFile(d.path, p.subRelativeTo(d.path))
case p if p.last.endsWith(".scala") =>
Inputs.SourceScalaFile(d.path, p.subRelativeTo(d.path))
case p if p.last.endsWith(".sc") =>
Expand All @@ -252,16 +252,16 @@ object Inputs {
.sortBy(_.subPath.segments)
}

def projectSettingsFiles(elements: Seq[Inputs.Element]): Seq[Inputs.SettingsScalaFile] =
def projectSettingsFiles(elements: Seq[Inputs.Element]): Seq[Inputs.ProjectScalaFile] =
elements.flatMap {
case f: SettingsScalaFile => Seq(f)
case d: Directory => Inputs.configFileFromDirectory(d)
case _ => Nil
case f: ProjectScalaFile => Seq(f)
case d: Directory => Inputs.configFileFromDirectory(d)
case _ => Nil
}.distinct

def configFileFromDirectory(d: Inputs.Directory): Seq[Inputs.SettingsScalaFile] =
if (os.exists(d.path / "project.settings.scala"))
Seq(Inputs.SettingsScalaFile(d.path, os.sub / "project.settings.scala"))
def configFileFromDirectory(d: Inputs.Directory): Seq[Inputs.ProjectScalaFile] =
if (os.exists(d.path / "project.scala"))
Seq(Inputs.ProjectScalaFile(d.path, os.sub / "project.scala"))
else Nil

private def inputsHash(elements: Seq[Element]): String = {
Expand All @@ -272,7 +272,7 @@ object Inputs {
case _: Inputs.Directory => "dir:"
case _: Inputs.ResourceDirectory => "resource-dir:"
case _: Inputs.JavaFile => "java:"
case _: Inputs.SettingsScalaFile => "config:"
case _: Inputs.ProjectScalaFile => "config:"
case _: Inputs.SourceScalaFile => "scala:"
case _: Inputs.CFile => "c:"
case _: Inputs.Script => "sc:"
Expand Down Expand Up @@ -318,7 +318,7 @@ object Inputs {
settingsFiles.headOption.map { s =>
if (settingsFiles.length > 1)
System.err.println(
s"Warning: more than one project.settings.scala file has been found. Setting ${s.base} as the project root directory for this run."
s"Warning: more than one project.scala file has been found. Setting ${s.base} as the project root directory for this run."
)
(s.base, true, WorkspaceOrigin.SourcePaths)
}.orElse {
Expand Down Expand Up @@ -458,7 +458,7 @@ object Inputs {
List(resolve(url, content))
}
}
else if (path.last == "project.settings.scala") Right(Seq(SettingsScalaFile(dir, subPath)))
else if (path.last == "project.scala") Right(Seq(ProjectScalaFile(dir, subPath)))
else if (arg.endsWith(".sc")) Right(Seq(Script(dir, subPath)))
else if (arg.endsWith(".scala")) Right(Seq(SourceScalaFile(dir, subPath)))
else if (arg.endsWith(".java")) Right(Seq(JavaFile(dir, subPath)))
Expand Down
14 changes: 7 additions & 7 deletions modules/build/src/test/scala/scala/build/tests/InputsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ class InputsTests extends munit.FunSuite {
}
}

test("project settings file") {
test("project.scala file") {
val testInputs = TestInputs(
files = Seq(
os.rel / "custom-dir" / "project.settings.scala" -> "",
os.rel / "project.settings.scala" -> s"//> using javaProp \"foo=bar\"".stripMargin,
os.rel / "custom-dir" / "project.scala" -> "",
os.rel / "project.scala" -> s"//> using javaProp \"foo=bar\"".stripMargin,
os.rel / "foo.scala" ->
s"""object Foo {
| def main(args: Array[String]): Unit =
| println("Foo")
|}
|""".stripMargin
),
inputArgs = Seq("custom-dir", "foo.scala", "project.settings.scala")
inputArgs = Seq("foo.scala", "custom-dir", "project.scala")
)
testInputs.withBuild(buildOptions, buildThreads, bloopConfigOpt) {
(root, _, buildMaybe) =>
Expand Down Expand Up @@ -84,7 +84,7 @@ class InputsTests extends munit.FunSuite {
}
}

test("passing project settings file and its parent directory") {
test("passing project.scala and its parent directory") {
val testInputs = TestInputs(
files = Seq(
os.rel / "foo.scala" ->
Expand All @@ -93,9 +93,9 @@ class InputsTests extends munit.FunSuite {
| println("Foo")
|}
|""".stripMargin,
os.rel / "project.settings.scala" -> ""
os.rel / "project.scala" -> ""
),
inputArgs = Seq(".", "project.settings.scala")
inputArgs = Seq(".", "project.scala")
)
testInputs.withBuild(buildOptions, buildThreads, bloopConfigOpt) {
(root, inputs, _) =>
Expand Down
30 changes: 15 additions & 15 deletions website/docs/reference/root-dir.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,49 @@ Scala CLI needs a root directory:

## Setting root directory

First of all, Scala CLI checks every passed input (in the same order in which inputs were passed) for the `project.settings.scala` file:
- If the `project.settings.scala` file is passed explicitly as a **source**, Scala CLI sets its parent directory as the root directory.
- If the input is a **directory**, Scala CLI looks for the `project.settings.scala` inside this directory. If the file is found, Scala CLI sets the passed directory as the root directory.
First of all, Scala CLI checks every passed input (in the same order in which inputs were passed) for the `project.scala` file:
- If the `project.scala` file is passed explicitly as a **source**, Scala CLI sets its parent directory as the root directory.
- If the input is a **directory**, Scala CLI looks for the `project.scala` inside this directory. If the file is found, Scala CLI sets the passed directory as the root directory.

If more than one `project.settings.scala` file is found, Scala CLI uses only **the first one** to set the root directory and raises **warning** saying which one was used.
If more than one `project.scala` file is found, Scala CLI uses only **the first one** to set the root directory and raises **warning** saying which one was used.

If no `project.settings.scala` files are found, Scala CLI sets the root directory based on the first file/directory input:
If no `project.scala` files are found, Scala CLI sets the root directory based on the first file/directory input:
- If the input is a **directory**, it is set as the root directory.
- If the input is a **file**, Scala CLI sets its parent directory as the root directory.

If more then one file/directory input has ben passed Scala CLI raises the warning saying which directory has been set as the project root directory.

If no `project.settings.scala` files are found and no file/directory inputs have ben passed, Scala CLI sets the current working directory (where Scala CLI was invoked from) as the project root directory.
If no `project.scala` files are found and no file/directory inputs have ben passed, Scala CLI sets the current working directory (where Scala CLI was invoked from) as the project root directory.

#### Example

Let's say we have the following file structure:

```
project
│ project.settings.scala
│ project.scala
└───dir1
│ │ file1.scala
│ │
│ └───dir2
│ │ project.settings.scala
│ │ project.scala
│ │ file2.scala
└───dir3
│ project.settings.scala
│ project.scala
│ file3.scala
```

And user runs the following command:
```
project> scala-cli dir1/file1.scala dir1/dir2 dir3/project.settings.scala
project> scala-cli dir1/file1.scala dir1/dir2 dir3/project.scala
```

Scala CLI will find 2 `project.settings.scala` files:
- inside `dir2`, since this directory was passed as an input and it has `project.settings.scala` inside.
- inside `dir3`, since `dir3/project.settings.scala` was passed explicitly as a source
Scala CLI will find 2 `project.scala` files:
- inside `dir2`, since this directory was passed as an input and it has `project.scala` inside.
- inside `dir3`, since `dir3/project.scala` was passed explicitly as a source

`dir1/dir2` was passed before `dir3/project.settings.scala`, so `dir2` will be set as the **root** directory for this build.
`dir1/dir2` was passed before `dir3/project.scala`, so `dir2` will be set as the **root** directory for this build.

Since more than one `project.settings.scala` has been found, Scala CLI will raise the warning saying that more than one `project.settings.scala` file has been found and `dir1/dir2` has been set as the project root directory.
Since more than one `project.scala` has been found, Scala CLI will raise the warning saying that more than one `project.scala` file has been found and `dir1/dir2` has been set as the project root directory.

0 comments on commit bf5ebc2

Please sign in to comment.