Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually order config codecs #1106

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 88 additions & 54 deletions README.md

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions bin/src/Flags.purs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ pursArgs =
)
)

execArgs :: Parser (Maybe (Array String))
execArgs =
exec_args :: Parser (Maybe (Array String))
exec_args =
thomashoneyman marked this conversation as resolved.
Show resolved Hide resolved
OT.optional
$ Array.fromFoldable
<$> O.many
Expand Down Expand Up @@ -285,4 +285,3 @@ depsOnly =
( O.long "deps-only"
<> O.help "Build depedencies only"
)

16 changes: 8 additions & 8 deletions bin/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ type RunArgs =
, pedanticPackages :: Boolean
, pursArgs :: List String
, backendArgs :: List String
, execArgs :: Maybe (Array String)
, exec_args :: Maybe (Array String)
thomashoneyman marked this conversation as resolved.
Show resolved Hide resolved
, main :: Maybe String
, ensureRanges :: Boolean
, strict :: Maybe Boolean
Expand All @@ -136,7 +136,7 @@ type TestArgs =
, pedanticPackages :: Boolean
, pursArgs :: List String
, backendArgs :: List String
, execArgs :: Maybe (Array String)
, exec_args :: Maybe (Array String)
, strict :: Maybe Boolean
, statVerbosity :: Maybe Core.StatVerbosity
}
Expand Down Expand Up @@ -330,7 +330,7 @@ runArgsParser = Optparse.fromRecord
{ selectedPackage: Flags.selectedPackage
, pursArgs: Flags.pursArgs
, backendArgs: Flags.backendArgs
, execArgs: Flags.execArgs
, exec_args: Flags.exec_args
, output: Flags.output
, pedanticPackages: Flags.pedanticPackages
, main: Flags.moduleName
Expand All @@ -344,7 +344,7 @@ testArgsParser = Optparse.fromRecord
{ selectedPackage: Flags.selectedPackage
, pursArgs: Flags.pursArgs
, backendArgs: Flags.backendArgs
, execArgs: Flags.execArgs
, exec_args: Flags.exec_args
, output: Flags.output
, pedanticPackages: Flags.pedanticPackages
, strict: Flags.strict
Expand Down Expand Up @@ -740,11 +740,11 @@ mkRunEnv runArgs { dependencies, purs } = do
runConf f = selected.package.run >>= f

moduleName = fromMaybe "Main" (runArgs.main <|> runConf _.main)
execArgs = fromMaybe [] (runArgs.execArgs <|> runConf _.execArgs)
exec_args = fromMaybe [] (runArgs.exec_args <|> runConf _.exec_args)

runOptions =
{ moduleName
, execArgs
, exec_args
, sourceDir: Paths.cwd
, executeDir: Paths.cwd
, successMessage: Nothing
Expand All @@ -770,10 +770,10 @@ mkTestEnv testArgs { dependencies, purs } = do
testConf f = selected.package.test >>= f

moduleName = fromMaybe "Test.Main" (testConf (_.main >>> Just))
execArgs = fromMaybe [] (testArgs.execArgs <|> testConf _.execArgs)
exec_args = fromMaybe [] (testArgs.exec_args <|> testConf _.exec_args)
in
{ moduleName
, execArgs
, exec_args
, selected
}

Expand Down
152 changes: 76 additions & 76 deletions core/src/Config.purs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ module Spago.Core.Config

import Spago.Core.Prelude

import Data.Array.NonEmpty (NonEmptyArray)
import Data.Array.NonEmpty as NonEmptyArray
import Data.Codec.Argonaut as CA
import Data.Codec.Argonaut.Record as CAR
Expand All @@ -57,17 +56,18 @@ import Registry.Range as Range
import Registry.Sha256 as Sha256
import Registry.Version as Version
import Spago.FS as FS
import Type.Proxy (Proxy(..))

type Config =
{ package :: Maybe PackageConfig
, workspace :: Maybe WorkspaceConfig
}

configCodec :: JsonCodec Config
configCodec = CAR.object "Config"
{ package: CAR.optional packageConfigCodec
, workspace: CAR.optional workspaceConfigCodec
}
configCodec = CA.object "Config"
$ CA.recordPropOptional (Proxy :: _ "package") packageConfigCodec
$ CA.recordPropOptional (Proxy :: _ "workspace") workspaceConfigCodec
$ CA.record
Comment on lines +67 to +70
Copy link
Member Author

@thomashoneyman thomashoneyman Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally stuck to the order I saw defined in the type synonyms, with the exception of potentially long lists (like extra_packages), which I generally moved to the end.

Let me know if you want anything ordered differently and it's easy enough to change.


type PackageConfig =
{ name :: PackageName
Expand All @@ -81,16 +81,16 @@ type PackageConfig =
}

packageConfigCodec :: JsonCodec PackageConfig
packageConfigCodec = CAR.object "PackageConfig"
{ name: PackageName.codec
, description: CAR.optional CA.string
, dependencies: dependenciesCodec
, build: CAR.optional packageBuildOptionsCodec
, bundle: CAR.optional bundleConfigCodec
, run: CAR.optional runConfigCodec
, test: CAR.optional testConfigCodec
, publish: CAR.optional publishConfigCodec
}
packageConfigCodec = CA.object "PackageConfig"
$ CA.recordProp (Proxy :: _ "name") PackageName.codec
$ CA.recordPropOptional (Proxy :: _ "description") CA.string
$ CA.recordProp (Proxy :: _ "dependencies") dependenciesCodec
$ CA.recordPropOptional (Proxy :: _ "build") packageBuildOptionsCodec
$ CA.recordPropOptional (Proxy :: _ "bundle") bundleConfigCodec
$ CA.recordPropOptional (Proxy :: _ "run") runConfigCodec
$ CA.recordPropOptional (Proxy :: _ "test") testConfigCodec
$ CA.recordPropOptional (Proxy :: _ "publish") publishConfigCodec
$ CA.record

type PublishConfig =
{ version :: Version
Expand All @@ -101,54 +101,54 @@ type PublishConfig =
}

publishConfigCodec :: JsonCodec PublishConfig
publishConfigCodec = CAR.object "PublishConfig"
{ version: Version.codec
, license: License.codec
, location: CAR.optional Location.codec
, include: CAR.optional (CA.array CA.string)
, exclude: CAR.optional (CA.array CA.string)
}
publishConfigCodec = CA.object "PublishConfig"
$ CA.recordProp (Proxy :: _ "version") Version.codec
$ CA.recordProp (Proxy :: _ "license") License.codec
$ CA.recordPropOptional (Proxy :: _ "location") Location.codec
$ CA.recordPropOptional (Proxy :: _ "include") (CA.array CA.string)
$ CA.recordPropOptional (Proxy :: _ "exclude") (CA.array CA.string)
$ CA.record

type RunConfig =
{ main :: Maybe String
, execArgs :: Maybe (Array String)
, exec_args :: Maybe (Array String)
thomashoneyman marked this conversation as resolved.
Show resolved Hide resolved
}

runConfigCodec :: JsonCodec RunConfig
runConfigCodec = CAR.object "RunConfig"
{ main: CAR.optional CA.string
, execArgs: CAR.optional (CA.array CA.string)
}
runConfigCodec = CA.object "RunConfig"
$ CA.recordPropOptional (Proxy :: _ "main") CA.string
$ CA.recordPropOptional (Proxy :: _ "exec_args") (CA.array CA.string)
$ CA.record

type TestConfig =
{ main :: String
, execArgs :: Maybe (Array String)
, exec_args :: Maybe (Array String)
, dependencies :: Dependencies
, censor_test_warnings :: Maybe CensorBuildWarnings
, strict :: Maybe Boolean
, pedantic_packages :: Maybe Boolean
}

testConfigCodec :: JsonCodec TestConfig
testConfigCodec = CAR.object "TestConfig"
{ main: CA.string
, execArgs: CAR.optional (CA.array CA.string)
, dependencies: dependenciesCodec
, censor_test_warnings: CAR.optional censorBuildWarningsCodec
, strict: CAR.optional CA.boolean
, pedantic_packages: CAR.optional CA.boolean
}
testConfigCodec = CA.object "TestConfig"
$ CA.recordProp (Proxy :: _ "main") CA.string
$ CA.recordPropOptional (Proxy :: _ "exec_args") (CA.array CA.string)
$ CA.recordPropOptional (Proxy :: _ "censor_test_warnings") censorBuildWarningsCodec
$ CA.recordPropOptional (Proxy :: _ "strict") CA.boolean
$ CA.recordPropOptional (Proxy :: _ "pedantic_packages") CA.boolean
$ CA.recordProp (Proxy :: _ "dependencies") dependenciesCodec
$ CA.record

type BackendConfig =
{ cmd :: String
, args :: Maybe (Array String)
}

backendConfigCodec :: JsonCodec BackendConfig
backendConfigCodec = CAR.object "BackendConfig"
{ cmd: CA.string
, args: CAR.optional (CA.array CA.string)
}
backendConfigCodec = CA.object "BackendConfig"
$ CA.recordProp (Proxy :: _ "cmd") CA.string
$ CA.recordPropOptional (Proxy :: _ "args") (CA.array CA.string)
$ CA.record

type PackageBuildOptionsInput =
{ censor_project_warnings :: Maybe CensorBuildWarnings
Expand All @@ -157,11 +157,11 @@ type PackageBuildOptionsInput =
}

packageBuildOptionsCodec :: JsonCodec PackageBuildOptionsInput
packageBuildOptionsCodec = CAR.object "PackageBuildOptionsInput"
{ censor_project_warnings: CAR.optional censorBuildWarningsCodec
, strict: CAR.optional CA.boolean
, pedantic_packages: CAR.optional CA.boolean
}
packageBuildOptionsCodec = CA.object "PackageBuildOptionsInput"
$ CA.recordPropOptional (Proxy :: _ "censor_project_warnings") censorBuildWarningsCodec
$ CA.recordPropOptional (Proxy :: _ "strict") CA.boolean
$ CA.recordPropOptional (Proxy :: _ "pedantic_packages") CA.boolean
$ CA.record

type BundleConfig =
{ minify :: Maybe Boolean
Expand All @@ -173,14 +173,14 @@ type BundleConfig =
}

bundleConfigCodec :: JsonCodec BundleConfig
bundleConfigCodec = CAR.object "BundleConfig"
{ minify: CAR.optional CA.boolean
, module: CAR.optional CA.string
, outfile: CAR.optional CA.string
, platform: CAR.optional bundlePlatformCodec
, type: CAR.optional bundleTypeCodec
, extra_args: CAR.optional (CA.array CA.string)
}
bundleConfigCodec = CA.object "BundleConfig"
$ CA.recordPropOptional (Proxy :: _ "minify") CA.boolean
$ CA.recordPropOptional (Proxy :: _ "module") CA.string
$ CA.recordPropOptional (Proxy :: _ "outfile") CA.string
$ CA.recordPropOptional (Proxy :: _ "platform") bundlePlatformCodec
$ CA.recordPropOptional (Proxy :: _ "type") bundleTypeCodec
$ CA.recordPropOptional (Proxy :: _ "extra_args") (CA.array CA.string)
$ CA.record

data BundlePlatform = BundleNode | BundleBrowser

Expand Down Expand Up @@ -292,13 +292,13 @@ type WorkspaceConfig =
}

workspaceConfigCodec :: JsonCodec WorkspaceConfig
workspaceConfigCodec = CAR.object "WorkspaceConfig"
{ package_set: CAR.optional setAddressCodec
, extra_packages: CAR.optional (Internal.Codec.packageMap extraPackageCodec)
, backend: CAR.optional backendConfigCodec
, build_opts: CAR.optional buildOptionsCodec
, lock: CAR.optional CA.boolean
}
workspaceConfigCodec = CA.object "WorkspaceConfig"
$ CA.recordPropOptional (Proxy :: _ "lock") CA.boolean
$ CA.recordPropOptional (Proxy :: _ "package_set") setAddressCodec
$ CA.recordPropOptional (Proxy :: _ "backend") backendConfigCodec
$ CA.recordPropOptional (Proxy :: _ "build_opts") buildOptionsCodec
$ CA.recordPropOptional (Proxy :: _ "extra_packages") (Internal.Codec.packageMap extraPackageCodec)
$ CA.record

type WorkspaceBuildOptionsInput =
{ output :: Maybe FilePath
Expand All @@ -307,11 +307,11 @@ type WorkspaceBuildOptionsInput =
}

buildOptionsCodec :: JsonCodec WorkspaceBuildOptionsInput
buildOptionsCodec = CAR.object "WorkspaceBuildOptionsInput"
{ output: CAR.optional CA.string
, censor_library_warnings: CAR.optional censorBuildWarningsCodec
, stat_verbosity: CAR.optional statVerbosityCodec
}
buildOptionsCodec = CA.object "WorkspaceBuildOptionsInput"
$ CA.recordPropOptional (Proxy :: _ "output") CA.string
$ CA.recordPropOptional (Proxy :: _ "censor_library_warnings") censorBuildWarningsCodec
$ CA.recordPropOptional (Proxy :: _ "stat_verbosity") statVerbosityCodec
$ CA.record

data CensorBuildWarnings
= CensorAllWarnings
Expand Down Expand Up @@ -457,12 +457,12 @@ type GitPackage =
}

gitPackageCodec :: JsonCodec GitPackage
gitPackageCodec = CAR.object "GitPackage"
{ git: CA.string
, ref: CA.string
, subdir: CAR.optional CA.string
, dependencies: CAR.optional dependenciesCodec
}
gitPackageCodec = CA.object "GitPackage"
$ CA.recordProp (Proxy :: _ "git") CA.string
$ CA.recordProp (Proxy :: _ "ref") CA.string
$ CA.recordPropOptional (Proxy :: _ "subdir") CA.string
$ CA.recordPropOptional (Proxy :: _ "dependencies") dependenciesCodec
$ CA.record

-- | The format of a legacy packages.json package set entry for an individual
-- | package.
Expand All @@ -473,11 +473,11 @@ type LegacyPackageSetEntry =
}

