Skip to content

Commit

Permalink
refactor: kcl lib home and install lock
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Sep 18, 2024
1 parent 7e88a1e commit b50e88a
Show file tree
Hide file tree
Showing 20 changed files with 179 additions and 23 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module kcl-lang.io/lib

go 1.19
go 1.23

require (
github.com/ebitengine/purego v0.7.1
github.com/gofrs/flock v0.12.1
google.golang.org/protobuf v1.34.2
)

Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ebitengine/purego v0.7.1 h1:6/55d26lG3o9VCZX8lping+bZcmShseiqlh2bnUDiPA=
github.com/ebitengine/purego v0.7.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/gofrs/flock v0.12.1 h1:MTLVXXHf8ekldpJk3AKicLij9MdwOWkZ+a/jHHZby9E=
github.com/gofrs/flock v0.12.1/go.mod h1:9zxTsyu5xtJ9DK+1tFZyibEV7y3uwDxPPfbxeeHCoD0=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18 changes: 15 additions & 3 deletions go/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
"runtime"

lib "kcl-lang.io/lib/go/lib"
"github.com/gofrs/flock"
)

const KCLVM_VERSION = "v0.10.0"
Expand Down Expand Up @@ -54,16 +54,28 @@ func InstallKclvm(installRoot string) error {
return err
}
versionMatched, err := checkVersion(installRoot)
if err != nil {
return err
}

err = os.MkdirAll(installRoot, 0777)
if err != nil {
return err
}
// Create a lock file for installing.
lockFilePath := filepath.Join(installRoot, "install.lock")
fileLock := flock.New(lockFilePath)

// Install kclvm binary.
err = installBin(installRoot, "kclvm_cli", lib.CliBin, versionMatched)
// Try to obtain a lock with a timeout.
locked, err := fileLock.TryLock()
if err != nil {
return err
}
if !locked {
return fmt.Errorf("another installation is already in progress")
}
defer fileLock.Unlock() // Ensure the lock is released when done.

