Skip to content

Commit

Permalink
Merge pull request #44 from mikoto2000/support-mac
Browse files Browse the repository at this point in the history
Apple Silicon 系 macOS 対応を追加。
  • Loading branch information
mikoto2000 authored Dec 1, 2024
2 parents 6e3d18a + 776060e commit ff25717
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 65 deletions.
24 changes: 16 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
APP_NAME := devcontainer.vim
GOARCH := amd64
WINDOWS_BINARY_NAME := ${APP_NAME}-windows-${GOARCH}.exe
LINUX_BINARY_NAME := ${APP_NAME}-linux-${GOARCH}
DARWIN_BINARY_NAME := ${APP_NAME}-darwin-${GOARCH}
GOARCH_AMD64 := amd64
GOARCH_ARM64 := arm64
WINDOWS_BINARY_NAME := ${APP_NAME}-windows-${GOARCH_AMD64}.exe
LINUX_BINARY_NAME := ${APP_NAME}-linux-${GOARCH_AMD64}
DARWIN_BINARY_NAME := ${APP_NAME}-darwin-${GOARCH_ARM64}

GO_BIN := ${GOPATH}/bin
VERSION := 1.3.1
Expand All @@ -13,15 +14,22 @@ DEST := ./build
WATCH_SRC := ./main.go \
./devcontainer/DevcontainerJson.go \
./devcontainer/devcontainer.go \
./devcontainer/dockerVimArgs_darwin_arm64.go \
./devcontainer/dockerVimArgs_linux_amd64.go \
./devcontainer/dockerVimArgs_windows_amd64.go \
./devcontainer/readConfigurationResult.go \
./devcontainer/upCommandResult.go \
./docker/docker.go \
./docker/dockerPsResult.go \
./dockercompose/dockerCompose.go \
./dockercompose/dockerComposePsResult.go \
./tools/tools.go \
./tools/vim_linux_amd64.go \
./tools/vim_darwin_arm64.go \
./tools/vim_windows_amd64.go \
./tools/devcontainer.go \
./tools/devcontainer_nowindows.go \
./tools/devcontainer_darwin.go \
./tools/devcontainer_linux.go \
./tools/devcontainer_windows.go \
./tools/clipboard-data-receiver.go \
./util/util.go
Expand All @@ -38,15 +46,15 @@ build-all: build-windows build-linux build-darwin

build-windows: build/${WINDOWS_BINARY_NAME}
build/${WINDOWS_BINARY_NAME}: ${WATCH_SRC}
GOOS=windows GOARCH=${GOARCH} go build -ldflags=${LD_FLAGS} -trimpath -o build/${WINDOWS_BINARY_NAME}
GOOS=windows GOARCH=${GOARCH_AMD64} go build -ldflags=${LD_FLAGS} -trimpath -o build/${WINDOWS_BINARY_NAME}

build-linux: build/${LINUX_BINARY_NAME}
build/${LINUX_BINARY_NAME}: ${WATCH_SRC}
GOOS=linux GOARCH=${GOARCH} go build -ldflags=${LD_FLAGS} -trimpath -o build/${LINUX_BINARY_NAME}
GOOS=linux GOARCH=${GOARCH_AMD64} go build -ldflags=${LD_FLAGS} -trimpath -o build/${LINUX_BINARY_NAME}

build-darwin: build/${DARWIN_BINARY_NAME}
build/${DARWIN_BINARY_NAME}: ${WATCH_SRC}
GOOS=darwin GOARCH=${GOARCH} go build -ldflags=${LD_FLAGS} -trimpath -o build/${DARWIN_BINARY_NAME}
GOOS=darwin GOARCH=${GOARCH_ARM64} go build -ldflags=${LD_FLAGS} -trimpath -o build/${DARWIN_BINARY_NAME}

.PHONY: lint
lint:
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ VSCode 向けに作成された `devcontainer.json` に追加する形で Vim
- docker


ARM の場合、コンテナ内に `tar` コマンドが存在していること


## Usage:

