Esempio n. 1
0
func getMagookVersionCode() int {
	pro := props.NewProperties()
	proFile, err := os.Open(path.Join(magookHome, magookProjectName, "config.properties"))
	defer proFile.Close()
	mag.CheckError(err)
	err = pro.Load(proFile)
	mag.CheckError(err)
	return string2Int(pro.Get("magookVersionCode"))
}
Esempio n. 2
0
func main() {
	mux.Lock()
	defer mux.Unlock()

	flag.Usage = showUsage
	flag.Parse()

	if *jsonParentPath == "" {
		showUsage()
	} else {
		jsonFilePath = path.Join(*jsonParentPath, "META.JSON")
	}

	//	fmt.Println("magookHome:", magookHome)
	//	fmt.Println("JsonFilePath:", *jsonFilePath)
	//	fmt.Println("ResourceHomePath:", *resourceHomePath)

	if !mag.IsFileExists(jsonFilePath) || !mag.IsDirExists(resHomePath) {
		panic("json file or resourceFile is not exist.")
	}

	// Copy magazine.json config file to magook project root.
	magookProjectPath := path.Join(magookHome, magookProjectName)

	// clean workspace.
	var err error
	err = os.RemoveAll(path.Join(magookProjectPath, "magazine"))
	if err == nil {
		log.Println("clean magazine dir success.")
	} else {
		mag.CheckError(err)
	}

	// judge the json config file suffix whether 'magazine.json'
	if mag.IsFileExists(path.Join(magookProjectPath, "magazine.json")) {
		err = os.Remove(path.Join(magookProjectPath, "magazine.json"))
		if err == nil {
			log.Println("clean magazine.json success.")
		} else {
			mag.CheckError(err)
		}
	}

	// json file where magookProject root path.
	var jsonFileName string = path.Base(jsonFilePath)
	if jsonFileName != "magazine.json" {
		jsonFileName = "magazine.json"
	}

	// copy json config file from remote address to local address.
	_, err = mag.CopyFile(path.Join(magookProjectPath, jsonFileName), jsonFilePath)
	mag.CheckError(err)

	// parse json file.
	var magazine []Magazine
	magazineJson, err := os.Open(path.Join(magookProjectPath, jsonFileName))
	mag.CheckError(err)
	jsonStr, err := ioutil.ReadAll(magazineJson)
	mag.CheckError(err)
	err = json.Unmarshal(jsonStr, &magazine)
	mag.CheckError(err)

	// copy file condition is deps magookAppType.
	for _, item := range magazine {
		if item.MagookAppType == "magook_single" || item.MagookAppType == "magook_qrcode" {
			files, err := mag.ListDir(path.Join(resHomePath, item.Bkdh), ".png")
			mag.CheckError(err)
			for _, f := range files {
				dst, _ := filepath.Rel(resHomePath, f)
				mag.CopyFile(path.Join(magookProjectPath, "magazine", dst), f)
			}

			// handle image to 72x72 and 144x144 size.
			files, err = mag.WalkDir(path.Join(magookProjectPath, "magazine"), ".png")
			mag.CheckError(err)
			for _, f := range files {
				if strings.HasSuffix(f, "logo1024.png") {
					mag.ResizeImg(f, path.Join(filepath.Dir(f), "logo72.png"), 72, 72)
					mag.ResizeImg(f, path.Join(filepath.Dir(f), "logo144.png"), 144, 144)
				}
			}
		}
	}

	//	// call gradle execult
	gradlePath, err := exec.LookPath("gradle")
	mag.CheckError(err)
	results := exec.Command(gradlePath, "-b", path.Join(magookProjectPath, "build.gradle"), "clean")
	d, err := results.CombinedOutput()
	if err != nil {
		mag.CheckError(err)
	} else {
		log.Println(string(d))
	}
	if strings.Contains(string(d), "BUILD SUCCESSFUL") {
		cmd := exec.Command(gradlePath, "-b", path.Join(magookProjectPath, "build.gradle"), "assembleRelease")
		stdoutResults, err := cmd.CombinedOutput()
		if err == nil {
			mag.CheckError(err)
		} else {
			log.Println(string(stdoutResults))
		}

		// must check success and write flag to mysql db.
		if strings.Contains(string(stdoutResults), "BUILD SUCCESSFUL") {
			log.Println("APK Generate Success.")

			mysqlDb := mag.NewMagServer().Open()
			defer mysqlDb.Close()

			var apkurl, ftpbasepath string
			var isSuccess bool
			for _, item := range magazine {
				switch item.MagookAppType {
				case "magook_shop":
					apkurl = path.Join(magookProjectPath, "apk", "shop", int2String(getMagookVersionCode()), "com.magook.apk")
					if mag.IsFileExists(apkurl) {
						ftpbasepath = path.Join("apk", "shop", int2String(getMagookVersionCode()))
						isSuccess = mag.UploadFile(mag.CONFIG_FTP_HOST, mag.CONFIG_FTP_USRNAME, mag.CONFIG_FTP_USRPWD, ftpbasepath, apkurl)
						if isSuccess {
							magVerInfo := mag.NewMagVerInfo("magook_shop", string2Int(item.Magazineid), getMagookVersionCode(), getMagookVersionName(),
								notes, path.Join(ftpbasepath, filepath.Base(apkurl)), item.Rid)
							mysqlDb.Insert(magVerInfo)
						}
					}
				case "magook_share":
					apkurl = path.Join(magookProjectPath, "apk", "share", int2String(getMagookVersionCode()), "com.magook.apk")
					if mag.IsFileExists(apkurl) {
						ftpbasepath = path.Join("apk", "share", int2String(getMagookVersionCode()))
						isSuccess = mag.UploadFile(mag.CONFIG_FTP_HOST, mag.CONFIG_FTP_USRNAME, mag.CONFIG_FTP_USRPWD, ftpbasepath, apkurl)
						if isSuccess {
							magVerInfo := mag.NewMagVerInfo("magook_share", string2Int(item.Magazineid), getMagookVersionCode(), getMagookVersionName(),
								notes, path.Join(ftpbasepath, filepath.Base(apkurl)), item.Rid)
							mysqlDb.Insert(magVerInfo)
						}
					}
				case "magook_qrcode":
					apkbkdh := strings.Replace(item.Bkdh, "-", "_", -1)
					apkurl = path.Join(magookProjectPath, "apk", "qrcode", int2String(getMagookVersionCode()), "com.magook"+apkbkdh+".apk")
					if mag.IsFileExists(apkurl) {
						ftpbasepath = path.Join("apk", "qrcode", int2String(getMagookVersionCode()))
						isSuccess = mag.UploadFile(mag.CONFIG_FTP_HOST, mag.CONFIG_FTP_USRNAME, mag.CONFIG_FTP_USRPWD, ftpbasepath, apkurl)
						if isSuccess {
							magVerInfo := mag.NewMagVerInfo("magook_qrcode", string2Int(item.Magazineid), getMagookVersionCode(), getMagookVersionName(),
								notes, path.Join(ftpbasepath, filepath.Base(apkurl)), item.Rid)
							mysqlDb.Insert(magVerInfo)
						}
					}
				case "magook_single":
					apkbkdh := strings.Replace(item.Bkdh, "-", "_", -1)
					apkurl = path.Join(magookProjectPath, "apk", "single", int2String(getMagookVersionCode()), "com.magook.kind"+apkbkdh+".apk")
					if mag.IsFileExists(apkurl) {
						ftpbasepath = path.Join("apk", "single", int2String(getMagookVersionCode()))
						isSuccess = mag.UploadFile(mag.CONFIG_FTP_HOST, mag.CONFIG_FTP_USRNAME, mag.CONFIG_FTP_USRPWD, ftpbasepath, apkurl)
						if isSuccess {
							magVerInfo := mag.NewMagVerInfo("magook_single", string2Int(item.Magazineid), getMagookVersionCode(), getMagookVersionName(),
								notes, path.Join(ftpbasepath, filepath.Base(apkurl)), item.Rid)
							mysqlDb.Insert(magVerInfo)
						}
					}
				}
			}
			os.Exit(0)
		} else {
			log.Fatalln("APK Generate Failure.")
		}

	} else {
		log.Fatalln("gradle clean failure.")
	}
}