Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: install to the user home when using pipx as sudo #225

Merged
merged 9 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ words:
- copr
- CPATH
- Cppcheck
- nodistro
- dearmor
- CPPFLAGS
- cpprc
- Cpython
- DCMAKE
- dearmor
- deps
- devel
- DVCPKG
Expand Down Expand Up @@ -63,6 +62,7 @@ words:
- mxschmitt
- nala
- noconfirm
- nodistro
- noprogressbar
- nothrow
- npmrc
Expand All @@ -84,13 +84,15 @@ words:
- Trofimovich
- tsbuildinfo
- ucrt
- untildified
- untildify
- upleveled
- vbatts
- vcpkg
- VCPKG
- vcvarsall
- venv
- venvs
- visualc
- visualcpp
- vsversion
Expand Down
32 changes: 16 additions & 16 deletions dist/actions/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/actions/setup-cpp.js.map

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions dist/legacy/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/legacy/setup-cpp.js.map

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions dist/modern/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/modern/setup-cpp.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"is-url-online": "^1.5.0",
"jest": "^29.7.0",
"micro-memoize": "^4.1.2",
"mkdirp": "^3.0.1",
"mri": "^1.2.0",
"msvc-dev-cmd": "github:aminya/msvc-dev-cmd#97843d525947e3f3776ee359b597316909754c4d",
"npm-check-updates": "^16.14.12",
Expand Down
14 changes: 12 additions & 2 deletions packages/untildify-user/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@ npm install --save untildify-user

<!-- INSERT GENERATED DOCS START -->

### `userHomeDir` (function)

**returns:** string

### `untildifyUser` (function)

Replaces a tilde with the user's home directory

**Parameters:**

- path (`string`)
- path (`string`) - The path to untildify

**returns:** any
**returns:** string

```tsx
UntildifyUser("~/foo") // /home/user/foo
```

<!-- INSERT GENERATED DOCS END -->

