Skip to content

Commit

Permalink
Implement nimble shellenv and nimble shell
Browse files Browse the repository at this point in the history
Added commands `nimble shell` and `nimble shellenv` to allow using shell in the
scope of the project
  • Loading branch information
yyoncho committed Mar 21, 2023
1 parent 371f4b6 commit dbbd667
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/nimble.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const
gitIgnoreFileName = ".gitignore"
hgIgnoreFileName = ".hgignore"
nimblePathsEnv = "__NIMBLE_PATHS"
separator = when defined(windows): ";" else: ":"

proc refresh(options: Options) =
## Downloads the package list from the specified URL.
Expand Down Expand Up @@ -1994,6 +1995,36 @@ proc setup(options: Options) =
setupNimbleConfig(options)
setupVcsIgnoreFile()

proc getAlteredPath(options: Options): string =
let pkgInfo = getPkgInfo(getCurrentDir(), options)
var pkgs = pkgInfo.processAllDependencies(options).toSeq.toOrderedSet
pkgs.incl(pkgInfo)

result = getEnv("PATH")
for pkg in pkgs:
let fullInfo = pkg.toFullInfo(options)
for bin, _ in fullInfo.bin:
let folder = fullInfo.getOutputDir(bin).parentDir.quoteShell
result = fmt "{folder}{separator}{result}"

proc shellenv(options: var Options) =
setVerbosity(SilentPriority)
options.verbosity = SilentPriority
const prefix = when defined(windows): "set PATH=" else: "export PATH="
echo prefix & getAlteredPath(options)

proc shell(options: Options) =
putEnv("PATH", getAlteredPath(options))

when defined windows:
var shell = getEnv("ComSpec")
if shell == "": shell = "powershell"
else:
var shell = getEnv("SHELL")
if shell == "": shell = "/bin/bash"

discard waitForExit startProcess(shell, options = {poParentStreams})

proc getPackageForAction(pkgInfo: PackageInfo, options: Options): PackageInfo =
## Returns the `PackageInfo` for the package in `pkgInfo`'s dependencies tree
## with the name specified in `options.package`. If `options.package` is empty
Expand Down Expand Up @@ -2102,6 +2133,10 @@ proc doAction(options: var Options) =
sync(options)
of actionSetup:
setup(options)
of actionShellEnv:
shellenv(options)
of actionShell:
shell(options)
of actionNil:
assert false
of actionCustom:
Expand Down
13 changes: 12 additions & 1 deletion src/nimblepkg/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type
actionInstall, actionSearch, actionList, actionBuild, actionPath,
actionUninstall, actionCompile, actionDoc, actionCustom, actionTasks,
actionDevelop, actionCheck, actionLock, actionRun, actionSync, actionSetup,
actionClean, actionDeps
actionClean, actionDeps, actionShellEnv, actionShell

DevelopActionType* = enum
datAdd, datRemoveByPath, datRemoveByName, datInclude, datExclude
Expand Down Expand Up @@ -106,6 +106,8 @@ type
custRunFlags*: seq[string]
of actionDeps:
format*: string
of actionShellEnv, actionShell:
discard

const
help* = """
Expand Down Expand Up @@ -205,6 +207,11 @@ Commands:
system paths to the dependencies. Also
includes the paths file in the `config.nims`
file to make them available for the compiler.
shell Creates a new shell with PATH modified to contain
the bin folders of the dependencies.
shellenv Similar to shell command but it returns the script to run in
order to alter the environment. This is intended to be
used in scripts.
Nimble Options:
-h, --help Print this help message.
Expand Down Expand Up @@ -300,6 +307,10 @@ proc parseActionType*(action: string): ActionType =
result = actionSync
of "setup":
result = actionSetup
of "shellenv":
result = actionShellEnv
of "shell":
result = actionShell
else:
result = actionCustom

Expand Down
28 changes: 28 additions & 0 deletions tests/shellenv/nimble.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"version": 2,
"packages": {
"unittest2": {
"version": "0.0.6",
"vcsRevision": "883c7a50ad3b82158e64d074c5578fe33ab3c452",
"url": "https://github.com/status-im/nim-unittest2",
"downloadMethod": "git",
"dependencies": [],
"checksums": {
"sha1": "540e3959bd489b423dcd7d5f6373fa43153c35c8"
}
},
"testutils": {
"version": "0.5.0",
"vcsRevision": "dfc4c1b39f9ded9baf6365014de2b4bfb4dafc34",
"url": "https://github.com/status-im/nim-testutils",
"downloadMethod": "git",
"dependencies": [
"unittest2"
],
"checksums": {
"sha1": "756d0757c4dd06a068f9d38c7f238576ba5ee897"
}
}
},
"tasks": {}
}
11 changes: 11 additions & 0 deletions tests/shellenv/shellenv.nimble
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Package

version = "0.1.0"
author = "Ivan Yonchovski"
description = "A new awesome nimble package"
license = "MIT"
srcDir = "src"
bin = @["demo"]

# Dependencies
requires "testutils == 0.5.0"
Empty file added tests/shellenv/src/demo.nim
Empty file.
1 change: 1 addition & 0 deletions tests/tester.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import tnimbledump
import tnimblerefresh
import tnimbletasks
import tnimscript
import tshellenv
import tpathcommand
import treversedeps
import truncommand
Expand Down
30 changes: 30 additions & 0 deletions tests/tshellenv.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (C) Dominik Picheta. All rights reserved.
# BSD License. Look at license.txt for more info.

{.used.}

import unittest, os, osproc, strutils
import testscommon
from nimblepkg/common import cd

const
separator = when defined(windows): ";" else: ":"

suite "Shell env":
test "Shell env":
cd "shellenv":
let (output, exitCode) = execCmdEx(nimblePath & " shellenv")
check exitCode == QuitSuccess
let
prefixValPair = split(output, "=")
prefix = prefixValPair[0]
value = prefixValPair[1]
dirs = value.split(separator)

when defined windows:
check prefix == "set PATH"
else:
check prefix == "export PATH"

check dirs[0].extractFileName == "shellenv"
check dirs[1].extractFileName == "testutils-0.5.0-756d0757c4dd06a068f9d38c7f238576ba5ee897"

0 comments on commit dbbd667

Please sign in to comment.