-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Giving the content of default .gitignore or .github/workflows/ci.yml files. Use like ``` $ scala-cli default gitignore /.bsp/ /.scala-build/ $ scala-cli default workflow --write $ cat .github/workflows/ci.yml … ```
- Loading branch information
1 parent
0881bc5
commit f1e457b
Showing
10 changed files
with
253 additions
and
1 deletion.
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
30 changes: 30 additions & 0 deletions
30
modules/cli-options/src/main/scala/scala/cli/commands/default/DefaultOptions.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,30 @@ | ||
package scala.cli.commands.default | ||
|
||
import caseapp._ | ||
|
||
import scala.cli.commands.LoggingOptions | ||
|
||
// format: off | ||
final case class DefaultOptions( | ||
@Recurse | ||
logging: LoggingOptions = LoggingOptions(), | ||
@Group("Default") | ||
@HelpMessage("Write result to files rather than to stdout") | ||
write: Boolean = false, | ||
@Group("Default") | ||
@HelpMessage("List available default files") | ||
list: Boolean = false, | ||
@Group("Default") | ||
@HelpMessage("List available default file ids") | ||
listIds: Boolean = false, | ||
@Group("Default") | ||
@HelpMessage("Force overwriting destination files") | ||
@ExtraName("f") | ||
force: Boolean = false | ||
) | ||
// format: on | ||
|
||
object DefaultOptions { | ||
implicit lazy val parser: Parser[DefaultOptions] = Parser.derive | ||
implicit lazy val help: Help[DefaultOptions] = Help.derive | ||
} |
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
108 changes: 108 additions & 0 deletions
108
modules/cli/src/main/scala/scala/cli/commands/default/Default.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,108 @@ | ||
package scala.cli.commands.default | ||
|
||
import caseapp.core.RemainingArgs | ||
|
||
import java.io.File | ||
|
||
import scala.build.Logger | ||
import scala.cli.commands.ScalaCommand | ||
import scala.cli.commands.util.CommonOps._ | ||
import scala.cli.internal.Constants | ||
import scala.util.Using | ||
|
||
object Default extends ScalaCommand[DefaultOptions] { | ||
|
||
private def readDefaultFile(path: String): Array[Byte] = { | ||
val resourcePath = Constants.defaultFilesResourcePath + "/" + path | ||
val cl = Thread.currentThread().getContextClassLoader | ||
val resUrl = cl.getResource(resourcePath) | ||
if (resUrl == null) | ||
sys.error(s"Should not happen - resource $resourcePath not found") | ||
Using.resource(resUrl.openStream())(_.readAllBytes()) | ||
} | ||
|
||
final case class DefaultFile( | ||
path: os.SubPath, | ||
content: () => Array[Byte] | ||
) { | ||
def printablePath = path.segments.mkString(File.separator) | ||
} | ||
|
||
def defaultWorkflow: Array[Byte] = | ||
readDefaultFile("workflows/default.yml") | ||
def defaultGitignore: Array[Byte] = | ||
readDefaultFile("gitignore") | ||
|
||
val defaultFiles = Map( | ||
"workflow" -> DefaultFile(os.sub / ".github" / "workflows" / "ci.yml", () => defaultWorkflow), | ||
"gitignore" -> DefaultFile(os.sub / ".gitignore", () => defaultGitignore) | ||
) | ||
val defaultFilesByRelPath = defaultFiles.flatMap { | ||
case (_, d) => | ||
// d.path.toString and d.printablePath differ on Windows (one uses '/', the other '\') | ||
Seq( | ||
d.path.toString -> d, | ||
d.printablePath -> d | ||
) | ||
} | ||
|
||
private def unrecognizedFile(name: String, logger: Logger): Nothing = { | ||
logger.error( | ||
s"Error: unrecognized default file $name (available: ${defaultFiles.keys.toVector.sorted.mkString(", ")})" | ||
) | ||
sys.exit(1) | ||
} | ||
|
||
def run(options: DefaultOptions, args: RemainingArgs): Unit = { | ||
|
||
val logger = options.logging.logger | ||
|
||
lazy val allArgs = { | ||
val l = args.all | ||
if (l.isEmpty) { | ||
logger.error("No default file asked") | ||
sys.exit(1) | ||
} | ||
l | ||
} | ||
|
||
if (options.list || options.listIds) | ||
for ((name, d) <- defaultFiles.toVector.sortBy(_._1)) { | ||
if (options.listIds) | ||
println(name) | ||
if (options.list) | ||
println(d.printablePath) | ||
} | ||
else if (options.write) | ||
for (arg <- allArgs) | ||
defaultFiles.get(arg).orElse(defaultFilesByRelPath.get(arg)) match { | ||
case Some(f) => | ||
val dest = os.pwd / f.path | ||
if (!options.force && os.exists(dest)) { | ||
logger.error( | ||
s"Error: ${f.path} already exists. Pass --force to force erasing it." | ||
) | ||
sys.exit(1) | ||
} | ||
if (options.force) | ||
os.write.over(dest, f.content(), createFolders = true) | ||
else | ||
os.write(dest, f.content(), createFolders = true) | ||
logger.message(s"Wrote ${f.path}") | ||
case None => | ||
unrecognizedFile(arg, logger) | ||
} | ||
else { | ||
if (allArgs.length > 1) { | ||
logger.error(s"Error: expected only one argument, got ${allArgs.length}") | ||
sys.exit(1) | ||
} | ||
|
||
val arg = allArgs.head | ||
val f = defaultFiles.get(arg).orElse(defaultFilesByRelPath.get(arg)).getOrElse { | ||
unrecognizedFile(arg, logger) | ||
} | ||
System.out.write(f.content()) | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
modules/integration/src/test/scala/scala/cli/integration/DefaultTests.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,25 @@ | ||
package scala.cli.integration | ||
|
||
import com.eed3si9n.expecty.Expecty.expect | ||
|
||
class DefaultTests extends munit.FunSuite { | ||
|
||
test("Print .gitignore") { | ||
val res = os.proc(TestUtil.cli, "default", ".gitignore") | ||
.call() | ||
val output = res.out.text() | ||
expect(output.linesIterator.toVector.contains("/.scala-build/")) | ||
} | ||
|
||
test("Write .gitignore") { | ||
TestInputs(Nil).fromRoot { root => | ||
os.proc(TestUtil.cli, "default", ".gitignore", "--write") | ||
.call(cwd = root, stdout = os.Inherit) | ||
val dest = root / ".gitignore" | ||
expect(os.isFile(dest)) | ||
val content = os.read(dest) | ||
expect(content.linesIterator.toVector.contains("/.scala-build/")) | ||
} | ||
} | ||
|
||
} |
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,4 @@ | ||
{ | ||
"label": "Miscellaneous", | ||
"position": 17 | ||
} |
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,28 @@ | ||
--- | ||
title: Default | ||
sidebar_position: 1 | ||
--- | ||
|
||
The `default` sub-command provides sensible default content for files | ||
such as `.gitignore` or for GitHub actions workflows, for Scala CLI projects. | ||
|
||
To list the available files, pass it `--list`: | ||
```text | ||
$ scala-cli default --list | ||
.gitignore | ||
.github/workflows/ci.yml | ||
``` | ||
|
||
Get the content of a default file with | ||
```text | ||
$ scala-cli default .gitignore | ||
/.bsp/ | ||
/.scala-build/ | ||
``` | ||
|
||
Optionally, write the content of one or more default files by passing `--write`: | ||
```text | ||
$ scala-cli default --write .gitignore .github/workflows/ci.yml | ||
Wrote .gitignore | ||
Wrote .github/workflows/ci.yml | ||
``` |
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