Skip to content

Commit

Permalink
chore: fix translateConfig test + related touchups
Browse files Browse the repository at this point in the history
  • Loading branch information
tydeu committed May 24, 2024
1 parent c13a0b0 commit 4a801a6
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 18 deletions.
6 changes: 6 additions & 0 deletions src/lake/Lake/CLI/Help.lean
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ USAGE:
Exits with code 0 if the workspace's root package has a properly
configured lint driver. Errors (with code 1) otherwise.
Does NOT verify that the configured test driver actually exists in the
package or its dependencies. It merely verifies that one is specified.
"

def helpLint :=
Expand All @@ -194,6 +197,9 @@ USAGE:
Exits with code 0 if the workspace's root package has a properly
configured lint driver. Errors (with code 1) otherwise.
Does NOT verify that the configured lint driver actually exists in the
package or its dependencies. It merely verifies that one is specified.
"

def helpUpload :=
Expand Down
11 changes: 5 additions & 6 deletions src/lake/Lake/CLI/Main.lean
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ protected def test : CliM PUnit := do

protected def checkTest : CliM PUnit := do
processOptions lakeOption
let wsloadWorkspace ( ← mkLoadConfig (← getThe LakeOptions))
noArgsRem do exit <| if ws.root.testDriver.isEmpty then 1 else 0
let pkgloadPackage (← mkLoadConfig (← getThe LakeOptions))
noArgsRem do exit <| if pkg.testDriver.isEmpty then 1 else 0

protected def lint : CliM PUnit := do
processOptions lakeOption
Expand All @@ -366,8 +366,8 @@ protected def lint : CliM PUnit := do

protected def checkLint : CliM PUnit := do
processOptions lakeOption
let wsloadWorkspace ( ← mkLoadConfig (← getThe LakeOptions))
noArgsRem do exit <| if ws.root.lintDriver.isEmpty then 1 else 0
let pkgloadPackage (← mkLoadConfig (← getThe LakeOptions))
noArgsRem do exit <| if pkg.lintDriver.isEmpty then 1 else 0

