Skip to content

Commit

Permalink
feat: Make mackupApplicationsDir func agnostic to mackup's installati…
Browse files Browse the repository at this point in the history
…on method
  • Loading branch information
ronlut authored and twpayne committed Dec 11, 2023
1 parent 4ea2d5b commit 31780bf
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 54 deletions.
53 changes: 23 additions & 30 deletions internal/cmd/mackupcmd_darwin.go
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

Expand All @@ -21,7 +22,6 @@ var (
mackupCommentRx = regexp.MustCompile(`\A#.*\z`)
mackupKeyValueRx = regexp.MustCompile(`\A(\w+)\s*=\s*(.*)\z`)
mackupSectionRx = regexp.MustCompile(`\A\[(.*)\]\z`)
mackupVersionRx = regexp.MustCompile(`\AMackup\s+(\d+\.\d+\.\d+)\s*\z`)
)

type mackupApplicationApplicationConfig struct {
Expand Down Expand Up @@ -122,49 +122,42 @@ func (c *Config) runMackupAddCmd(
}

func (c *Config) mackupApplicationsDir() (chezmoi.AbsPath, error) {
brewPrefixCmd := exec.Command("brew", "--prefix")
brewPrefixData, err := brewPrefixCmd.Output()
mackupBinaryPath, err := exec.LookPath("mackup")
if err != nil {
return chezmoi.EmptyAbsPath, err
return chezmoi.EmptyAbsPath, fmt.Errorf("mackup binary not found in PATH (%w)", err)
}
brewPrefix := chezmoi.NewAbsPath(strings.TrimRight(string(brewPrefixData), "\n"))

mackupVersionCmd := exec.Command("mackup", "--version")
mackupVersionData, err := mackupVersionCmd.Output()
mackupBinaryPathResolved, err := filepath.EvalSymlinks(mackupBinaryPath)
if err != nil {
return chezmoi.EmptyAbsPath, err
}
mackupVersionMatch := mackupVersionRx.FindSubmatch(mackupVersionData)
if mackupVersionMatch == nil {
return chezmoi.EmptyAbsPath, fmt.Errorf(
"%q: cannot determine Mackup version",
mackupVersionData,
)
}
mackupVersion := string(mackupVersionMatch[1])
mackupBinaryPathAbs := chezmoi.NewAbsPath(mackupBinaryPathResolved)

libDirAbsPath := brewPrefix.JoinString("Cellar", "mackup", mackupVersion, "libexec", "lib")
libDirAbsPath := mackupBinaryPathAbs.Dir().Dir().JoinString("lib")
dirEntries, err := c.baseSystem.ReadDir(libDirAbsPath)
if err != nil {
return chezmoi.EmptyAbsPath, err
}
var pythonDirRelPath chezmoi.RelPath

for _, dirEntry := range dirEntries {
if dirEntry.IsDir() && strings.HasPrefix(dirEntry.Name(), "python") {
pythonDirRelPath = chezmoi.NewRelPath(dirEntry.Name())
break
if !dirEntry.IsDir() || !strings.HasPrefix(dirEntry.Name(), "python") {
continue
}
}
if pythonDirRelPath.Empty() {
return chezmoi.EmptyAbsPath, fmt.Errorf(
"%s: could not find python directory",
libDirAbsPath,
)

pythonDirRelPath := chezmoi.NewRelPath(dirEntry.Name())
mackupAppsDir := libDirAbsPath.Join(pythonDirRelPath).
JoinString("site-packages", "mackup", "applications")

if _, err := os.Stat(mackupAppsDir.String()); errors.Is(err, os.ErrNotExist) {
continue
}

return mackupAppsDir, nil
}

return libDirAbsPath.Join(pythonDirRelPath).
JoinString("site-packages", "mackup", "applications"),
nil
return chezmoi.EmptyAbsPath, fmt.Errorf(
"mackup python directory cannot be found: %s",
libDirAbsPath,
)
}

func parseMackupApplication(data []byte) (mackupApplicationConfig, error) {
Expand Down
29 changes: 5 additions & 24 deletions internal/cmd/testdata/scripts/mackupbrew_darwin.txtar
@@ -1,8 +1,9 @@
[!darwin] skip 'Darwin only'

# simulate a brew installation of mackup
chmod 755 opt/homebrew/Cellar/mackup/0.8.32/bin/mackup
chmod 755 opt/homebrew/bin/brew
chmod 755 opt/homebrew/Cellar/mackup/0.8.32/libexec/bin/mackup
mkdir opt/homebrew/Cellar/mackup/0.8.32/bin opt/homebrew/bin
symlink opt/homebrew/Cellar/mackup/0.8.32/bin/mackup -> ../libexec/bin/mackup
symlink opt/homebrew/bin/mackup -> ../Cellar/mackup/0.8.32/bin/mackup
env PATH=$WORK/opt/homebrew/bin:$PATH

Expand All @@ -29,17 +30,8 @@ name = Curl
[configuration_files]
.netrc
.curlrc
-- opt/homebrew/Cellar/mackup/0.8.32/bin/mackup --
#!/bin/sh

case "$*" in
"--version")
echo "Mackup 0.8.32"
;;
*)
echo "Usage:"
;;
esac
-- opt/homebrew/Cellar/mackup/0.8.32/libexec/bin/mackup --
# mackup binary
-- opt/homebrew/Cellar/mackup/0.8.32/libexec/lib/python3.9/site-packages/mackup/applications/vscode.cfg --
[application]
name = Visual Studio Code
Expand All @@ -53,14 +45,3 @@ Library/Application Support/Code/User/settings.json
Code/User/snippets
Code/User/keybindings.json
Code/User/settings.json
-- opt/homebrew/bin/brew --
#!/bin/sh

case "$*" in
"--prefix")
echo "opt/homebrew"
;;
*)
echo "Error: Unknown command $*"
;;
esac
46 changes: 46 additions & 0 deletions internal/cmd/testdata/scripts/mackupmacports_darwin.txtar
@@ -0,0 +1,46 @@
[!darwin] skip 'Darwin only'