// Install kclvm libs.
err = installLib(installRoot, "kclvm_cli_cdylib", versionMatched)
if err != nil {
Expand Down
Binary file removed go/lib/darwin-amd64/kclvm_cli
Binary file not shown.
Binary file removed go/lib/darwin-arm64/kclvm_cli
Binary file not shown.
3 changes: 0 additions & 3 deletions go/lib/kclvm_cli_shared_lib_darwin_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@ import (

//go:embed darwin-amd64/libkclvm_cli_cdylib.dylib
var CliLib []byte

//go:embed darwin-amd64/kclvm_cli
var CliBin []byte
3 changes: 0 additions & 3 deletions go/lib/kclvm_cli_shared_lib_darwin_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@ import (

//go:embed darwin-arm64/libkclvm_cli_cdylib.dylib
var CliLib []byte

//go:embed darwin-arm64/kclvm_cli
var CliBin []byte
3 changes: 0 additions & 3 deletions go/lib/kclvm_cli_shared_lib_linux_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@ import (

//go:embed linux-amd64/libkclvm_cli_cdylib.so
var CliLib []byte

//go:embed linux-amd64/kclvm_cli
var CliBin []byte
3 changes: 0 additions & 3 deletions go/lib/kclvm_cli_shared_lib_linux_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@ import (

//go:embed linux-arm64/libkclvm_cli_cdylib.so
var CliLib []byte

//go:embed linux-arm64/kclvm_cli
var CliBin []byte
3 changes: 0 additions & 3 deletions go/lib/kclvm_cli_shared_lib_windows_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ var CliLib []byte

//go:embed windows-amd64/kclvm_cli_cdylib.lib
var ExportLib []byte

//go:embed windows-amd64/kclvm_cli.exe
var CliBin []byte
3 changes: 0 additions & 3 deletions go/lib/kclvm_cli_shared_lib_windows_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ var CliLib []byte

//go:embed windows-amd64/kclvm_cli_cdylib.lib
var ExportLib []byte

//go:embed windows-arm64/kclvm_cli.exe
var CliBin []byte
Binary file removed go/lib/linux-amd64/kclvm_cli
Binary file not shown.
Binary file removed go/lib/linux-arm64/kclvm_cli
Binary file not shown.
Binary file removed go/lib/windows-amd64/kclvm_cli.exe
Binary file not shown.
Binary file removed go/lib/windows-arm64/kclvm_cli.exe
Binary file not shown.
3 changes: 2 additions & 1 deletion go/native/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"runtime"

"kcl-lang.io/lib/go/install"
lazypath "kcl-lang.io/lib/go/path"
)

const libName = "kclvm_cli_cdylib"

func libPath() (path string, err error) {
libHome := os.Getenv("KCL_LIB_HOME")
if libHome == "" {
return os.MkdirTemp("", "kcl_lib_home")
return lazypath.CacheHome(), nil
}
return libHome, nil
}
Expand Down
75 changes: 75 additions & 0 deletions go/path/home.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright The KCL Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Reference: k8s.io/client-go/util/homedir
package path

import (
"os"
"runtime"
)

// HomeDir returns the home directory for the current user.
// On Windows:
// 1. if none of those locations are writeable, the first of %HOME%, %USERPROFILE%, %HOMEDRIVE%%HOMEPATH% that exists is returned.
// 2. if none of those locations exists, the first of %HOME%, %USERPROFILE%, %HOMEDRIVE%%HOMEPATH% that is set is returned.
func HomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOME")
homeDriveHomePath := ""
if homeDrive, homePath := os.Getenv("HOMEDRIVE"), os.Getenv("HOMEPATH"); len(homeDrive) > 0 && len(homePath) > 0 {
homeDriveHomePath = homeDrive + homePath
}
userProfile := os.Getenv("USERPROFILE")

firstSetPath := ""
firstExistingPath := ""

// Prefer %USERPROFILE% over %HOMEDRIVE%/%HOMEPATH% for compatibility with other auth-writing tools
for _, p := range []string{home, userProfile, homeDriveHomePath} {
if len(p) == 0 {
continue
}
if len(firstSetPath) == 0 {
// remember the first path that is set
firstSetPath = p
}
info, err := os.Stat(p)
if err != nil {
continue
}
if len(firstExistingPath) == 0 {
// remember the first path that exists
firstExistingPath = p
}
if info.IsDir() && info.Mode().Perm()&(1<<(uint(7))) != 0 {
// return first path that is writeable
return p
}
}

// If none are writeable, return first location that exists
if len(firstExistingPath) > 0 {
return firstExistingPath
}

// If none exist, return first location that is set
if len(firstSetPath) > 0 {
return firstSetPath
}

// We've got nothing
return ""
}
return os.Getenv("HOME")
}
25 changes: 25 additions & 0 deletions go/path/lazypath_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build darwin
// +build darwin

package path

import (
"path/filepath"
)

// DataHome defines the base directory relative to which user specific data files should be stored.
func DataHome() string {
return filepath.Join(HomeDir(), "Library")
}

// ConfigHome defines the base directory relative to which user specific configuration files should
// be stored.
func ConfigHome() string {
return filepath.Join(HomeDir(), "Library", "Preferences")
}

// CacheHome defines the base directory relative to which user specific non-essential data files
// should be stored.
func CacheHome() string {
return filepath.Join(HomeDir(), "Library", "Caches")
}
25 changes: 25 additions & 0 deletions go/path/lazypath_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build !windows && !darwin
// +build !windows,!darwin

package path

import (
"path/filepath"
)

// DataHome defines the base directory relative to which user specific data files should be stored.
func DataHome() string {
return filepath.Join(HomeDir(), ".local", "share")
}

// ConfigHome defines the base directory relative to which user specific configuration files should
// be stored.
func ConfigHome() string {
return filepath.Join(HomeDir(), ".config")
}

// CacheHome defines the base directory relative to which user specific non-essential data files
// should be stored.
func CacheHome() string {
return filepath.Join(HomeDir(), ".cache")
}
23 changes: 23 additions & 0 deletions go/path/lazypath_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build windows
// +build windows

package path

import "os"

// DataHome defines the base directory relative to which user specific data files should be stored.
func DataHome() string {
return ConfigHome()
}

// ConfigHome defines the base directory relative to which user specific configuration files should
// be stored.
func ConfigHome() string {
return os.Getenv("APPDATA")
}

// CacheHome defines the base directory relative to which user specific non-essential data files
// should be stored.
func CacheHome() string {
return os.Getenv("TEMP")
}

0 comments on commit b50e88a

Please sign in to comment.