```
Expand Down Expand Up @@ -301,9 +304,9 @@ devcontainer.vim runargs -g

## Limitation:

- amd64 のコンテナしか使用できません
- alpine 系のコンテナでは使用できません
- arm 系 macOS では動作しません
- Windows, Linux では、amd64 のコンテナしか使用できません
- amd64 では、 alpine 系のコンテナでは使用できません
- macOS では、arm64 のコンテナしか使用できません


## Install:
Expand Down
8 changes: 5 additions & 3 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ The following commands are installed and in the PATH.

- docker

For ARM, the `tar` command must be present in the container.


## Usage:

Expand Down Expand Up @@ -310,9 +312,9 @@ devcontainer.vim runargs -g

## Limitation:

- Only amd64 containers can be used
- Not supported in Alpine-based containers
- Does not work on arm-based macOS
- On Windows and Linux, only amd64 containers can be used.
- On amd64, alpine-based containers cannot be used.
- On macOS, only arm64 containers can be used.


## Install:
Expand Down
20 changes: 4 additions & 16 deletions devcontainer/devcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (e *UnknownTypeError) Error() string {

// devcontainer でコンテナを立ち上げ、 Vim を転送し、実行する。
// 既存実装の都合上、configFilePath から configDirForDevcontainer を抽出している
func ExecuteDevcontainer(args []string, devcontainerPath string, vimFilePath string, cdrPath, configFilePath string, vimrc string) error {
func Start(args []string, devcontainerPath string, vimFilePath string, cdrPath, configFilePath string, vimrc string) error {

vimFileName := filepath.Base(vimFilePath)

Expand Down Expand Up @@ -71,15 +71,11 @@ func ExecuteDevcontainer(args []string, devcontainerPath string, vimFilePath str
// コンテナへ appimage を転送して実行権限を追加
// `docker cp <os.UserCacheDir/devcontainer.vim/Vim-AppImage> <dockerrun 時に標準出力に表示される CONTAINER ID>:/`
containerID := upCommandResult.ContainerID
dockerCpArgs := []string{"cp", vimFilePath, containerID + ":/"}
fmt.Printf("Copy AppImage: `%s \"%s\"` ...", containerCommand, strings.Join(dockerCpArgs, "\" \""))
copyResult, err := exec.Command(containerCommand, dockerCpArgs...).CombinedOutput()

err = docker.Cp("vim", vimFilePath, containerID, "/")
if err != nil {
fmt.Fprintln(os.Stderr, "AppImage copy error.")
fmt.Fprintln(os.Stderr, string(copyResult))
return err
}
fmt.Printf(" done.\n")

