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

Increase the independence of the Windows initializer #440

Merged
merged 22 commits into from
Mar 6, 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
3 changes: 1 addition & 2 deletions .github/workflows/ci-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ jobs:
os:
- ubuntu-latest
- macos-14 # M1 - https://github.blog/changelog/2024-01-30-github-actions-introducing-the-new-m1-macos-runner-available-to-open-source/
- macos-13 # Intel(Maybe)
- windows-latest
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -37,7 +36,7 @@ jobs:
with:
go-version-file: 'go.mod'
cache-dependency-path: 'go.sum'
# - run: go test
- run: go test ./...
- run: go build -v -race ./...
lint:
runs-on: ubuntu-latest
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/ci-home.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ jobs:
os:
- ubuntu-22.04
- macos-14 # M1 - https://github.blog/changelog/2024-01-30-github-actions-introducing-the-new-m1-macos-runner-available-to-open-source/
- macos-13 # Intel(Maybe)
# Disabled macOS-13 because the runner is too slow.
# I have Intel Mac, but it have latest darwin. So only testing in ubuntu runner and macos-14 runner is enough.
# - macos-13 # Intel

runs-on: ${{ matrix.os }}
steps:
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/ci-nix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ jobs:
os:
- ubuntu-latest
- macos-14 # M1 - https://github.blog/changelog/2024-01-30-github-actions-introducing-the-new-m1-macos-runner-available-to-open-source/
- macos-13 # Intel(Maybe)
runs-on: ${{ matrix.os }}
timeout-minutes: 30
steps:
Expand Down
25 changes: 22 additions & 3 deletions .github/workflows/ci-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,30 @@ jobs:
with:
go-version-file: 'go.mod'
cache-dependency-path: 'go.sum'
- name: Rebel against unacceptable default
- name: Make sure there are what files before build
run: Get-ChildItem
- name: Build winit-* # Avoiding run to measure which step taking minutes
run: |
# go build does not automatically create the directory
New-Item -Force -ItemType "Directory" -Path dist
# Need -o to keep product in multiple packages
go build -o dist -v .\...
- name: Make sure there are what files after build
run: |
go run ./cmd/winit-rebel list
go run ./cmd/winit-rebel run --all
Get-ChildItem
Get-ChildItem .\dist
# Do not write depending winget and WSL2 logcs for now
# https://github.com/microsoft/winget-cli/issues/3872
# https://github.com/actions/runner-images/issues/910
# https://github.com/microsoft/winget-cli/blob/b07d2ebb7d865f95320e2bc708a2d1efb2152c5a/README.md#L14
- name: Rebel against unacceptable default
run: |
.\dist\winit-reg.exe list
.\dist\winit-reg.exe run --all
# This logics can be finished even if tools are not installed
- name: Put config files around terminals
run: |
.\dist\winit-conf.exe -pwsh_profile_path "$PROFILE"
- name: Make sure correctly copied
run: |
Get-Content "$PROFILE"
12 changes: 6 additions & 6 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ before:
- go generate ./...
# https://github.com/orgs/goreleaser/discussions/1531
builds:
- id: winit-rebel
binary: winit-rebel
- id: winit-reg
binary: winit-reg
env:
- CGO_ENABLED=0
main: ./cmd/winit-rebel
main: ./cmd/winit-reg
goos:
- windows
goarch:
- amd64
- id: winit-terminal
binary: winit-terminal
- id: winit-conf
binary: winit-conf
env:
- CGO_ENABLED=0
main: ./cmd/winit-terminal
main: ./cmd/winit-conf
goos:
- windows
goarch:
Expand Down
17 changes: 14 additions & 3 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies = [
'build',
'test',
'lint',
'test-go-race',
'build-go-race-as-the-race-test',
]

[tasks.deps]
Expand All @@ -56,7 +56,7 @@ args = [
'./...',
]

