Help us understand the problem. What is going on with this article?

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

経緯

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした