Skip to content

Commit

Permalink
Make build-dir configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
tbidne committed Feb 12, 2025
1 parent 8e1b24f commit 7a72597
Show file tree
Hide file tree
Showing 57 changed files with 942 additions and 123 deletions.
2 changes: 2 additions & 0 deletions src/Pacer/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ deriving stock instance
( Eq a,
Eq (Convert.ConvertParamsF p a),
Eq (Derive.DeriveQuantityF p a),
Eq (Chart.BuildDirF p),
Eq (Chart.PathF p Dir),
Eq (Chart.PathF p File),
Eq (Scale.ScaleQuantityF p a)
Expand All @@ -60,6 +61,7 @@ deriving stock instance
( Show a,
Show (Convert.ConvertParamsF p a),
Show (Derive.DeriveQuantityF p a),
Show (Chart.BuildDirF p),
Show (Chart.PathF p Dir),
Show (Chart.PathF p File),
Show (Scale.ScaleQuantityF p a)
Expand Down
30 changes: 21 additions & 9 deletions src/Pacer/Command/Chart.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import Effectful.FileSystem.PathWriter.Dynamic qualified as PW
import Effectful.Logger.Dynamic qualified as Logger
import Effectful.Process.Typed.Dynamic qualified as TP
import FileSystem.OsPath (decodeLenient, decodeThrowM)
import FileSystem.Path qualified as Path
import GHC.IO.Exception (ExitCode (ExitFailure, ExitSuccess))
import Pacer.Command.Chart.Data.Chart (Chart)
import Pacer.Command.Chart.Data.Chart qualified as Chart
Expand All @@ -42,10 +41,19 @@ import Pacer.Command.Chart.Data.Run
( SomeRuns,
)
import Pacer.Command.Chart.Params
( ChartParams (chartRequestsPath, cleanInstall, json, runsPath, runsType),
( ChartParams
( buildDir,
chartRequestsPath,
cleanInstall,
json,
runsPath,
runsType
),
ChartParamsFinal,
RunsType (RunsDefault, RunsGarmin),
)
import Pacer.Config.Env.Types (CachedPaths)
import Pacer.Config.Env.Types qualified as Types
import Pacer.Data.Distance.Units (DistanceUnit)
import Pacer.Exception
( GarminE (GarminUnitRequired),
Expand All @@ -70,6 +78,7 @@ handle ::
LoggerNS :> es,
PathReader :> es,
PathWriter :> es,
State CachedPaths :> es,
TypedProcess :> es
) =>
ChartParamsFinal ->
Expand All @@ -84,14 +93,18 @@ createCharts ::
LoggerNS :> es,
PathReader :> es,
PathWriter :> es,
State CachedPaths :> es,
TypedProcess :> es
) =>
ChartParamsFinal ->
Eff es ()
createCharts params = addNamespace "createCharts" $ do
cwdOsPath <- PR.getCurrentDirectory
cwdPath <- Path.parseAbsDir cwdOsPath
cwdPath <- Types.getCachedCurrentDirectory
let cwdOsPath = pathToOsPath cwdPath

$(Logger.logInfo)
$ "Using build-dir: "
<> (Utils.showtPath params.buildDir)
$(Logger.logInfo)
$ "Using chart-requests: "
<> (Utils.showtPath params.chartRequestsPath)
Expand All @@ -100,7 +113,7 @@ createCharts params = addNamespace "createCharts" $ do
if params.json
then do
-- params.json is active, so stop after json generation
let jsonPath = cwdPath <</>> jsonName
let jsonPath = params.buildDir <</>> jsonName
createChartsJsonFile
True
params.runsType
Expand Down Expand Up @@ -198,18 +211,17 @@ createCharts params = addNamespace "createCharts" $ do
$(Logger.logInfo)
$ mconcat
[ "Successfully created charts in '",
packText (decodeLenient buildDest),
packText (decodeLenient buildDirOsPath),
"' directory"
]
where
buildDirOsPath = pathToOsPath params.buildDir
copyConfig =
MkCopyDirConfig
{ overwrite = OverwriteNone,
targetName = TargetNameLiteral buildDest
targetName = TargetNameLiteral buildDirOsPath
}

buildDest = [osp|build|]

runNpm ::
( HasCallStack,
TypedProcess :> es
Expand Down
11 changes: 10 additions & 1 deletion src/Pacer/Command/Chart/Args.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Options.Applicative qualified as OA
import Pacer.Command.Chart.Params
( ChartParams
( MkChartParams,
buildDir,
chartRequestsPath,
cleanInstall,
dataDir,
Expand All @@ -25,6 +26,7 @@ import Pacer.Prelude
-- | Parse chart args.
parser :: Parser ChartParamsArgs
parser = do
buildDir <- mBuildDirPath
chartRequestsPath <- mChartRequestsParser
cleanInstall <- cleanInstallParser
dataDir <- dataDirParser
Expand All @@ -34,14 +36,21 @@ parser = do

pure
$ MkChartParams
{ chartRequestsPath,
{ buildDir,
chartRequestsPath,
cleanInstall,
dataDir,
json,
runsPath,
runsType
}

mBuildDirPath :: Parser (Maybe OsPath)
mBuildDirPath =
mOsPathParser Nothing "build-dir" "PATH" helpTxt
where
helpTxt = "Optional path to build directory. Defaults to ./build."

mChartRequestsParser :: Parser (Maybe OsPath)
mChartRequestsParser =
mOsPathParser Nothing "chart-requests" "PATH" helpTxt
Expand Down
75 changes: 58 additions & 17 deletions src/Pacer/Command/Chart/Params.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Pacer.Command.Chart.Params
evolvePhase,

-- * Type families
BuildDirF,
PathF,
)
where
Expand All @@ -24,11 +25,12 @@ import FileSystem.Path qualified as Path
import GHC.TypeError qualified as TE
import GHC.TypeLits (ErrorMessage (ShowType, (:<>:)), TypeError)
import Pacer.Config.Env.Types (CachedPaths, getCachedXdgConfigPath)
import Pacer.Config.Env.Types qualified as Types
import Pacer.Config.Phase
( ConfigPhase (ConfigPhaseArgs, ConfigPhaseFinal),
)
import Pacer.Config.Toml
( Toml (chartRequestsPath, dataDir, runsPath),
( Toml (chartBuildDir, chartRequestsPath, dataDir, runsPath),
TomlWithPath (dirPath, toml),
)
import Pacer.Exception
Expand Down Expand Up @@ -72,10 +74,17 @@ type family PathF p t where
:<>: TE.Text "' invalid for PathF."
)

type BuildDirF :: ConfigPhase -> Type
type family BuildDirF p where
BuildDirF ConfigPhaseArgs = Maybe OsPath
BuildDirF ConfigPhaseFinal = Path Abs Dir

-- | Chart params.
type ChartParams :: ConfigPhase -> Type
data ChartParams p = MkChartParams
{ -- | If true, copies clean install of web dir and installs node deps.
{ -- | Build directory.
buildDir :: BuildDirF p,
-- | If true, copies clean install of web dir and installs node deps.
cleanInstall :: Bool,
-- | Optional path to chart-requests.toml.
chartRequestsPath :: PathF p File,
Expand All @@ -95,13 +104,15 @@ type ChartParamsArgs = ChartParams ConfigPhaseArgs
type ChartParamsFinal = ChartParams ConfigPhaseFinal

deriving stock instance
( Eq (PathF p Dir),
( Eq (BuildDirF p),
Eq (PathF p Dir),
Eq (PathF p File)
) =>
Eq (ChartParams p)

deriving stock instance
( Show (PathF p Dir),
( Show (BuildDirF p),
Show (PathF p Dir),
Show (PathF p File)
) =>
Show (ChartParams p)
Expand All @@ -123,16 +134,46 @@ evolvePhase @es params mTomlWithPath = do
assertExists chartRequestsPath
assertExists runsPath

buildDir <-
case params.buildDir of
Just d -> do
-- --build-dir exists, parse absolute or resolve relative to cwd
cwdPath <- Types.getCachedCurrentDirectory
customBuildDir cwdPath d
Nothing -> case mTomlWithPath of
Just t ->
case t.toml.chartBuildDir of
-- toml.build-dir exists, parse absolute or resolve relative to
-- toml path.
Just d -> customBuildDir t.dirPath d
-- otherwise use default, cwd/build
Nothing -> defaultBuildDir
Nothing -> defaultBuildDir

pure
$ MkChartParams
{ cleanInstall = params.cleanInstall,
{ buildDir,
cleanInstall = params.cleanInstall,
chartRequestsPath,
dataDir = (),
json = params.json,
runsPath,
runsType = params.runsType
}
where
defaultBuildDir :: Eff es (Path Abs Dir)
defaultBuildDir = do
cwdPath <- Types.getCachedCurrentDirectory
pure $ cwdPath <</>> [reldir|build|]

customBuildDir :: Path Abs Dir -> OsPath -> Eff es (Path Abs Dir)
customBuildDir absDir unknownDir =
handleUnknownPath
absDir
Path.parseAbsDir
Path.parseRelDir
unknownDir

-- Retrieves (Tuple2 chart-requests-path runs-path)
getChartInputs :: Eff es (Path Abs File, Path Abs File)
getChartInputs = do
Expand Down Expand Up @@ -214,19 +255,19 @@ evolvePhase @es params mTomlWithPath = do
-- 2.1. Toml.path exists, use it
Just p ->
Just
<$> handleTomlPath
<$> handleUnknownPath
tomlWithPath.dirPath
Path.parseAbsFile
Path.parseRelFile
tomlWithPath.dirPath
p
Nothing -> case tomlWithPath.toml.dataDir of
Just dataDir -> do
-- 2.2. Toml.data-dir/path exists, try it
tomlDataDirPath <-
handleTomlPath
handleUnknownPath
tomlWithPath.dirPath
Path.parseAbsDir
Path.parseRelDir
tomlWithPath.dirPath
dataDir
findFirstMatch tomlDataDirPath
Nothing -> pure Nothing
Expand Down Expand Up @@ -270,26 +311,26 @@ evolvePhase @es params mTomlWithPath = do
Utils.showtPath dataDir
]

-- Handles toml config path, resolving any relative paths. Polymorphic
-- over file/dir path.
handleTomlPath ::
-- Handles an unknown (wrt. absolute/relative) path, resolving any
-- relative paths. Polymorphic over file/dir path.
handleUnknownPath ::
-- Containing dir for relative paths.
Path Abs Dir ->
-- Absolute parser.
(OsPath -> Eff es (Path Abs t)) ->
-- Relative parser.
(OsPath -> Eff es (Path Rel t)) ->
-- Dir containing the toml config.
Path Abs Dir ->
-- Path to resolve
OsPath ->
Eff es (Path Abs t)
handleTomlPath absParser relParser tomlDir otherPath =
handleUnknownPath parentDir absParser relParser otherPath =
if OsPath.isAbsolute otherPath
-- Path is absolute, just parse it.
then absParser otherPath
-- Path is relative, parse and combine with tomlDir.
-- Path is relative, parse and combine with parentDir.
else do
relFile <- relParser otherPath
pure $ tomlDir <</>> relFile
pure $ parentDir <</>> relFile

assertExists :: (HasCallStack) => Path b t -> Eff es ()
assertExists p = do
Expand Down
Loading

0 comments on commit 7a72597

Please sign in to comment.