Skip to content

Commit

Permalink
Add nimble guide (#1318)
Browse files Browse the repository at this point in the history
* (add) `guide`/`manual` command

This command opens up the Nimble guide
(https://nim-lang.github.io/nimble/index.html) in the user's preferred
web browser.

Related to #1272

* (add) show reference to `nimble guide` on error

This allows new users to check how to use Nimble.

* (add) show `nimble guide` in help message
  • Loading branch information
xTrayambak authored Dec 19, 2024
1 parent 5a5e2a4 commit 30e7014
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/nimble.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (C) Dominik Picheta. All rights reserved.
# BSD License. Look at license.txt for more info.

import os, tables, strtabs, json, algorithm, sets, uri, sugar, sequtils, osproc,
import os, tables, strtabs, json, browsers, algorithm, sets, uri, sugar, sequtils, osproc,
strformat

import std/options as std_opt
Expand Down Expand Up @@ -2276,6 +2276,14 @@ proc run(options: Options) =
let exitCode = cmd.execCmd
raise nimbleQuit(exitCode)

proc openNimbleManual =
const NimbleGuideURL = "https://nim-lang.github.io/nimble/index.html"
display(
"Opened", "the Nimble guide in your default browser."
)
displayInfo("If it did not open, you can try going to the link manually: " & NimbleGuideURL)
openDefaultBrowser(NimbleGuideURL)

proc doAction(options: var Options) =
if options.showHelp:
writeHelp()
Expand Down Expand Up @@ -2348,6 +2356,8 @@ proc doAction(options: var Options) =
assert false
of actionAdd:
addPackages(options.action.packages, options)
of actionManual:
openNimbleManual()
of actionCustom:
var optsCopy = options
optsCopy.task = options.action.command.normalize
Expand All @@ -2374,7 +2384,9 @@ proc doAction(options: var Options) =
raise nimbleError(msg = "Could not find task $1 in $2" %
[options.action.command, nimbleFile],
hint = "Run `nimble --help` and/or `nimble tasks` for" &
" a list of possible commands.")
" a list of possible commands." & '\n' &
"If you want a tutorial on how to use Nimble, run `nimble guide`."
)

proc setNimBin*(options: var Options) =
# Find nim binary and set into options
Expand Down
9 changes: 6 additions & 3 deletions src/nimblepkg/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type
actionInstall, actionSearch, actionList, actionBuild, actionPath,
actionUninstall, actionCompile, actionDoc, actionCustom, actionTasks,
actionDevelop, actionCheck, actionLock, actionRun, actionSync, actionSetup,
actionClean, actionDeps, actionShellEnv, actionShell, actionAdd
actionClean, actionDeps, actionShellEnv, actionShell, actionAdd, actionManual

DevelopActionType* = enum
datAdd, datRemoveByPath, datRemoveByName, datInclude, datExclude
Expand All @@ -80,7 +80,7 @@ type
Action* = object
case typ*: ActionType
of actionNil, actionList, actionPublish, actionTasks, actionCheck,
actionSetup, actionClean: nil
actionSetup, actionClean, actionManual: nil
of actionSync:
listOnly*: bool
of actionRefresh:
Expand Down Expand Up @@ -173,6 +173,7 @@ Commands:
build [opts, ...] [bin] Builds a package. Passes options to the Nim
compiler.
clean Clean build artifacts.
guide Open the Nimble User Guide in your preferred browser.
add Adds packages to your project's dependencies.
run [opts, ...] [bin] Builds and runs a package.
Binary needs to be specified after any
Expand Down Expand Up @@ -339,6 +340,8 @@ proc parseActionType*(action: string): ActionType =
result = actionShell
of "add":
result = actionAdd
of "manual", "guide":
result = actionManual
else:
result = actionCustom

Expand Down Expand Up @@ -918,4 +921,4 @@ proc isSubdirOf*(subdir, baseDir: string): bool =
when defined(windows):
normalizedSubdir.toLower.startsWith(normalizedBaseDir.toLower)
else:
normalizedSubdir.startsWith(normalizedBaseDir)
normalizedSubdir.startsWith(normalizedBaseDir)

0 comments on commit 30e7014

Please sign in to comment.