-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
121 additions
and
7 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1,122 @@ | ||
package post | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gvcgo/goutils/pkgs/gtea/gprint" | ||
"github.com/gvcgo/goutils/pkgs/gutils" | ||
"github.com/gvcgo/version-manager/internal/cnf" | ||
"github.com/gvcgo/version-manager/internal/download" | ||
"github.com/gvcgo/version-manager/internal/utils" | ||
) | ||
|
||
func init() { | ||
RegisterPostInstallHandler(MoonbitSdkName, PostInstallForMoonbit) | ||
} | ||
|
||
const ( | ||
MoonbitSdkName string = "moonbit" | ||
) | ||
|
||
/* | ||
https://cli.moonbitlang.com/cores/core-latest.tar.gz | ||
*/ | ||
var MoonbitCoreUrl string = "https://cli.moonbitlang.com/cores/core-latest.tar.gz" | ||
|
||
func MoonbitChmod(moonbitInstallDir string) { | ||
if runtime.GOOS == gutils.Windows { | ||
return | ||
} | ||
dItems, _ := os.ReadDir(moonbitInstallDir) | ||
for _, d := range dItems { | ||
if d.IsDir() { | ||
continue | ||
} | ||
if strings.HasPrefix(d.Name(), "lib") { | ||
continue | ||
} | ||
fPath := filepath.Join(moonbitInstallDir, d.Name()) | ||
os.Chmod(fPath, 0755) | ||
} | ||
} | ||
|
||
func PostInstallForMoonbit(versionName string, version download.Item) { | ||
versionDir := cnf.GetVersionsDir() | ||
d := filepath.Join(versionDir, fmt.Sprintf("%s_versions", MoonbitSdkName)) | ||
moonbitInstallDir := filepath.Join(d, fmt.Sprintf("%s-%s", MoonbitSdkName, versionName)) | ||
if ok, _ := gutils.PathIsExist(moonbitInstallDir); !ok { | ||
return | ||
} | ||
|
||
MoonbitChmod(moonbitInstallDir) | ||
|
||
// download core lib. | ||
coreTarFilePath := filepath.Join(moonbitInstallDir, "core-latest.tar.gz") | ||
fetcher := cnf.GetFetcher(MoonbitCoreUrl) | ||
fetcher.Timeout = 30 * time.Minute | ||
if size := fetcher.GetAndSaveFile(coreTarFilePath, true); size <= 100 { | ||
os.RemoveAll(coreTarFilePath) | ||
return | ||
} | ||
|
||
// Decompress archived file to temp dir. | ||
tempDir := cnf.GetTempDir() | ||
os.RemoveAll(tempDir) | ||
if err := utils.Extract(coreTarFilePath, tempDir); err != nil { | ||
gprint.PrintError("Extract file failed: %+v.", err) | ||
os.RemoveAll(filepath.Dir(coreTarFilePath)) | ||
return | ||
} | ||
|
||
// find dir to copy | ||
dirFinder := utils.NewFinder() | ||
dirFinder.SetFlags("core") | ||
dirFinder.SetFlagDirExcepted(false) | ||
dirFinder.Find(tempDir) | ||
dirToCopy := dirFinder.GetDirName() | ||
if dirToCopy == "" { | ||
gprint.PrintError("Can't find dir to copy.") | ||
os.RemoveAll(filepath.Dir(coreTarFilePath)) | ||
return | ||
} | ||
|
||
// Copy extracted files to install directory. | ||
defer func() { | ||
os.RemoveAll(tempDir) | ||
}() | ||
if err := gutils.CopyDirectory(dirToCopy, moonbitInstallDir, true); err != nil { | ||
gprint.PrintError("Copy directory failed: %+v.", err) | ||
os.RemoveAll(filepath.Dir(coreTarFilePath)) | ||
return | ||
} | ||
|
||
coreLibPath := filepath.Join(moonbitInstallDir, "core") | ||
moonBinaryPath := filepath.Join(moonbitInstallDir, "moon") | ||
if runtime.GOOS == gutils.Windows { | ||
moonBinaryPath += ".exe" | ||
} | ||
|
||
// bundle core lib. | ||
if ok, _ := gutils.PathIsExist(coreLibPath); ok { | ||
newPathEnv := utils.JoinPath(moonbitInstallDir, os.Getenv("PATH")) | ||
os.Setenv("PATH", newPathEnv) | ||
_, err := gutils.ExecuteSysCommand( | ||
false, | ||
moonbitInstallDir, | ||
moonBinaryPath, | ||
"bundle", | ||
"--all", | ||
"--source-dir", | ||
coreLibPath, | ||
) | ||
if err != nil { | ||
gprint.PrintError("Execute moon command failed: %+v.", err) | ||
} | ||
} | ||
os.RemoveAll(coreTarFilePath) | ||
} |