当前位置: 动力学知识库 > 问答 > 编程问答 >

haskell - Is Shake suitable for building a semi-automated tool for human users?

问题描述:

How can I make script that fill perform an "external" action once every build?

import Development.Shake

main = shakeArgs shakeOptions $ do

want [".finished"]

".finished" %> \out -> do

liftIO $ putStrLn "You sure?" >> getLine >> putStrLn "Missiles fired!"

$ runhaskell Main.hs

You sure?

no

Missiles fired!

Error when running Shake build system:

* .finished

Error, rule ".finished" failed to build file:

.finished

网友答案:

Since your action doesn't produce a file, it needs to be marked as a phony rule:

import Development.Shake
import Control.Monad (unless)

main = shakeArgs shakeOptions $ do
    want [".finished"]
    phony ".finished" $ do
        ok <- fmap (== "yes") $ liftIO $ putStrLn "You sure?" >> getLine
        unless ok $ fail "Your commitment to the Great War is lacking!"
        liftIO $ putStrLn "Missiles fired!"

Example sessions:

$ runhaskell shake-phony.hs
You sure?
yes
Missiles fired!
Build completed in 0:29m

$ runhaskell shake-phony.hs
You sure?
no
Error when running Shake build system:
* .finished
Your commitment to the Great War is lacking!
网友答案:

The smallest fix to your code is to use phony like @Cactus suggests. An alternative is to use action directly:

import Development.Shake
import Control.Monad (unless)

main = shakeArgs shakeOptions $ do
    action $ do
        ok <- fmap (== "yes") $ liftIO $ putStrLn "You sure?" >> getLine
        unless ok $ fail "Your commitment to the Great War is lacking!"
        liftIO $ putStrLn "Missiles fired!"

If instead of running the fire missiles at any point during the build, you actually want to run it at the end (after you have built the missiles and stocked up on tin cans), you can write:

main = do
    shakeArgs shakeOptions $ do
        ...normal build rules go here...
    ok <- fmap (== "yes") $ putStrLn "You sure?" >> getLine
    unless ok $ fail "Your commitment to the Great War is lacking!"
    putStrLn "Missiles fired!"

Here you are using normal Haskell to fire the missiles, after running the Shake build.

分享给朋友:
您可能感兴趣的文章:
随机阅读: