all: cleanup code and upgrade to modern Go facilities

Signed-off-by: ddkwork
This commit is contained in:
Admin
2025-05-05 23:57:55 +08:00
committed by Elias Naur
parent ab2d621e47
commit ae8dd5433d
12 changed files with 37 additions and 44 deletions
+1 -1
View File
@@ -121,7 +121,7 @@ func (d *AndroidTestDriver) tryUninstall() {
}
}
func (d *AndroidTestDriver) adb(args ...interface{}) []byte {
func (d *AndroidTestDriver) adb(args ...any) []byte {
strs := []string{}
for _, arg := range args {
strs = append(strs, fmt.Sprint(arg))
+11 -17
View File
@@ -191,10 +191,7 @@ func compileAndroid(tmpDir string, tools *androidTools, bi *buildInfo) (err erro
if err != nil {
return err
}
minSDK := 17
if bi.minsdk > minSDK {
minSDK = bi.minsdk
}
minSDK := max(bi.minsdk, 17)
tcRoot := filepath.Join(ndkRoot, "toolchains", "llvm", "prebuilt", archNDK())
var builds errgroup.Group
for _, a := range bi.archs {
@@ -213,7 +210,7 @@ func compileAndroid(tmpDir string, tools *androidTools, bi *buildInfo) (err erro
}
}
archDir := filepath.Join(tmpDir, "jni", arch.jniArch)
if err := os.MkdirAll(archDir, 0755); err != nil {
if err := os.MkdirAll(archDir, 0o755); err != nil {
return fmt.Errorf("failed to create %q: %v", archDir, err)
}
libFile := filepath.Join(archDir, "libgio.so")
@@ -252,7 +249,7 @@ func compileAndroid(tmpDir string, tools *androidTools, bi *buildInfo) (err erro
}
if len(javaFiles) > 0 {
classes := filepath.Join(tmpDir, "classes")
if err := os.MkdirAll(classes, 0755); err != nil {
if err := os.MkdirAll(classes, 0o755); err != nil {
return err
}
javac := exec.Command(
@@ -349,13 +346,10 @@ func exeAndroid(tmpDir string, tools *androidTools, bi *buildInfo, extraJars, pe
})
classFiles = append(classFiles, extraJars...)
dexDir := filepath.Join(tmpDir, "apk")
if err := os.MkdirAll(dexDir, 0755); err != nil {
if err := os.MkdirAll(dexDir, 0o755); err != nil {
return err
}
minSDK := 16
if bi.minsdk > minSDK {
minSDK = bi.minsdk
}
minSDK := max(bi.minsdk, 16)
// https://developer.android.com/distribute/best-practices/develop/target-sdk
targetSDK := 33
if bi.targetsdk > 0 {
@@ -387,7 +381,7 @@ func exeAndroid(tmpDir string, tools *androidTools, bi *buildInfo, extraJars, pe
v21Dir := filepath.Join(resDir, "values-v21")
v26mipmapDir := filepath.Join(resDir, `mipmap-anydpi-v26`)
for _, dir := range []string{valDir, v21Dir, v26mipmapDir} {
if err := os.MkdirAll(dir, 0755); err != nil {
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
}
@@ -411,17 +405,17 @@ func exeAndroid(tmpDir string, tools *androidTools, bi *buildInfo, extraJars, pe
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@mipmap/ic_launcher_adaptive" />
<foreground android:drawable="@mipmap/ic_launcher_adaptive" />
</adaptive-icon>`), 0660)
</adaptive-icon>`), 0o660)
if err != nil {
return err
}
iconSnip = `android:icon="@mipmap/ic_launcher"`
}
err = os.WriteFile(filepath.Join(valDir, "themes.xml"), []byte(themes), 0660)
err = os.WriteFile(filepath.Join(valDir, "themes.xml"), []byte(themes), 0o660)
if err != nil {
return err
}
err = os.WriteFile(filepath.Join(v21Dir, "themes.xml"), []byte(themesV21), 0660)
err = os.WriteFile(filepath.Join(v21Dir, "themes.xml"), []byte(themesV21), 0o660)
if err != nil {
return err
}
@@ -477,7 +471,7 @@ func exeAndroid(tmpDir string, tools *androidTools, bi *buildInfo, extraJars, pe
return err
}
manifest := filepath.Join(tmpDir, "AndroidManifest.xml")
if err := os.WriteFile(manifest, manifestBuffer.Bytes(), 0660); err != nil {
if err := os.WriteFile(manifest, manifestBuffer.Bytes(), 0o660); err != nil {
return err
}
@@ -679,7 +673,7 @@ func signAAB(tmpDir string, aabFile string, tools *androidTools, bi *buildInfo)
}
var alias string
for _, t := range strings.Split(keytoolList, "\n") {
for t := range strings.SplitSeq(keytoolList, "\n") {
if i, _ := fmt.Sscanf(t, "Alias name: %s", &alias); i > 0 {
break
}
+1 -1
View File
@@ -180,7 +180,7 @@ func getAppID(pkgMetadata *packageMetadata) string {
name = "." + domain[0]
domain[0] = "localhost"
} else {
for i := 0; i < len(domain)/2; i++ {
for i := range len(domain) / 2 {
opp := len(domain) - 1 - i
domain[i], domain[opp] = domain[opp], domain[i]
}
+10 -9
View File
@@ -12,6 +12,7 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
@@ -56,7 +57,7 @@ func buildIOS(tmpDir, target string, bi *buildInfo) error {
}
}
bi.archs = append(bi.archs[:i], bi.archs[i+1:]...)
bi.archs = slices.Delete(bi.archs, i, i+1)
}
if !forDevice && !strings.HasSuffix(out, ".app") {
return fmt.Errorf("the specified output directory %q does not end in .app or .ipa", out)
@@ -66,7 +67,7 @@ func buildIOS(tmpDir, target string, bi *buildInfo) error {
}
payload := filepath.Join(tmpDir, "Payload")
appDir := filepath.Join(payload, appName+".app")
if err := os.MkdirAll(appDir, 0755); err != nil {
if err := os.MkdirAll(appDir, 0o755); err != nil {
return err
}
if err := exeIOS(tmpDir, target, appDir, bi); err != nil {
@@ -139,7 +140,7 @@ func signIOS(bi *buildInfo, tmpDir, app string) error {
return err
}
entFile := filepath.Join(tmpDir, "entitlements.plist")
if err := os.WriteFile(entFile, []byte(entitlements), 0660); err != nil {
if err := os.WriteFile(entFile, []byte(entitlements), 0o660); err != nil {
return err
}
identity := sha1.Sum(certDER)
@@ -157,7 +158,7 @@ func exeIOS(tmpDir, target, app string, bi *buildInfo) error {
if err := os.RemoveAll(app); err != nil {
return err
}
if err := os.Mkdir(app, 0755); err != nil {
if err := os.Mkdir(app, 0o755); err != nil {
return err
}
appName := UppercaseName(bi.name)
@@ -205,7 +206,7 @@ func exeIOS(tmpDir, target, app string, bi *buildInfo) error {
}
infoPlist := buildInfoPlist(bi)
plistFile := filepath.Join(app, "Info.plist")
if err := os.WriteFile(plistFile, []byte(infoPlist), 0660); err != nil {
if err := os.WriteFile(plistFile, []byte(infoPlist), 0o660); err != nil {
return err
}
if _, err := os.Stat(bi.iconPath); err == nil {
@@ -233,7 +234,7 @@ func exeIOS(tmpDir, target, app string, bi *buildInfo) error {
// iosIcons returns the asset plist file to be merged into Info.plist.
func iosIcons(bi *buildInfo, tmpDir, appDir, icon string) (string, error) {
assets := filepath.Join(tmpDir, "Assets.xcassets")
if err := os.Mkdir(assets, 0700); err != nil {
if err := os.Mkdir(assets, 0o700); err != nil {
return "", err
}
appIcon := filepath.Join(assets, "AppIcon.appiconset")
@@ -270,7 +271,7 @@ func iosIcons(bi *buildInfo, tmpDir, appDir, icon string) (string, error) {
]
}`
contentFile := filepath.Join(appIcon, "Contents.json")
if err := os.WriteFile(contentFile, []byte(contentJson), 0600); err != nil {
if err := os.WriteFile(contentFile, []byte(contentJson), 0o600); err != nil {
return "", err
}
assetPlist := filepath.Join(tmpDir, "assets.plist")
@@ -386,7 +387,7 @@ func archiveIOS(tmpDir, target, frameworkRoot string, bi *buildInfo) error {
frameworkDir := filepath.Join(frameworkRoot, "Versions", "A")
for _, dir := range []string{"Headers", "Modules"} {
p := filepath.Join(frameworkDir, dir)
if err := os.MkdirAll(p, 0755); err != nil {
if err := os.MkdirAll(p, 0o755); err != nil {
return err
}
}
@@ -457,7 +458,7 @@ func archiveIOS(tmpDir, target, frameworkRoot string, bi *buildInfo) error {
export *
}`, framework)
moduleFile := filepath.Join(frameworkDir, "Modules", "module.modulemap")
return os.WriteFile(moduleFile, []byte(module), 0644)
return os.WriteFile(moduleFile, []byte(module), 0o644)
}
func iosCompilerFor(target, arch string, minsdk int) (string, []string, error) {
+1 -1
View File
@@ -61,7 +61,7 @@ func (d *JSTestDriver) Start(path string) {
pr, pw := io.Pipe()
d.Cleanup(func() { pw.Close() })
d.output = pr
chromedp.ListenTarget(ctx, func(ev interface{}) {
chromedp.ListenTarget(ctx, func(ev any) {
switch ev := ev.(type) {
case *runtime.EventConsoleAPICalled:
switch ev.Type {
+3 -3
View File
@@ -20,7 +20,7 @@ func buildJS(bi *buildInfo) error {
if out == "" {
out = bi.name
}
if err := os.MkdirAll(out, 0700); err != nil {
if err := os.MkdirAll(out, 0o700); err != nil {
return err
}
cmd := exec.Command(
@@ -48,7 +48,7 @@ func buildJS(bi *buildInfo) error {
if err != nil {
return err
}
if err := os.WriteFile(filepath.Join(out, filepath.Base(bi.iconPath)), icon, 0600); err != nil {
if err := os.WriteFile(filepath.Join(out, filepath.Base(bi.iconPath)), icon, 0o600); err != nil {
return err
}
faviconPath = filepath.Base(bi.iconPath)
@@ -70,7 +70,7 @@ func buildJS(bi *buildInfo) error {
return err
}
if err := os.WriteFile(filepath.Join(out, "index.html"), b.Bytes(), 0600); err != nil {
if err := os.WriteFile(filepath.Join(out, "index.html"), b.Bytes(), 0o600); err != nil {
return err
}
+5 -6
View File
@@ -89,7 +89,7 @@ func (b *macBuilder) setIcon(path string) (err error) {
}
out := filepath.Join(b.TempDir, "iconset.iconset")
if err := os.MkdirAll(out, 0777); err != nil {
if err := os.MkdirAll(out, 0o777); err != nil {
return err
}
@@ -107,7 +107,6 @@ func (b *macBuilder) setIcon(path string) (err error) {
{path: "icon_16x16@2x.png", size: 32},
{path: "icon_16x16.png", size: 16},
})
if err != nil {
return err
}
@@ -171,18 +170,18 @@ func (b *macBuilder) setInfo(buildInfo *buildInfo, name string) error {
func (b *macBuilder) buildProgram(buildInfo *buildInfo, binDest string, name string, arch string) error {
for _, path := range []string{"/Contents/MacOS", "/Contents/Resources"} {
if err := os.MkdirAll(filepath.Join(binDest, path), 0755); err != nil {
if err := os.MkdirAll(filepath.Join(binDest, path), 0o755); err != nil {
return err
}
}
if len(b.Icons) > 0 {
if err := os.WriteFile(filepath.Join(binDest, "/Contents/Resources/icon.icns"), b.Icons, 0755); err != nil {
if err := os.WriteFile(filepath.Join(binDest, "/Contents/Resources/icon.icns"), b.Icons, 0o755); err != nil {
return err
}
}
if err := os.WriteFile(filepath.Join(binDest, "/Contents/Info.plist"), b.Manifest, 0755); err != nil {
if err := os.WriteFile(filepath.Join(binDest, "/Contents/Info.plist"), b.Manifest, 0o755); err != nil {
return err
}
@@ -206,7 +205,7 @@ func (b *macBuilder) buildProgram(buildInfo *buildInfo, binDest string, name str
func (b *macBuilder) signProgram(buildInfo *buildInfo, binDest string, name string, arch string) error {
options := filepath.Join(b.TempDir, "ent.ent")
if err := os.WriteFile(options, b.Entitlements, 0777); err != nil {
if err := os.WriteFile(options, b.Entitlements, 0o777); err != nil {
return err
}
+1 -1
View File
@@ -199,7 +199,7 @@ func buildIcons(baseDir, icon string, variants []iconVariant) error {
v := v
resizes.Go(func() (err error) {
path := filepath.Join(baseDir, v.path)
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return err
}
f, err := os.Create(path)
+1 -1
View File
@@ -174,7 +174,7 @@ func (d *WaylandTestDriver) Screenshot() image.Image {
return img
}
func (d *WaylandTestDriver) swaymsg(args ...interface{}) {
func (d *WaylandTestDriver) swaymsg(args ...any) {
strs := []string{"--socket", d.socket}
for _, arg := range args {
strs = append(strs, fmt.Sprint(arg))
+1 -1
View File
@@ -358,7 +358,7 @@ const (
valueText uint16 = 1
)
func newValue(valueType uint16, key string, input interface{}) windowsInfoValue {
func newValue(valueType uint16, key string, input any) windowsInfoValue {
v := windowsInfoValue{
Type: valueType,
Length: 6,
+1 -1
View File
@@ -145,7 +145,7 @@ func (d *X11TestDriver) Screenshot() image.Image {
return img
}
func (d *X11TestDriver) xdotool(args ...interface{}) string {
func (d *X11TestDriver) xdotool(args ...any) string {
d.Helper()
strs := make([]string, len(args))
for i, arg := range args {
+1 -2
View File
@@ -10,6 +10,7 @@ import (
"errors"
"flag"
"fmt"
"go/format"
"io"
"os"
"path/filepath"
@@ -17,8 +18,6 @@ import (
"strings"
"unicode"
"go/format"
"gioui.org/f32"
)