在Haskell中,同時使用堆疊,有三個地方,我們可以定義依賴的package.yaml檔案; 在library和下executables。在這些地方定義依賴項和我什么時候應該使用它們之間有什么區別?
uj5u.com熱心網友回復:
假設您想使用兩個前端構建和應用程式:CLI 和 GUI,兩者都使用相同的底層邏輯。顯然,您的 CLI 不使用任何圖形庫,因此不應依賴它們,反之亦然。你package.yaml會看起來像。
name: my-app
version: 0.1.0.0
# This are the dependencies all the components depends on.
dependencies:
- base >= 4.7 && < 5
# This is the description of your "common logic" i.e. the library
library:
source-dirs: src
# depends on whatever libraries of your choice
dependencies:
- bytestring
- megaparsec
- parser-combinators
- transformers
executables:
# The executable name for the cli
my-app-cli:
# The main function is in cli.hs
main: cli.hs
source-dirs: app
ghc-options:
- -Wall
- -Wextra
- -threaded
- -rtsopts
- -with-rtsopts=-qg
# It depends on you library, the base library (which remember is at the top of the file), and on whatever other libraries you use to build the cli, for example optparse-applicative
dependencies:
- my-app
- optparse-applicative
# The executable name for the GUI
my-app-gui:
# The main function is on gui.hs
main: gui.hs
source-dirs: app
ghc-options:
- -Wall
- -Wextra
- -threaded
- -rtsopts
- -with-rtsopts=-qg
# Again, It depends on your library, the base, and a GUI library of your choice. Example monomer.
dependencies:
- my-app
- monomer
現在,當您運行stack build它時,它將創建兩個可執行檔案,一個命名為my-app-cliother my-app-gui,每個都有自己的依賴項,但共享共同的依賴項。理想情況下,可以運行stack build my-app:my-app-cli只構建一個可執行檔案,但由于某種原因stack構建了所有內容(這顯然是由于某些cabal行為......不知道,不在乎)
話雖如此,我認為這與其他編程語言沒有什么不同。例如,我傾向于以Python相同的方式構建我的代碼。一個有自己的公共庫,requirements.txt然后不同的應用程式每個都有自己requirement.txt的應用程式,這取決于應用程式是 Web 服務器、機器學習模型還是其他任何東西……例如,這簡化了在 docker 中創建快取層的程序。如果我需要向 Web 服務器添加一些依賴項,它們將添加到 Web 服務器requirements.txt而不是庫服務器。這當然是自以為是,但我的觀點是這不是特定于 haskell 的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/387036.html
