102 lines
2.3 KiB
Go
102 lines
2.3 KiB
Go
package base
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.fsdpf.net/go/contracts"
|
|
"github.com/samber/do"
|
|
)
|
|
|
|
type PkgVersion struct {
|
|
Id int64 `db:"id"`
|
|
Name string `db:"pkg"`
|
|
Bundle string `db:"bundle"`
|
|
Hash string `db:"hash"`
|
|
FileName string `db:"filename"`
|
|
SourceMapFileName string `db:"sourcemapFileName"`
|
|
Imports []string `db:"imports"`
|
|
Platform string `db:"platform"`
|
|
ReleaseAt string `db:"release_at"`
|
|
UpdatedAt string `db:"updated_at"`
|
|
CreatedAt string `db:"created_at"`
|
|
}
|
|
|
|
func (this PkgVersion) GetFilePath(prefix ...string) string {
|
|
return filepath.Join(
|
|
filepath.Join(prefix...),
|
|
this.Platform,
|
|
strings.Replace(this.FileName, ".", fmt.Sprintf("-%s.", this.Hash), 1),
|
|
)
|
|
}
|
|
|
|
func (this PkgVersion) GetSourceMapPath(prefix ...string) string {
|
|
return filepath.Join(
|
|
filepath.Join(prefix...),
|
|
this.Platform,
|
|
strings.Replace(this.SourceMapFileName, ".", fmt.Sprintf("-%s.", this.Hash), 1),
|
|
)
|
|
}
|
|
|
|
func (this PkgVersion) GetFile(app *do.Injector) (file string, err error) {
|
|
if this.Id == 0 {
|
|
return file, contracts.ErrPkgUnknown
|
|
}
|
|
|
|
res, ok := do.MustInvoke[contracts.GetResource](app)("PkgVersion")
|
|
|
|
if !ok {
|
|
return file, contracts.ErrResNotFound
|
|
}
|
|
|
|
_, err = res.GetDBTable().Where("id", this.Id).Value(&file, "file")
|
|
|
|
return file, err
|
|
}
|
|
|
|
func (this PkgVersion) GetSourceMap(app *do.Injector) (file string, err error) {
|
|
if this.Id == 0 {
|
|
return file, contracts.ErrPkgUnknown
|
|
}
|
|
|
|
res, ok := do.MustInvoke[contracts.GetResource](app)("PkgVersion")
|
|
|
|
if !ok {
|
|
return file, contracts.ErrResNotFound
|
|
}
|
|
|
|
_, err = res.GetDBTable().Where("id", this.Id).Value(&file, "sourcemap")
|
|
|
|
return file, err
|
|
}
|
|
|
|
type GetPkgVersion func(pkg string, opts ...PkgOption) (PkgVersion, bool)
|
|
|
|
// 筛选选项
|
|
type PkgOption func(option *PkgOptions)
|
|
type PkgOptions struct {
|
|
platform string
|
|
hash string
|
|
}
|
|
|
|
func (this PkgOptions) Platform() string {
|
|
return this.platform
|
|
}
|
|
|
|
func (this PkgOptions) Hash() string {
|
|
return this.hash
|
|
}
|
|
|
|
func PkgPlatform(value string) PkgOption {
|
|
return func(option *PkgOptions) {
|
|
option.platform = value
|
|
}
|
|
}
|
|
|
|
func PkgHash(value string) PkgOption {
|
|
return func(option *PkgOptions) {
|
|
option.hash = value
|
|
}
|
|
}
|