func resourceBuildToDir(ImportPackageList []string, tmpDirPath string) (response resourceBuildToDirResponse) { builder := &tBuilder{ pkgMap: map[string]*pkg{}, } for _, importPath := range ImportPackageList { builder.handlePkg(importPath) } for _, pkg := range builder.pkgDepOrder { builder.JsContent = append(builder.JsContent, pkg.JsContent...) builder.JsContent = append(builder.JsContent, byte('\n')) builder.CssContent = append(builder.CssContent, pkg.CssContent...) builder.CssContent = append(builder.CssContent, byte('\n')) } response.CssFileName = kmgCrypto.Md5Hex(builder.CssContent) + ".css" response.JsFileName = kmgCrypto.Md5Hex(builder.JsContent) + ".js" kmgFile.MustMkdir(tmpDirPath) kmgFile.MustWriteFile(filepath.Join(tmpDirPath, response.CssFileName), builder.CssContent) kmgFile.MustWriteFile(filepath.Join(tmpDirPath, response.JsFileName), builder.JsContent) for _, pkg := range builder.pkgDepOrder { for _, filePath := range pkg.ResourceFilePathList { kmgFile.MustWriteFile(filepath.Join(tmpDirPath, filepath.Base(filePath)), kmgFile.MustReadFile(filePath)) } } for _, pkg := range builder.pkgDepOrder { response.NeedCachePathList = append(response.NeedCachePathList, pkg.Dirpath) } response.ImportPackageList = ImportPackageList return response }
//从 .gitFakeSubmodule 中还原旧的fakeSubmoudle,并且将所有子项目都切换到该文件里面写的分支(使用reset切分支,保证不掉数据) func (repo *Repository) MustFakeSubmoduleUpdate() { rootPath := repo.gitPath SubmoduleList := map[string]SubRepositoryInfo{} kmgJson.MustReadFile(filepath.Join(rootPath, ".gitFakeSubmodule"), &SubmoduleList) for repoPath, SubmoduleInfo := range SubmoduleList { repoRealPath := filepath.Join(rootPath, repoPath) //子项目存在? if !MustIsRepositoryAtPath(repoRealPath) { tmpPath := filepath.Join(repoRealPath, kmgRand.MustCryptoRandToHex(8)) kmgFile.MustMkdir(tmpPath) MustGitClone(SubmoduleInfo.RemoteUrl, tmpPath) kmgFile.MustRename(filepath.Join(tmpPath, ".git"), filepath.Join(repoRealPath, ".git")) kmgFile.MustDelete(tmpPath) } //子项目的远程路径正确? subRepo := MustGetRepositoryFromPath(repoRealPath) if subRepo.MustGetRemoteUrl("origin") != SubmoduleInfo.RemoteUrl { subRepo.MustSetRemoteUrl("origin", SubmoduleInfo.RemoteUrl) } //子项目的版本号正确? if subRepo.MustGetHeadCommitId() != SubmoduleInfo.CommitId { subRepo.MustResetToCommitId(SubmoduleInfo.CommitId) } } }
func GitTestCb(f func()) { oldwd, err := os.Getwd() if err != nil { panic(err) } defer func() { os.Chdir(oldwd) kmgFile.MustDelete("testFile") }() kmgFile.MustDelete("testFile") kmgFile.MustMkdir("testFile") os.Chdir("testFile") f() }
func TestFileMd5ChangeCacheOneDir(t *testing.T) { callLog := make([]string, 32) //递归可用 kmgFile.MustDeleteFile(getFileChangeCachePath("test_file_change_cache")) kmgFile.MustDelete("testFile/d1") kmgFile.MustMkdirAll("testFile/d1/d2") kmgFile.MustWriteFile("testFile/d1/d2/f3", []byte("1")) MustMd5FileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[3] = "f3" }) kmgTest.Equal(callLog[3], "f3") //没有碰过任何东西,缓存有效 MustMd5FileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[4] = "f3" }) kmgTest.Equal(callLog[4], "") //修改文件内容,缓存应该无效 kmgFile.MustWriteFile("testFile/d1/d2/f3", []byte("2")) MustMd5FileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[5] = "f3" }) kmgTest.Equal(callLog[5], "f3") //删除文件,缓存应该无效 kmgFile.MustDelete("testFile/d1/d2/f3") MustMd5FileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[6] = "f4" }) kmgTest.Equal(callLog[6], "f4") //添加文件,缓存应该无效 kmgFile.MustWriteFile("testFile/d1/d2/f4", []byte("3")) MustMd5FileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[7] = "f4" }) kmgTest.Equal(callLog[7], "f4") //读取文件,缓存有效 kmgFile.MustReadFile("testFile/d1/d2/f4") MustMd5FileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[8] = "f4" }) kmgTest.Equal(callLog[8], "") //创建目录,缓存有效 kmgFile.MustMkdir("testFile/d1/d2/f5") MustMd5FileChangeCache("test_file_change_cache", []string{ "testFile/d1", }, func() { callLog[9] = "f4" }) kmgTest.Equal(callLog[9], "") }