Skip to content

Commit 5ed9519

Browse files
committed
Add example for custom-preprocessor
1 parent 9388d36 commit 5ed9519

File tree

2 files changed

+94
-34
lines changed

2 files changed

+94
-34
lines changed

doc/configure/yaml/project.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,95 @@ custom-preprocessor-extensions:
315315
- erb
316316
~~~
317317

318-
TODO: Add a simple example of how to use custom preprocessors.
318+
??? example "Use of a custom preprocessor"
319+
320+
The [Ruby](https://www.ruby-lang.org/en/) programming language provides
321+
[`erb`](https://docs.ruby-lang.org/en/master/ERB.html) at the command line.
322+
`erb` provides a templating system for Ruby. The following example uses
323+
`erb` as a custom preprocessor.
324+
325+
The example is a single-package project with a customised `Setup.hs`, which
326+
Stack will use to build:
327+
~~~haskell
328+
{-# LANGUAGE CPP #-}
329+
330+
module Main
331+
( main
332+
) where
333+
334+
import Distribution.Simple ( defaultMainWithHooks, simpleUserHooks )
335+
import Distribution.Simple.PreProcess
336+
( PreProcessor (..), mkSimplePreProcessor, unsorted )
337+
import Distribution.Simple.UserHooks ( UserHooks (..) )
338+
import Distribution.Types.BuildInfo ( BuildInfo )
339+
import Distribution.Types.ComponentLocalBuildInfo
340+
( ComponentLocalBuildInfo )
341+
import Distribution.Types.LocalBuildInfo ( LocalBuildInfo )
342+
import System.Process ( readCreateProcess, proc, shell )
343+
344+
main :: IO ()
345+
main = defaultMainWithHooks simpleUserHooks
346+
{ hookedPreProcessors = [("erb", runRuby)]
347+
}
348+
349+
runRuby ::
350+
BuildInfo
351+
-> LocalBuildInfo
352+
-> ComponentLocalBuildInfo
353+
-> PreProcessor
354+
runRuby _ _ _ = PreProcessor
355+
{ platformIndependent = True
356+
, ppOrdering = unsorted
357+
, runPreProcessor = mkSimplePreProcessor $ \erbFile fout verbosity ->
358+
readCreateProcess (erbProcess erbFile) "" >>= writeFile fout
359+
}
360+
where
361+
erbProcess erbFile =
362+
#if defined(mingw32_HOST_OS)
363+
shell $ "erb " <> erbFile
364+
#else
365+
proc "erb" [erbFile]
366+
#endif
367+
~~~
368+
369+
The example has a package description file (`package.yaml`) that specifies a
370+
`Custom` build type:
371+
~~~yaml
372+
spec-version: 0.36.0
373+
name: my-package
374+
version: 0.1.0.0
375+
build-type: Custom
376+
377+
dependencies: base
378+
379+
custom-setup:
380+
dependencies:
381+
- base
382+
- Cabal
383+
- process
384+
385+
library:
386+
source-dirs: src
387+
generated-exposed-modules: MyModule
388+
~~~
389+
390+
The example has a `src/MyModule.erb` file that will be preprocessed to
391+
create Haskell source code:
392+
~~~text
393+
module MyModule where
394+
395+
<% (1..5).each do |i| %>
396+
test<%= i %> :: Int
397+
test<%= i %> = <%= i %>
398+
<% end %>
399+
~~~
400+
401+
The example has a project-level configuration file (`stack.yaml`):
402+
~~~yaml
403+
snapshot: lts-22.30
404+
custom-preprocessor-extensions:
405+
- erb
406+
~~~
319407

320408
## extra-package-dbs
321409

doc/faq.md

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@
273273

274274
??? question "How do I use a custom package index?"
275275

276-
You can configure this in your project-level configuration file (`stack.yaml`,
277-
by default). See [YAML configuration](configure/yaml/yaml_configuration.md).
276+
See the [`package-index`](configure/yaml/non-project.md#package-index)
277+
non-project specific configuration option documentation.
278278

279279
## Package-related
280280

@@ -315,37 +315,9 @@
315315

316316
??? question "How do I use a custom preprocessor?"
317317

318-
If you have a custom preprocessor, for example, Ruby, you may have a file like:
319-
320-
***B.erb***
321-
322-
~~~haskell
323-
module B where
324-
325-
<% (1..5).each do |i| %>
326-
test<%= i %> :: Int
327-
test<%= i %> = <%= i %>
328-
<% end %>
329-
~~~
330-
331-
To ensure that Stack picks up changes to this file for rebuilds, add the
332-
following lines to your `stack.yaml` file:
333-
334-
~~~yaml
335-
custom-preprocessor-extensions:
336-
- erb
337-
338-
require-stack-version: ">= 2.6.0"
339-
~~~
340-
341-
And for backwards compatability with older versions of Stack, also add the
342-
following line to your Cabal file:
343-
344-
extra-source-files: B.erb
345-
346-
You could also use the
347-
[`--custom-preprocessor-extensions`](configure/yaml/project.md#custom-preprocessor-extensions)
348-
flag.
318+
See the
319+
[`customer-prepocessor-extensions`](configure/yaml/project.md#custom-preprocessor-extensions)
320+
project-specific configuration option documentation.
349321

350322
??? question "How do I get extra tools used during building?"
351323

0 commit comments

Comments
 (0)