package com.muyu.rule.server;

import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.io.IOException;

public class FileCreator {

    public static void main(String[] args) {
        String moduleRelativePath = "com.muyu.rule.server.NewService";
        String fileName = "NewService.java";
        String sourceCode = "package com.example.module;\n" +
                "\n" +
                "public class NewService {\n" +
                "    public String getServiceName() {\n" +
                "        return \"New Service\";\n" +
                "    }\n" +
                "}\n";

        createJavaFile(moduleRelativePath, fileName, sourceCode);
    }

    private static void createJavaFile(String moduleRelativePath, String fileName, String sourceCode) {
        Path modulePath = Paths.get(moduleRelativePath);
        Path filePath = modulePath.resolve(fileName);

        try {
            // 创建模块目录，如果不存在
            Files.createDirectories(modulePath);

            // 如果文件已存在，则删除它
            if (Files.exists(filePath)) {
                Files.delete(filePath);
            }

            // 创建新文件并写入源码
            Files.createFile(filePath);
            Files.write(filePath, sourceCode.getBytes(StandardCharsets.UTF_8));

            System.out.println("Java source file generated successfully at " + filePath.toAbsolutePath());
        } catch (IOException e) {
            System.err.println("Failed to generate Java source file: " + e.getMessage());
        }
    }
}
