diff --git a/Makefile b/Makefile index 82356a7..fce7fc7 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -13,6 +14,9 @@ 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 \ @@ -20,8 +24,12 @@ WATCH_SRC := ./main.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 @@ -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: diff --git a/README.md b/README.md index b230910..4c12ddf 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,9 @@ VSCode 向けに作成された `devcontainer.json` に追加する形で Vim - docker +ARM の場合、コンテナ内に `tar` コマンドが存在していること + + ## Usage: ``` @@ -301,9 +304,9 @@ devcontainer.vim runargs -g ## Limitation: -- amd64 のコンテナしか使用できません -- alpine 系のコンテナでは使用できません -- arm 系 macOS では動作しません +- Windows, Linux では、amd64 のコンテナしか使用できません +- amd64 では、 alpine 系のコンテナでは使用できません +- macOS では、arm64 のコンテナしか使用できません ## Install: diff --git a/README_en.md b/README_en.md index d24dab6..708fa4e 100644 --- a/README_en.md +++ b/README_en.md @@ -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: @@ -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: diff --git a/devcontainer/devcontainer.go b/devcontainer/devcontainer.go index acac90f..124ed8f 100644 --- a/devcontainer/devcontainer.go +++ b/devcontainer/devcontainer.go @@ -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) @@ -71,15 +71,11 @@ func ExecuteDevcontainer(args []string, devcontainerPath string, vimFilePath str // コンテナへ appimage を転送して実行権限を追加 // `docker cp :/` 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 chmod +x /Vim-AppImage` dockerChownArgs := []string{"exec", "--user", "root", containerID, "sh", "-c", "chmod +x /" + vimFileName} @@ -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 diff --git a/devcontainer/dockerVimArgs_darwin_arm64.go b/devcontainer/dockerVimArgs_darwin_arm64.go new file mode 100644 index 0000000..9f85766 --- /dev/null +++ b/devcontainer/dockerVimArgs_darwin_arm64.go @@ -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"} + +} + + diff --git a/devcontainer/dockerVimArgs_linux_amd64.go b/devcontainer/dockerVimArgs_linux_amd64.go new file mode 100644 index 0000000..a1ef104 --- /dev/null +++ b/devcontainer/dockerVimArgs_linux_amd64.go @@ -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"} + +} diff --git a/devcontainer/dockerVimArgs_windows_amd64.go b/devcontainer/dockerVimArgs_windows_amd64.go new file mode 100644 index 0000000..65ded61 --- /dev/null +++ b/devcontainer/dockerVimArgs_windows_amd64.go @@ -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"} + +} + diff --git a/docker/docker.go b/docker/docker.go index fbf7a45..6f2e82c 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -222,3 +222,5 @@ func Cp(tagForLog string, from string, containerID string, to string) error { fmt.Printf(" done.\n") return nil } + +// TODO: Exec diff --git a/main.go b/main.go index 6998194..b038858 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/tools/clipboard-data-receiver.go b/tools/clipboard-data-receiver.go index ad85ad8..5d486d6 100644 --- a/tools/clipboard-data-receiver.go +++ b/tools/clipboard-data-receiver.go @@ -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 @@ -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) diff --git a/tools/devcontainer_darwin.go b/tools/devcontainer_darwin.go new file mode 100644 index 0000000..e15943b --- /dev/null +++ b/tools/devcontainer_darwin.go @@ -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 }}" + diff --git a/tools/devcontainer_nowindows.go b/tools/devcontainer_linux.go similarity index 93% rename from tools/devcontainer_nowindows.go rename to tools/devcontainer_linux.go index 3cbfd8e..73fb525 100644 --- a/tools/devcontainer_nowindows.go +++ b/tools/devcontainer_linux.go @@ -1,4 +1,4 @@ -//go:build !windows +//go:build linux package tools diff --git a/tools/tools.go b/tools/tools.go index 26cbeb8..61a2715 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -7,8 +7,6 @@ import ( "os" "path/filepath" "runtime" - "strings" - "text/template" "github.com/mikoto2000/devcontainer.vim/util" ) @@ -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 diff --git a/tools/vim_darwin_arm64.go b/tools/vim_darwin_arm64.go new file mode 100644 index 0000000..4082f60 --- /dev/null +++ b/tools/vim_darwin_arm64.go @@ -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 + + }, +} + diff --git a/tools/vim_linux_amd64.go b/tools/vim_linux_amd64.go new file mode 100644 index 0000000..a9c55ae --- /dev/null +++ b/tools/vim_linux_amd64.go @@ -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) + }, +} + + diff --git a/tools/vim_windows_amd64.go b/tools/vim_windows_amd64.go new file mode 100644 index 0000000..5f66a21 --- /dev/null +++ b/tools/vim_windows_amd64.go @@ -0,0 +1,44 @@ +//go:build windows && 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) + }, +} + + +