diff --git a/.github/workflows/ci-go.yml b/.github/workflows/ci-go.yml index d2653a81..47ff0c5f 100644 --- a/.github/workflows/ci-go.yml +++ b/.github/workflows/ci-go.yml @@ -27,6 +27,7 @@ jobs: os: - ubuntu-latest - macos-13 + - windows-latest runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 @@ -35,14 +36,8 @@ jobs: with: go-version-file: 'go.mod' cache-dependency-path: 'go.sum' - # https://github.com/davidB/rust-cargo-make/pull/178 is the blocker, even if this action is recommended in cargo-make official docs - - uses: asdf-vm/actions/install@v2 - with: - # Keep same version as used in *.nix - tool_versions: | - cargo-make 0.36.12 # - run: go test - - run: makers test-go-race + - run: go build -v -race ./... lint: runs-on: ubuntu-latest steps: diff --git a/.vscode/settings.json b/.vscode/settings.json index 5cd26682..715f1115 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,5 +19,8 @@ "nil": { "formatting": { "command": ["nixpkgs-fmt"] } } + }, + "gopls": { + "build.buildFlags": ["-tags=linux,windows,darwin"] } } diff --git a/cmd/add_nix_channels/main.go b/cmd/add_nix_channels/main.go index e65ce064..6eea395f 100644 --- a/cmd/add_nix_channels/main.go +++ b/cmd/add_nix_channels/main.go @@ -1,3 +1,5 @@ +//go:build linux || darwin + package main import ( diff --git a/cmd/deps/main.go b/cmd/deps/main.go index f139bdca..b2ecfba8 100644 --- a/cmd/deps/main.go +++ b/cmd/deps/main.go @@ -1,3 +1,5 @@ +//go:build linux || darwin + package main import ( diff --git a/cmd/disable_windows_beeps/main.go b/cmd/disable_windows_beeps/main.go new file mode 100644 index 00000000..e74420d4 --- /dev/null +++ b/cmd/disable_windows_beeps/main.go @@ -0,0 +1,25 @@ +//go:build windows + +package main + +import ( + "log" + + "golang.org/x/sys/windows/registry" +) + +// # https://github.com/kachick/times_kachick/issues/214 +func main() { + key, err := registry.OpenKey(registry.CURRENT_USER, `Control Panel\Sound`, registry.SET_VALUE) + if err != nil { + log.Fatalf("Failed to open registry key: %+v", err) + } + defer key.Close() + + err = key.SetStringValue("Beep", "no") + if err != nil { + log.Fatalf("Failed to update registry: %+v", err) + } + + log.Println("Completed to disable beeps, you need to restart Windows to activate settings") +} diff --git a/cmd/enable_nix_login_shells/main.go b/cmd/enable_nix_login_shells/main.go index d3bf5df0..d5be05c4 100644 --- a/cmd/enable_nix_login_shells/main.go +++ b/cmd/enable_nix_login_shells/main.go @@ -1,3 +1,5 @@ +//go:build linux || darwin + package main import ( diff --git a/cmd/enable_windows_verbose_context_menu/main.go b/cmd/enable_windows_verbose_context_menu/main.go new file mode 100644 index 00000000..5a7f44ff --- /dev/null +++ b/cmd/enable_windows_verbose_context_menu/main.go @@ -0,0 +1,34 @@ +//go:build windows + +package main + +import ( + "log" + + "golang.org/x/sys/windows/registry" +) + +func main() { + key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Classes\CLSID`, registry.CREATE_SUB_KEY) + if err != nil { + log.Fatalf("Failed to open registry key: %+v", err) + } + defer key.Close() + + newKey, isExists, err := registry.CreateKey(key, `{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32`, registry.SET_VALUE) + if err != nil { + log.Fatalf("Failed to update registry: %+v", err) + } + defer newKey.Close() + if isExists { + log.Println("Skipped to create registry key, because it is already exists") + return + } + + err = newKey.SetStringValue("", "") + if err != nil { + log.Fatalf("Failed to set empty default value, may need to fallback manually: %+v", err) + } + + log.Println("Completed to enable classic style of context menu, you need to restart all explorer.exe processes to activate settings") +} diff --git a/cmd/fmt/main.go b/cmd/fmt/main.go index 7421b75f..794676d9 100644 --- a/cmd/fmt/main.go +++ b/cmd/fmt/main.go @@ -1,3 +1,5 @@ +//go:build linux || darwin + package main import ( diff --git a/cmd/lint/main.go b/cmd/lint/main.go index ad9dce0e..33cc9c79 100644 --- a/cmd/lint/main.go +++ b/cmd/lint/main.go @@ -1,3 +1,5 @@ +//go:build linux || darwin + package main import ( diff --git a/cmd/setup_windows_terminals/main.go b/cmd/setup_windows_terminals/main.go new file mode 100644 index 00000000..06f28275 --- /dev/null +++ b/cmd/setup_windows_terminals/main.go @@ -0,0 +1,65 @@ +//go:build windows + +package main + +import ( + "flag" + "log" + "os" + "path" + "path/filepath" + + "github.com/kachick/dotfiles" +) + +func main() { + dotsPathFlag := flag.String("dotfiles_path", "", "Specify dotfiles repository path in your local") + pwshProfilePathFlag := flag.String("pwsh_profile_path", "", "Specify PowerShell profile path") + flag.Parse() + dotsPath := filepath.Clean(*dotsPathFlag) + pwshProfilePath := filepath.Clean(*pwshProfilePathFlag) + + if dotsPath == "" || pwshProfilePath == "" || len(os.Args) < 2 { + flag.Usage() + log.Fatalf("called with wrong arguments") + } + + homePath, err := os.UserHomeDir() + if err != nil { + log.Fatalf("Failed to get home directory: %+v", err) + } + + appdataPath, ok := os.LookupEnv("APPDATA") + if !ok { + log.Fatalln("ENV APPDATA is not found") + } + + // As I understand it, unix like permission masks will work even in windows... + err = os.MkdirAll(filepath.Join(homePath, ".config", "alacritty"), 0750) + if err != nil { + log.Fatalf("Failed to create dotfiles directory: %+v", err) + } + err = os.MkdirAll(path.Join(appdataPath, "alacritty"), 0750) + if err != nil { + log.Fatalf("Failed to create path that will have alacritty.yml: %+v", err) + } + + copies := []dotfiles.Copy{ + {Src: filepath.Join(dotsPath, "home", ".config", "starship.toml"), Dst: filepath.Join(homePath, ".config", "starship.toml")}, + {Src: filepath.Join(dotsPath, "home", ".config", "alacritty", "alacritty-common.yml"), Dst: filepath.Join(homePath, ".config", "alacritty", "alacritty-common.yml")}, + {Src: filepath.Join(dotsPath, "home", ".config", "alacritty", "alacritty-windows.yml"), Dst: filepath.Join(homePath, ".config", "alacritty", "alacritty.yml")}, + {Src: filepath.Join(dotsPath, "windows", "config", "Profile.ps1"), Dst: pwshProfilePath}, + } + + for _, copy := range copies { + err := copy.Run() + if err != nil { + log.Fatalf("Failed to copy file: %+v %+v", copy, err) + } + } + + log.Printf(`Completed, you need to restart terminals + +If you faced slow execution of PowerShell after this script, exclude %s from Anti Virus as Microsoft Defender +`, pwshProfilePath) +} diff --git a/cmd/setup_wsl/main.go b/cmd/setup_wsl/main.go index 0fcf2cce..4cfa87b3 100644 --- a/cmd/setup_wsl/main.go +++ b/cmd/setup_wsl/main.go @@ -1,16 +1,15 @@ //go:build linux -// +build linux package main import ( "fmt" - "io" "log" "os" "os/exec" "strings" + "github.com/kachick/dotfiles" "golang.org/x/sys/unix" ) @@ -75,17 +74,7 @@ func mustPersistDockerZshCompletions() { } // Can't make immutable symlink, so copy and make immutable here // https://unix.stackexchange.com/questions/586430/how-to-make-a-symlink-read-only-chattr-i-location-symlink - target, err := os.Create(completionLoadablePath) - if err != nil { - log.Panicf("%+v\n", err) - } - defer target.Close() - integration, err := os.Open("dependencies/docker/zsh-vendor-completions.zsh") - if err != nil { - log.Panicf("%+v\n", err) - } - defer integration.Close() - _, err = io.Copy(target, integration) + err = dotfiles.Copy{Src: "dependencies/docker/zsh-vendor-completions.zsh", Dst: completionLoadablePath}.Run() if err != nil { log.Panicf("%+v\n", err) } diff --git a/copy.go b/copy.go new file mode 100644 index 00000000..c8763934 --- /dev/null +++ b/copy.go @@ -0,0 +1,27 @@ +package dotfiles + +import ( + "io" + "os" +) + +type Copy struct { + Src string + Dst string +} + +func (c Copy) Run() error { + src, err := os.Open(c.Src) + if err != nil { + return err + } + defer src.Close() + dst, err := os.Create(c.Dst) + if err != nil { + return err + } + defer dst.Close() + + _, err = io.Copy(dst, src) + return err +} diff --git a/windows/README.md b/windows/README.md index b64f07e3..913ab3b6 100644 --- a/windows/README.md +++ b/windows/README.md @@ -1,18 +1,34 @@ # FAQ -## Configuration steps after installation packages +## Installation +1. ```powershell + winget import --import-file "\\wsl.localhost\Ubuntu\home\kachick\repos\dotfiles\windows\config\winget-pkgs-basic.json" + winget import --import-file "\\wsl.localhost\Ubuntu\home\kachick\repos\dotfiles\windows\config\winget-pkgs-dev.json" + ``` +1. New session of pwsh + ```powershell + go run github.com/kachick/dotfiles/cmd/setup_windows_terminals -dotfiles_path "\\wsl.localhost\Ubuntu\home\kachick\repos\dotfiles" -pwsh_profile_path "$PROFILE" + go run github.com/kachick/dotfiles/cmd/disable_windows_beeps + go run github.com/kachick/dotfiles/cmd/enable_windows_verbose_context_menu + ``` 1. Change Dropbox storage path from `C:\Users`, default path made problems in System Restore. \ See https://zmzlz.blogspot.com/2014/10/windows-dropbox.html for detail -1. On powershell - ```powershell - Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser - \\wsl.localhost\Ubuntu\home\kachick\repos\dotfiles\windows\scripts\bootstrap.ps1 -DotfilesPath "\\wsl.localhost\Ubuntu\home\kachick\repos\dotfiles" - ``` -1. Exclude the `$PROFILE\Profile.ps1` from Anti Virus detection as Microsoft Defender 1. Enable Bitlocker and backup the restore key +## How to run go scripts in this repo? + +After installed golang with winget, you can run github hosting files + +```console +Administrator in ~ psh +> go run github.com/kachick/dotfiles/cmd/disable_windows_beeps@39ac6dc +2023/08/22 15:34:18 Completed to disable beeps, you need to restart Windows to activate settings +``` + +Specifying with branch name with the @ref may use cache, then specify commit ref + ## How to install WSL2? winget does not support it, run as follows @@ -35,7 +51,7 @@ One more noting, if you cannot find ngen.exe, dig under "C:\Windows\Microsoft.NE ## How to export winget list? ```powershell -winget export --output "\\wsl.localhost\Ubuntu\home\kachick\repos\dotfiles\windows\config\winget-list-$(Get-Date -UFormat '%F')-raw.json" +winget export --output "\\wsl.localhost\Ubuntu\home\kachick\repos\dotfiles\windows\config\winget-pkgs-$(Get-Date -UFormat '%F')-raw.json" ``` It may be better to remove some packages such as `Mozilla.Firefox.DeveloperEdition`. diff --git a/windows/config/winget-list-core.json b/windows/config/winget-pkgs-basic.json similarity index 66% rename from windows/config/winget-list-core.json rename to windows/config/winget-pkgs-basic.json index e1fc9f92..90edfde1 100644 --- a/windows/config/winget-list-core.json +++ b/windows/config/winget-pkgs-basic.json @@ -9,15 +9,6 @@ { "PackageIdentifier" : "7zip.7zip" }, - { - "PackageIdentifier" : "BurntSushi.ripgrep.MSVC" - }, - { - "PackageIdentifier" : "Canonical.Ubuntu.2204" - }, - { - "PackageIdentifier" : "Docker.DockerDesktop" - }, { "PackageIdentifier" : "Dropbox.Dropbox" }, @@ -27,9 +18,6 @@ { "PackageIdentifier" : "Microsoft.EdgeWebView2Runtime" }, - { - "PackageIdentifier" : "Microsoft.WindowsTerminal" - }, { "PackageIdentifier" : "Microsoft.OneDrive" }, @@ -48,24 +36,9 @@ { "PackageIdentifier" : "Microsoft.VCRedist.2015+.x64" }, - { - "PackageIdentifier" : "Lapce.Lapce" - }, - { - "PackageIdentifier" : "Microsoft.PowerShell" - }, - { - "PackageIdentifier" : "Alacritty.Alacritty" - }, { "PackageIdentifier" : "Google.GoogleDrive" }, - { - "PackageIdentifier" : "Starship.Starship" - }, - { - "PackageIdentifier" : "Microsoft.VisualStudioCode" - }, { "PackageIdentifier" : "Google.Chrome" }, diff --git a/windows/config/winget-pkgs-dev.json b/windows/config/winget-pkgs-dev.json new file mode 100644 index 00000000..51cf0158 --- /dev/null +++ b/windows/config/winget-pkgs-dev.json @@ -0,0 +1,58 @@ +{ + "$schema" : "https://aka.ms/winget-packages.schema.2.0.json", + "CreationDate" : "2023-08-22T11:50:00.783-00:00", + "Sources" : + [ + { + "Packages" : + [ + { + "PackageIdentifier" : "BurntSushi.ripgrep.MSVC", + "Version" : "13.0.0" + }, + { + "PackageIdentifier" : "Canonical.Ubuntu.2204" + }, + { + "PackageIdentifier" : "Docker.DockerDesktop", + "Version" : "4.22.0" + }, + { + "PackageIdentifier" : "Microsoft.WindowsTerminal" + }, + { + "PackageIdentifier" : "GoLang.Go", + "Version" : "1.20.0" + }, + { + "PackageIdentifier" : "Lapce.Lapce", + "Version" : "0.2.8" + }, + { + "PackageIdentifier" : "Microsoft.PowerShell", + "Version" : "7.3.6.0" + }, + { + "PackageIdentifier" : "Alacritty.Alacritty", + "Version" : "0.12.2" + }, + { + "PackageIdentifier" : "Starship.Starship", + "Version" : "1.16.0" + }, + { + "PackageIdentifier" : "Microsoft.VisualStudioCode", + "Version" : "1.81.1" + } + ], + "SourceDetails" : + { + "Argument" : "https://cdn.winget.microsoft.com/cache", + "Identifier" : "Microsoft.Winget.Source_8wekyb3d8bbwe", + "Name" : "winget", + "Type" : "Microsoft.PreIndexed.Package" + } + } + ], + "WinGetVersion" : "1.5.2201" +} \ No newline at end of file diff --git a/windows/config/winget-list-entertainment.json b/windows/config/winget-pkgs-entertainment.json similarity index 100% rename from windows/config/winget-list-entertainment.json rename to windows/config/winget-pkgs-entertainment.json diff --git a/windows/scripts/bootstrap.ps1 b/windows/scripts/bootstrap.ps1 deleted file mode 100644 index 00cd7352..00000000 --- a/windows/scripts/bootstrap.ps1 +++ /dev/null @@ -1,14 +0,0 @@ -Param( - [String]$DotfilesPath -) - -mkdir ~/.config -ErrorAction SilentlyContinue -mkdir ~/.config/alacritty -ErrorAction SilentlyContinue -mkdir "$($env:APPDATA)/alacritty" -ErrorAction SilentlyContinue - -Copy-Item "${DotfilesPath}\home\.config/starship.toml" -Destination ~/.config -Copy-Item "${DotfilesPath}\home\.config/alacritty/alacritty-common.yml" -Destination ~/.config/alacritty -Copy-Item "${DotfilesPath}\home\.config/alacritty/alacritty-windows.yml" -Destination "$($env:APPDATA)/alacritty/alacritty.yml" -Copy-Item "${DotfilesPath}\windows\config/Profile.ps1" -Destination "$PROFILE" - -Write-Output 'Completed, you need to restart terminals' diff --git a/windows/scripts/disable_beeps.ps1 b/windows/scripts/disable_beeps.ps1 deleted file mode 100644 index d36515da..00000000 --- a/windows/scripts/disable_beeps.ps1 +++ /dev/null @@ -1,3 +0,0 @@ -# https://github.com/kachick/times_kachick/issues/214 -reg add "HKCU\Control Panel\Sound" /v Beep /t REG_SZ /d "no" /f -Write-Output 'Completed, you need to restart Windows' diff --git a/windows/scripts/enable_verbose_context_menu.ps1 b/windows/scripts/enable_verbose_context_menu.ps1 deleted file mode 100644 index d01d25de..00000000 --- a/windows/scripts/enable_verbose_context_menu.ps1 +++ /dev/null @@ -1,2 +0,0 @@ -reg add "HKCU\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32" /ve /f -Write-Output 'Completed, you need to close and restart all "explorer" processes'