Expand Down
3 changes: 1 addition & 2 deletions packages/untildify-user/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"build": "tsc"
},
"dependencies": {
"admina": "1.0.1",
"untildify": "^5.0.0"
"admina": "1.0.1"
},
"keywords": [
"tilde",
Expand Down
35 changes: 29 additions & 6 deletions packages/untildify-user/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import { join } from "path"
import untildify from "untildify"
import { isSudo } from "admina"
import { homedir } from "os"

export function untildifyUser(path: string) {
if (isSudo() && typeof process.env.SUDO_USER === "string") {
export function userHomeDir() {
if (isSudo() && typeof process.env.SUDO_USER === "string" && process.env.SUDO_USER !== "") {
// use the user profile even if root
if (process.platform === "darwin") {
return join("/Users/", process.env.SUDO_USER, path)
return join("/Users/", process.env.SUDO_USER)
} else {
return join("/home/", process.env.SUDO_USER, path)
return join("/home/", process.env.SUDO_USER)
}
} else {
return untildify(`~/${path}`)
const maybeHomeDir = homedir()
if (maybeHomeDir === "") {
return undefined
}
return maybeHomeDir
}
}

const tildeRegex = /^~(?=$|\/|\\)/

/**
* Replaces a tilde with the user's home directory
*
* @example UntildifyUser("~/foo") // /home/user/foo
*
* @param path The path to untildify
* @returns The untildified path
*/
export function untildifyUser(path: string) {
const maybeHomeDir = userHomeDir()
if (maybeHomeDir === undefined) {
return path
}

return path.replace(tildeRegex, maybeHomeDir)
}
17 changes: 9 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/kcov/kcov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ async function getCmake() {
if (cmake === null) {
const { binDir } = await setupCmake(
getVersion("cmake", undefined, await ubuntuVersion()),
join(untildifyUser(""), "cmake"),
join(untildifyUser("~"), "cmake"),
"",
)
cmake = join(binDir, "cmake")
}
const ninja = which.sync("ninja", { nothrow: true })
if (ninja === null) {
await setupNinja(getVersion("ninja", undefined, await ubuntuVersion()), join(untildifyUser(""), "ninja"), "")
await setupNinja(getVersion("ninja", undefined, await ubuntuVersion()), join(untildifyUser("~"), "ninja"), "")
}
return cmake
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function main(args: string[]): Promise<number> {
const arch = opts.architecture ?? process.arch

// the installation dir for the tools that are downloaded directly
const setupCppDir = process.env.SETUP_CPP_DIR ?? untildifyUser("")
const setupCppDir = process.env.SETUP_CPP_DIR ?? untildifyUser("~")

// report messages
const successMessages: string[] = []
Expand Down
8 changes: 7 additions & 1 deletion src/nala/__tests__/nala.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ describe("setup-nala", () => {
afterAll(() => {
// remove nala to run the rest of the tests with apt-get
execRootSync("apt-get", ["remove", "-y", "nala"])
execRootSync("apt-get", ["remove", "-y", "nala-legacy"])

try {
execRootSync("apt-get", ["remove", "-y", "nala-legacy"])
} catch (err) {
// ignore
console.error(err)
}
})
})
3 changes: 2 additions & 1 deletion src/python/__tests__/python.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ describe("setup-python", () => {

const installInfo = await setupPython(getVersion("python", "true", await ubuntuVersion()), directory, process.arch)

await testBin("python", ["--version"], installInfo.binDir)
const python = process.platform === "darwin" ? "python3" : "python"
await testBin(python, ["--version"], installInfo.binDir)
})

afterAll(async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/env/addEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export async function addPath(path: string) {
}
}

export const cpprc_path = untildifyUser(".cpprc")
export const cpprc_path = untildifyUser("~/.cpprc")

async function addEnvSystem(name: string, valGiven: string | undefined, options: AddEnvOptions) {
const val = valGiven ?? ""
Expand Down Expand Up @@ -188,12 +188,12 @@ export async function setupCppInProfile() {

try {
// source cpprc in .profile
const profile_path = untildifyUser(".profile")
const profile_path = untildifyUser("~/.profile")
appendFileSync(profile_path, source_cpprc_string)
info(`${source_cpprc_string} was added to ${profile_path}`)

// source cpprc in .bashrc too
const bashrc_path = untildifyUser(".bashrc")
const bashrc_path = untildifyUser("~/.bashrc")
appendFileSync(bashrc_path, source_cpprc_string)
info(`${source_cpprc_string} was added to ${bashrc_path}`)
} catch (err) {
Expand Down
58 changes: 58 additions & 0 deletions src/utils/setup/setupPipPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { hasDnf } from "../env/hasDnf"
import { setupDnfPack } from "./setupDnfPack"
import { isUbuntu } from "../env/isUbuntu"
import { setupAptPack } from "./setupAptPack"
import { untildifyUser } from "untildify-user"
import { mkdirp } from "mkdirp"

export type SetupPipPackOptions = {
/** Whether to use pipx instead of pip */
Expand Down Expand Up @@ -56,8 +58,17 @@ export async function setupPipPackWithPython(
const upgradeFlag = upgrade ? (isPipx ? ["upgrade"] : ["install", "--upgrade"]) : ["install"]
const userFlag = !isPipx && user ? ["--user"] : []

const env = process.env

if (isPipx && user) {
// install to user home
env.PIPX_HOME = await getPipxHome()
env.PIPX_BIN_DIR = await getPipxBinDir()
}

execaSync(givenPython, ["-m", pip, ...upgradeFlag, ...userFlag, nameAndVersion], {
stdio: "inherit",
env,
})
} catch (err) {
info(`Failed to install ${name} via ${pip}: ${err}.`)
Expand All @@ -83,6 +94,53 @@ export async function hasPipx(givenPython: string) {
return (await execa(givenPython, ["-m", "pipx", "--help"], { stdio: "ignore", reject: false })).exitCode === 0
}

async function getPipxHome_raw() {
let pipxHome = process.env.PIPX_HOME
if (pipxHome !== undefined) {
return pipxHome
}

// Based on https://pipx.pypa.io/stable/installation/
const compatHome = untildifyUser("~/.local/pipx")
if (await pathExists(compatHome)) {
return compatHome
}

switch (process.platform) {
case "win32": {
pipxHome = untildifyUser("~/AppData/Local/pipx")
break
}
case "darwin": {
pipxHome = untildifyUser("~/Library/Application Support/pipx")
break
}
default: {
pipxHome = untildifyUser("~/.local/share/pipx")
break
}
}

await mkdirp(pipxHome)
await mkdirp(join(pipxHome, "trash"))
await mkdirp(join(pipxHome, "shared"))
await mkdirp(join(pipxHome, "venv"))
return pipxHome
}
const getPipxHome = memoize(getPipxHome_raw, { isPromise: true })

async function getPipxBinDir_raw() {
if (process.env.PIPX_BIN_DIR !== undefined) {
return process.env.PIPX_BIN_DIR
}

const pipxBinDir = untildifyUser("~/.local/bin")
await addPath(pipxBinDir)
await mkdirp(pipxBinDir)
return pipxBinDir
}
const getPipxBinDir = memoize(getPipxBinDir_raw, { isPromise: true })

async function getPython_raw(): Promise<string> {
const pythonBin = (await setupPython(getVersion("python", undefined, await ubuntuVersion()), "", process.arch)).bin
if (pythonBin === undefined) {
Expand Down
Loading