Skip to content

Commit

Permalink
Add --format flag to spago docs (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotdavies authored and f-f committed Jul 2, 2019
1 parent 7bf293a commit a77b869
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Bugfixes:

New features:
- Add support for starting a repl within a folder which has not been setup as a spago project (#168)
- Add `--format` flag to `spago docs` (#294)

## [0.8.5] - 2019-06-18

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,12 @@ $ spago docs
This will generate all the documentation in the `./generated-docs` folder of your project.
You might then want to open the `index.html` file in there.

To build the documentation as Markdown instead of HTML, or to generate tags for your project,
you can pass a `format` flag:
```bash
$ spago docs --format ctags
```


### Publish my library

Expand Down
11 changes: 6 additions & 5 deletions app/Spago.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import Spago.Messages as Messages
import Spago.Packages (PackageName (..), PackagesFilter (..), JsonFlag(..))
import qualified Spago.Packages
import qualified Spago.PscPackage as PscPackage
import Spago.Watch (ClearScreen (..))
import qualified Spago.Purs as Purs
import Spago.Watch (ClearScreen (..))


-- | Commands that this program handles
Expand All @@ -40,7 +41,7 @@ data Command
| Repl (Maybe Int) (Maybe CacheFlag) [PackageName] [SourcePath] [ExtraArg]

-- | Generate documentation for the project and its dependencies
| Docs [SourcePath]
| Docs (Maybe Purs.DocsFormat) [SourcePath]

-- | Build the project paths src/ and test/ plus the specified source paths
| Build BuildOptions
Expand Down Expand Up @@ -166,7 +167,7 @@ parser = do
"transitive" -> Just TransitiveDeps
_ -> Nothing
in CLI.optional $ CLI.opt wrap "filter" 'f' "Filter packages: direct deps with `direct`, transitive ones with `transitive`"

docsFormat = CLI.optional $ CLI.opt Purs.parseDocsFormat "format" 'f' "Docs output format (markdown | html | etags | ctags)"
projectCommands = CLI.subcommandGroup "Project commands:"
[ initProject
, build
Expand Down Expand Up @@ -223,7 +224,7 @@ parser = do
docs =
( "docs"
, "Generate docs for the project and its dependencies"
, Docs <$> sourcePaths
, Docs <$> docsFormat <*> sourcePaths
)


Expand Down Expand Up @@ -357,7 +358,7 @@ main = do
-> Spago.Build.bundleApp WithMain modName tPath shouldBuild buildOptions
BundleModule modName tPath shouldBuild buildOptions
-> Spago.Build.bundleModule modName tPath shouldBuild buildOptions
Docs sourcePaths -> Spago.Build.docs sourcePaths
Docs format sourcePaths -> Spago.Build.docs format sourcePaths
Version -> printVersion
PscPackageLocalSetup force -> liftIO $ PscPackage.localSetup force
PscPackageInsDhall -> liftIO $ PscPackage.insDhall
Expand Down
6 changes: 3 additions & 3 deletions src/Spago/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ bundleModule maybeModuleName maybeTargetPath noBuild buildOpts = do
NoBuild -> bundleAction

-- | Generate docs for the `sourcePaths`
docs :: Spago m => [Purs.SourcePath] -> m ()
docs sourcePaths = do
docs :: Spago m => Maybe Purs.DocsFormat -> [Purs.SourcePath] -> m ()
docs format sourcePaths = do
echoDebug "Running `spago docs`"
config <- Config.ensureConfig
deps <- Packages.getProjectDeps config
echo "Generating documentation for the project. This might take a while.."
Purs.docs $ Config.configSourcePaths config <> Packages.getGlobs deps <> sourcePaths
Purs.docs format $ Config.configSourcePaths config <> Packages.getGlobs deps <> sourcePaths
31 changes: 27 additions & 4 deletions src/Spago/Purs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,36 @@ bundle withMain (ModuleName moduleName) (TargetPath targetPath) = do
("Bundle failed.")


docs :: Spago m => [SourcePath] -> m ()
docs sourcePaths = do
data DocsFormat
= Html
| Markdown
| Ctags
| Etags

parseDocsFormat :: Text -> Maybe DocsFormat
parseDocsFormat = \case
"html" -> Just Html
"markdown" -> Just Markdown
"ctags" -> Just Ctags
"etags" -> Just Etags
_ -> Nothing

printDocsFormat :: DocsFormat -> Text
printDocsFormat = \case
Html -> "html"
Markdown -> "markdown"
Ctags -> "ctags"
Etags -> "etags"


docs :: Spago m => Maybe DocsFormat -> [SourcePath] -> m ()
docs format sourcePaths = do
let
paths = Text.intercalate " " $ Messages.surroundQuote <$> map unSourcePath sourcePaths
cmd = "purs docs " <> paths <> " --format html"
formatStr = printDocsFormat $ fromMaybe Html format
cmd = "purs docs " <> paths <> " --format " <> formatStr
runWithOutput cmd
("Docs generated. Index is at " <> Messages.surroundQuote "./generated-docs/index.html")
"Docs generation succeeded."
"Docs generation failed."

version :: Spago m => m (Maybe Version.SemVer)
Expand Down

0 comments on commit a77b869

Please sign in to comment.