我目前正在测试将我们的构建系统从 make 移植到 shake,但遇到了障碍:
鉴于以下项目结构:
static/a.js
static/b.coffee
build/a.js
build/b.js
也就是说,各种输入扩展映射到相同的输出扩展,因此简单的 "build//*.js"%>
规则不起作用。
如果可能的话,我想避免使用优先级,并且编写一个临时构建规则来检查是否存在可能的输入感觉很笨拙(特别是因为这种情况也发生在其他文件类型上),所以我编写了以下内容:
data StaticFileMapping a = StaticFileMapping String String (FilePath -> FilePath -> Action a)
staticInputs :: FilePath -> StaticFileMapping a -> Action [FilePath]
staticInputs dir (StaticFileMapping iExt _ _) = (findFiles (dir </> "static") [iExt])
staticInputToOutput :: StaticFileMapping a -> FilePath -> FilePath
staticInputToOutput (StaticFileMapping _ oExt _) = (remapDir ["build"]) . (-<.> oExt)
staticTargets :: FilePath -> StaticFileMapping a -> Action [FilePath]
staticTargets dir sfm = (map $ staticInputToOutput sfm) <$> staticInputs dir sfm
rules :: FilePath -> StaticFileMapping a -> Rules ()
rules dir sfm@(StaticFileMapping _ _ process) = join $ mconcat . (map buildInputRule) <$> staticInputs dir sfm
where buildInputRule :: FilePath -> Rules ()
buildInputRule input = (staticInputToOutput sfm input) %> (process input)
这样我就可以为每个输入类型(.coffee -> .js
、.svg -> .png
)等定义一个映射,只需一个实现每个转换的少量代码。而且它几乎可以工作了。
但似乎不可能从 (Action a)
转到 Rules _
而不先将 Action
内的值扔掉,到目前为止据我所知。
是否存在类型为 (Action a) -> (a -> Rules ()) -> Rules ()
或 (Action a) -> (Rules a)< 的函数
?我可以自己实现其中之一吗,还是需要修改库的代码?
或者这整个方法是愚蠢的,我应该采取其他路线?
最佳答案
首先,使用priority
不起作用,因为它静态地选择一个规则然后运行它 - 它不会回溯。同样重要的是,Shake 不运行任何 Action
生产操作Rules
(根据您提出的两个功能)自 Action
可能会打电话 need
在Rule
上它本身定义,或者由另一个操作规则定义,从而使这些 Action
进行排序。调用可见。您可以添加IO (Rules ()) -> Rules ()
,这可能足以满足您的想法(目录列表),但它目前尚未公开(我有一个内部函数可以做到这一点)。
举几个示例方法,定义合理的命令来转换 .js
是很有用的。/.coffee
文件:
cmdCoffee :: FilePath -> FilePath -> Action ()
cmdCoffee src out = do
need [src]
cmd "coffee-script-convertor" [src] [out]
cmdJavascript :: FilePath -> FilePath -> Action ()
cmdJavascript = copyFile'
方法 1:使用 doesFileExist
这将是我的标准方法,编写如下内容:
"build/*.js" %> \out -> do
let srcJs = "static" </> dropDirectory1 out
let srcCf = srcJs -<.> "coffee"
b <- doesFileExist srcCf
if b then cmdCoffee srcCf out else cmdJavascript srcJs out
这准确地捕获了如果用户添加 .coffee
的依赖关系文件位于目录中,则应重新运行规则。你可以想象给doesFileExist
加糖。如果这是您的常见模式。您甚至可以从 StaticFileMapping
列表中驱动它结构(在 group
字段上执行 oExt
为每个 oExt
添加一条规则,而不是依次在每个 doesFileExists
上调用 iExt
)。这种方法的一个优点是,如果您这样做 shake build/out.js
它不需要进行目录列表,尽管成本可能可以忽略不计。
方法 2:在调用 shake
之前列出文件
而不是写main = shakeArgs ...
做:
import System.Directory.Extra(listFilesRecursive) -- from the "extra" package
main = do
files <- listFilesRecursive "static"
shakeArgs shakeOptions $ do
forM_ files $ \src -> case takeExtension src of
".js" -> do
let out = "build" </> takeDirectory1 src
want [out]
out %> \_ -> cmdJavascript src out
-- rules for all other types you care about
_ -> return ()
这里通过IO操作获取文件列表,然后可以引用之前捕获的值来添加规则。添加rulesIO :: IO (Rules ()) -> Rules ()
将允许您列出 shakeArgs
内的文件.
方法 3:列出规则内的文件
您可以定义文件名和输出之间的映射,从目录列表驱动:
buildJs :: Action (Map FilePath (Action ()))
buildJs = do
js <- getDirectoryFiles "static" ["*.js"]
cf <- getDirectoryFiles "static" ["*.coffee"]
return $ Map.fromList $
[("build" </> j, cmdJavascript ("static" </> j) ("build" </> j)) | j <- js] ++
[("build" </> c, cmdCoffee ("static" </> c) ("")) | c <- cf]
然后将其提升为一组规则:
action $ do
mpJs <- buildJs
need $ Map.keys mpJs
"//*.js" %> \out -> do
mpJs <- buildJs
mpJs Map.! out
但是,这会重新计算我们构建的每个文件的目录列表,因此我们应该缓存它并确保它只计算一次:
mpJs <- newCache $ \() -> buildJs
action $ do
mpJs <- mpJs ()
need $ Map.keys mpJs
"//*.js" %> \out -> do
mpJs <- mpJs ()
mpJs Map.! out
此解决方案可能最接近您原来的方法,但我发现它是最复杂的。
关于haskell - 从(操作 a)的内容动态生成规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29873635/