legacyPackageSetEntryCodec :: JsonCodec LegacyPackageSetEntry
legacyPackageSetEntryCodec = CAR.object "LegacyPackageSetEntry"
{ dependencies: CA.array PackageName.codec
, repo: CA.string
, version: CA.string
}
legacyPackageSetEntryCodec = CA.object "LegacyPackageSetEntry"
$ CA.recordProp (Proxy :: _ "repo") CA.string
$ CA.recordProp (Proxy :: _ "version") CA.string
$ CA.recordProp (Proxy :: _ "dependencies") (CA.array PackageName.codec)
$ CA.record

readConfig :: forall a. FilePath -> Spago (LogEnv a) (Either String { doc :: YamlDoc Config, yaml :: Config })
readConfig path = do
Expand Down
2 changes: 1 addition & 1 deletion src/Spago/Command/Init.purs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ defaultConfig' opts =
, run: Nothing
, test: test <#> \{ moduleMain, censorTestWarnings, strict, pedanticPackages, dependencies: testDeps } ->
{ dependencies: fromMaybe (Dependencies Map.empty) testDeps
, execArgs: Nothing
, exec_args: Nothing
, main: moduleMain
, censor_test_warnings: censorTestWarnings
, strict
Expand Down
6 changes: 3 additions & 3 deletions src/Spago/Command/Run.purs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type RunEnv a =
}

type RunOptions =
{ execArgs :: Array String
{ exec_args :: Array String
, moduleName :: String
, sourceDir :: FilePath
, executeDir :: FilePath
Expand Down Expand Up @@ -80,7 +80,7 @@ run = do
packageJsonPath = Path.concat [ runDir, "package.json" ]
packageJsonContents = "{\"type\":\"module\" }"

nodeArgs = [ runJsPath ] <> opts.execArgs
nodeArgs = [ runJsPath ] <> opts.exec_args

nodeContents =
Array.fold
Expand Down Expand Up @@ -126,7 +126,7 @@ run = do
logDebug $ show err
die opts.failureMessage
Just backend -> do
let args = [ "--run", opts.moduleName <> ".main" ] <> opts.execArgs
let args = [ "--run", opts.moduleName <> ".main" ] <> opts.exec_args
logDebug $ "Running command `" <> backend.cmd <> " " <> show args <> "`"
Cmd.exec backend.cmd args execOptions >>= case _ of
Right _r -> case opts.successMessage of
Expand Down
6 changes: 3 additions & 3 deletions src/Spago/Command/Test.purs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ type TestEnv a =
}

type SelectedTest =
{ execArgs :: Array String
{ exec_args :: Array String
, moduleName :: String
, selected :: WorkspacePackage
}

run :: forall a. Spago (TestEnv a) Unit
run = do
{ workspace, logOptions, node, selectedPackages, dependencies, purs } <- ask
void $ for selectedPackages \{ execArgs, moduleName, selected } -> do
void $ for selectedPackages \{ exec_args, moduleName, selected } -> do

let
name = selected.package.name
Expand All @@ -38,7 +38,7 @@ run = do
, failureMessage: "Tests failed for package \"" <> PackageName.print name <> "\"."
, executeDir: Paths.cwd
, sourceDir: Paths.cwd
, execArgs
, exec_args
, moduleName
}

Expand Down
Loading
Loading