protected def clean : CliM PUnit := do
processOptions lakeOption
Expand Down Expand Up @@ -452,8 +452,7 @@ protected def translateConfig : CliM PUnit := do
let lang ← parseLangSpec (← takeArg "configuration language")
let outFile? := (← takeArg?).map FilePath.mk
noArgsRem do
Lean.searchPathRef.set cfg.lakeEnv.leanSearchPath
let (pkg, _) ← loadPackage "[root]" cfg
let pkg ← loadPackage cfg
let outFile := outFile?.getD <| pkg.configFile.withExtension lang.fileExtension
if (← outFile.pathExists) then
throw (.outputConfigExists outFile)
Expand Down
12 changes: 7 additions & 5 deletions src/lake/Lake/CLI/Translate/Lean.lean
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ def LeanConfig.addDeclFields (cfg : LeanConfig) (fs : Array DeclField) : Array D
@[inline] def mkDeclValWhere? (fields : Array DeclField) : Option (TSyntax ``declValWhere) :=
if fields.isEmpty then none else Unhygienic.run `(declValWhere|where $fields*)

def PackageConfig.mkSyntax (cfg : PackageConfig) : PackageDecl := Unhygienic.run do
let declVal? := mkDeclValWhere? <|Array.empty
def PackageConfig.mkSyntax (cfg : PackageConfig)
(testDriver := cfg.testDriver) (lintDriver := cfg.lintDriver)
: PackageDecl := Unhygienic.run do
let declVal? := mkDeclValWhere? <| Array.empty
|> addDeclFieldD `precompileModules cfg.precompileModules false
|> addDeclFieldD `moreGlobalServerArgs cfg.moreGlobalServerArgs #[]
|> addDeclFieldD `srcDir cfg.srcDir "."
Expand All @@ -102,9 +104,9 @@ def PackageConfig.mkSyntax (cfg : PackageConfig) : PackageDecl := Unhygienic.run
|> addDeclField? `releaseRepo (cfg.releaseRepo <|> cfg.releaseRepo?)
|> addDeclFieldD `buildArchive (cfg.buildArchive?.getD cfg.buildArchive) (defaultBuildArchive cfg.name)
|> addDeclFieldD `preferReleaseBuild cfg.preferReleaseBuild false
|> addDeclFieldD `testDriver cfg.testDriver ""
|> addDeclFieldD `testDriver testDriver ""
|> addDeclFieldD `testDriverArgs cfg.testDriverArgs #[]
|> addDeclFieldD `lintDriver cfg.lintDriver ""
|> addDeclFieldD `lintDriver lintDriver ""
|> addDeclFieldD `lintDriverArgs cfg.lintDriverArgs #[]
|> cfg.toWorkspaceConfig.addDeclFields
|> cfg.toLeanConfig.addDeclFields
Expand Down Expand Up @@ -176,7 +178,7 @@ protected def Dependency.mkSyntax (cfg : Dependency) : RequireDecl := Unhygienic
/-- Create a Lean module that encodes the declarative configuration of the package. -/
def Package.mkLeanConfig (pkg : Package) : TSyntax ``module := Unhygienic.run do
let defaultTargets := pkg.defaultTargets.foldl NameSet.insert NameSet.empty
let pkgConfig := pkg.config.mkSyntax
let pkgConfig := pkg.config.mkSyntax pkg.testDriver pkg.lintDriver
let requires := pkg.depConfigs.map (·.mkSyntax)
let leanLibs := pkg.leanLibConfigs.toArray.map fun cfg =>
cfg.mkSyntax (defaultTargets.contains cfg.name)
Expand Down
11 changes: 8 additions & 3 deletions src/lake/Lake/Load/Main.lean
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def configFileExists (cfgFile : FilePath) : BaseIO Bool :=
leanFile.pathExists <||> tomlFile.pathExists

/-- Loads a Lake package configuration (either Lean or TOML). -/
def loadPackage
def loadPackageCore
(name : String) (cfg : LoadConfig)
: LogIO (Package × Option Environment) := do
if let some ext := cfg.relConfigFile.extension then
Expand Down Expand Up @@ -88,7 +88,7 @@ def loadDepPackage
(dep : MaterializedDep) (leanOpts : Options) (reconfigure : Bool)
: StateT Workspace LogIO Package := fun ws => do
let name := dep.name.toString (escape := false)
let (pkg, env?) ← loadPackage name {
let (pkg, env?) ← loadPackageCore name {
lakeEnv := ws.lakeEnv
wsDir := ws.dir
relPkgDir := dep.relPkgDir
Expand All @@ -110,7 +110,7 @@ Does not resolve dependencies.
-/
def loadWorkspaceRoot (config : LoadConfig) : LogIO Workspace := do
Lean.searchPathRef.set config.lakeEnv.leanSearchPath
let (root, env?) ← loadPackage "[root]" config
let (root, env?) ← loadPackageCore "[root]" config
let ws : Workspace := {
root, lakeEnv := config.lakeEnv
moduleFacetConfigs := initModuleFacetConfigs
Expand All @@ -122,6 +122,11 @@ def loadWorkspaceRoot (config : LoadConfig) : LogIO Workspace := do
else
return ws

/-- Loads a Lake package as a single independent object (without dependencies). -/
def loadPackage (config : LoadConfig) : LogIO Package := do
Lean.searchPathRef.set config.lakeEnv.leanSearchPath
(·.1) <$> loadPackageCore "[root]" config

/-- Recursively visits a package dependency graph, avoiding cycles. -/
private def resolveDepsAcyclic
[Monad m] [MonadError m]
Expand Down
4 changes: 3 additions & 1 deletion src/lake/tests/translateConfig/out.expected.lean
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ open System Lake DSL
package test where
releaseRepo := ""
buildArchive := ""
testDriver := "b"
lintDriver := "b"
platformIndependent := true

require foo from "-" with Lake.NameMap.empty |>.insert `foo "bar"
Expand All @@ -13,4 +15,4 @@ require bar from git "https://example.com"@"abc"/"sub/dir"

@[default_target] lean_lib A

@[test_runner] lean_exe b
lean_exe b
3 changes: 2 additions & 1 deletion src/lake/tests/translateConfig/out.expected.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name = "test"
testRunner = "b"
testDriver = "b"
lintDriver = "b"
defaultTargets = ["A"]

[leanOptions]
Expand Down
3 changes: 2 additions & 1 deletion src/lake/tests/translateConfig/source.lean
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ package test where
preferReleaseBuild := false
packagesDir := defaultPackagesDir
leanOptions := #[⟨`pp.unicode.fun, true⟩]
lintDriver := "b"

require foo from "dir" with NameMap.empty.insert `foo "bar"
require bar from git "https://example.com" @ "abc" / "sub" / "dir"
Expand All @@ -45,7 +46,7 @@ lean_lib A where
nativeFacets := fun _ => #[]
toLeanConfig := testLeanConfig

@[test_runner]
@[test_driver]
lean_exe b where
srcDir := "."
root := `b
Expand Down
3 changes: 2 additions & 1 deletion src/lake/tests/translateConfig/source.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
testRunner = "b"
testDriver = "b"
lintDriver = "b"
defaultTargets = ['A']

name = "test"
Expand Down

0 comments on commit 4a801a6

Please sign in to comment.