# simulate a macports installation of mackup
chmod 755 opt/local/Library/Frameworks/Python.framework/Versions/3.11/bin/mackup
mkdir opt/local/bin
symlink opt/local/bin/mackup -> ../Library/Frameworks/Python.framework/Versions/3.11/bin/mackup
env PATH=$WORK/opt/local/bin:$PATH

# test that chezmoi mackup add adds normal dotfiles
exec chezmoi mackup add curl
cmp $CHEZMOISOURCEDIR/dot_curlrc golden/dot_curlrc

# test that chezmoi mackup add adds XDG configuration files
exec chezmoi mackup add vscode
cmp $CHEZMOISOURCEDIR/dot_config/Code/User/settings.json golden/settings.json

-- golden/dot_curlrc --
# contents of .curlrc
-- golden/settings.json --
# contents of .config/Code/User/settings.json
-- home/user/.config/Code/User/settings.json --
# contents of .config/Code/User/settings.json
-- home/user/.curlrc --
# contents of .curlrc
-- home/user/.mackup/curl.cfg --
[application]
name = Curl

[configuration_files]
.netrc
.curlrc
-- opt/local/Library/Frameworks/Python.framework/Versions/3.11/bin/mackup --
# mackup binary
-- opt/local/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/mackup/applications/vscode.cfg --
[application]
name = Visual Studio Code

[configuration_files]
Library/Application Support/Code/User/snippets
Library/Application Support/Code/User/keybindings.json
Library/Application Support/Code/User/settings.json

[xdg_configuration_files]
Code/User/snippets
Code/User/keybindings.json
Code/User/settings.json
46 changes: 46 additions & 0 deletions internal/cmd/testdata/scripts/mackuppip_darwin.txtar
@@ -0,0 +1,46 @@
[!darwin] skip 'Darwin only'

# simulate a pip installation of mackup
chmod 755 usr/local/bin/mackup
# version 3.9 of python without mackup
mkdir usr/local/lib/python3.9/site-packages
env PATH=$WORK/usr/local/bin:$PATH

# test that chezmoi mackup add adds normal dotfiles
exec chezmoi mackup add curl
cmp $CHEZMOISOURCEDIR/dot_curlrc golden/dot_curlrc

# test that chezmoi mackup add adds XDG configuration files
exec chezmoi mackup add vscode
cmp $CHEZMOISOURCEDIR/dot_config/Code/User/settings.json golden/settings.json

-- golden/dot_curlrc --
# contents of .curlrc
-- golden/settings.json --
# contents of .config/Code/User/settings.json
-- home/user/.config/Code/User/settings.json --
# contents of .config/Code/User/settings.json
-- home/user/.curlrc --
# contents of .curlrc
-- home/user/.mackup/curl.cfg --
[application]
name = Curl

[configuration_files]
.netrc
.curlrc
-- usr/local/bin/mackup --
# mackup binary
-- usr/local/lib/python3.11/site-packages/mackup/applications/vscode.cfg --
[application]
name = Visual Studio Code

[configuration_files]
Library/Application Support/Code/User/snippets
Library/Application Support/Code/User/keybindings.json
Library/Application Support/Code/User/settings.json

[xdg_configuration_files]
Code/User/snippets
Code/User/keybindings.json
Code/User/settings.json
46 changes: 46 additions & 0 deletions internal/cmd/testdata/scripts/mackuppipx_darwin.txtar
@@ -0,0 +1,46 @@
[!darwin] skip 'Darwin only'

# simulate a pipx installation of mackup
chmod 755 home/.local/pipx/venvs/mackup/bin/mackup
mkdir home/.local/bin
symlink home/.local/bin/mackup -> ../pipx/venvs/mackup/bin/mackup
env PATH=$WORK/home/.local/bin:$PATH

# test that chezmoi mackup add adds normal dotfiles
exec chezmoi mackup add curl
cmp $CHEZMOISOURCEDIR/dot_curlrc golden/dot_curlrc

# test that chezmoi mackup add adds XDG configuration files
exec chezmoi mackup add vscode
cmp $CHEZMOISOURCEDIR/dot_config/Code/User/settings.json golden/settings.json

-- golden/dot_curlrc --
# contents of .curlrc
-- golden/settings.json --
# contents of .config/Code/User/settings.json
-- home/.local/pipx/venvs/mackup/bin/mackup --
# mackup binary
-- home/.local/pipx/venvs/mackup/lib/python3.11/site-packages/mackup/applications/vscode.cfg --
[application]
name = Visual Studio Code

[configuration_files]
Library/Application Support/Code/User/snippets
Library/Application Support/Code/User/keybindings.json
Library/Application Support/Code/User/settings.json

[xdg_configuration_files]
Code/User/snippets
Code/User/keybindings.json
Code/User/settings.json
-- home/user/.config/Code/User/settings.json --
# contents of .config/Code/User/settings.json
-- home/user/.curlrc --
# contents of .curlrc
-- home/user/.mackup/curl.cfg --
[application]
name = Curl

[configuration_files]
.netrc
.curlrc

0 comments on commit 31780bf

Please sign in to comment.