Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Add feature contributor #70

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.azure.spring.initializr.separation.contributor;


import com.azure.spring.initializr.separation.util.CodeGeneratorUtils;
import io.spring.initializr.generator.buildsystem.Dependency;
import io.spring.initializr.generator.io.template.TemplateRenderer;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.project.contributor.ProjectContributor;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

public class BackendConfigurationContributor implements ProjectContributor {

private final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

private final ProjectDescription description;

private final TemplateRenderer templateRenderer;


public BackendConfigurationContributor(ProjectDescription description, TemplateRenderer templateRenderer) {
this.description = description;
this.templateRenderer = templateRenderer;
}

@Override
public void contribute(Path projectRoot) throws IOException {
Resource root = this.resolver.getResource("classpath:web-backend");
Resource resource = this.resolver.getResource("classpath:web-backend/src/main/resources/application.yml");
if (resource.isReadable()) {
CodeGeneratorUtils.copyFile(projectRoot, root, resource);
}
Map<String, Object> tempMap = new HashMap<>();
for (Map.Entry<String, Dependency> entry : description.getRequestedDependencies().entrySet()) {
tempMap.put(entry.getKey(), entry.getValue());
}

Files.createDirectories(projectRoot.resolve("src/main/resources/"));
Path codeFileDev = Files.createFile(projectRoot.resolve("src/main/resources/application-dev.yml"));
Path codeFileTest = Files.createFile(projectRoot.resolve("src/main/resources/application-test.yml"));
Path codeFileProd = Files.createFile(projectRoot.resolve("src/main/resources/application-prod.yml"));
String code = this.templateRenderer.render("application.yml", tempMap);

try (PrintWriter codeWriterDev = new PrintWriter(Files.newBufferedWriter(codeFileDev))) {
codeWriterDev.print(code);
}
try (PrintWriter codeWriterTest = new PrintWriter(Files.newBufferedWriter(codeFileTest))) {
codeWriterTest.print(code);
}
try (PrintWriter codeWriterProd = new PrintWriter(Files.newBufferedWriter(codeFileProd))) {
codeWriterProd.print(code);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.azure.spring.initializr.separation.contributor;

import com.azure.spring.initializr.separation.util.CodeGeneratorUtils;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.project.contributor.ProjectContributor;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

public class RbacFeatureContributor implements ProjectContributor {

private final ProjectDescription description;

public RbacFeatureContributor(ProjectDescription description) {
this.description = description;
}


@Override
public void contribute(Path projectRoot) throws IOException {
String artifactId = description.getArtifactId();

List<String> backendLocationPatterns = new ArrayList<>();
backendLocationPatterns.add("classpath:web-backend/src/main/java/com/example/demo/config/GlobalExceptionHandler.java");
backendLocationPatterns.add("classpath:web-backend/src/main/java/com/example/demo/config/WebMvcConfig.java");

backendLocationPatterns.add("classpath:web-backend/src/main/java/com/example/demo/sys/user/config/**");
backendLocationPatterns.add("classpath:web-backend/src/main/java/com/example/demo/sys/user/controller/**");
backendLocationPatterns.add("classpath:web-backend/src/main/java/com/example/demo/sys/user/dto/**");
backendLocationPatterns.add("classpath:web-backend/src/main/java/com/example/demo/sys/user/entity/**");
backendLocationPatterns.add("classpath:web-backend/src/main/java/com/example/demo/sys/user/mapper/**");
backendLocationPatterns.add("classpath:web-backend/src/main/java/com/example/demo/sys/user/param/**");
backendLocationPatterns.add("classpath:web-backend/src/main/java/com/example/demo/sys/user/service/**");

backendLocationPatterns.add("classpath:web-backend/src/main/resources/sql/UserMapper.xml");

backendLocationPatterns.add("classpath:web-backend/src/main/resources/ddl/mysql/sys_rbac.sql");
backendLocationPatterns.add("classpath:web-backend/src/main/resources/ddl/mysql/sys_delegator.sql");

backendLocationPatterns.add("classpath:web-backend/src/main/resources/i18n/**");
backendLocationPatterns.add("classpath:web-backend/README.md");
CodeGeneratorUtils.backendCodeGenerator(projectRoot, backendLocationPatterns);

List<String> frontendLocationPatterns = new ArrayList<>();
frontendLocationPatterns.add("classpath:web-frontend/src/api/system/rbac.js");
frontendLocationPatterns.add("classpath:web-frontend/src/mock/system/rbac.js");
frontendLocationPatterns.add("classpath:web-frontend/src/views/system/rbac/**");
CodeGeneratorUtils.frontendCodeGenerator(projectRoot, artifactId, frontendLocationPatterns);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.azure.spring.initializr.separation.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;

public class CodeGeneratorUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(CodeGeneratorUtils.class);

private static final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();


public static void backendCodeGenerator(Path rootPath, List<String> locationPatterns) {
Resource rootResource = resolver.getResource("classpath:web-backend");
locationPatterns.parallelStream().forEach(locationPattern -> {
try {
Resource[] resources = resolver.getResources(locationPattern);
Arrays.stream(resources).forEach(resource -> {
if (resource.isReadable()) {
try {
copyFile(rootPath, rootResource, resource);
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}
});
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
});
}

public static void frontendCodeGenerator(Path rootPath, String artifactId, List<String> locationPatterns) {
Resource rootResource = resolver.getResource("classpath:web-frontend");
Path frontendPath = rootPath.getParent().resolve(artifactId + "-frontend");

locationPatterns.parallelStream().forEach(locationPattern -> {
try {
Resource[] resources = resolver.getResources(locationPattern);
Arrays.stream(resources).forEach(resource -> {
if (resource.isReadable()) {
try {
copyFile(frontendPath, rootResource, resource);
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}
});
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
});
}

public static void copyFile(Path path, Resource root, Resource resource) throws IOException {
String filename = extractFileName(root.getURI(), resource.getURI());
Path output = path.resolve(filename);
if (Files.notExists(output)) {
Files.createDirectories(output.getParent());
Files.createFile(output);
FileCopyUtils.copy(resource.getInputStream(), Files.newOutputStream(output));
//Files.copy(resource.getInputStream(), output, StandardCopyOption.REPLACE_EXISTING);
}
}

public static String extractFileName(URI root, URI resource) {
String candidate = resource.toString().substring(root.toString().length());
return StringUtils.trimLeadingCharacter(candidate, '/');
}

/**
* 包名转换为包路径
*
* @param packageName 包名
* @return
*/
private static String packageToPath(String packageName) {
String packagePath = packageName.replace(".", "/");
return StringUtils.trimTrailingCharacter(packagePath, '/');
}
}