Skip to content

Commit

Permalink
feat: support custom install root. (#108)
Browse files Browse the repository at this point in the history
* refactor: remove duplicated logic for work directory.

* chore: use string contains for high go version because of the vfs.stat error message changed.

* refactor: remove duplicated logic for commands.

* feat: support custom install root.

* chore: use parallel 1 for tests

* chore: bump kclvm-go to v0.5.0-alpha.1
  • Loading branch information
Peefy committed May 23, 2023
1 parent affee80 commit bd4cb83
Show file tree
Hide file tree
Showing 19 changed files with 269 additions and 226 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/main_darwin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ jobs:
with:
go-version: 1.17


- run: go run ./cmds/kcl-go run hello.k
- run: go test ./...
- run: go test -p 1 ./...
11 changes: 1 addition & 10 deletions .github/workflows/main_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,8 @@ jobs:
with:
go-version: 1.17

- run: sudo apt-get update
- run: sudo apt-get install python3 python3-pip -y
- run: sudo apt-get install -y python3-dev libffi-dev
- run: sudo apt-get install -y zlib1g-dev ncurses-dev build-essential libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev



- run: go run ./cmds/kcl-go run hello.k

- run: go test -v -coverprofile=profile.cov ./...

- run: go test -p 1 -v -coverprofile=profile.cov ./...
- uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: profile.cov
2 changes: 1 addition & 1 deletion .github/workflows/main_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:

- run: go install ./... && C:\Users\runneradmin\go\bin\kcl-go.exe run hello.k
- run: go run ./cmds/kcl-go run hello.k
- run: go test ./...
- run: go test -p 1 ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ zz_*
**/.vscode
__pycache__
/vendor
profile.cov
23 changes: 22 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
# Copyright 2023 The KCL Authors. All rights reserved.

KCLVM_URL_MIRRORS:=
PROJECT_NAME = kcl-go

PWD:=$(shell pwd)

BUILD_IMAGE:=kusionstack/kclvm-builder

# export DOCKER_DEFAULT_PLATFORM=linux/amd64
# or
# --platform linux/amd64

RUN_IN_DOCKER:=docker run -it --rm
RUN_IN_DOCKER+=-v ~/.ssh:/root/.ssh
RUN_IN_DOCKER+=-v ~/.gitconfig:/root/.gitconfig
RUN_IN_DOCKER+=-v ${PWD}:/root/kcl
RUN_IN_DOCKER+=-w /root/kcl ${BUILD_IMAGE}

default:
go run ./cmds/kcl-go run hello.k

clean:
-rm -rf ./_build

# ----------------
# Docker
# ----------------

sh-in-docker:
${RUN_IN_DOCKER} bash
1 change: 0 additions & 1 deletion cmds/kcl-go/command/cmd_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func Main() {

NewListCmd(),
NewLispAppCmd(),
NewTourCmd(),
}

if len(os.Args) == 2 && os.Args[1] == "-gen-markdown" {
Expand Down
32 changes: 0 additions & 32 deletions cmds/kcl-go/command/cmd_plugin.go

This file was deleted.

21 changes: 0 additions & 21 deletions cmds/kcl-go/command/cmd_tour.go

This file was deleted.

16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ go 1.18
require (
github.com/chai2010/jsonv v1.1.3
github.com/chai2010/protorpc v1.1.4
github.com/golang/protobuf v1.5.2
github.com/golang/protobuf v1.5.3
github.com/google/go-cmp v0.5.9
github.com/julienschmidt/httprouter v1.3.0
github.com/mitchellh/mapstructure v1.5.0
github.com/powerman/rpc-codec v1.2.2
github.com/stretchr/testify v1.7.1
github.com/stretchr/testify v1.8.2
github.com/urfave/cli/v2 v2.6.0
google.golang.org/grpc v1.46.0
google.golang.org/protobuf v1.28.0
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
gopkg.in/yaml.v3 v3.0.1
kusionstack.io/kclvm-artifact-go v0.4.7-0.20230509055550-ad9843183664
kusionstack.io/kclvm-artifact-go v0.5.0-alpha.1
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
)
138 changes: 19 additions & 119 deletions go.sum

Large diffs are not rendered by default.

17 changes: 0 additions & 17 deletions pkg/kcl/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,6 @@ func ParseArgs(pathList []string, opts ...Option) (Option, error) {
}
}

if len(args.KFilenameList) == 0 {
if isDir(args.WorkDir) {
args.KCodeList = nil

files, err := os.ReadDir(args.WorkDir)
if err != nil {
return Option{}, err
}

for _, file := range files {
name := file.Name()
if !strings.HasPrefix(name, "_") && strings.HasSuffix(name, ".k") {
args.KFilenameList = append(args.KFilenameList, filepath.Join(args.WorkDir, name))
}
}
}
}
if len(args.KFilenameList) == 0 {
return Option{}, fmt.Errorf("kcl.Run: no kcl file")
}
Expand Down
17 changes: 4 additions & 13 deletions pkg/kclvm_runtime/kclvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,18 @@ package kclvm_runtime
import (
_ "embed"
"errors"
"go/build"
"os"
"os/exec"
"path/filepath"
"runtime"

kclvmArtifact "kusionstack.io/kclvm-artifact-go"
"kusionstack.io/kclvm-go/pkg/logger"
"kusionstack.io/kclvm-go/pkg/path"
)

func init() {

gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}

err := kclvmArtifact.InstallKclvm(gopath)
err := kclvmArtifact.InstallKclvm(path.LibPath())
if err != nil {
logger.GetLogger().Warningf("install kclvm failed: %s", err.Error())
}
Expand All @@ -32,11 +26,8 @@ func init() {
}

var (
g_KclvmRoot string
)

var (
ErrKclvmRootNotFound = errors.New("kclvm root not found")
g_KclvmRoot string
ErrKclvmRootNotFound = errors.New("kclvm root not found, please ensure kcl is in your PATH")
)

func InitKclvmRoot(kclvmRoot string) {
Expand Down
75 changes: 75 additions & 0 deletions pkg/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")
}
49 changes: 49 additions & 0 deletions pkg/path/lazypath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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.

package path

import (
"os"
"path/filepath"
)

const (
// LibHomeEnvVar is the environment variable used by kcl
// for the lib install directory. When no value is set a default is used.
LibHomeEnvVar = "KCL_LIB_HOME"
)

// lazypath is an lazy-loaded path buffer for the XDG base directory specification.
type lazypath string

func (l lazypath) path(envVar string, defaultFn func() string, elem ...string) string {

// There is an order to checking for a path.
// 1. See if a KCL specific environment variable has been set.
// 2. Fall back to a default
base := os.Getenv(envVar)
if base != "" {
return filepath.Join(base, filepath.Join(elem...))
}
if base == "" {
base = defaultFn()
}
return filepath.Join(base, string(l), filepath.Join(elem...))
}

// libPath defines the base directory relative to which user specific non-essential data files
// should be stored.
func (l lazypath) libPath(elem ...string) string {
return l.path(LibHomeEnvVar, libHome, filepath.Join(elem...))
}
24 changes: 24 additions & 0 deletions pkg/path/lazypath_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.

//go:build darwin

package path

import (
"path/filepath"
)

func libHome() string {
return filepath.Join(HomeDir(), "Library", "Preferences")
}
Loading

0 comments on commit bd4cb83

Please sign in to comment.