import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GenCodeTest {
    @Test
    public void testGenRouter() {
        String className = "DeploySystem";
        try {
            File file = new File("/Users/drew/Projects/Java/awesomeAdmin/src/main/vue/src/router", "index.js");
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            StringBuilder routerJs = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                routerJs.append(line).append("\n");
                System.out.println(line);
            }
            reader.close();

            int insertLocation = routerJs.indexOf("/**INSERT_LOCATION**/");
            if (insertLocation > -1) {
                String routeName = StringUtils.capitalize(className);
                String routePath = StringUtils.uncapitalize(className);
                String route = "{\n                    path: '/"
                        + routePath
                        + "',\n                    name: '"
                        + routeName
                        + "',\n                    component: () => import('../pages/"
                        + routeName
                        + "')\n                }\n                ";
                boolean needComma = !routerJs.toString().substring(0, insertLocation).trim().endsWith(",");
                if (needComma) {
                    routerJs.insert(routerJs.toString().substring(0, insertLocation).lastIndexOf("}") + 1, ",");
                    insertLocation++;
                }
                routerJs.insert(insertLocation, route);
            }

            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
            bufferedOutputStream.write(routerJs.toString().trim().getBytes());
            bufferedOutputStream.close();

            System.out.println("成功生成路由:" + file.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testUpdateRouter() {
        System.out.println(System.getProperties().getProperty("os.name"));
    }
}
