Skip to content

Commit

Permalink
finish plugin_updater
Browse files Browse the repository at this point in the history
  • Loading branch information
obgnail committed Aug 29, 2023
1 parent 0436b85 commit ebbec90
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 37 deletions.
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,32 @@
| 20 | go_top | 一键到文章顶部 ||
| 21 | truncate_text | 暂时隐藏内容,提高大文件渲染性能 ||
| 22 | custom | 用户自定义命令(高级) ||
| 23 | right_click_menu | 右键菜单统一管理、调用插件 ||
| 24 | mermaid_replace | 替换 mermaid 组件 | × |
| 25 | old_window_tab | 标签页管理(已废弃) | × |
| 23 | plugin_updater | 一键更新插件 ||
| 24 | right_click_menu | 右键菜单统一管理、调用插件 ||
| 25 | mermaid_replace | 替换 mermaid 组件 | × |
| 26 | old_window_tab | 标签页管理(已废弃) | × |

> 如果各位有其他的需求,或发现 BUG,欢迎提 issue。如果能给我颗 star ⭐ 就更好了 : )


## 如何使用
## 如何使用(自动)

> 目前此方法仅限 windows 平台,不想去管理交叉编译的文件了。
1. 下载插件源码。
2. 进入 Typora 安装路径,找到包含 `window.html` 的文件夹 A。(一般是 `Typora/resources/app/window.html` 或者 `Typora/resources/window.html`
3. 打开文件夹 A,将源码的 plugin 文件夹粘贴进该文件夹下。
4. 进入文件夹 `A/plugin/updater`,双击运行 `updater.exe`。如果看到下图,说明成功。
5. 验证:在正文区域点击鼠标右键,弹出右键菜单栏,如果能看到【启动插件】栏目,说明一切顺利。

![installer](assets/installer.png)

> NOTE:`updater.exe` 同时集成了 install 和 update 两个功能,后续插件更新需要用到此文件,请勿移动位置、删除。如果您不信任此 exe 文件,请自行编译此目录下的 `updater.go` 文件。


## 如何使用(手动)

1. 下载插件源码。
2. 进入 Typora 安装路径,找到包含 `window.html` 的文件夹 A。(一般是 `Typora/resources/app/window.html` 或者 `Typora/resources/window.html`,推荐使用 everything 找一下)
Expand Down Expand Up @@ -419,6 +436,14 @@ const BUILTIN = [


### plugin_updater:一键更新插件

使用方式:右键菜单 -> 启用插件 -> 静默更新插件。

> 此插件依赖于 commander 插件,请勿禁用 commander 插件。


### right_click_menu:右键菜单统一管理插件

目前所有插件都支持在右键菜单中直接调用。
Expand Down
Binary file added assets/installer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions plugin/commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ class commanderPlugin extends global._basePlugin {
}

exec = (cmd, shell, resolve, reject) => {
this.modal.input.value = cmd;
this.modal.shellSelect.value = shell;
const _shell = this.getShellCommand(shell);
const _cmd = this.replaceArgs(cmd, shell);
this.utils.Package.ChildProcess.exec(
Expand Down
2 changes: 1 addition & 1 deletion plugin/custom/plugins/pluginUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class pluginUpdater extends BaseCustomPlugin {
title: "设置代理",
components: [
{
label: "设置代理,为空则不设置",
label: "代理(为空则不设置)",
type: "input",
value: "",
placeholder: "http://127.0.0.1:7890",
Expand Down
2 changes: 1 addition & 1 deletion plugin/global/settings/custom_plugin.default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,5 @@ tc_args = '/O /T /P=L /L="$f"'

############### pluginUpdater ###############
[pluginUpdater]
name = "更新插件"
name = "静默更新插件"
enable = true
Binary file modified plugin/updater/updater.exe
Binary file not shown.
77 changes: 46 additions & 31 deletions plugin/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

type Installer struct {
root string
match string
insertFile string
insertContent string
}
Expand All @@ -39,26 +38,7 @@ func newInstaller() (*Installer, error) {

func (i *Installer) prepare() (err error) {
fmt.Println("[step 2] prepare")
betaDir := "app"
normalDir := "appsrc"
betaMatch := `<script src="./app/window/frame.js" defer="defer"></script>`
normalMatch := `<script src="./appsrc/window/frame.js" defer="defer"></script>`

if err = checkExist(i.root, i.insertFile); err != nil {
return err
}

err = checkExist(i.root, betaDir)
if err == nil {
i.match = betaMatch
return nil
}
err = checkExist(i.root, normalDir)
if err == nil {
i.match = normalMatch
return nil
}
return err
return checkExist(i.root, i.insertFile)
}

func (i *Installer) backupFile() (err error) {
Expand All @@ -80,7 +60,19 @@ func (i *Installer) run() (err error) {
return
}

result := bytes.Replace(file, []byte(i.match), []byte(i.match+i.insertContent), 1)
match := ""
betaMatch := `<script src="./app/window/frame.js" defer="defer"></script>`
normalMatch := `<script src="./appsrc/window/frame.js" defer="defer"></script>`
if bytes.Contains(file, []byte(betaMatch)) {
match = betaMatch
} else if bytes.Contains(file, []byte(normalMatch)) {
match = normalMatch
}
if match == "" {
return fmt.Errorf("has not match")
}

result := bytes.Replace(file, []byte(match), []byte(match+i.insertContent), 1)
err = ioutil.WriteFile(filePath, result, 0644)
if err != nil {
return err
Expand All @@ -96,6 +88,7 @@ type Updater struct {
root string
versionFile string
downloadFile string
workDir string
unzipDir string

userSettingFiles []string
Expand All @@ -111,6 +104,11 @@ func NewUpdater(proxy string) (*Updater, error) {
return nil, err
}

tempDir, err := ioutil.TempDir("", "unzip-")
if err != nil {
return nil, err
}

var uri *url.URL
if proxy != "" {
if uri, err = url.Parse(proxy); err != nil {
Expand All @@ -120,16 +118,17 @@ func NewUpdater(proxy string) (*Updater, error) {

updater := &Updater{
url: "https://api.github.com/repos/obgnail/typora_plugin/releases/latest",
timeout: 30,
timeout: 40,
proxy: uri,
root: filepath.Dir(filepath.Dir(curDir)),
versionFile: filepath.Join(curDir, "version.json"),
downloadFile: filepath.Join(curDir, "download.zip"),
unzipDir: curDir,
downloadFile: filepath.Join(tempDir, "download.zip"),
workDir: tempDir, // 使用临时目录,避免爆炸
userSettingFiles: []string{
"./plugin/global/settings/custom_plugin.user.toml",
"./plugin/global/settings/settings.user.toml",
},
unzipDir: "",
oldVersionInfo: nil,
newVersionInfo: nil,
}
Expand Down Expand Up @@ -269,7 +268,7 @@ func (u *Updater) unzip() (err error) {
}
defer zippedFile.Close()

extractedFilePath := filepath.Join(u.unzipDir, file.Name)
extractedFilePath := filepath.Join(u.workDir, file.Name)
if file.FileInfo().IsDir() {
//fmt.Println("Creating directory:", extractedFilePath)
return os.MkdirAll(extractedFilePath, file.Mode())
Expand Down Expand Up @@ -298,7 +297,7 @@ func (u *Updater) unzip() (err error) {
return
}
}
u.unzipDir = filepath.Join(u.unzipDir, zipReader.Reader.File[0].Name)
u.unzipDir = filepath.Join(u.workDir, zipReader.Reader.File[0].Name)
return
}

Expand Down Expand Up @@ -338,12 +337,20 @@ func (u *Updater) syncDir() (err error) {
return copyDir(src, dst)
}

func (u *Updater) deleteUseless() (err error) {
fmt.Println("[step 7] delete useless file")
func (u *Updater) deleteUselessAndSave() (err error) {
fmt.Println("[step 7] delete useless file and save version.json")
if err = os.Remove(u.downloadFile); err != nil {
return
}
return os.RemoveAll(u.unzipDir)
if err = os.RemoveAll(u.workDir); err != nil {
return
}
content, err := json.Marshal(u.newVersionInfo)
if err != nil {
return
}
err = ioutil.WriteFile(u.versionFile, content, 0777)
return
}

func copyFile(src, dst string) (err error) {
Expand Down Expand Up @@ -422,6 +429,12 @@ func pathExists(path string) (bool, error) {
return false, err
}

func wait() {
fmt.Printf("Press Enter to exit ...")
endKey := make([]byte, 1)
os.Stdin.Read(endKey)
}

func install() (err error) {
installer, err := newInstaller()
if err != nil {
Expand All @@ -437,6 +450,7 @@ func install() (err error) {
return err
}
fmt.Println("Done")
wait()
return nil
}

Expand All @@ -446,6 +460,7 @@ func update(proxy string) (err error) {
return err
}
if need := updater.needUpdate(); !need {
fmt.Println("dont need update")
return
}
if err = updater.downloadLatestVersion(); err != nil {
Expand All @@ -460,7 +475,7 @@ func update(proxy string) (err error) {
if err = updater.syncDir(); err != nil {
return
}
if err = updater.deleteUseless(); err != nil {
if err = updater.deleteUselessAndSave(); err != nil {
return
}
fmt.Println("Done")
Expand Down

0 comments on commit ebbec90

Please sign in to comment.