sbt: cache `passwordCommand` output

This will cache the output of `passwordCommand` per authentication
realm.

Context: the `credentials` key in `sbt` is a `TaskKey[Seq[Credentials]]`.
In `sbt`, tasks are evaluated on-demand and their output is not cached.
This particular key is referenced by all submodules in a project. When
the command is relatively expensive (e.g.: `pass show foo`), this
results in several seconds of delay when doing basic things like
`compile` or `test` which makes this unusable without some kind of
caching.
This commit is contained in:
Philippe Laflamme 2022-10-02 21:26:14 -04:00 committed by Robert Helgesson
parent 599e22b1c7
commit 7fee13eb4c
No known key found for this signature in database
GPG Key ID: 36BDAA14C2797E89
2 changed files with 11 additions and 6 deletions

View File

@ -8,13 +8,16 @@ let
addSbtPlugin("${plugin.org}" % "${plugin.artifact}" % "${plugin.version}")
'';
renderCredential = cred: ''
credentials += Credentials("${cred.realm}", "${cred.host}", "${cred.user}", "${cred.passwordCommand}".!!.trim)
'';
renderCredential = idx: cred:
let symbol = "credential_${toString idx}";
in ''
lazy val ${symbol} = "${cred.passwordCommand}".!!.trim
credentials += Credentials("${cred.realm}", "${cred.host}", "${cred.user}", ${symbol})
'';
renderCredentials = creds: ''
import scala.sys.process._
${concatStrings (map renderCredential creds)}'';
${concatStrings (imap0 renderCredential creds)}'';
renderRepository = value:
if isString value then ''

View File

@ -19,8 +19,10 @@ let
];
expectedCredentialsSbt = pkgs.writeText "credentials.sbt" ''
import scala.sys.process._
credentials += Credentials("Sonatype Nexus Repository Manager", "example.com", "user", "echo password".!!.trim)
credentials += Credentials("Sonatype Nexus Repository Manager X", "v2.example.com", "user1", "echo password1".!!.trim)
lazy val credential_0 = "echo password".!!.trim
credentials += Credentials("Sonatype Nexus Repository Manager", "example.com", "user", credential_0)
lazy val credential_1 = "echo password1".!!.trim
credentials += Credentials("Sonatype Nexus Repository Manager X", "v2.example.com", "user1", credential_1)
'';
credentialsSbtPath = ".sbt/1.0/credentials.sbt";
in {