Skip to content

Commit

Permalink
优化选项
Browse files Browse the repository at this point in the history
  • Loading branch information
hulutech-web committed Dec 8, 2024
0 parents commit bc6dbed
Show file tree
Hide file tree
Showing 42 changed files with 2,883 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 定义要执行的命令
YAEGI_CMD := yaegi extract
SOURCE_DIR := goravel/app/models
TARGET_DIR := ./symbols

# 目标
all: check_yaegi extract_models move_file
# 检查是否已经安装 yaegi,如果没有则安装
check_yaegi:
@if ! command -v yaegi &> /dev/null; then \
echo "yaegi not found, installing..."; \
go install github.com/traefik/yaegi/cmd/[email protected]; \
else \
echo "yaegi is already installed"; \
fi

# 执行 yaegi extract 仅针对指定目录
extract_models:
$(YAEGI_CMD) $(SOURCE_DIR)

# 移动生成的文件到目标目录,并修改 package 声明
move_file:
@find . -maxdepth 1 -name 'goravel-app-models.go' -exec sed -i 's/package tinker/package symbols/' {} \;
@mv goravel-app-models.go $(TARGET_DIR)/
@echo "Moved and updated package name in goravel-app-models.go to 'symbols'"
.PHONY: all install_yaegi extract_models move_file
46 changes: 46 additions & 0 deletions Makefile2
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 判断操作系统,定义不同的变量
ifeq ($(OS),Windows_NT)
CHECK_CMD_EXISTS := if not exist "%GOPATH%\bin\yaegi.exe" (echo 1) else (echo 0)
INSTALL_CMD := go install github.com/traefik/yaegi/cmd/[email protected]
SED_CMD := powershell -Command "Get-ChildItem -Path. -Filter 'goravel-app-models.go' | ForEach-Object { (Get-Content $_.FullName) -replace 'package tinker', 'package symbols' | Set-Content $_.FullName }"
MOVE_CMD := powershell -Command "Move-Item -Path goravel-app-models.go -Destination $(TARGET_DIR)"
else
CHECK_CMD_EXISTS := if ! command -v yaegi &> /dev/null; then echo 1; else echo 0; fi
INSTALL_CMD := go install github.com/traefik/yaegi/cmd/[email protected]
SED_CMD := sed -i '' 's/package tinker/package symbols/' goravel-app-models.go
MOVE_CMD := mv goravel-app-models.go $(TARGET_DIR)/
endif

# 定义要执行的命令
YAEGI_CMD := yaegi extract
SOURCE_DIR := goravel/app/models
TARGET_DIR := ./symbols

# 目标
all: check_yaegi extract_models move_file

# 检查是否已经安装 yaegi,如果没有则安装
check_yaegi:
@$(CHECK_CMD_EXISTS) > /tmp/yaegi_check_result.txt 2>&1
@if [ "$(cat /tmp/yaegi_check_result.txt)" = "1" ]; then \
echo "yaegi not found, installing..."; \
$(INSTALL_CMD); \
else \
echo "yaegi is already installed"; \
fi
@rm -f /tmp/yaegi_check_result.txt

# 执行 yaegi extract 仅针对指定目录
extract_models:
$(YAEGI_CMD) $(SOURCE_DIR)

# 移动生成的文件到目标目录,并修改 package 声明
move_file:
@$(SED_CMD)
@$(MOVE_CMD)
@echo "Moved and updated package name in goravel-app-models.go to 'symbols'"

.PHONY: all check_yaegi extract_models move_file



74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
## 环境
项目的go.mod文件中,必须为 module goravel 否则会出现错误
``go.mod`` 文件内容应该如下所示:
```go
module goravel
````

### 简介
tinker 是一个go的运行环境,可以在该环境下执行go语言的语法,与浏览器的控制台运行js代码同理,且该扩展实现了goravel的内置功能,如数据库db操作,orm操作,cache缓存,秘钥key生成,jwt秘钥等。

### tinker 安装
- 克隆该扩展包,然后放入packages/目录下
- 注册服务提供者,config.app中:
```go
import "goravel/packages/tinker"
"providers": []foundation.ServiceProvider{
...
&tinker.ServiceProvider{},
}
```
- 系统命令:``go run . artisan tinker` 进入tinker环境,根据提示完成操作
- 预加载
进入到tinker目录下,命令行执行 ``make`` ,将自动安装所需依赖
- 运行go generate
go generate ./...
- ``go mod tidy``
#### 数据库方法
```go
db.Find("depts")
db.WhereMap("users",map[string]interface{}{"name":"市场部-经理"})
db.WhereNot("users",map[string]interface{}{"name":"市场部-经理"})
db.WhereNotEqual("users","name","市场部-经理")
db.WhereIn("users","name", []string{"市场部-经理", "市场部-主管"})
db.WhereLike("users","name", "市场部")
db.WhereBetween("users","created_at", `2024-08-22 00:00:00`, `2024-08-23 23:59:59`)
```
#### orm方法,请参考gorm的用法
使用方式为,进入orm下,命令行输入shebang回车,进入shebang多行命令行模式,编写完成后,输入命令:save保存回车,查看命令执行结果,默认引入了goravel/app/models模型。
- 函数调用参考gorm官方实现方式,可查看gorm官方文档
- 示例:orm命令行模式下,控制台参考命令如下,
- 命令行 ``go run . artisan tinker `` //1-终端命令行命令,完成后回车
- 选择orm菜单,进入orm tinker环境 // 2-上下箭头选择选项,回车进入
- 输入命令``shebang`` 进入多行语法环境模式 // 3-进入go 语言环境
```go
//查看所有记录
users:=[]models.User{} //4-编写完成后回车
query.Query().Model(&models.User{}).Where("name LIKE ?", "%市场%").FindAll(&users) //5-编写完成后回车
:save //6-输入:save 后回车保存查看执行结果

//查看单个记录
user:=models.User{} //1-编写完成后回车
query.Query().Model(&models.User{}).Where("name LIKE ?", "%%").FindOne(&user) //2-编写完成后回车
:save //3-输入:save 后回车保存查看执行结果
```
#### 文件扫描,tinker 环境下,扩展会观察app/models下的模型文件,当该文件夹下的内容有变化时,会自动执行makefile文件,这个过程需要一定时间,请耐心等待

