-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #679 from obgnail/dev
Dev
- Loading branch information
Showing
22 changed files
with
408 additions
and
914 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <sys/stat.h> | ||
#include <libgen.h> | ||
|
||
#ifdef _WIN32 | ||
#define PATH_SEPARATOR '\\' | ||
#else | ||
#include <linux/limits.h> | ||
#define PATH_SEPARATOR '/' | ||
#endif | ||
|
||
#define APP "app" | ||
#define APP_SRC "appsrc" | ||
#define WINDOW_HTML "window.html" | ||
#define WINDOW_HTML_BAK "window.html.bak" | ||
#define PLUGIN_SCRIPT "<script src=\"./plugin/index.js\" defer=\"defer\"></script>" | ||
#define OLD_FRAME_SCRIPT "<script src=\"./app/window/frame.js\" defer=\"defer\"></script>" | ||
#define NEW_FRAME_SCRIPT "<script src=\"./appsrc/window/frame.js\" defer=\"defer\"></script>" | ||
|
||
void finish() { | ||
printf("Press Enter to exit..."); | ||
getchar(); | ||
exit(0); | ||
} | ||
|
||
int main() { | ||
char cwd[PATH_MAX]; | ||
char root_path[PATH_MAX]; | ||
char app_path[PATH_MAX]; | ||
char appsrc_path[PATH_MAX]; | ||
char window_html_path[PATH_MAX]; | ||
char window_html_bak_path[PATH_MAX]; | ||
const char *frame_script; | ||
|
||
getcwd(cwd, PATH_MAX); | ||
strcpy(root_path, dirname(dirname(cwd))); | ||
sprintf(app_path, "%s%c%s", root_path, PATH_SEPARATOR, APP); | ||
sprintf(appsrc_path, "%s%c%s", root_path, PATH_SEPARATOR, APP_SRC); | ||
sprintf(window_html_path, "%s%c%s", root_path, PATH_SEPARATOR, WINDOW_HTML); | ||
sprintf(window_html_bak_path, "%s%c%s", root_path, PATH_SEPARATOR, WINDOW_HTML_BAK); | ||
|
||
printf("[1/5] check whether file window.html exists in %s\n", root_path); | ||
struct stat st; | ||
if (stat(window_html_path, &st) == -1) { | ||
fprintf(stderr, "window.html does not exist in %s\n", root_path); | ||
finish(); | ||
} | ||
|
||
printf("[2/5] check whether folder app/appsrc exists in %s\n", root_path); | ||
if (stat(appsrc_path, &st) == 0) { | ||
frame_script = NEW_FRAME_SCRIPT; | ||
} else if (stat(app_path, &st) == 0) { | ||
frame_script = OLD_FRAME_SCRIPT; | ||
} else { | ||
fprintf(stderr, "appsrc/app does not exist in %s\n", root_path); | ||
finish(); | ||
} | ||
|
||
FILE *window_html_fp = fopen(window_html_path, "r+"); | ||
if (window_html_fp == NULL) { | ||
fprintf(stderr, "failed to open %s\n", window_html_path); | ||
finish(); | ||
} | ||
|
||
fseek(window_html_fp, 0, SEEK_END); | ||
long window_html_size = ftell(window_html_fp); | ||
rewind(window_html_fp); | ||
char *file_content = malloc(window_html_size + 1); | ||
if (file_content == NULL) { | ||
fprintf(stderr, "malloc error\n"); | ||
finish(); | ||
} | ||
fread(file_content, sizeof(char), window_html_size, window_html_fp); | ||
file_content[window_html_size] = '\0'; | ||
|
||
printf("[3/5] check window.html content\n"); | ||
char *p = strstr(file_content, frame_script); | ||
if (p == NULL) { | ||
fprintf(stderr, "window.html does not contains %s\n", frame_script); | ||
finish(); | ||
} | ||
if (strstr(file_content, PLUGIN_SCRIPT) != NULL) { | ||
printf("plugin has already been installed\n"); | ||
finish(); | ||
} | ||
|
||
printf("[4/5] backup window.html\n"); | ||
FILE *window_html_bak_fp = fopen(window_html_bak_path, "w"); | ||
if (window_html_bak_fp == NULL) { | ||
fprintf(stderr, "failed to open %s\n", window_html_bak_path); | ||
finish(); | ||
} | ||
fwrite(file_content, 1, strlen(file_content), window_html_bak_fp); | ||
fclose(window_html_bak_fp); | ||
|
||
printf("[5/5] update window.html\n"); | ||
fseek(window_html_fp, 0, SEEK_SET); | ||
char replacement[strlen(frame_script) + strlen(PLUGIN_SCRIPT) + 1]; | ||
sprintf(replacement, "%s%s", frame_script, PLUGIN_SCRIPT); | ||
char new_file_content[window_html_size + strlen(PLUGIN_SCRIPT) + 1]; | ||
sprintf(new_file_content, "%.*s%s%s", (int) (p - file_content), file_content, replacement, p + strlen(frame_script)); | ||
fwrite(new_file_content, 1, strlen(new_file_content), window_html_fp); | ||
|
||
fclose(window_html_fp); | ||
free(file_content); | ||
printf("plugin install successfully\n"); | ||
finish(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type Installer struct { | ||
root string | ||
insertFile string | ||
insertContent string | ||
oldMatch string | ||
newMatch string | ||
} | ||
|
||
func newInstaller() (*Installer, error) { | ||
fmt.Println("[1/4] new installer") | ||
curDir, err := os.Getwd() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Installer{ | ||
root: filepath.Dir(filepath.Dir(curDir)), | ||
insertFile: "window.html", | ||
insertContent: `<script src="./plugin/index.js" defer="defer"></script>`, | ||
oldMatch: `<script src="./app/window/frame.js" defer="defer"></script>`, | ||
newMatch: `<script src="./appsrc/window/frame.js" defer="defer"></script>`, | ||
}, nil | ||
} | ||
|
||
func (i *Installer) prepare() (err error) { | ||
fmt.Println("[2/4] prepare") | ||
if err = checkExist(i.root, i.insertFile); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (i *Installer) backupFile() (err error) { | ||
fmt.Println("[3/4] backup window.html") | ||
filePath := filepath.Join(i.root, i.insertFile) | ||
backupFilePath := filePath + ".bak" | ||
if err = copyFile(filePath, backupFilePath); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (i *Installer) run() (err error) { | ||
fmt.Println("[4/4] update window.html") | ||
filePath := filepath.Join(i.root, i.insertFile) | ||
file, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
return err | ||
} | ||
if bytes.Contains(file, []byte(i.insertContent)) { | ||
return | ||
} | ||
|
||
match := "" | ||
if bytes.Contains(file, []byte(i.oldMatch)) { | ||
match = i.oldMatch | ||
} else if bytes.Contains(file, []byte(i.newMatch)) { | ||
match = i.newMatch | ||
} | ||
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 | ||
} | ||
return nil | ||
} | ||
|
||
func copyFile(src, dst string) (err error) { | ||
var srcFd *os.File | ||
var dstFd *os.File | ||
var srcInfo os.FileInfo | ||
|
||
if srcFd, err = os.Open(src); err != nil { | ||
return err | ||
} | ||
defer srcFd.Close() | ||
|
||
if dstFd, err = os.Create(dst); err != nil { | ||
return err | ||
} | ||
defer dstFd.Close() | ||
|
||
if _, err = io.Copy(dstFd, srcFd); err != nil { | ||
return err | ||
} | ||
if srcInfo, err = os.Stat(src); err != nil { | ||
return err | ||
} | ||
if err = os.Chmod(dst, srcInfo.Mode()); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func checkExist(root, sub string) error { | ||
if sub != "" { | ||
root = filepath.Join(root, sub) | ||
} | ||
exist, err := pathExists(root) | ||
if !exist { | ||
err = fmt.Errorf("%s is not exist", root) | ||
} | ||
return err | ||
} | ||
|
||
func pathExists(path string) (bool, error) { | ||
_, err := os.Stat(path) | ||
if err == nil { | ||
return true, nil | ||
} | ||
if os.IsNotExist(err) { | ||
return false, err | ||
} | ||
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 { | ||
return err | ||
} | ||
if err = installer.prepare(); err != nil { | ||
return err | ||
} | ||
if err = installer.backupFile(); err != nil { | ||
return err | ||
} | ||
if err = installer.run(); err != nil { | ||
return err | ||
} | ||
fmt.Println("plugin install successfully") | ||
wait() | ||
return nil | ||
} | ||
|
||
func main() { | ||
if err := install(); err != nil { | ||
fmt.Println() | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
如果不信任 `install_windows_amd_x64.exe` 和 `install_linux_amd_x64`,有如下两种方法: | ||
|
||
1. 找到目录下的 `install_windows.ps1` 文件,右键它,点击【使用 PowerShell 运行】。 | ||
2. 本项目开放所有代码,可以 review 同目录下的 `install.c` 或者 `install.go` 文件并编译。 |
Oops, something went wrong.