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

CLIの内部をリファクタする #86

Merged
merged 2 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
55 changes: 0 additions & 55 deletions src/main/scala/com/github/windymelt/zmm/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,61 +75,6 @@ abstract class Cli(logLevel: String = "INFO")
} yield ()
}

/** 現在のディレクトリをZMMプロジェクトとして初期化する。
*
* 現在のディレクトリに原稿XMLファイルや生成物配置用のディレクトリを作成する。 既にディレクトリやファイルが存在している場合は何もしない。
*
* @return
* Unitを返す。
*/
def initializeProject(): IO[Unit] = {
val agreed = for {
cwd <- IO.pure(os.pwd.toString())
_ <- IO.print(s"$cwd を ZMMプロジェクトとして初期化しますか? [y/N]?>")
ynString <- IO.readLine
} yield ynString == "y"

val placeXml: IO[Unit] = IO {
os.exists(os.pwd / "script.xml") match {
case true => IO.println("script.xml は既に存在するのでスキップされました")
case false => IO(os.write(os.pwd / "script.xml", xml.script().body))
}
}.flatten

val digArtifacts: IO[Unit] = IO {
os.exists(os.pwd / "artifacts") match {
case true => IO.println("artifacts/ は既に存在するのでスキップされました")
case false => IO(os.makeDir(os.pwd / "artifacts"))
}
}.flatten

val digArtifactsHtml: IO[Unit] = IO {
os.exists(os.pwd / "artifacts" / "html") match {
case true => IO.println("artifacts/html/ は既に存在するのでスキップされました")
case false => IO(os.makeDir(os.pwd / "artifacts" / "html"))
}
}.flatten

val digAssets: IO[Unit] = IO {
os.exists(os.pwd / "assets") match {
case true => IO.println("assets/ は既に存在するのでスキップされました")
case false => IO(os.makeDir(os.pwd / "assets"))
}
}.flatten

val init = for {
_ <- placeXml
_ <- digArtifacts >> digArtifactsHtml
_ <- digAssets
} yield ()

// ZMMプロジェクトを構成するいくつかのファイル/ディレクトリについて、存在しなかったらテンプレートをもとに作成する、を繰り返す
agreed flatMap {
case true => init
case false => IO.println("中断します")
}
}

def generate(filePath: String, outPathString: String): IO[Unit] = {
val content = IO.delay(scala.xml.XML.loadFile(filePath))

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/github/windymelt/zmm/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ object Main
) >>
IO.pure(ExitCode.Success)
case InitializeCommand() =>
defaultCli.initializeProject() >> IO.pure(ExitCode.Success)
application.Init.initializeProject() >> IO.pure(ExitCode.Success)
}
}

Expand Down
61 changes: 61 additions & 0 deletions src/main/scala/com/github/windymelt/zmm/application/Init.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.github.windymelt.zmm.application

import cats.effect.IO

object Init {

/** 現在のディレクトリをZMMプロジェクトとして初期化する。
*
* 現在のディレクトリに原稿XMLファイルや生成物配置用のディレクトリを作成する。 既にディレクトリやファイルが存在している場合は何もしない。
*
* @return
* Unitを返す。
*/
def initializeProject(): IO[Unit] = {
val agreed = for {
cwd <- IO.pure(os.pwd.toString())
_ <- IO.print(s"$cwd を ZMMプロジェクトとして初期化しますか? [y/N]?>")
ynString <- IO.readLine
} yield ynString == "y"

val placeXml: IO[Unit] = IO {
os.exists(os.pwd / "script.xml") match {
case true => IO.println("script.xml は既に存在するのでスキップされました")
case false => IO(os.write(os.pwd / "script.xml", xml.script().body))
}
}.flatten

val digArtifacts: IO[Unit] = IO {
os.exists(os.pwd / "artifacts") match {
case true => IO.println("artifacts/ は既に存在するのでスキップされました")
case false => IO(os.makeDir(os.pwd / "artifacts"))
}
}.flatten

val digArtifactsHtml: IO[Unit] = IO {
os.exists(os.pwd / "artifacts" / "html") match {
case true => IO.println("artifacts/html/ は既に存在するのでスキップされました")
case false => IO(os.makeDir(os.pwd / "artifacts" / "html"))
}
}.flatten

val digAssets: IO[Unit] = IO {
os.exists(os.pwd / "assets") match {
case true => IO.println("assets/ は既に存在するのでスキップされました")
case false => IO(os.makeDir(os.pwd / "assets"))
}
}.flatten

val init = for {
_ <- placeXml
_ <- digArtifacts >> digArtifactsHtml
_ <- digAssets
} yield ()

// ZMMプロジェクトを構成するいくつかのファイル/ディレクトリについて、存在しなかったらテンプレートをもとに作成する、を繰り返す
agreed flatMap {
case true => init
case false => IO.println("中断します")
}
}
}