| /- | |
| Copyright (c) 2021 Mac Malone. All rights reserved. | |
| Released under Apache 2.0 license as described in the file LICENSE. | |
| Authors: Mac Malone | |
| -/ | |
| module | |
| prelude | |
| public import Lake.Util.Exit | |
| public import Lake.Load.Config | |
| public import Lake.CLI.Error | |
| public import Lake.CLI.Shake | |
| public import Lake.CLI.Check | |
| import Lake.Version | |
| import Lake.Build.Run | |
| import Lake.Build.Infos | |
| import Lake.Build.Targets | |
| import Lake.Build.Target.Fetch | |
| import Lake.Load.Package | |
| import Lake.Load.Workspace | |
| import Lake.Load.Toml | |
| import Lake.Util.IO | |
| import Lake.Util.Git | |
| import Lake.Util.MainM | |
| import Lake.Util.Cli | |
| import Lake.CLI.Init | |
| import Lake.CLI.Help | |
| import Lake.CLI.Build | |
| import LeanExport.Basic | |
| import Lake.CLI.Actions | |
| import Lake.CLI.Translate | |
| import Lake.CLI.Serve | |
| public import Lake.CLI.BuiltinLint | |
| import Lake.CLI.Samply | |
| import Init.Data.String.Modify | |
| -- # CLI | |
| open System | |
| open Lean (Json toJson fromJson? NameMap) | |
| namespace Lake | |
| /-! ## General options for top-level `lake` -/ | |
| public structure LakeOptions where | |
| args : List String := [] | |
| rootDir : FilePath := "." | |
| configFile : FilePath := defaultConfigFile | |
| elanInstall? : Option ElanInstall := none | |
| leanInstall? : Option LeanInstall := none | |
| lakeInstall? : Option LakeInstall := none | |
| configOpts : NameMap String := {} | |
| packageOverrides : Array PackageEntry := #[] | |
| subArgs : List String := [] | |
| wantsHelp : Bool := false | |
| verbosity : Verbosity := .normal | |
| updateDeps : Bool := false | |
| updateToolchain : Bool := true | |
| reconfigure : Bool := false | |
| oldMode : Bool := false | |
| trustHash : Bool := true | |
| allowEmpty : Bool := false | |
| noBuild : Bool := false | |
| failFast : Bool := false | |
| noCache : Option Bool := none | |
| failLv : LogLevel := .error | |
| outLv? : Option LogLevel := .none | |
| ansiMode : AnsiMode := .auto | |
| outFormat : OutFormat := .text | |
| offline : Bool := false | |
| outputsFile? : Option FilePath := none | |
| overwrite? : Option Bool := none | |
| forceDownload : Bool := false | |
| mappingsOnly : Bool := false | |
| service? : Option String := none | |
| package? : Option String := none | |
| scope? : Option CacheServiceScope := none | |
| platform? : Option CachePlatform := none | |
| toolchain? : Option CacheToolchain := none | |
| rev? : Option GitRev := none | |
| maxRevs : Nat := 100 | |
| shake : Shake.Args := {} | |
| comparatorConfig? : Option FilePath := none | |
| /-- File received via `lake check --from-export` -/ | |
| checkFromExport? : Option FilePath := none | |
| /-- File received via `lake comparator --challenge-from-export` -/ | |
| challengeFromExport? : Option FilePath := none | |
| /-- File received via `lake comparator --solution-from-export` -/ | |
| solutionFromExport? : Option FilePath := none | |
| /-- Whether `lake check` and `lake comparator` run every bundled checker (via `--paranoid`). -/ | |
| paranoid : Bool := false | |
| /-- Whether `lake check` and `lake comparator` should disable their sandbox (very insecure) -/ | |
| inadvisablyNoSandbox : Bool := false | |
| builtinLint : BuiltinLint.Args := {} | |
| /-- Whether `lake lint` should also run builtin lints (via `--builtin-lint`). -/ | |
| runBuiltinLint : Bool := false | |
| /-- Whether `lake lint` should skip the lint driver (via `--builtin-only`). -/ | |
| builtinOnly : Bool := false | |
| samplyRaw : Bool := false | |
| samplyNoServe : Bool := false | |
| def LakeOptions.outLv (opts : LakeOptions) : LogLevel := | |
| opts.outLv?.getD opts.verbosity.minLogLv | |
| @[inline] def LakeOptions.toLogConfig (opts : LakeOptions) : LogConfig where | |
| failLv := opts.failLv | |
| outLv := opts.outLv | |
| ansiMode := opts.ansiMode | |
| out := .stderr | |
| /-- Get the Lean installation. Error if missing. -/ | |
| def LakeOptions.getLeanInstall (opts : LakeOptions) : Except CliError LeanInstall := | |
| match opts.leanInstall? with | |
| | none => .error CliError.unknownLeanInstall | |
| | some lean => .ok lean | |
| /-- Get the Lake installation. Error if missing. -/ | |
| def LakeOptions.getLakeInstall (opts : LakeOptions) : Except CliError LakeInstall := | |
| match opts.lakeInstall? with | |
| | none => .error CliError.unknownLakeInstall | |
| | some lake => .ok lake | |
| /-- Get the Lean and Lake installation. Error if either is missing. -/ | |
| def LakeOptions.getInstall (opts : LakeOptions) : Except CliError (LeanInstall × LakeInstall) := do | |
| return (← opts.getLeanInstall, ← opts.getLakeInstall) | |
| /-- Compute the Lake environment based on `opts`. Error if an install is missing. -/ | |
| def LakeOptions.computeEnv (opts : LakeOptions) : EIO CliError Lake.Env := do | |
| Env.compute (← opts.getLakeInstall) (← opts.getLeanInstall) opts.elanInstall? | |
| opts.noCache |>.adapt fun msg => .invalidEnv msg | |
| /-- Make a `LoadConfig` from a `LakeOptions`. -/ | |
| public def LakeOptions.mkLoadConfig (opts : LakeOptions) : EIO CliError LoadConfig := do | |
| let some wsDir ← resolvePath? opts.rootDir | |
| | throw <| .missingRootDir opts.rootDir | |
| return { | |
| lakeArgs? := opts.args.toArray | |
| lakeEnv := ← opts.computeEnv | |
| wsDir | |
| relConfigFile := opts.configFile | |
| packageOverrides := opts.packageOverrides | |
| lakeOpts := opts.configOpts | |
| leanOpts := Lean.Options.empty | |
| reconfigure := opts.reconfigure | |
| updateDeps := opts.updateDeps | |
| updateToolchain := opts.updateToolchain | |
| } | |
| /-- Make a `BuildConfig` from a `LakeOptions`. -/ | |
| def LakeOptions.mkBuildConfig | |
| (opts : LakeOptions) (out := OutStream.stderr) (showSuccess := false) | |
| (outputsPackage? : Option Package := none) | |
| : BuildConfig where | |
| oldMode := opts.oldMode | |
| trustHash := opts.trustHash | |
| noBuild := opts.noBuild | |
| failFast := opts.failFast | |
| verbosity := opts.verbosity | |
| failLv := opts.failLv | |
| outLv := opts.outLv | |
| ansiMode := opts.ansiMode | |
| outputsFile? := opts.outputsFile?.filter fun _ => outputsPackage?.isSome | |
| outputsIdx := outputsPackage?.elim 0 (·.wsIdx) | |
| out; showSuccess | |
| export LakeOptions (mkLoadConfig mkBuildConfig) | |
| /-! ## Monad -/ | |
| abbrev CliMainM := ExceptT CliError MainM | |
| abbrev CliStateM := StateT LakeOptions CliMainM | |
| abbrev CliM := ArgsT CliStateM | |
| def CliM.run (self : CliM α) (args : List String) : BaseIO ExitCode := do | |
| let (elanInstall?, leanInstall?, lakeInstall?) ← findInstall? | |
| let main := self.run' args |>.run' {args, elanInstall?, leanInstall?, lakeInstall?} | |
| let main := main.run >>= fun | .ok a => pure a | .error e => error e.toString | |
| main.run | |
| def CliStateM.runLogIO (x : LogIO α) : CliStateM α := do | |
| MainM.runLogIO x (← get).toLogConfig | |
| instance (priority := low) : MonadLift LogIO CliStateM := ⟨CliStateM.runLogIO⟩ | |
| def CliStateM.runLoggerIO (x : LoggerIO α) : CliStateM α := do | |
| MainM.runLoggerIO x (← get).toLogConfig | |
| instance (priority := low) : MonadLift LoggerIO CliStateM := ⟨CliStateM.runLoggerIO⟩ | |
| /-! ## Argument Parsing -/ | |
| def takeArg (arg : String) : CliM String := do | |
| match (← takeArg?) with | |
| | none => throw <| CliError.missingArg arg | |
| | some arg => pure arg | |
| def takeOptArg (opt arg : String) : CliM String := do | |
| match (← takeArg?) with | |
| | none => throw <| CliError.missingOptArg opt arg | |
| | some arg => pure arg | |
| @[inline] def takeOptArg' (opt arg : String) (f : String → Option α) : CliM α := do | |
| if let some a := f (← takeOptArg opt arg) then return a | |
| throw <| CliError.invalidOptArg opt arg | |
| /-- | |
| Verify that there are no CLI arguments remaining | |
| before running the given action. | |
| -/ | |
| def noArgsRem (act : CliStateM α) : CliM α := do | |
| let args ← getArgs | |
| if args.isEmpty then act else | |
| throw <| CliError.unexpectedArguments args | |
| /-! ## Option Parsing -/ | |
| def getWantsHelp : CliStateM Bool := | |
| (·.wantsHelp) <$> get | |
| def setConfigOpt (kvPair : String) : CliM PUnit := | |
| let pos := kvPair.find '=' | |
| let (key, val) := | |
| if h : pos.IsAtEnd then | |
| (kvPair.toName, "") | |
| else | |
| (kvPair.extract kvPair.startPos pos |>.toName, kvPair.extract (pos.next h) kvPair.endPos) | |
| modifyThe LakeOptions fun opts => | |
| {opts with configOpts := opts.configOpts.insert key val} | |
| def lakeShortOption : (opt : Char) → CliM PUnit | |
| | 'q' => modifyThe LakeOptions ({· with verbosity := .quiet}) | |
| | 'v' => modifyThe LakeOptions ({· with verbosity := .verbose}) | |
| | 'd' => do let rootDir ← takeOptArg "-d" "path"; modifyThe LakeOptions ({· with rootDir}) | |
| | 'f' => do let configFile ← takeOptArg "-f" "path"; modifyThe LakeOptions ({· with configFile}) | |
| | 'o' => do let outputsFile? ← takeOptArg "-o" "path"; modifyThe LakeOptions ({· with outputsFile?}) | |
| | 'K' => do setConfigOpt <| ← takeOptArg "-K" "key-value pair" | |
| | 'U' => do | |
| logWarning "the '-U' shorthand for '--update' is deprecated" | |
| modifyThe LakeOptions ({· with updateDeps := true}) | |
| | 'R' => modifyThe LakeOptions ({· with reconfigure := true}) | |
| | 'h' => modifyThe LakeOptions ({· with wantsHelp := true}) | |
| | 'H' => modifyThe LakeOptions ({· with trustHash := false}) | |
| | 'J' => modifyThe LakeOptions ({· with outFormat := .json}) | |
| | opt => throw <| CliError.unknownShortOption opt | |
| /-- Returns an error if the string is not valid GitHub repository name. -/ | |
| -- Limitations derived from https://github.com/dead-claudia/github-limits | |
| def validateRepo? (repo : String) : Option String := Id.run do | |
| unless repo.all isValidRepoChar do | |
| return "invalid characters in repository name" | |
| match repo.split '/' |>.toStringList with | |
| | [owner, name] => | |
| if owner.lengthAssumingAscii > 39 then | |
| return "invalid repository name; owner must be at most 390 characters long" | |
| if name.lengthAssumingAscii > 100 then | |
| return "invalid repository name; owner must be at most 100 characters long" | |
| | _ => return "invalid repository name; must contain exactly one '/'" | |
| return none | |
| where | |
| /-- Returns whether `c` is a valid character in GitHub repository name -/ | |
| isValidRepoChar (c : Char) : Bool := | |
| c.isAlphanum || c == '-' || c == '_' || c == '.' || c == '/' | |
| def flushLinterOptions : CliM PUnit := do | |
| modifyThe LakeOptions fun opts => | |
| { opts with builtinLint.linterOverrides := #[] } | |
| def modifyLintOnlyFlag (b : Bool) : CliM PUnit := do | |
| modifyThe LakeOptions fun opts => | |
| { opts with builtinLint := {opts.builtinLint with lintOnly := b} } | |
| /-- | |
| Parses a comma-separated list of Boolean-valued `Lean.Option` names. | |
| If the name is prefixed with `-`, this means that option will be set to `false`. | |
| Otherwise, it will be `true`. | |
| -/ | |
| def parseLintersSpec (spec : String) : CliM PUnit := do | |
| let mut entries : Array (Lean.Name × Bool) := #[] | |
| for raw in spec.split (· == ',') do | |
| let mut s := raw.trimAscii | |
| let mut optionValue := true | |
| if s.isEmpty then continue | |
| if s.startsWith "-" then | |
| s := (s.drop 1).trimAscii | |
| optionValue := false | |
| if s.startsWith "." then | |
| s := "linter" ++ s | |
| entries := entries.push (s.toName, optionValue) | |
| modifyThe LakeOptions fun opts => | |
| { opts with runBuiltinLint := true, builtinLint.linterOverrides := | |
| opts.builtinLint.linterOverrides ++ entries } | |
| def setShakeAddDownstream (opt : String) (isExported : Bool) : CliM PUnit := do | |
| let mod ← takeOptArg opt "module to add" | |
| if (← getThe LakeOptions).shake.addDownstream?.isSome then | |
| error "at most one of `--add-downstream` and `--add-public-downstream` can be used, and only once" | |
| modifyThe LakeOptions fun opts => | |
| {opts with shake.addDownstream? := some { module := mod.toName, isExported }} | |
| def lakeLongOption : (opt : String) → CliM PUnit | |
| | "--quiet" => modifyThe LakeOptions ({· with verbosity := .quiet}) | |
| | "--verbose" => modifyThe LakeOptions ({· with verbosity := .verbose}) | |
| | "--update" => modifyThe LakeOptions ({· with updateDeps := true}) | |
| | "--keep-toolchain" => modifyThe LakeOptions ({· with updateToolchain := false}) | |
| | "--reconfigure" => modifyThe LakeOptions ({· with reconfigure := true}) | |
| | "--old" => modifyThe LakeOptions ({· with oldMode := true}) | |
| | "--text" => modifyThe LakeOptions ({· with outFormat := .text}) | |
| | "--json" => modifyThe LakeOptions ({· with outFormat := .json}) | |
| | "--allow-empty" => modifyThe LakeOptions ({· with allowEmpty := true}) | |
| | "--no-build" => modifyThe LakeOptions ({· with noBuild := true}) | |
| | "--fail-fast" => modifyThe LakeOptions ({· with failFast := true}) | |
| | "--no-cache" => modifyThe LakeOptions ({· with noCache := true}) | |
| | "--try-cache" => modifyThe LakeOptions ({· with noCache := false}) | |
| | "--rehash" => modifyThe LakeOptions ({· with trustHash := false}) | |
| | "--offline" => modifyThe LakeOptions ({· with offline := true}) | |
| | "--wfail" => modifyThe LakeOptions ({· with failLv := .warning}) | |
| | "--iofail" => modifyThe LakeOptions ({· with failLv := .info}) | |
| | "--no-overwrite" => modifyThe LakeOptions ({· with overwrite? := some false}) | |
| | "--force-overwrite" => modifyThe LakeOptions ({· with overwrite? := some true}) | |
| | "--force-download" => modifyThe LakeOptions ({· with forceDownload := true}) | |
| | "--download-arts" => modifyThe LakeOptions ({· with mappingsOnly := false}) | |
| | "--mappings-only" => modifyThe LakeOptions ({· with mappingsOnly := true}) | |
| | "--package" => do | |
| let pkgName ← takeOptArg "--package" "package name" | |
| modifyThe LakeOptions ({· with package? := some pkgName}) | |
| | "--service" => do | |
| let service ← takeOptArg "--service" "service name" | |
| modifyThe LakeOptions ({· with service? := some service}) | |
| | "--scope" => do | |
| let scope ← takeOptArg "--scope" "cache scope" | |
| modifyThe LakeOptions ({· with scope? := some (.ofString scope)}) | |
| | "--repo" => do | |
| let repo ← takeOptArg "--repo" "GitHub repository" | |
| if let some e := validateRepo? repo then error e | |
| modifyThe LakeOptions ({· with scope? := some (.ofRepo repo)}) | |
| | "--platform" => do | |
| let platform ← takeOptArg "--platform" "cache platform" | |
| if platform.chars.length > 100 then | |
| error "invalid platform; platform is expected to be at most 100 characters long" | |
| modifyThe LakeOptions ({· with platform? := some <| .ofString platform}) | |
| | "--toolchain" => do | |
| let toolchain ← takeOptArg "--toolchain" "cache toolchain" | |
| let toolchain := if toolchain.isEmpty then .none else .ofString toolchain | |
| if toolchain.length > 256 then | |
| error "invalid toolchain version; toolchain is expected to be at most 256 characters long" | |
| modifyThe LakeOptions ({· with toolchain? := some toolchain}) | |
| | "--rev" => do | |
| let rev ← takeOptArg "--rev" "Git revision" | |
| modifyThe LakeOptions ({· with rev? := some rev}) | |
| | "--max-revs" => do | |
| let some n ← (·.toNat?) <$> takeOptArg "--max-revs" "number of revisions" | |
| | error "argument to `--max-revs` should be a natural number" | |
| modifyThe LakeOptions ({· with maxRevs := n}) | |
| | "--log-level" => do | |
| let outLv ← takeOptArg' "--log-level" "log level" LogLevel.ofString? | |
| modifyThe LakeOptions ({· with outLv? := outLv}) | |
| | "--fail-level" => do | |
| let failLv ← takeOptArg' "--fail-level" "log level" LogLevel.ofString? | |
| modifyThe LakeOptions ({· with failLv}) | |
| | "--ansi" => modifyThe LakeOptions ({· with ansiMode := .ansi}) | |
| | "--no-ansi" => modifyThe LakeOptions ({· with ansiMode := .noAnsi}) | |
| | "--packages" => do | |
| let file ← takeOptArg "--packages" "package overrides file" | |
| let overrides ← Manifest.loadEntries file | |
| modifyThe LakeOptions fun opts => | |
| {opts with packageOverrides := opts.packageOverrides ++ overrides} | |
| | "--dir" => do | |
| let rootDir ← takeOptArg "--dir" "path" | |
| modifyThe LakeOptions ({· with rootDir}) | |
| | "--file" => do | |
| let configFile ← takeOptArg "--file" "path" | |
| modifyThe LakeOptions ({· with configFile}) | |
| | "--help" => modifyThe LakeOptions ({· with wantsHelp := true}) | |
| | "--" => do | |
| let subArgs ← takeArgs | |
| modifyThe LakeOptions ({· with subArgs}) | |
| -- Builtin lint options (using any of these implicitly enables --builtin-lint) | |
| | "--builtin-lint" => modifyThe LakeOptions ({· with runBuiltinLint := true}) | |
| | "--builtin-only" => modifyThe LakeOptions ({· with runBuiltinLint := true, builtinOnly := true}) | |
| | "--record-exceptions" => | |
| modifyThe LakeOptions ({· with runBuiltinLint := true, builtinLint.mode := .recordExceptions}) | |
| | "--code-quality" => | |
| modifyThe LakeOptions ({· with runBuiltinLint := true, builtinOnly := true, builtinLint.mode := .codeQuality}) | |
| | "--linters" => do | |
| let opts ← getThe LakeOptions | |
| if opts.builtinLint.lintOnly then | |
| flushLinterOptions | |
| modifyLintOnlyFlag false | |
| let spec ← takeOptArg "--linters" "comma-separated linter spec" | |
| parseLintersSpec spec | |
| | "--lint-only" => do | |
| let opts ← getThe LakeOptions | |
| if !opts.builtinLint.lintOnly then | |
| flushLinterOptions | |
| modifyLintOnlyFlag true | |
| let spec ← takeOptArg "--lint-only" "comma-separated linter spec" | |
| parseLintersSpec spec | |
| | "--checks" => do | |
| let spec ← takeOptArg "--checks" "comma-separated module names" | |
| let mut checks : Array Lean.Name := #[] | |
| for raw in spec.split (· == ',') do | |
| let s := raw.trimAscii | |
| unless s.isEmpty do | |
| checks := checks.push s.toName | |
| modifyThe LakeOptions fun opts => | |
| { opts with runBuiltinLint := true, builtinOnly := true, | |
| builtinLint.checks := opts.builtinLint.checks ++ checks, | |
| builtinLint.mode := .codeQuality } | |
| -- Samply options | |
| | "--raw" => modifyThe LakeOptions ({· with samplyRaw := true}) | |
| | "--no-serve" => modifyThe LakeOptions ({· with samplyNoServe := true}) | |
| -- Shared options | |
| | "--force" => modifyThe LakeOptions ({· with shake.force := true}) | |
| -- Shake options | |
| | "--keep-implied" => modifyThe LakeOptions ({· with shake.keepImplied := true}) | |
| | "--keep-prefix" => modifyThe LakeOptions ({· with shake.keepPrefix := true}) | |
| | "--keep-public" => modifyThe LakeOptions ({· with shake.keepPublic := true}) | |
| | "--add-public" => modifyThe LakeOptions ({· with shake.addPublic := true}) | |
| | "--gh-style" => modifyThe LakeOptions ({· with shake.githubStyle := true}) | |
| | "--explain" => modifyThe LakeOptions ({· with shake.explain := true}) | |
| | "--trace" => modifyThe LakeOptions ({· with shake.trace := true}) | |
| | "--fix" => modifyThe LakeOptions ({· with shake.fix := true}) | |
| | "--only" => do | |
| let mod ← takeOptArg "--only" "minimize only this module" | |
| modifyThe LakeOptions fun opts => | |
| {opts with shake.onlyMods := opts.shake.onlyMods.push mod.toName} | |
| | "--add-downstream" => setShakeAddDownstream "--add-downstream" (isExported := false) | |
| | "--add-public-downstream" => setShakeAddDownstream "--add-public-downstream" (isExported := true) | |
| -- Check and comparator options | |
| | "--config" => do | |
| let file ← takeOptArg "--config" "path" | |
| modifyThe LakeOptions ({· with comparatorConfig? := some file}) | |
| | "--from-export" => do | |
| let file ← takeOptArg "--from-export" "path" | |
| modifyThe LakeOptions ({· with checkFromExport? := some file}) | |
| | "--challenge-from-export" => do | |
| let file ← takeOptArg "--challenge-from-export" "path" | |
| modifyThe LakeOptions ({· with challengeFromExport? := some file}) | |
| | "--solution-from-export" => do | |
| let file ← takeOptArg "--solution-from-export" "path" | |
| modifyThe LakeOptions ({· with solutionFromExport? := some file}) | |
| | "--paranoid" => modifyThe LakeOptions ({· with paranoid := true}) | |
| | "--inadvisably-no-sandbox" => modifyThe LakeOptions ({· with inadvisablyNoSandbox := true}) | |
| | opt => throw <| CliError.unknownLongOption opt | |
| def lakeOption := | |
| option { | |
| short := lakeShortOption | |
| long := lakeLongOption | |
| longShort := shortOptionWithArg lakeShortOption | |
| } | |
| /-! ## Actions -/ | |
| /-- Verify the Lean version Lake was built with matches that of the give Lean installation. -/ | |
| def verifyLeanVersion (leanInstall : LeanInstall) : Except CliError PUnit := do | |
| unless leanInstall.githash == Lean.githash do | |
| throw <| CliError.leanRevMismatch Lean.githash leanInstall.githash | |
| /-- Output the detected installs and verify the Lean version. -/ | |
| def verifyInstall (opts : LakeOptions) : ExceptT CliError MainM PUnit := do | |
| IO.println s!"Elan:\n{repr <| opts.elanInstall?}" | |
| IO.println s!"Lean:\n{repr <| opts.leanInstall?}" | |
| IO.println s!"Lake:\n{repr <| opts.lakeInstall?}" | |
| let (leanInstall, _) ← opts.getInstall | |
| verifyLeanVersion leanInstall | |
| def parseScriptSpec (ws : Workspace) (spec : String) : Except CliError Script := | |
| match spec.split '/' |>.toStringList with | |
| | [scriptName] => | |
| match ws.findScript? (stringToLegalOrSimpleName scriptName) with | |
| | some script => return script | |
| | none => throw <| CliError.unknownScript spec | |
| | [pkg, scriptName] => do | |
| let pkg ← parsePackageSpec ws pkg | |
| match pkg.scripts.find? (stringToLegalOrSimpleName scriptName) with | |
| | some script => return script | |
| | none => throw <| CliError.unknownScript spec | |
| | _ => throw <| CliError.invalidScriptSpec spec | |
| def parseTemplateSpec (spec : String) : Except CliError InitTemplate := do | |
| if spec.isEmpty then | |
| return default | |
| else if let some tmp := InitTemplate.ofString? spec.toLower then | |
| return tmp | |
| else | |
| throw <| CliError.unknownTemplate spec | |
| def parseLangSpec (spec : String) : Except CliError ConfigLang := | |
| if spec.isEmpty then | |
| return default | |
| else if let some lang := ConfigLang.ofString? spec.toLower then | |
| return lang | |
| else | |
| throw <| CliError.unknownConfigLang spec | |
| def parseTemplateLangSpec (spec : String) : Except CliError (InitTemplate × ConfigLang) := do | |
| match spec.split '.' |>.toStringList with | |
| | [tmp, lang] => return (← parseTemplateSpec tmp, ← parseLangSpec lang) | |
| | [tmp] => return (← parseTemplateSpec tmp, default) | |
| | _ => return default | |
| /-! ## Commands -/ | |
| namespace lake | |
| /-! ### `lake cache` CLI -/ | |
| namespace cache | |
| @[inline] def findUploadCacheService (cfg : LoadedLakeConfig) (service : String) : LoggerIO CacheService := do | |
| cfg.cacheServices.find? (.mkSimple service) |>.getDM do | |
| error <| serviceNotFound service | |
| where serviceNotFound service := | |
| -- Note: skips suggesting the default Reservoir service as it does not support uploads | |
| let services := cfg.config.cache.services | |
| if services.isEmpty then | |
| s!"service not found `{service}`; no upload services configured" | |
| else | |
| let msg := s!"service not found `{service}`; available services:" | |
| services.foldl (init := msg) fun msg name _ => | |
| s!"{msg}\n {name.toString (escape := false)}" | |
| def collectCacheServices (ws : Workspace) : OrdNameMap String := | |
| let map : OrdNameMap String := .empty | |
| let map := ws.lakeConfig.cacheServices.foldl (init := map) fun map name _ => | |
| map.insert name (name.toString (escape := false)) | |
| ws.packages.foldl (init := map) fun map pkg => | |
| pkg.cacheServices.foldl (init := map) fun map name _ => | |
| map.insert name s!"{name.toString (escape := false)} (from {pkg.prettyName})" | |
| def serviceNotFound (ws : Workspace) (service : String) : String := | |
| let msg := s!"service not found `{service}`; configured services:" | |
| collectCacheServices ws |>.foldl (init := msg) fun msg name => | |
| s!"{msg}\n {name}" | |
| @[inline] def _root_.Lake.Workspace.findCacheService (ws : Workspace) (service : String) : LoggerIO CacheService := do | |
| ws.findCacheService? service |>.getDM (error <| serviceNotFound ws service) | |
| @[inline] def _root_.Lake.Workspace.validateCacheService (ws : Workspace) (service : String) : LoggerIO Unit := do | |
| unless (ws.findCacheService? service).isSome do | |
| error (serviceNotFound ws service) | |
| @[inline] def cacheToolchain (pkg : Package) (toolchain : CacheToolchain) : CacheToolchain := | |
| if pkg.fixedToolchain || pkg.bootstrap then .none else toolchain | |
| @[inline] def cachePlatform (pkg : Package) (platform : CachePlatform) : CachePlatform := | |
| if pkg.isPlatformIndependent then .none else platform | |
| -- since 2026-02-19 | |
| def endpointDeprecation : String := | |
| "configuring the cache service via environment variables is deprecated; use --service instead" | |
| protected def get : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let mappings? ← takeArg? | |
| noArgsRem do | |
| let cfg ← mkLoadConfig opts | |
| let ws ← loadWorkspace cfg | |
| let cache := ws.lakeCache | |
| let overwrite := opts.overwrite?.getD true | |
| unless overwrite do | |
| -- artifacts of skipped mappings with `--no-overwrite` cannot be cleanly handled | |
| error "`--no-overwrite` is not supported for `cache get`" | |
| let pkg? ← opts.package?.bindM (liftM <| parsePackageSpec ws ·) | |
| if let some file := mappings? then liftM (m := LoggerIO) do | |
| if opts.mappingsOnly then | |
| error "`--mappings-only` is not supported with a mappings file; use `lake cache add` instead" | |
| if opts.rev?.isSome then | |
| logWarning "the `--rev` option does nothing for `cache get` with a mappings file" | |
| if opts.failLv ≤ .warning then | |
| failure | |
| if opts.platform?.isSome || opts.toolchain?.isSome then | |
| logWarning "the `--platform` and `--toolchain` options do nothing for `cache get` with a mappings file" | |
| if opts.failLv ≤ .warning then | |
| failure | |
| let some remoteScope := opts.scope? | |
| | error "to use `cache get` with a mappings file, `--scope` or `--repo` must be set" | |
| let service : CacheService ← id do | |
| if let some service := opts.service? then | |
| ws.findCacheService service | |
| else if let some artifactEndpoint := ws.lakeEnv.cacheArtifactEndpoint? then | |
| logWarning endpointDeprecation | |
| return .downloadArtsService artifactEndpoint ws.lakeEnv.cacheService? | |
| else | |
| return ws.defaultCacheService | |
| let map ← CacheMap.load file | |
| let localScope := pkg?.elim ws.root.cacheScope (·.cacheScope) | |
| cache.writeMap localScope map service.name? (some remoteScope) overwrite | |
| let descrs ← map.collectOutputDescrs | |
| service.downloadArtifacts descrs cache remoteScope opts.forceDownload | |
| else | |
| let platform := opts.platform?.getD .system | |
| let toolchain := opts.toolchain?.getD ws.cacheToolchain | |
| let service : CacheService ← id do | |
| if let some service := opts.service? then | |
| ws.findCacheService service | |
| else | |
| match ws.lakeEnv.cacheArtifactEndpoint?, ws.lakeEnv.cacheRevisionEndpoint? with | |
| | some artifactEndpoint, some revisionEndpoint => | |
| logWarning endpointDeprecation | |
| if opts.mappingsOnly then | |
| error "`--mappings-only` requires services to be configured | |
| via the Lake system configuration (not environment variables)" | |
| return .downloadService artifactEndpoint revisionEndpoint ws.lakeEnv.cacheService? | |
| | none, none => | |
| return ws.defaultCacheService | |
| | some artifactEndpoint, none => | |
| logWarning endpointDeprecation | |
| error (invalidEndpointConfig artifactEndpoint "") | |
| | none, some revisionEndpoint => | |
| logWarning endpointDeprecation | |
| error (invalidEndpointConfig "" revisionEndpoint) | |
| if let some remoteScope := opts.scope? then | |
| if !remoteScope.isRepo && service.isReservoir then | |
| -- `--scope` with Reservoir would imply downloading artifacts for a different package. | |
| -- This is likely user error (they meant `--repo`) rather than something actually useful. | |
| error "to use `cache get` with `--scope`, a custom endpoint must be set (not Reservoir); \ | |
| if you instead want to download artifacts for a fork of the package, use `--repo`" | |
| let pkg := pkg?.getD ws.root | |
| fetchOutputs cache service pkg remoteScope opts platform toolchain overwrite | |
| else if service.isReservoir then | |
| if let some pkg := pkg? then | |
| let some remoteScope := pkg.reservoirScope? | |
| | error s!"{pkg.prettyName}: not a Reservoir dependency and no `--scope` or `--repo` set" | |
| fetchOutputs cache service pkg remoteScope opts platform toolchain overwrite | |
| else if opts.rev?.isSome then | |
| error "the `--rev` option is not supported for a multi-package Reservoir `cache get`" | |
| else | |
| -- TODO: Parallelize? | |
| let ok ← ws.packages.foldlM (start := 1) (init := true) (m := LoggerIO) fun ok pkg => do | |
| let some remoteScope := pkg.reservoirScope? | |
| | logInfo s!"{pkg.prettyName}: skipping non-Reservoir dependency" | |
| return ok | |
| try | |
| fetchOutputs cache service pkg remoteScope opts platform toolchain overwrite | |
| return ok | |
| catch _ => | |
| return false | |
| unless ok do | |
| error "failed to download artifacts for some dependencies" | |
| else | |
| error "to use `cache get` with a custom endpoint, the `--scope` or `--repo` option must be set" | |
| where | |
| invalidEndpointConfig artifactEndpoint revisionEndpoint := | |
| s!"invalid endpoint configuration:\ | |
| \n LAKE_CACHE_ARTIFACT_ENDPOINT={artifactEndpoint}\ | |
| \n LAKE_CACHE_REVISION_ENDPOINT={revisionEndpoint}\n\ | |
| To use `cache get` with a custom endpoint, both environment variables \ | |
| must be set to non-empty strings. To use Reservoir, neither should be set." | |
| fetchOutputs cache service pkg remoteScope opts platform toolchain overwrite : LoggerIO Unit := do | |
| let platform := cachePlatform pkg platform | |
| let toolchain := cacheToolchain pkg toolchain | |
| let map : CacheMap ← id do | |
| let repo := GitRepo.mk pkg.dir | |
| if let some rev := opts.rev? then | |
| let rev ← repo.resolveRevision rev | |
| let some map ← service.downloadRevisionOutputs? rev cache pkg.cacheScope remoteScope platform toolchain opts.forceDownload | |
| | error s!"{remoteScope}: outputs not found for revision {rev}" | |
| return map | |
| else | |
| if (← repo.hasDiff) then | |
| logWarning s!"{pkg.prettyName}: package has changes; \ | |
| only artifacts for committed code will be downloaded" | |
| if opts.failLv ≤ .warning then | |
| failure | |
| let n := opts.maxRevs | |
| let revs ← repo.getHeadRevisions n | |
| let map? ← revs.findSomeM? fun rev => | |
| service.downloadRevisionOutputs? rev cache pkg.cacheScope remoteScope platform toolchain opts.forceDownload | |
| let some map := map? | |
| | let revisions := | |
| if n = 0 || revs.size < n then "for any revision" else s!"in {n} revisions from HEAD" | |
| error s!"{remoteScope}: no outputs found {revisions}" | |
| return map | |
| cache.writeMap pkg.cacheScope map service.name? (some remoteScope) overwrite | |
| unless opts.mappingsOnly do | |
| let descrs ← map.collectOutputDescrs | |
| service.downloadArtifacts descrs cache remoteScope opts.forceDownload | |
| def computeUploadService | |
| (service? : Option String) (lakeEnv : Env) (lakeCfg : LoadedLakeConfig) | |
| : CliStateM CacheService := do | |
| if let some service := service? then | |
| let service ← findUploadCacheService lakeCfg service | |
| if service.isReservoir then | |
| error unsupportedReservoirService | |
| let some key := lakeEnv.cacheKey? | |
| | error missingLakeCacheKey | |
| return service.withKey key | |
| else | |
| match lakeEnv.cacheKey?, lakeEnv.cacheArtifactEndpoint?, lakeEnv.cacheRevisionEndpoint? with | |
| | some key, some artifactEndpoint, some revisionEndpoint => | |
| return .uploadService key artifactEndpoint revisionEndpoint | |
| | key?, none, none => | |
| if let some service := lakeCfg.defaultCacheUploadService? then | |
| if service.isReservoir then | |
| error unsupportedReservoirService | |
| let some key := key? | |
| | error missingLakeCacheKey | |
| return service.withKey key | |
| else | |
| error noDefaultService | |
| | key?, artifactEndpoint?, revisionEndpoint? => | |
| error (invalidEndpointConfig key? artifactEndpoint? revisionEndpoint?) | |
| where | |
| unsupportedReservoirService := | |
| "Reservoir services do not support uploads" | |
| missingLakeCacheKey := | |
| "uploads require an authentication key configured through `LAKE_CACHE_KEY`" | |
| noDefaultService := | |
| "no default upload service configured; the `--service` option must be set" | |
| invalidEndpointConfig key? artifactEndpoint? revisionEndpoint? := | |
| s!"invalid endpoint configuration:\ | |
| \n LAKE_CACHE_KEY is {if key?.isNone then "unset" else "set"}\ | |
| \n LAKE_CACHE_ARTIFACT_ENDPOINT={artifactEndpoint?.getD ""}\ | |
| \n LAKE_CACHE_REVISION_ENDPOINT={revisionEndpoint?.getD ""}\n\ | |
| To upload, these environment variables must be set to non-empty strings." | |
| def computePackageRev (pkgDir : FilePath) : CliStateM String := do | |
| let repo := GitRepo.mk pkgDir | |
| if (← repo.hasDiff) then | |
| logWarning s!"package has changes; \ | |
| artifacts will be uploaded for the most recent commit" | |
| if (← getThe LakeOptions).failLv ≤ .warning then | |
| exit 1 | |
| repo.getHeadRevision | |
| def putCore | |
| (rev : GitRev) (outputs : FilePath) (artDir : FilePath) | |
| (service : CacheService) (scope : CacheServiceScope) | |
| (platform := CachePlatform.none) (toolchain := CacheToolchain.none) | |
| : LoggerIO Unit := do | |
| let map ← CacheMap.load outputs | |
| let descrs ← map.collectOutputDescrs | |
| let paths ← computeArtifactPaths descrs | |
| service.uploadArtifacts ⟨descrs, rfl⟩ paths scope | |
| -- Mappings are uploaded after artifacts to allow downloads to assume that | |
| -- if the mappings exist, the artifacts should also exist | |
| service.uploadRevisionOutputs rev outputs scope platform toolchain | |
| where | |
| computeArtifactPaths (descrs : Array ArtifactDescr) : LogIO (Vector FilePath descrs.size) := throwIfLogs do | |
| (Vector.mk descrs rfl).mapM fun out => do | |
| let art := artDir / out.relPath | |
| unless (← art.pathExists) do | |
| logError s!"artifact not found in cache: {art}" | |
| return art | |
| protected def put : CliM PUnit := do | |
| processOptions lakeOption | |
| let file ← takeArg "mappings" | |
| let opts ← getThe LakeOptions | |
| let some scope := opts.scope? | |
| | error "the `--scope` or `--repo` option must be set" | |
| if opts.rev?.isSome then | |
| error "the `--rev` option is not supported for `cache put`; \ | |
| to upload artifacts for different revisions, use the staging workflow, \ | |
| e.g., `lake cache stage` and `lake cache put-staged`" | |
| noArgsRem do | |
| let cfg ← mkLoadConfig opts | |
| let ws ← loadWorkspace cfg | |
| let pkg ← match opts.package? with | |
| | some pkg => parsePackageSpec ws pkg | |
| | _ => pure ws.root | |
| let platform := cachePlatform pkg (opts.platform?.getD .system) | |
| let toolchain := cacheToolchain pkg (opts.toolchain?.getD ws.lakeEnv.cacheToolchain) | |
| let service ← computeUploadService opts.service? ws.lakeEnv ws.lakeConfig | |
| let rev ← computePackageRev pkg.dir | |
| putCore rev file ws.lakeCache.artifactDir service scope platform toolchain | |
| protected def add : CliM PUnit := do | |
| processOptions lakeOption | |
| let file ← takeArg "mappings" | |
| let opts ← getThe LakeOptions | |
| noArgsRem do | |
| let cfg ← mkLoadConfig opts | |
| let ws ← loadWorkspace cfg | |
| let pkg ← match opts.package? with | |
| | some pkg => parsePackageSpec ws pkg | |
| | _ => pure ws.root | |
| let localScope := pkg.cacheScope | |
| if opts.scope?.isSome && opts.service?.isNone then | |
| error "`--scope` and `--repo` require `--service`" | |
| let service? ← id do | |
| let some service := opts.service? | |
| | return none | |
| ws.validateCacheService service | |
| return some (.ofString service) | |
| let map ← CacheMap.load file | |
| let overwrite := opts.overwrite?.getD true | |
| ws.lakeCache.writeMap localScope map service? opts.scope? overwrite | |
| def stagingOutputsFile := "outputs.jsonl" | |
| protected def stage : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let mappingsFile ← FilePath.mk <$> takeArg "mappings" | |
| let stagingDir ← FilePath.mk <$> takeArg "staging directory" | |
| noArgsRem do | |
| let cfg ← mkLoadConfig opts | |
| let cache ← id do | |
| if (← configFileExists cfg.configFile) then | |
| return (← loadWorkspaceRoot cfg).lakeCache | |
| else if let some cache := cfg.lakeEnv.lakeCache? then | |
| return cache | |
| else | |
| error "no workspace configuration found and no system cache detected" | |
| let map ← CacheMap.load mappingsFile | |
| let descrs ← map.collectOutputDescrs | |
| IO.FS.createDirAll stagingDir | |
| copyFile mappingsFile (stagingDir / stagingOutputsFile) | |
| let overwrite := opts.overwrite?.getD false | |
| let ok ← descrs.foldlM (init := true) fun ok descr => do | |
| let cachePath := cache.artifactDir / descr.relPath | |
| let stagingPath := stagingDir / descr.relPath | |
| unless overwrite || !(← stagingPath.pathExists) do | |
| return ok | |
| match (← copyFile cachePath stagingPath |>.toBaseIO) with | |
| | .ok _ => | |
| return ok | |
| | .error (.noFileOrDirectory ..) => | |
| logError s!"artifact not found in cache: {cachePath}" | |
| return false | |
| | .error e => | |
| logError s!"failed to copy artifact: {e}" | |
| return false | |
| unless ok do | |
| logError "failed to copy all outputs to the staging directory" | |
| exit 1 | |
| protected def unstage : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let stagingDir ← FilePath.mk <$> takeArg "staging directory" | |
| let pkg? ← takeArg? | |
| noArgsRem do | |
| let cfg ← mkLoadConfig opts | |
| let ws ← loadWorkspace cfg | |
| let pkg ← match pkg? with | |
| | some pkg => parsePackageSpec ws pkg | |
| | _ => pure ws.root | |
| let localScope := pkg.cacheScope | |
| if opts.scope?.isSome && opts.service?.isNone then | |
| error "`--scope` and `--repo` require `--service`" | |
| let service? ← id do | |
| let some service := opts.service? | |
| | return none | |
| ws.validateCacheService service | |
| return some (.ofString service) | |
| let map ← CacheMap.load (stagingDir / stagingOutputsFile) | |
| let descrs ← map.collectOutputDescrs | |
| let artDir := ws.lakeCache.artifactDir | |
| IO.FS.createDirAll artDir | |
| let overwrite := opts.overwrite?.getD false | |
| let ok ← descrs.foldlM (init := true) fun ok descr => do | |
| let cachePath := artDir/ descr.relPath | |
| let stagingPath := stagingDir / descr.relPath | |
| if (← cachePath.pathExists) then | |
| if overwrite then | |
| -- Cache artifacts are read-only, so the old artifact must be deleted first. | |
| IO.FS.removeFile cachePath | |
| else | |
| return ok | |
| match (← copyFile stagingPath cachePath |>.toBaseIO) with | |
| | .ok _ => | |
| return ok | |
| | .error (.noFileOrDirectory ..) => | |
| logError s!"output artifact not found in staging directory: {stagingPath}" | |
| return false | |
| | .error e => | |
| logError s!"failed to copy artifact: {e}" | |
| return false | |
| unless ok do | |
| logError "failed to copy all outputs to the staging directory" | |
| exit 1 | |
| ws.lakeCache.writeMap localScope map service? opts.scope? overwrite | |
| protected def putStaged : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let stagingDir ← FilePath.mk <$> takeArg "staging directory" | |
| let some rev := opts.rev? | |
| | error "the `--rev` option must be set" | |
| let some scope := opts.scope? | |
| | error "the `--scope` or `--repo` option must be set" | |
| if opts.package?.isSome then | |
| error "the `--package` option does nothing for `cache put-staged`" | |
| noArgsRem do | |
| let cfg ← mkLoadConfig opts | |
| let lakeCfg ← loadLakeConfig cfg.lakeEnv | |
| let platform := opts.platform?.getD .none | |
| let toolchain := opts.toolchain?.getD .none | |
| let service ← computeUploadService opts.service? cfg.lakeEnv lakeCfg | |
| let outputsFile := stagingDir / stagingOutputsFile | |
| putCore rev outputsFile stagingDir service scope platform toolchain | |
| protected def services : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| noArgsRem do | |
| let cfg ← mkLoadConfig opts | |
| if (← configFileExists cfg.configFile) then | |
| let ws ← loadWorkspace cfg | |
| (collectCacheServices ws).forM (IO.println ·) | |
| else | |
| let cfg ← loadLakeConfig cfg.lakeEnv | |
| cfg.cacheServices.forM fun name _ => | |
| IO.println <| name.toString (escape := false) | |
| protected def clean : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| noArgsRem do | |
| let cfg ← mkLoadConfig opts | |
| let dir ← id do | |
| if (← configFileExists cfg.configFile) then | |
| return (← loadWorkspaceRoot cfg).lakeCache.dir | |
| else if let some cache := cfg.lakeEnv.lakeCache? then | |
| return cache.dir | |
| else | |
| error "no cache to delete; no workspace configuration found and no system cache detected" | |
| removeDirAllIfExists dir | |
| protected def help : CliM PUnit := do | |
| IO.println <| helpCache <| ← takeArgD "" | |
| end cache | |
| def cacheCli : (cmd : String) → CliM PUnit | |
| | "add" => cache.add | |
| | "get" => cache.get | |
| | "put" => cache.put | |
| | "stage" => cache.stage | |
| | "unstage" => cache.unstage | |
| | "put-staged" => cache.putStaged | |
| | "clean" => cache.clean | |
| | "services" => cache.services | |
| | "help" => cache.help | |
| | cmd => throw <| CliError.unknownCommand cmd | |
| /-! ### `lake script` CLI -/ | |
| namespace script | |
| protected def list : CliM PUnit := do | |
| processOptions lakeOption | |
| let config ← mkLoadConfig (← getThe LakeOptions) | |
| noArgsRem do | |
| let ws ← loadWorkspace config | |
| ws.packages.forM fun pkg => do | |
| pkg.scripts.forM fun _ script => | |
| IO.println script.name | |
| protected nonrec def run : CliM PUnit := do | |
| processLeadingOptions lakeOption -- between `lake [script] run` and `<name>` | |
| let config ← mkLoadConfig (← getThe LakeOptions) | |
| let ws ← loadWorkspace config | |
| if let some spec ← takeArg? then | |
| let args ← takeArgs | |
| let script ← parseScriptSpec ws spec | |
| exit <| ← script.run args |>.run {opaqueWs := ws} | |
| else | |
| for script in ws.root.defaultScripts do | |
| exitIfErrorCode <| ← script.run [] |>.run {opaqueWs := ws} | |
| exit 0 | |
| protected def doc : CliM PUnit := do | |
| processOptions lakeOption | |
| let spec ← takeArg "script name" | |
| let config ← mkLoadConfig (← getThe LakeOptions) | |
| noArgsRem do | |
| let ws ← loadWorkspace config | |
| let script ← parseScriptSpec ws spec | |
| match script.doc? with | |
| | some doc => IO.println doc | |
| | none => throw <| CliError.missingScriptDoc script.name | |
| protected def help : CliM PUnit := do | |
| IO.println <| helpScript <| ← takeArgD "" | |
| end script | |
| def scriptCli : (cmd : String) → CliM PUnit | |
| | "list" => script.list | |
| | "run" => script.run | |
| | "doc" => script.doc | |
| | "help" => script.help | |
| | cmd => throw <| CliError.unknownCommand cmd | |
| /-! ### `lake` CLI -/ | |
| protected def new : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let name ← takeArg "package name" | |
| let (tmp, lang) ← parseTemplateLangSpec <| ← takeArgD "" | |
| noArgsRem do new name tmp lang (← opts.computeEnv) opts.rootDir opts.offline | |
| protected def init : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let name := ← takeArgD "." | |
| let (tmp, lang) ← parseTemplateLangSpec <| ← takeArgD "" | |
| noArgsRem do init name tmp lang (← opts.computeEnv) opts.rootDir opts.offline | |
| protected def build : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let config ← mkLoadConfig opts | |
| let ws ← loadWorkspace config | |
| let targetSpecs ← takeArgs | |
| let specs ← parseTargetSpecs ws targetSpecs | |
| if specs.isEmpty && !opts.allowEmpty then | |
| logWarning "no targets specified and no default targets configured\ | |
| \n Note: This will be an error in a future version of Lake.\ | |
| \n Hint: This warning (or error) can be suppressed with '--allow-empty'." | |
| if opts.failLv ≤ .warning then | |
| exit 1 | |
| specs.forM fun spec => | |
| unless spec.buildable do | |
| throw <| .invalidBuildTarget spec.info.key.toSimpleString | |
| let outputsPackage? ← id do | |
| match opts.outputsFile?, opts.package? with | |
| | some _, some pkg => some <$> parsePackageSpec ws pkg | |
| | some _, none => return some ws.root | |
| | none, some _ => error "`--package` requires `-o`" | |
| | none, none => return none | |
| let buildConfig := mkBuildConfig opts (out := .stdout) (showSuccess := true) outputsPackage? | |
| ws.runBuild (buildSpecs specs) buildConfig | |
| protected def checkBuild : CliM PUnit := do | |
| processOptions lakeOption | |
| let pkg ← loadPackage (← mkLoadConfig (← getThe LakeOptions)) | |
| noArgsRem do exit <| if pkg.defaultTargets.isEmpty then 1 else 0 | |
| protected def query : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let config ← mkLoadConfig opts | |
| let ws ← loadWorkspace config | |
| let targetSpecs ← takeArgs | |
| let specs ← parseTargetSpecs ws targetSpecs | |
| let fmt := opts.outFormat | |
| let buildConfig := mkBuildConfig opts | |
| let results ← ws.runBuild (querySpecs specs fmt) buildConfig | |
| results.forM (IO.println ·) | |
| protected def queryKind : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let config ← mkLoadConfig opts | |
| let ws ← loadWorkspace config | |
| let targetSpecs ← takeArgs | |
| let keys ← targetSpecs.toArray.mapM fun spec => | |
| IO.ofExcept <| PartialBuildKey.parse spec | |
| let buildConfig := mkBuildConfig opts | |
| let r ← ws.runFetchM (cfg := buildConfig) <| keys.mapM fun key => do | |
| let ⟨_, job⟩ ← key.fetchInCore ws.root | |
| let kind := job.kind | |
| let job ← maybeRegisterJob key.toString job.toOpaque | |
| return (kind.name, job) | |
| r.forM (IO.println ·.1) | |
| r.forM fun (_, job) => do | |
| match (← job.wait?) with | |
| | some _ => pure () | |
| | none => error "build failed" | |
| protected def resolveDeps : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let config ← mkLoadConfig opts | |
| noArgsRem do | |
| discard <| loadWorkspace config | |
| protected def update : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let config ← mkLoadConfig opts | |
| let toUpdate := (← getArgs).foldl (·.insert <| stringToLegalOrSimpleName ·) {} | |
| updateManifest config toUpdate | |
| protected def pack : CliM PUnit := do | |
| processOptions lakeOption | |
| let file? ← takeArg? | |
| noArgsRem do | |
| let ws ← loadWorkspace (← mkLoadConfig (← getThe LakeOptions)) | |
| let file := (FilePath.mk <$> file?).getD ws.root.buildArchiveFile | |
| ws.root.pack file | |
| protected def unpack : CliM PUnit := do | |
| processOptions lakeOption | |
| let file? ← takeArg? | |
| noArgsRem do | |
| let ws ← loadWorkspace (← mkLoadConfig (← getThe LakeOptions)) | |
| let file := (FilePath.mk <$> file?).getD ws.root.buildArchiveFile | |
| ws.root.unpack file | |
| protected def upload : CliM PUnit := do | |
| processOptions lakeOption | |
| let tag ← takeArg "release tag" | |
| noArgsRem do | |
| let ws ← loadWorkspace (← mkLoadConfig (← getThe LakeOptions)) | |
| ws.root.uploadRelease tag | |
| protected def cache : CliM PUnit := do | |
| if let some cmd ← takeArg? then | |
| processLeadingOptions lakeOption -- between `lake cache <cmd>` and args | |
| if (← getWantsHelp) then | |
| IO.println <| helpCache cmd | |
| else | |
| cacheCli cmd | |
| else | |
| throw <| CliError.missingCommand | |
| protected def setupFile : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let loadConfig ← mkLoadConfig opts | |
| let buildConfig := mkBuildConfig opts | |
| let filePath ← takeArg "file path" | |
| let header? ← takeArg? | |
| noArgsRem do | |
| let header ← header?.mapM fun header => do | |
| let header ← if header == "-" then IO.getStdin >>= (·.getLine) else pure header | |
| match Json.parse header >>= fromJson? with | |
| | .ok header => pure header | |
| | .error e => error s!"failed to parse header JSON: {e}" | |
| exit <| ← setupFile loadConfig filePath header buildConfig | |
| protected def test : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let ws ← loadWorkspace (← mkLoadConfig opts) | |
| noArgsRem do | |
| let x := ws.root.test opts.subArgs (mkBuildConfig opts) | |
| exit <| ← x.run (mkLakeContext ws) | |
| protected def checkTest : CliM PUnit := do | |
| processOptions lakeOption | |
| let pkg ← loadPackage (← mkLoadConfig (← getThe LakeOptions)) | |
| noArgsRem do exit <| if pkg.testDriver.isEmpty then 1 else 0 | |
| def runBuiltinLint | |
| (opts : LakeOptions) (ws : Workspace) (specifiedMods : Array Lean.Name) | |
| : CliM UInt32 := do | |
| let mods := if specifiedMods.isEmpty then ws.defaultTargetRoots else specifiedMods | |
| if mods.isEmpty then | |
| error "no modules specified and there are no applicable default targets" | |
| let args := opts.builtinLint | |
| let checks := (ws.root.config.checks ++ args.checks).foldl (init := #[]) | |
| fun acc c => if acc.contains c then acc else acc.push c | |
| for c in checks do | |
| unless (ws.findTargetModule? c).isSome do | |
| error s!"unknown checks module `{c}`; it must be a module of a package in the workspace" | |
| let args := { args with mods, checks, srcSearchPath := ws.augmentedLeanSrcPath } | |
| -- Checks modules are imported alongside each lint target, so they must be built as well. | |
| let buildMods := mods ++ checks.filter (!mods.contains ·) | |
| let specs ← parseTargetSpecs ws (buildMods.map (s!"+{·}") |>.toList) | |
| let lintOpts := BuiltinLint.leanOptOverrides args | |
| let overrides : Lean.NameMap Lean.LeanOptions := | |
| if lintOpts.values.isEmpty then | |
| {} | |
| else | |
| mods.foldl (init := ({} : Lean.NameMap Lean.LeanOptions)) | |
| fun m modName => | |
| match ws.findTargetModule? modName with | |
| | some mod => m.insert mod.pkg.baseName lintOpts | |
| | none => m | |
| let buildCfg := { mkBuildConfig opts with | |
| outLv := .error | |
| leanOptOverrides := overrides | |
| } | |
| ws.runBuild (buildSpecs specs) buildCfg | |
| Lean.searchPathRef.set ws.augmentedLeanPath | |
| BuiltinLint.run args | |
| protected def lint : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let ws ← loadWorkspace (← mkLoadConfig opts) | |
| let hasDriver := !ws.root.lintDriver.isEmpty && !opts.builtinOnly | |
| let pkgBuiltinLint := ws.root.config.builtinLint? | |
| let doBuiltinLint := opts.runBuiltinLint || pkgBuiltinLint == some true | |
| let mut exitCode : UInt32 := 0 | |
| if doBuiltinLint then | |
| let mods := (← takeArgs).toArray.map (·.toName) | |
| exitCode ← runBuiltinLint opts ws mods | |
| if hasDriver then | |
| let driverExitCode ← noArgsRem do | |
| ws.root.lint opts.subArgs (mkBuildConfig opts) |>.run (mkLakeContext ws) | |
| unless driverExitCode == 0 do | |
| exitCode := driverExitCode | |
| unless doBuiltinLint || hasDriver do | |
| error s!"no lint driver configured and builtin linting is disabled" | |
| exit exitCode | |
| protected def checkLint : CliM PUnit := do | |
| processOptions lakeOption | |
| let pkg ← loadPackage (← mkLoadConfig (← getThe LakeOptions)) | |
| noArgsRem do | |
| let hasLint := !pkg.lintDriver.isEmpty || pkg.config.builtinLint? == some true | |
| exit <| if hasLint then 0 else 1 | |
| protected def clean : CliM PUnit := do | |
| processOptions lakeOption | |
| let config ← mkLoadConfig (← getThe LakeOptions) | |
| let ws ← loadWorkspace config | |
| let pkgSpecs ← takeArgs | |
| if pkgSpecs.isEmpty then | |
| ws.clean | |
| else | |
| let pkgs ← pkgSpecs.mapM fun pkgSpec => | |
| match ws.findPackageByName? <| stringToLegalOrSimpleName pkgSpec with | |
| | none => throw <| .unknownPackage pkgSpec | |
| | some pkg => pure pkg | |
| pkgs.forM (·.clean) | |
| /-- The `lake shake` command: minimize imports in Lean source files. -/ | |
| protected def shake : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let config ← mkLoadConfig opts | |
| let ws ← loadWorkspace config | |
| -- Get remaining arguments as module names | |
| let mods := (← takeArgs).toArray.map (·.toName) | |
| -- Get default target modules from workspace if no modules specified | |
| let mods := if mods.isEmpty then ws.defaultTargetRoots else mods | |
| if mods.isEmpty then | |
| error "no modules specified and there are no applicable default targets" | |
| let args := {opts.shake with mods} | |
| unless args.force do | |
| let specs ← parseTargetSpecs ws [] | |
| let upToDate ← ws.checkNoBuild (buildSpecs specs) | |
| unless upToDate do | |
| error "there are out of date oleans; run `lake build` or fetch them from a cache first" | |
| -- Run shake with workspace search paths | |
| Lean.searchPathRef.set ws.augmentedLeanPath | |
| let exitCode ← Shake.run args ws.augmentedLeanSrcPath | |
| if exitCode != 0 then | |
| exit exitCode | |
| /-- The `lake comparator` command: judge a solution against a challenge. -/ | |
| protected def comparator : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| noArgsRem do | |
| let (leanInstall, lakeInstall) ← opts.getInstall | |
| -- The workspace is deliberately not loaded here: evaluating the project's configuration is code | |
| -- execution, and containing it is what the sandbox is for. | |
| let cfg ← mkLoadConfig opts | |
| exit <| ← | |
| Check.runComparator opts.comparatorConfig? opts.challengeFromExport? opts.solutionFromExport? | |
| opts.paranoid opts.inadvisablyNoSandbox (opts.outFormat matches .json) leanInstall lakeInstall | |
| cfg.wsDir | |
| /-- | |
| The half of `lake check` that runs inside the sandbox, selected by `LAKE_CHECK_EXPORT`. | |
| Resolves the default targets to modules, builds them, and dumps the export of everything in scope. | |
| The export goes to standard out and everything else to standard error, so the outer half can read | |
| one from the other. | |
| -/ | |
| protected def checkExport : CliM PUnit := do | |
| let opts ← getThe LakeOptions | |
| let ws ← loadWorkspace (← mkLoadConfig opts) | |
| let buildConfig := mkBuildConfig opts | |
| ws.runBuild (buildSpecs (← parseTargetSpecs ws [])) buildConfig | |
| let mods ← ws.runBuild ws.root.defaultModules.fetch buildConfig | |
| Lean.initSearchPath ws.lakeEnv.lean.sysroot ws.augmentedLeanPath | |
| let env ← Lean.importModules (mods.map fun mod => {module := mod.name}) {} | |
| LeanExport.dumpEnv env | |
| /-- The `lake check` command: check this project against the kernel. -/ | |
| protected def check : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| if (← IO.getEnv "LAKE_CHECK_EXPORT").isSome then | |
| lake.checkExport | |
| else | |
| noArgsRem do | |
| let (leanInstall, lakeInstall) ← opts.getInstall | |
| -- The workspace is deliberately not loaded here: evaluating the project's configuration is code | |
| -- execution, and containing it is what the sandbox is for. | |
| let cfg ← mkLoadConfig opts | |
| exit <| ← Check.runCheck opts.checkFromExport? opts.paranoid opts.inadvisablyNoSandbox | |
| (opts.outFormat matches .json) leanInstall lakeInstall cfg.wsDir | |
| protected def script : CliM PUnit := do | |
| if let some cmd ← takeArg? then | |
| processLeadingOptions lakeOption -- between `lake script <cmd>` and args | |
| if (← getWantsHelp) then | |
| IO.println <| helpScript cmd | |
| else | |
| scriptCli cmd | |
| else | |
| throw <| CliError.missingCommand | |
| protected def serve : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let args := opts.subArgs.toArray | |
| let config ← mkLoadConfig opts | |
| noArgsRem do exit <| ← serve config args | |
| protected def env : CliM PUnit := do | |
| let config ← mkLoadConfig (← getThe LakeOptions) | |
| let env ← do | |
| if (← configFileExists config.configFile) then | |
| pure (← loadWorkspace config).augmentedEnvVars | |
| else | |
| pure config.lakeEnv.vars | |
| if let some cmd ← takeArg? then | |
| let child ← IO.Process.spawn {cmd, args := (← takeArgs).toArray, env} | |
| exit <| ← child.wait | |
| else | |
| env.forM fun (var, val?) => | |
| IO.println s!"{var}={val?.getD ""}" | |
| exit 0 | |
| protected def exe : CliM PUnit := do | |
| let exeSpec ← takeArg "executable target" | |
| let args ← takeArgs | |
| let opts ← getThe LakeOptions | |
| let config ← mkLoadConfig opts | |
| let ws ← loadWorkspace config | |
| let exe ← parseExeTargetSpec ws exeSpec | |
| let exeFile ← ws.runBuild exe.fetch (mkBuildConfig opts) | |
| exit <| ← (Lake.env exeFile.toString args.toArray).run <| mkLakeContext ws | |
| protected def samply : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let exeSpec ← takeArg "executable target" | |
| let config ← mkLoadConfig opts | |
| let ws ← loadWorkspace config | |
| let exe ← parseExeTargetSpec ws exeSpec | |
| let exeFile ← ws.runBuild exe.fetch {mkBuildConfig opts with outputsFile? := none} | |
| discard <| Samply.run exeFile.toString opts.subArgs.toArray (opts.outputsFile?.map (·.toString)) | |
| (raw := opts.samplyRaw) (serve := !opts.samplyNoServe) (env := ws.augmentedEnvVars) | |
| protected def lean : CliM PUnit := do | |
| processOptions lakeOption | |
| let leanFile ← takeArg "Lean file" | |
| let opts ← getThe LakeOptions | |
| noArgsRem do | |
| let ws ← loadWorkspace (← mkLoadConfig opts) | |
| let rc ← ws.evalLeanFile leanFile opts.subArgs.toArray (mkBuildConfig opts) | |
| exit rc | |
| protected def translateConfig : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let cfg ← mkLoadConfig opts | |
| let lang ← parseLangSpec (← takeArg "configuration language") | |
| let outFile? := (← takeArg?).map FilePath.mk | |
| noArgsRem do | |
| let pkg ← loadPackage cfg | |
| let outFile := outFile?.getD <| pkg.configFile.withExtension lang.fileExtension | |
| if (← outFile.pathExists) then | |
| throw (.outputConfigExists outFile) | |
| IO.FS.writeFile outFile (← pkg.mkConfigString lang) | |
| if outFile?.isNone then | |
| IO.FS.rename pkg.configFile (pkg.configFile.addExtension "bak") | |
| def ReservoirConfig.currentSchemaVersion : StdVer := {major := 1} | |
| structure ReservoirConfig where | |
| name : String | |
| version : StdVer | |
| versionTags : List String | |
| description : String | |
| keywords : Array String | |
| homepage : String | |
| platformIndependent : Option Bool | |
| license : String | |
| licenseFiles : Array FilePath | |
| readmeFile : Option FilePath | |
| doIndex : Bool | |
| schemaVersion := ReservoirConfig.currentSchemaVersion | |
| deriving Lean.ToJson | |
| protected def reservoirConfig : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let cfg ← mkLoadConfig opts | |
| let _ ← id do | |
| let some verStr ← takeArg? | |
| | return ReservoirConfig.currentSchemaVersion | |
| match StdVer.parse verStr with | |
| | .ok ver => return ver | |
| | .error e => error s!"invalid target version: {e}" | |
| noArgsRem do | |
| let pkg ← loadPackage cfg | |
| let repoTags ← GitRepo.getTags pkg.dir | |
| let licenseFiles ← pkg.licenseFiles.filterMapM fun relPath => do | |
| return if (← (pkg.dir / relPath).pathExists) then some relPath else none | |
| let readmeFile := | |
| if (← pkg.readmeFile.pathExists) then some pkg.relReadmeFile else none | |
| let cfg : ReservoirConfig := { | |
| name := pkg.baseName.toString | |
| version := pkg.version | |
| versionTags := repoTags.filter pkg.versionTags.matches | |
| description := pkg.description | |
| homepage := pkg.homepage | |
| keywords := pkg.keywords | |
| platformIndependent := pkg.platformIndependent | |
| license := pkg.license | |
| licenseFiles := licenseFiles | |
| readmeFile := readmeFile | |
| doIndex := pkg.reservoir | |
| } | |
| IO.println (toJson cfg).pretty | |
| protected def versionTags : CliM PUnit := do | |
| processOptions lakeOption | |
| let opts ← getThe LakeOptions | |
| let cfg ← mkLoadConfig opts | |
| noArgsRem do | |
| let pkg ← loadPackage cfg | |
| let tags ← GitRepo.getTags pkg.dir | |
| for tag in tags do | |
| if pkg.versionTags.matches tag then | |
| IO.println tag | |
| protected def selfCheck : CliM PUnit := do | |
| processOptions lakeOption | |
| noArgsRem do verifyInstall (← getThe LakeOptions) | |
| protected def help : CliM PUnit := do | |
| IO.println <| help <| ← takeArgD "" | |
| end lake | |
| def lakeCli : (cmd : String) → CliM PUnit | |
| | "new" => lake.new | |
| | "init" => lake.init | |
| | "build" => lake.build | |
| | "check-build" => lake.checkBuild | |
| | "query" => lake.query | |
| | "query-kind" => lake.queryKind | |
| | "update" | "upgrade" => lake.update | |
| | "resolve-deps" => lake.resolveDeps | |
| | "pack" => lake.pack | |
| | "unpack" => lake.unpack | |
| | "upload" => lake.upload | |
| | "cache" => lake.cache | |
| | "setup-file" => lake.setupFile | |
| | "test" => lake.test | |
| | "check-test" => lake.checkTest | |
| | "lint" => lake.lint | |
| | "check-lint" => lake.checkLint | |
| | "clean" => lake.clean | |
| | "shake" => lake.shake | |
| | "comparator" => lake.comparator | |
| | "check" => lake.check | |
| | "script" => lake.script | |
| | "scripts" => lake.script.list | |
| | "run" => lake.script.run | |
| | "serve" => lake.serve | |
| | "env" => lake.env | |
| | "exe" | "exec" => lake.exe | |
| | "samply" => lake.samply | |
| | "lean" => lake.lean | |
| | "translate-config" => lake.translateConfig | |
| | "reservoir-config" => lake.reservoirConfig | |
| | "version-tags" => lake.versionTags | |
| | "self-check" => lake.selfCheck | |
| | "help" => lake.help | |
| | cmd => | |
| if cmd.startsWith "+" then | |
| throw <| CliError.unexpectedPlus | |
| else | |
| throw <| CliError.unknownCommand cmd | |
| def lake : CliM PUnit := do | |
| match (← getArgs) with | |
| | [] => IO.println usage | |
| | ["--version"] => IO.println uiVersionString | |
| | _ => -- normal CLI | |
| processLeadingOptions lakeOption -- between `lake` and command | |
| if let some cmd ← takeArg? then | |
| processLeadingOptions lakeOption -- between `lake <cmd>` and args | |
| if (← getWantsHelp) then | |
| IO.println <| help cmd | |
| else | |
| lakeCli cmd | |
| else | |
| if (← getWantsHelp) then | |
| IO.println usage | |
| else | |
| throw <| CliError.missingCommand | |
| public def cli (args : List String) : BaseIO ExitCode := | |
| inline <| (lake).run args |