// `docker exec <dockerrun 時に標準出力に表示される CONTAINER ID> chmod +x /Vim-AppImage`
dockerChownArgs := []string{"exec", "--user", "root", containerID, "sh", "-c", "chmod +x /" + vimFileName}
Expand Down Expand Up @@ -116,15 +112,7 @@ func ExecuteDevcontainer(args []string, devcontainerPath string, vimFilePath str
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

dockerVimArgs := []string{
"exec",
"--container-id",
containerID,
"--workspace-folder",
workspaceFolder,
"sh",
"-c",
"cd ~; /" + vimFileName + " --appimage-extract > /dev/null; cd -; ~/squashfs-root/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /SendToTcp.vim -S /vimrc"}
dockerVimArgs := DockerVimArgs(containerID, workspaceFolder, vimFileName)
fmt.Printf("Start vim: `%s \"%s\"`\n", devcontainerPath, strings.Join(dockerVimArgs, "\" \""))
dockerExec := exec.CommandContext(ctx, devcontainerPath, dockerVimArgs...)
dockerExec.Stdin = os.Stdin
Expand Down
18 changes: 18 additions & 0 deletions devcontainer/dockerVimArgs_darwin_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build darwin && arm64

package devcontainer

func DockerVimArgs(containerID string, workspaceFolder string, vimFileName string) []string {
return []string{
"exec",
"--container-id",
containerID,
"--workspace-folder",
workspaceFolder,
"sh",
"-c",
"cd /; tar zxf ./" + vimFileName + " -C ~/ > /dev/null; cd ~; sudo rm -rf ~/vim-static; mv $(ls -d ~/vim-*-aarch64) ~/vim-static;~/vim-static/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /SendToTcp.vim -S /vimrc"}

}


16 changes: 16 additions & 0 deletions devcontainer/dockerVimArgs_linux_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build linux && amd64

package devcontainer

func DockerVimArgs(containerID string, workspaceFolder string, vimFileName string) []string {
return []string{
"exec",
"--container-id",
containerID,
"--workspace-folder",
workspaceFolder,
"sh",
"-c",
"cd ~; /" + vimFileName + " --appimage-extract > /dev/null; cd -; ~/squashfs-root/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /SendToTcp.vim -S /vimrc"}

}
17 changes: 17 additions & 0 deletions devcontainer/dockerVimArgs_windows_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build windows && amd64

package devcontainer

func DockerVimArgs(containerID string, workspaceFolder string, vimFileName string) []string {
return []string{
"exec",
"--container-id",
containerID,
"--workspace-folder",
workspaceFolder,
"sh",
"-c",
"cd ~; /" + vimFileName + " --appimage-extract > /dev/null; cd -; ~/squashfs-root/AppRun --cmd \"let g:devcontainer_vim = v:true\" -S /SendToTcp.vim -S /vimrc"}

}

2 changes: 2 additions & 0 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,5 @@ func Cp(tagForLog string, from string, containerID string, to string) error {
fmt.Printf(" done.\n")
return nil
}

// TODO: Exec
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func main() {
}

// devcontainer を用いたコンテナ立ち上げ
err = devcontainer.ExecuteDevcontainer(args, devcontainerPath, vimPath, cdrPath, configFilePath, vimrc)
err = devcontainer.Start(args, devcontainerPath, vimPath, cdrPath, configFilePath, vimrc)
if err != nil {
if errors.Is(err, os.ErrPermission) {
fmt.Fprintf(os.Stderr, "Permission error: %v\n", err)
Expand Down
5 changes: 5 additions & 0 deletions tools/clipboard-data-receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import (
)

const CdrFileName = "clipboard-data-receiver"
const CdrFileNameForMac = "clipboard-data-receiver"
const cdrFileNameForWindows = "clipboard-data-receiver.exe"

// clipboard-data-receiver のダウンロード URL
const downloadURLCdrPattern = "https://github.com/mikoto2000/clipboard-data-receiver/releases/download/{{ .TagName }}/clipboard-data-receiver.linux-amd64"
const downloadURLCdrPatternForMac = "https://github.com/mikoto2000/clipboard-data-receiver/releases/download/{{ .TagName }}/clipboard-data-receiver.darwin-amd64"
const downloadURLCdrPatternForWindows = "https://github.com/mikoto2000/clipboard-data-receiver/releases/download/{{ .TagName }}/clipboard-data-receiver.windows-amd64.exe"

const vimScriptTemplateSendToCdr = `function! SendToCdr(register) abort
Expand All @@ -39,6 +41,9 @@ var CDR Tool = func() Tool {
if util.IsWsl() {
cdrFileName = cdrFileNameForWindows
tmpl, err = template.New("ducp").Parse(downloadURLCdrPatternForWindows)
} else if runtime.GOOS == "darwin" {
cdrFileName = CdrFileNameForMac
tmpl, err = template.New("ducp").Parse(downloadURLCdrPatternForMac)
} else {
cdrFileName = CdrFileName
tmpl, err = template.New("ducp").Parse(downloadURLCdrPattern)
Expand Down
9 changes: 9 additions & 0 deletions tools/devcontainer_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build darwin

package tools

const devcontainerFileName = "devcontainer"

// devcontainer-cli のダウンロード URL
const downloadURLDevcontainersCliPattern = "https://github.com/mikoto2000/devcontainers-cli/releases/download/{{ .TagName }}/devcontainer-darwin-arm64-{{ .TagName }}"

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !windows
//go:build linux

package tools

Expand Down
33 changes: 0 additions & 33 deletions tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"text/template"

"github.com/mikoto2000/devcontainer.vim/util"
)
Expand Down Expand Up @@ -54,37 +52,6 @@ func simpleInstall(downloadURL string, filePath string) (string, error) {
return filePath, nil
}

// Vim のダウンロード URL
const vimDownloadURLPattern = "https://github.com/vim/vim-appimage/releases/download/{{ .TagName }}/Vim-{{ .TagName }}.glibc2.29-x86_64.AppImage"

// Vim のツール情報
var VIM Tool = Tool{
FileName: "vim",
CalculateDownloadURL: func() string {
latestTagName, err := util.GetLatestReleaseFromGitHub("vim", "vim-appimage")
if err != nil {
panic(err)
}

pattern := "pattern"
tmpl, err := template.New(pattern).Parse(vimDownloadURLPattern)
if err != nil {
panic(err)
}

tmplParams := map[string]string{"TagName": latestTagName}
var downloadURL strings.Builder
err = tmpl.Execute(&downloadURL, tmplParams)
if err != nil {
panic(err)
}
return downloadURL.String()
},
installFunc: func(downloadURL string, filePath string) (string, error) {
return simpleInstall(downloadURL, filePath)
},
}

// 進捗表示用構造体
type ProgressWriter struct {
Total int64
Expand Down
53 changes: 53 additions & 0 deletions tools/vim_darwin_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//go:build darwin

package tools

import (
"strings"
"text/template"

"github.com/mikoto2000/devcontainer.vim/util"
)

// Vim のダウンロード URL
const vimDownloadURLPattern = "https://github.com/mikoto2000/vim-static/releases/download/{{ .TagName }}/vim-{{ .TagName }}-aarch64.tar.gz"

// Vim のツール情報
var VIM Tool = Tool{
FileName: "vim",
CalculateDownloadURL: func() string {
latestTagName, err := util.GetLatestReleaseFromGitHub("mikoto2000", "vim-static")
if err != nil {
panic(err)
}

pattern := "pattern"
tmpl, err := template.New(pattern).Parse(vimDownloadURLPattern)
if err != nil {
panic(err)
}

tmplParams := map[string]string{"TagName": latestTagName}
var downloadURL strings.Builder
err = tmpl.Execute(&downloadURL, tmplParams)
if err != nil {
panic(err)
}
return downloadURL.String()
},
installFunc: func(downloadURL string, filePath string) (string, error) {
_, err := simpleInstall(downloadURL, filePath)
if err != nil {
return "", err
}

// TODO, tar.gz を展開
//err = util.ExtractTarGz(filePath + ".tar.gz" ,filePath)
//if err != nil {
// return "", err
//}
return filePath, nil

},
}

43 changes: 43 additions & 0 deletions tools/vim_linux_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//go:build linux && amd64

package tools

import (
"strings"
"text/template"

"github.com/mikoto2000/devcontainer.vim/util"
)

// Vim のダウンロード URL
const vimDownloadURLPattern = "https://github.com/vim/vim-appimage/releases/download/{{ .TagName }}/Vim-{{ .TagName }}.glibc2.29-x86_64.AppImage"

// Vim のツール情報
var VIM Tool = Tool{
FileName: "vim",
CalculateDownloadURL: func() string {
latestTagName, err := util.GetLatestReleaseFromGitHub("vim", "vim-appimage")
if err != nil {
panic(err)
}

pattern := "pattern"
tmpl, err := template.New(pattern).Parse(vimDownloadURLPattern)
if err != nil {
panic(err)
}

tmplParams := map[string]string{"TagName": latestTagName}
var downloadURL strings.Builder
err = tmpl.Execute(&downloadURL, tmplParams)
if err != nil {
panic(err)
}
return downloadURL.String()
},
installFunc: func(downloadURL string, filePath string) (string, error) {
return simpleInstall(downloadURL, filePath)
},
}


Loading

0 comments on commit ff25717

Please sign in to comment.