[tasks.test-go-race]
[tasks.build-go-race-as-the-race-test]
dependencies = ['prepare-build']
command = 'go'
args = [
Expand All @@ -68,7 +68,17 @@ args = [
'./...',
]

[tasks.test-go]
command = 'go'
args = [
'test',
'./...',
]

[tasks.test]
dependencies = [
'test-go',
]
command = 'nix'
args = [
'run',
Expand Down Expand Up @@ -103,6 +113,7 @@ dependencies = [
'build',
# 'test', # Needs home-manager, so needless in Nix-CI
'lint',
'test-go-race',
'test-go',
'build-go-race-as-the-race-test',
'fmt',
]
130 changes: 130 additions & 0 deletions cmd/winit-conf/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package main

import (
"embed"
"flag"
"log"
"os"
"path"
"path/filepath"

"github.com/kachick/dotfiles/config"
)

const configFilePermission = 0600

type provisioner struct {
FS embed.FS
EmbedTree []string
DstTree []string
}

func newProvisioner(embedTree []string, dstTree []string) provisioner {
return provisioner{
FS: config.WindowsAssets,
EmbedTree: embedTree,
DstTree: dstTree,
}
}

func (p provisioner) EmbedPath() string {
// Should use "path", not "filepath" for embed
// https://github.com/golang/go/issues/44305#issuecomment-780111748
// https://github.com/golang/go/blob/f048829d706df6c1ca4d6fd22de9bd2609d3ed7c/src/io/fs/fs.go#L49
return path.Join(p.EmbedTree...)
}

func (p provisioner) DstPath() string {
// Should use "filepath" as other place
// https://mattn.kaoriya.net/software/lang/go/20171024130616.htm
return filepath.Join(p.DstTree...)
}

func provisioners(pwshProfilePath string) []provisioner {
homePath, err := os.UserHomeDir()
if err != nil {
log.Fatalf("Failed to get home directory: %+v", err)
}

// https://github.com/golang/go/blob/f0d1195e13e06acdf8999188decc63306f9903f5/src/os/file.go#L500-L509
appdataPath, err := os.UserConfigDir()
if err != nil {
log.Fatalln("UserConfigDir(basically APPDATA) is not found")
}

// Do NOT delete this tmpdir after finished, the winget files will be manually used
tmpdirPath, err := os.MkdirTemp("", "winit")
if err != nil {
log.Fatalln("Cannot create temp dir")
}

// As I understand it, unix like permission masks will work even in windows...
err = os.MkdirAll(filepath.Join(homePath, ".config", "alacritty", "themes"), 0750)
if err != nil {
log.Fatalf("Failed to create dotfiles directory: %+v", err)
}
err = os.MkdirAll(filepath.Join(appdataPath, "alacritty"), 0750)
if err != nil {
log.Fatalf("Failed to create path that will have alacritty.toml: %+v", err)
}
err = os.MkdirAll(filepath.Dir(pwshProfilePath), 0750)
if err != nil {
log.Fatalf("Failed to create path that will have PowerShell profiles: %+v", err)
}

return []provisioner{
newProvisioner([]string{"starship", "starship.toml"}, []string{homePath, ".config", "starship.toml"}),
newProvisioner([]string{"alacritty", "common.toml"}, []string{homePath, ".config", "alacritty", "common.toml"}),
// TODO: Copy all TOMLs under themes
newProvisioner([]string{"alacritty", "themes", "iceberg-dark.toml"}, []string{homePath, ".config", "alacritty", "themes", "iceberg-dark.toml"}),
newProvisioner([]string{"alacritty", "windows.toml"}, []string{appdataPath, "alacritty", "alacritty.toml"}),
newProvisioner([]string{"windows", "powershell", "Profile.ps1"}, []string{pwshProfilePath}),
newProvisioner([]string{"windows", "winget", "winget-pkgs-basic.json"}, []string{tmpdirPath, "winget-pkgs-basic.json"}),
newProvisioner([]string{"windows", "winget", "winget-pkgs-entertainment.json"}, []string{tmpdirPath, "winget-pkgs-entertainment.json"}),
newProvisioner([]string{"windows", "winget", "winget-pkgs-storage.json"}, []string{tmpdirPath, "winget-pkgs-storage.json"}),
}
}

func (p provisioner) Copy() error {
body, err := p.FS.ReadFile(p.EmbedPath())
if err != nil {
return err
}
err = os.WriteFile(p.DstPath(), body, configFilePermission)
if err != nil {
return err
}
// Make sure the permission even for umask problem
err = os.Chmod(p.DstPath(), configFilePermission)
if err != nil {
return err
}

return nil
}

func main() {
pwshProfilePathFlag := flag.String("pwsh_profile_path", "", "Specify PowerShell profile path")
flag.Parse()
// $PROFILE is an "Automatic Variables", not ENV
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.4
pwshProfilePath := filepath.Clean(*pwshProfilePathFlag)

if pwshProfilePath == "" {
flag.Usage()
log.Fatalf("called with wrong arguments")
}

for _, p := range provisioners(pwshProfilePath) {
log.Printf("%s => %s\n", p.EmbedPath(), p.DstPath())
err := p.Copy()
if err != nil {
log.Fatalf("Failed to copy file: %+v %+v", p, 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)
}
16 changes: 16 additions & 0 deletions cmd/winit-conf/winit-conf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"testing"

"github.com/kachick/dotfiles/config"
)

func TestAssets(t *testing.T) {
for _, p := range provisioners("dummy_path") {
_, err := config.WindowsAssets.ReadFile(p.EmbedPath())
if err != nil {
t.Fatalf("embed file not found: %v", err)
}
}
}
8 changes: 4 additions & 4 deletions cmd/winit-rebel/main.go → cmd/winit-reg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
)

func usage() string {
return `Usage: winit-rebel [SUB] [OPTIONS]
return `Usage: winit-reg [SUB] [OPTIONS]

Windows initialization to modify default settings

$ winit-rebel list
$ winit-rebel run --action disable_beeps
$ winit-rebel run --all
$ winit-reg list
$ winit-reg run --action disable_beeps
$ winit-reg run --all
`
}

Expand Down
71 changes: 0 additions & 71 deletions cmd/winit-terminal/main.go

This file was deleted.

3 changes: 2 additions & 1 deletion cmd/winit-wsl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strings"

"golang.org/x/sys/unix"
)

// Exists for remember https://github.com/kachick/dotfiles/pull/264#discussion_r1289600371
func mustActivateSystemDOnWSL() {
const path = "/etc/wsl.conf"
path := filepath.Join("etc", "wsl.conf")

const systemdEnablingEntry = `[boot]
systemd=true`
Expand Down
Loading
Loading