package com.x.base.core.project.build;

import java.io.File;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.eclipse.jgit.api.DescribeCommand;
import org.eclipse.jgit.api.Git;

import com.x.base.core.project.gson.XGsonBuilder;
import com.x.base.core.project.tools.DateTools;

public class CreateVersion {
	public static void main(String... args) throws Exception {
		File base = new File(args[0]);
		File file = new File(base, "version.o2");
		Map<String, String> map = fromGit(base);
		if (map.isEmpty()) {
			map = fromMaven(base);
		}
		FileUtils.write(file, XGsonBuilder.toJson(map), StandardCharsets.UTF_8);
	}

	private static Map<String, String> fromGit(File base) {
		Map<String, String> map = new LinkedHashMap<>();
		File dir = base.getParentFile().getAbsoluteFile();
		try (Git git = Git.init().setDirectory(dir).call()) {
			DescribeCommand describeCommand = git.describe().setTags(true).setAlways(true);
			String version = describeCommand.call();
			map.put("version", version);
			map.put("date", DateTools.format(new Date()));
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new LinkedHashMap<>();
	}

	private static Map<String, String> fromMaven(File base) {
		Map<String, String> map = new LinkedHashMap<>();
		try {
			File pom = new File(base, "pom.xml");
			MavenXpp3Reader reader = new MavenXpp3Reader();
			Model model = reader.read(new FileReader(pom));
			map.put("version", model.getVersion() + "");
			map.put("date", DateTools.format(new Date()));
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new LinkedHashMap<>();
	}

}

@@ -410,16 +410,68 @@
 api.Git;
+%0Aimport org.eclipse.jgit.api.errors.GitAPIException;
 %0A%0Aimport
@@ -1348,32 +1348,38 @@
 map;%0A%09%09%7D catch (
+GitAPI
 Exception e) %7B%0A%09
@@ -1380,34 +1380,87 @@
 e) %7B%0A%09%09%09
-e.printStackTrace(
+System.err.print(%22can not fetch git version, use maven version as tag.%22
 );%0A%09%09%7D%0A%09

fix git version fetch warning.