1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Turtle と stack script で Turtle と stack script 用のテンプレを生成してみる

Last updated at Posted at 2020-03-21

経緯

Stackを使って楽しくHaskellスクリプティングHaskellでShellみたいなことをやるのに便利なライブラリの紹介を読み実用的に感じたので,この先いくつかの script を書いて行きたい.
手始めに,"練習を兼ねて" template の generator を書いてみる.(使ったことないけどstackにはtemplate機能があったと思う.)

ひたすら書いていく

tempGen.hs
#!/usr/bin/env stack
-- stack script --resolver lts-15.3
-- --package=turtle

-- |
-- This is generated by https://github.com/sgsh-gh/snippet/blob/master/tempGen.hs
-- 

{-# OPTIONS_GHC -fno-defer-type-errors #-}
{-# LANGUAGE OverloadedStrings #-}
import Turtle

main :: IO ()
main = do  
  name:_ <- arguments :: IO [Text]
  let file = fromText $ name <> ".hs"
  touch  file
  output file temp
  where
   temp = "#!/usr/bin/env stack"
     <|>  "-- stack script --resolver lts-15.3"
     <|>  "-- --package=turtle"
     <|>  "-- --package=text"
     <|>  ""
     <|>  "-- |"
     <|>  "-- This is generated by https://github.com/sgsh-gh/snippet/blob/master/template.hs"
     <|>  "-- "
     <|>  ""
     <|>  "{-# OPTIONS_GHC -fno-defer-type-errors #-}"
     <|>  "{-# LANGUAGE OverloadedStrings #-}"
     <|>  "import System.Environment (getArgs)"
     <|>  "import Turtle"
     <|>  "import qualified Data.Text as T"
     <|>  ""
  • read :: String -> Turtle.FilePath が無く(私が見つけられてないだけ?)変換が必要だった.
    *追記(20.04.04)igrepさんよりコメントにてdecodeStringがあると教えていただきました.また,このversionではargumentsを使うように変更していたためそもそもreadのような関数は不要でした.
  • gap記法を使うと tempGen.hs: NewlineForbidden となってしまった.
  • output は権限さえあれば上書きしてしまう.

ここまで書いて自分自身を read すれば良いことに気づく...

tempGen.hs
#!/usr/bin/env stack
-- stack script --resolver lts-15.3
-- --package=turtle

-- |
-- This is generated by https://github.com/sgsh-gh/snippet/blob/master/tempGen.hs
-- 

{-# OPTIONS_GHC -fno-defer-type-errors #-}
{-# LANGUAGE OverloadedStrings #-}
import Turtle

main :: IO ()
main = do
  echo "tempGen"
  
  name:_ <- arguments :: IO [Text]
  let file = fromText $ name <> ".hs"
  False <- testfile file -- とりあえず上書きしないように
  sh $ incompleteDup file

incompleteDup :: Turtle.FilePath -> Shell ()
incompleteDup file = do
  liftIO  $ touch file --  fileを作成
  let temp = limit 16 $  input "tempGen.hs" :: Shell Line -- 16行目までをtempに束縛
  liftIO $ output file $ temp -- fileにtempを書き込み
  chmod executable file -- 実行権限を付与
  echo "created"                       
  • Text,Line,FilePathといった型の扱いに慣れが必要.
  • output の第2引数の型が Line では無く Shell Line な理由がよくわからなかった.(一行ずつ書き込んでる?)

実行してみる

>> chmod u+x tempGen.hs
>> ./tempGen.hs test
>> ls
nthPrime.hs* tempGen.hs*  test.hs*
>> less test.hs*
#!/usr/bin/env stack
-- stack script --resolver lts-15.3
-- --package=turtle
-- --package=text
...

うまく生成できた.(実行もできる.)

参考

stack script

turtle

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?