#### 示例:可查看example.md文件

#### 预览:
- dashboard
<p align="center">
<img src="https://github.com/hulutech-web/tinker/blob/master/images/dashboard.png?raw=true" width="750" />
</p>

- db查询
<p align="center">
<img src="https://github.com/hulutech-web/tinker/blob/master/images/db_query.png?raw=true" width="750" />
</p>

- orm查询
<p align="center">
<img src="https://github.com/hulutech-web/tinker/blob/master/images/orm_query.png?raw=true" width="750" />
</p>
53 changes: 53 additions & 0 deletions commands/tinker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package commands

import (
"fmt"
"github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/console/command"
"github.com/goravel/framework/support/path"
"goravel/packages/tinker/view"
"os"
)

type Tinker struct{}

func NewTinker() *Tinker {
return &Tinker{}
}

// Signature The name and signature of the console command.
func (receiver *Tinker) Signature() string {
return "tinker"
}

// Description The console command description.
func (receiver *Tinker) Description() string {
return "a tinker command tool"
}

// Extend The console command extend.
func (receiver *Tinker) Extend() command.Extend {
return command.Extend{}
}

// Handle Execute the console command.
func (receiver *Tinker) Handle(ctx console.Context) error {
// 指定要创建的文件路径和名称
dirPath := path.Storage("tmp")
filePath := path.Storage("tmp/test")

// 先判断目录是否存在,如果不存在则创建目录
err := os.MkdirAll(dirPath, 0755)
if err != nil {
fmt.Println("创建目录失败:", err)
}

// 在已创建或已存在的目录下创建文件
_, err1 := os.Create(filePath)

if err1 != nil {
panic(err1.Error())
}
view.Call()
return nil
}
12 changes: 12 additions & 0 deletions config/tinker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

import (
"github.com/goravel/framework/facades"
)

func init() {
config := facades.Config()
config.Add("tinker", map[string]any{
"name": "Tinker",
})
}
5 changes: 5 additions & 0 deletions contracts/tinker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package contracts

type Tinker interface {
Call()
}
19 changes: 19 additions & 0 deletions example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### orm方法
```go
users:=[]models.User{}
query.Query().Model(&models.User{}).Where("name LIKE ?", "%%").FindAll(&users)

user:=models.User{}
query.Query().Model(&models.User{}).Where("name LIKE ?", "%%").FindOne(&user)
```

### table方法
```go
db.Find("depts")
db.WhereMap("users",map[string]interface{}{"name":"市场部-经理"})
```

### cache方法
```go

```
18 changes: 18 additions & 0 deletions facades/tinker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package facades

import (
"log"

"goravel/packages/tinker"
"goravel/packages/tinker/contracts"
)

func Tinker() contracts.Tinker {
instance, err := tinker.App.Make(tinker.Binding)
if err != nil {
log.Println(err)
return nil
}

return instance.(contracts.Tinker)
}
19 changes: 19 additions & 0 deletions funcs/auth/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package auth

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"
"goravel/app/models"
)

func Auth(ctx http.Context) models.User {
var user models.User
facades.Auth(ctx).User(&user)
return user
}

func First() models.User {
var user models.User
facades.Orm().Query().First(&user)
return user
}
15 changes: 15 additions & 0 deletions funcs/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cache

import "github.com/goravel/framework/facades"

func Store(key string, value string) {
facades.Cache().Put(key, value, 0)
}

func Get(key string) any {
return facades.Cache().Get(key)
}

func RetString(str string) string {
return str
}
15 changes: 15 additions & 0 deletions funcs/command/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package command

import "github.com/goravel/framework/facades"

func JwtGenerate() {
facades.Artisan().Call("jwt:secret")
}

func AppkeyGenerate() {
facades.Artisan().Call("key:generate")
}

func Call(command string) {
facades.Artisan().Call(command)
}
Loading

0 comments on commit bc6dbed

Please sign in to comment.