Skip to content

Commit

Permalink
adjust extension repository model
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Oct 6, 2023
1 parent 661027e commit d8ae28c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 28 deletions.
10 changes: 7 additions & 3 deletions pkg/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const (
bucketExtension = "extension"
)

var (
ErrTaskNotFound = errors.New("task not found")
)

type Listener func(event *Event)

type Progress struct {
Expand Down Expand Up @@ -264,7 +268,7 @@ func (d *Downloader) Create(rrId string, opts *base.Options) (taskId string, err
func (d *Downloader) Pause(id string) (err error) {
task := d.GetTask(id)
if task == nil {
return
return ErrTaskNotFound
}
if err = d.doPause(task, base.DownloadStatusPause); err != nil {
return err
Expand All @@ -275,7 +279,7 @@ func (d *Downloader) Pause(id string) (err error) {
func (d *Downloader) Continue(id string) (err error) {
task := d.GetTask(id)
if task == nil {
return
return ErrTaskNotFound
}
d.tryRunning(func(remain int) {
if remain == 0 {
Expand Down Expand Up @@ -330,7 +334,7 @@ func (d *Downloader) Delete(id string, force bool) (err error) {
defer d.lock.Unlock()
task := d.GetTask(id)
if task == nil {
return
return ErrTaskNotFound
}
task.lock.Lock()
defer task.lock.Unlock()
Expand Down
58 changes: 40 additions & 18 deletions pkg/download/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ var (
type ActivationEvent string

const (
HookEventOnResolve ActivationEvent = "onResolve"
//HookEventOnError HookEvent = "onError"
//HookEventOnDone HookEvent = "onDone"
EventOnResolve ActivationEvent = "onResolve"
//EventOnError ActivationEvent = "onError"
//EventOnDone ActivationEvent = "onDone"
)

func (d *Downloader) InstallExtensionByGit(url string) (*Extension, error) {
Expand Down Expand Up @@ -69,12 +69,13 @@ func (d *Downloader) InstallExtensionByFolder(path string) (*Extension, error) {
func (d *Downloader) UpgradeCheckExtension(identity string) (newVersion string, err error) {
ext, err := d.GetExtension(identity)
if err != nil {
return "", err
return
}
if ext.InstallUrl == "" {
installUrl := ext.buildInstallUrl()
if installUrl == "" {
return
}
_, err = d.fetchExtensionByGit(ext.InstallUrl, func(tempExtPath string) (*Extension, error) {
_, err = d.fetchExtensionByGit(installUrl, func(tempExtPath string) (*Extension, error) {
tempExt, err := d.parseExtensionByPath(tempExtPath)
if err != nil {
return nil, err
Expand All @@ -92,10 +93,11 @@ func (d *Downloader) UpgradeExtension(identity string) error {
if err != nil {
return err
}
if ext.InstallUrl == "" {
installUrl := ext.buildInstallUrl()
if installUrl == "" {
return nil
}
if _, err := d.InstallExtensionByGit(ext.InstallUrl); err != nil {
if _, err := d.InstallExtensionByGit(installUrl); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -227,7 +229,7 @@ func (d *Downloader) triggerOnResolve(req *base.Request) (res *base.Resource) {
var err error
for _, ext := range d.extensions {
for _, script := range ext.Scripts {
if script.match(HookEventOnResolve, req.URL) {
if script.match(EventOnResolve, req.URL) {
scriptFilePath := filepath.Join(d.extensionPath(ext), script.Entry)
if _, err = os.Stat(scriptFilePath); os.IsNotExist(err) {
continue
Expand Down Expand Up @@ -259,7 +261,7 @@ func (d *Downloader) triggerOnResolve(req *base.Request) (res *base.Resource) {
// TODO: log
return
}
if fn, ok := gopeed.Events[HookEventOnResolve]; ok {
if fn, ok := gopeed.Events[EventOnResolve]; ok {
_, err = engine.CallFunction(fn, ctx)
if err != nil {
// TODO: log
Expand Down Expand Up @@ -298,12 +300,10 @@ type Extension struct {
Version string `json:"version"`
// Homepage homepage url
Homepage string `json:"homepage"`
// InstallUrl install url
InstallUrl string `json:"installUrl"`
// Repository git repository url
Repository string `json:"repository"`
Scripts []*Script `json:"scripts"`
Settings []*Setting `json:"settings"`
// Repository git repository info
Repository *Repository `json:"repository"`
Scripts []*Script `json:"scripts"`
Settings []*Setting `json:"settings"`

Disabled bool `json:"disabled"`
CreatedAt time.Time `json:"createdAt"`
Expand All @@ -330,13 +330,30 @@ func (e *Extension) buildIdentity() string {
return e.Author + "@" + e.Name
}

func (e *Extension) buildInstallUrl() string {
if e.Repository == nil || e.Repository.Url == "" {
return ""
}
repoUrl := e.Repository.Url
if e.Repository.Directory != "" {
if strings.HasSuffix(repoUrl, "/") {
repoUrl = repoUrl[:len(repoUrl)-1]
}
dir := e.Repository.Directory
if strings.HasPrefix(dir, "/") {
dir = dir[1:]
}
repoUrl = repoUrl + "#" + dir
}
return repoUrl
}

func (e *Extension) update(newExt *Extension) error {
e.Title = newExt.Title
e.Description = newExt.Description
e.Icon = newExt.Icon
e.Version = newExt.Version
e.Homepage = newExt.Homepage
e.InstallUrl = newExt.InstallUrl
e.Repository = newExt.Repository
e.Scripts = newExt.Scripts
// don't override settings
Expand All @@ -345,6 +362,11 @@ func (e *Extension) update(newExt *Extension) error {
return nil
}

type Repository struct {
Url string `json:"url"`
Directory string `json:"directory"`
}

type Script struct {
// Event active event name
Event string `json:"event"`
Expand Down Expand Up @@ -410,7 +432,7 @@ func (h InstanceEvents) register(name ActivationEvent, fn goja.Callable) {
}

func (h InstanceEvents) OnResolve(fn goja.Callable) {
h.register(HookEventOnResolve, fn)
h.register(EventOnResolve, fn)
}

//func (h InstanceEvents) OnError(fn goja.Callable) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/download/testdata/extensions/update/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"description": "Download github release assets",
"version": "0.0.1",
"homepage": "https://gopeed.com",
"repository": "https://github.com/GopeedLab/gopeed-extension-samples",
"installURL": "https://github.com/GopeedLab/gopeed-extension-samples#github-release-sample",
"repository": {
"url": "https://github.com/GopeedLab/gopeed-extension-samples",
"directory": "github-release-sample"
},
"scripts": [
{
"event": "onResolve",
Expand Down
17 changes: 16 additions & 1 deletion ui/flutter/lib/api/model/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Extension {
String version;
String homepage;
String installUrl;
String repository;
Repository? repository;
List<Setting>? settings;
bool disabled;

Expand All @@ -36,6 +36,21 @@ class Extension {
Map<String, dynamic> toJson() => _$ExtensionToJson(this);
}

@JsonSerializable()
class Repository {
String url;
String directory;

Repository({
required this.url,
required this.directory,
});

factory Repository.fromJson(Map<String, dynamic> json) =>
_$RepositoryFromJson(json);
Map<String, dynamic> toJson() => _$RepositoryToJson(this);
}

@JsonSerializable()
class Setting {
String name;
Expand Down
17 changes: 15 additions & 2 deletions ui/flutter/lib/api/model/extension.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ class ExtensionView extends GetView<ExtensionController> {
},
icon: const Icon(Icons.home))
: null,
extension.repository.isNotEmpty == true
extension.repository?.url.isNotEmpty == true
? IconButton(
onPressed: () {
launchUrl(
Uri.parse(extension.repository),
Uri.parse(
extension.repository!.url),
mode: LaunchMode
.externalApplication);
},
Expand Down

0 comments on commit d8ae28c

Please sign in to comment.