Skip to content

Commit a841752

Browse files
committed
Merge branch 'master' of https://github.com/nikololiahim/polystat-cli into added_far
2 parents e2e2bf7 + c8645d4 commit a841752

File tree

1 file changed

+70
-61
lines changed

1 file changed

+70
-61
lines changed

src/main/scala/org/polystat/cli/HoconConfig.scala

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,74 +20,83 @@ import PolystatConfig.*
2020
import SupportedLanguage.*
2121
import IncludeExclude.*
2222
import InputUtils.toInput
23+
import com.typesafe.config.Config
2324

2425
case class HoconConfig(path: Path):
2526

2627
import HoconConfig.{given, *}
2728

28-
private object hocon:
29-
private val config =
30-
IO.blocking(ConfigFactory.parseFile(path.toNioPath.toFile))
31-
def apply(s: String): ConfigValue[IO, HoconConfigValue] =
32-
ConfigValue.eval(config.map(c => hoconAt(c)(keys.toplevel)(s)))
33-
end hocon
34-
35-
private val lang = hocon(keys.inputLanguage).as[SupportedLanguage]
36-
private val j2eo = hocon(keys.j2eo).as[Path].option
37-
private val input = hocon(keys.input).as[Path].option.evalMap {
38-
case Some(path) => path.toInput
39-
case None => IO.pure(Input.FromStdin)
40-
}
41-
private val tmp = hocon(keys.tempDir).as[Path].option
42-
private val outputsDirs =
43-
hocon(keys.outputsDirs).as[List[Path]].default(List())
44-
private val outputsFiles =
45-
hocon(keys.outputsFiles).as[List[Path]].default(List())
46-
private val outputsConsole =
47-
hocon(keys.outputsConsole).as[Boolean].default(false)
48-
49-
private val outputs = (outputsDirs, outputsFiles, outputsConsole).parMapN {
50-
case (dirs, files, console) =>
51-
Output(dirs = dirs, files = files, console = console)
52-
}
53-
54-
private val j2eoVersion = hocon(keys.j2eoVersion).as[String].option
55-
private val outputFormats =
56-
hocon(keys.outputFormats).as[List[OutputFormat]].default(List.empty)
57-
private val inex: ConfigValue[IO, Option[IncludeExclude]] =
58-
hocon(keys.includeRules)
59-
.as[Include]
60-
.widen[IncludeExclude]
61-
.or(hocon(keys.excludeRules).as[Exclude].widen[IncludeExclude])
62-
.option
29+
def hocon(key: String)(using cfg: Config): ConfigValue[IO, HoconConfigValue] =
30+
hoconAt(cfg)(keys.toplevel)(key)
6331

6432
val config: ConfigValue[IO, PolystatUsage.Analyze] =
65-
(j2eo, j2eoVersion, inex, input, tmp, outputs, outputFormats, lang)
66-
.parMapN {
67-
case (
68-
j2eo,
69-
j2eoVersion,
70-
inex,
71-
input,
72-
tmp,
73-
outputs,
74-
outputFormats,
75-
lang,
76-
) =>
77-
PolystatUsage.Analyze(
78-
language = lang match
79-
case Java(_, _) => Java(j2eo, j2eoVersion)
80-
case other => other
81-
,
82-
config = AnalyzerConfig(
83-
inex = inex,
84-
input = input,
85-
tmp = tmp,
86-
outputFormats = outputFormats,
87-
output = outputs,
88-
),
89-
)
90-
}
33+
val loadConfig: IO[Config] =
34+
IO.blocking(ConfigFactory.parseFile(path.toNioPath.toFile))
35+
36+
val parseConfig: IO[ConfigValue[IO, PolystatUsage.Analyze]] =
37+
loadConfig.map(parsed =>
38+
given Config = parsed
39+
val lang = hocon(keys.inputLanguage).as[SupportedLanguage]
40+
val j2eo = hocon(keys.j2eo).as[Path].option
41+
val input = hocon(keys.input).as[Path].option.evalMap {
42+
case Some(path) => path.toInput
43+
case None => IO.pure(Input.FromStdin)
44+
}
45+
val tmp = hocon(keys.tempDir).as[Path].option
46+
val outputsDirs =
47+
hocon(keys.outputsDirs).as[List[Path]].default(List())
48+
val outputsFiles =
49+
hocon(keys.outputsFiles).as[List[Path]].default(List())
50+
val outputsConsole =
51+
hocon(keys.outputsConsole).as[Boolean].default(false)
52+
53+
val outputs = (outputsDirs, outputsFiles, outputsConsole).parMapN {
54+
case (dirs, files, console) =>
55+
Output(dirs = dirs, files = files, console = console)
56+
}
57+
58+
val j2eoVersion = hocon(keys.j2eoVersion).as[String].option
59+
val outputFormats =
60+
hocon(keys.outputFormats).as[List[OutputFormat]].default(List.empty)
61+
val inex: ConfigValue[IO, Option[IncludeExclude]] =
62+
hocon(keys.includeRules)
63+
.as[Include]
64+
.widen[IncludeExclude]
65+
.or(hocon(keys.excludeRules).as[Exclude].widen[IncludeExclude])
66+
.option
67+
68+
(j2eo, j2eoVersion, inex, input, tmp, outputs, outputFormats, lang)
69+
.parMapN {
70+
case (
71+
j2eo,
72+
j2eoVersion,
73+
inex,
74+
input,
75+
tmp,
76+
outputs,
77+
outputFormats,
78+
lang,
79+
) =>
80+
PolystatUsage.Analyze(
81+
language = lang match
82+
case Java(_, _) => Java(j2eo, j2eoVersion)
83+
case other => other
84+
,
85+
config = AnalyzerConfig(
86+
inex = inex,
87+
input = input,
88+
tmp = tmp,
89+
outputFormats = outputFormats,
90+
output = outputs,
91+
),
92+
)
93+
}
94+
)
95+
end parseConfig
96+
97+
ConfigValue.eval(parseConfig)
98+
end config
99+
91100
end HoconConfig
92101

93102
object HoconConfig:

0 commit comments

Comments
 (0)