Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.1.8 version update #21

Merged
merged 6 commits into from
Jan 19, 2024
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/on-pr-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
pull_request:
branches:
- main
- dev

permissions:
pull-requests: write
Expand Down
2 changes: 1 addition & 1 deletion documentation/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

ํ’€ ๋ฆฌํ€˜์ŠคํŠธ๋ฅผ ๋ณด๋‚ด์‹ค ๋•Œ, ์•„๋ž˜์˜ ์‚ฌํ•ญ๋“ค์„ ์œ ์˜ํ•ด์ฃผ์„ธ์š”:

- `main` ๋ธŒ๋žœ์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜์—ฌ ํ’€ ๋ฆฌํ€˜์ŠคํŠธ๋ฅผ ๋ณด๋‚ด์ฃผ์„ธ์š”.
- `dev` ๋ธŒ๋žœ์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜์—ฌ ํ’€ ๋ฆฌํ€˜์ŠคํŠธ๋ฅผ ๋ณด๋‚ด์ฃผ์„ธ์š”.
- ๋ณ€๊ฒฝ์‚ฌํ•ญ์— ๋Œ€ํ•œ ๋ช…ํ™•ํ•˜๊ณ  ๊ฐ„๊ฒฐํ•œ ์„ค๋ช…์„ ํฌํ•จํ•ด์ฃผ์„ธ์š”.
- ๋ณ€๊ฒฝ๋œ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ ์ ˆํ•œ ํ…Œ์ŠคํŠธ๊ฐ€ ์ˆ˜ํ–‰๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด์ฃผ์„ธ์š”.

Expand Down
44 changes: 42 additions & 2 deletions documentation/DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

### ์ƒ์„ฑ๋˜๋Š” ์†Œ์Šค์ฝ”๋“œ ํด๋”๋ช… ๋ณ€๊ฒฝํ•˜๊ธฐ

config.json์˜ `srcDirPrefix.value`์„ ์ˆ˜์ •ํ•˜์—ฌ, ์ƒ์„ฑ๋˜๋Š” ์†Œ์Šค์ฝ”๋“œ ํด๋”๋ช…์˜ prefix๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
`config.json`์˜ `srcDirPrefix.value`์„ ์ˆ˜์ •ํ•˜์—ฌ, ์ƒ์„ฑ๋˜๋Š” ์†Œ์Šค์ฝ”๋“œ ํด๋”๋ช…์˜ `prefix`๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์˜ˆ์‹œ๋กœ 1000๋ฒˆ ๋ฌธ์ œ์—์„œ `srcDirPrefix.value`์„ `BOJ_`๋กœ ์„ค์ •ํ•˜๋ฉด, `BOJ_1000` ํด๋”๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์„ค์ • ๊ฐ’์€ `p`์ž…๋‹ˆ๋‹ค.

```json
Expand Down Expand Up @@ -42,7 +42,7 @@ config.json์˜ `srcDirPrefix.value`์„ ์ˆ˜์ •ํ•˜์—ฌ, ์ƒ์„ฑ๋˜๋Š” ์†Œ์Šค์ฝ”๋“œ
import java.util.Scanner;

/*
BAEKJOON {{number}} {{title}}
BAEKJOON {{number}}๋ฒˆ {{title}}
https://www.acmicpc.net/problem/{{number}}
*/

Expand Down Expand Up @@ -94,3 +94,43 @@ public class Main {
}
}
```

### ์ƒ์„ฑ๋˜๋Š” README.md ํŒŒ์ผ์˜ ํ…œํ”Œ๋ฆฟ ๋ณ€๊ฒฝํ•˜๊ธฐ

`config.json`์˜ `markdownTemplate.value`์„ ์ˆ˜์ •ํ•˜์—ฌ, ์ƒ์„ฑ๋˜๋Š” `README.md`์˜ ํŒŒ์ผ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ธฐ๋ณธ ์„ค์ • ๊ฐ’์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

```markdown
# {{title}}

> ๋ฌธ์ œ ๋ฒˆํ˜ธ : {{number}} <br/>
> ์ถœ์ฒ˜ : {{url}}

## ๋ฌธ์ œ ์„ค๋ช…

{{description}}
```

#### ์˜ˆ์•…์–ด

ํ•ด๋‹น ๊ธฐ๋Šฅ์—์„  ๋„ค๊ฐ€์ง€ ์˜ˆ์•ฝ์–ด๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

- `{{title}}`: ๋ฌธ์ œ ์ œ๋ชฉ
- `{{number}}`: ๋ฌธ์ œ ๋ฒˆํ˜ธ
- `{{url}}`: ๋ฌธ์ œ ์ถœ์ฒ˜ URL
- `{{description}}`: ๋ฌธ์ œ ์„ค๋ช… *(html ํƒœ๊ทธ ํฌํ•จ)*

#### ๋น„ํ™œ์„ฑํ™” ๋ฐฉ๋ฒ•

`config.json`์˜ `enableReadme.value`๋ฅผ `"true"` ๋Œ€์‹  `"false"`๋กœ ์ˆ˜์ •ํ•˜๋ฉด, README.md ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. boolean
ํ˜•ํƒœ๊ฐ€ ์•„๋‹Œ `"`๋กœ ๊ฐ์‹ธ์ง„ ๋ฌธ์ž์—ด๋กœ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

```json
{
...
"enableReadme": {
"value": "false"
},
...
}
```
16 changes: 11 additions & 5 deletions src/main/java/kr/huni/BojStarter.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package kr.huni;

import java.io.IOException;
import kr.huni.code_generator.CodeGenerator;
import kr.huni.code_generator.FileContentGenerator;
import kr.huni.code_generator.GeneratedCode;
import kr.huni.code_runner.CodeOpenManager;
import kr.huni.file_generator.JavaSourceCodeFile;
import kr.huni.os.OperatingSystem;
import kr.huni.problem_parser.BaekjoonProblemParser;
import kr.huni.problem_parser.Problem;
import kr.huni.user_configuration.UserConfigurationLoader;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -17,12 +18,16 @@ public class BojStarter {

private final CodeOpenManager codeOpenManager;
private final JavaSourceCodeFile fileUtil;
private final CodeGenerator codeGenerator;
private final FileContentGenerator codeGenerator;
private final BaekjoonProblemParser problemParser;

public void run(final int problemNumber) {
Problem problem = problemParser.parse(problemNumber);
GeneratedCode generatedCode = codeGenerator.generate(problem);
if (!problem.isExist()) {
log.error("๋ฌธ์ œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
return;
}
GeneratedCode generatedCode = codeGenerator.generateCode(problem);

createSrcFile(problem, generatedCode);
openSourceCodeWithIde(problem);
Expand All @@ -37,15 +42,16 @@ private void openSourceCodeWithIde(Problem problem) {
IntelliJ IDEA์˜ idea ๋ช…๋ น์–ด๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
์ง์ ‘ IntelliJ IDEA๋ฅผ ์‹คํ–‰ํ•ด์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”.
์ƒ์„ฑ๋œ ํ”„๋กœ์ ํŠธ ๊ฒฝ๋กœ : {}
%n
์ƒ์„ธ ์˜ค๋ฅ˜ :
""", problem.getSourceRootDirectory(), e);
}
}

private void createSrcFile(Problem problem, GeneratedCode generatedCode) {
try {
fileUtil.write(problem.getSourceRootDirectory(), generatedCode.mainCode(),
generatedCode.testCode());
generatedCode.testCode(), codeGenerator.generateMarkdown(problem),
UserConfigurationLoader.getInstance().enableReadme.getValue().equals("true"));
} catch (IOException e) {
log.error("์†Œ์Šค์ฝ”๋“œ ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.", e);
}
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/kr/huni/code_generator/CodeGenerator.java

This file was deleted.

10 changes: 10 additions & 0 deletions src/main/java/kr/huni/code_generator/FileContentGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kr.huni.code_generator;

import kr.huni.problem_parser.Problem;

public interface FileContentGenerator {

GeneratedCode generateCode(Problem problem);

String generateMarkdown(Problem problem);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
import java.util.List;
import kr.huni.problem_parser.TestCase;

public interface SourceCodeTemplate {
public interface FileContentTemplate {

String TEST_JAVA_FILE = "code_sample/TestHelper.java";
String NO_TEST_JAVA_FILE = "code_sample/NoTestHelper.java";
String REPLACED_NUMBER = "{{number}}";
String REPLACED_TITLE = "{{title}}";
String REPLACED_DESCRIPTION = "{{description}}";
String REPLACED_URL = "{{url}}";
String REPLACED_TEST_CASES = "// {{test_case}}";
String DEFAULT_MAIN_CODE_TEMPLATE = """
import java.util.Scanner;

/*
BAEKJOON {{number}} {{title}}
BAEKJOON {{number}}๋ฒˆ {{title}}
https://www.acmicpc.net/problem/{{number}}
*/

Expand All @@ -27,6 +29,17 @@ public static void main(String[] args) {
}
}
""";
String DEFAULT_MARKDOWN_TEMPLATE = """
# {{title}}

> ๋ฌธ์ œ ๋ฒˆํ˜ธ : {{number}} \s
> ์ถœ์ฒ˜ : {{url}}

## ๋ฌธ์ œ ์„ค๋ช…

{{description}}

""";

/**
* Main.java ์†Œ์Šค์ฝ”๋“œ ๋ฌธ์ž๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
Expand All @@ -45,4 +58,6 @@ public static void main(String[] args) {
*/
String getTestCode(List<TestCase> testCases) throws IOException;

String getMarkdownContent(int number, String title, String description);

}
13 changes: 10 additions & 3 deletions src/main/java/kr/huni/code_generator/JavaCodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JavaCodeGenerator implements CodeGenerator {
public class JavaCodeGenerator implements FileContentGenerator {

@Override
public GeneratedCode generate(Problem problem) {
public GeneratedCode generateCode(Problem problem) {
if (problem.getTestCases() == null) {
throw new IllegalArgumentException("ํ…Œ์ŠคํŠธ๋Š” null์ด ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
}

try {
SourceCodeTemplateImpl sourceCodeTemplate = new SourceCodeTemplateImpl();
JavaTemplate sourceCodeTemplate = new JavaTemplate();
String mainCode = sourceCodeTemplate.getMainCode(problem.getNumber(), problem.getTitle());
String testCode = sourceCodeTemplate.getTestCode(problem.getTestCases());

Expand All @@ -25,4 +25,11 @@ public GeneratedCode generate(Problem problem) {
}
}

@Override
public String generateMarkdown(Problem problem) {
JavaTemplate markdownTemplate = new JavaTemplate();
return markdownTemplate.getMarkdownContent(problem.getNumber(), problem.getTitle(),
problem.getDescription());
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package kr.huni.code_generator;

import static kr.huni.problem_parser.BaekjoonProblemParser.PROBLEM_URL;

import java.io.IOException;
import java.util.List;
import kr.huni.file_generator.SourceCodeFile;
import kr.huni.problem_parser.TestCase;
import kr.huni.user_configuration.UserConfigurationField;
import kr.huni.user_configuration.UserConfigurationLoader;

public class SourceCodeTemplateImpl implements SourceCodeTemplate {
public class JavaTemplate implements FileContentTemplate {

public String getMainCode(int number, String title) {
String template = DEFAULT_MAIN_CODE_TEMPLATE;
Expand Down Expand Up @@ -47,4 +49,22 @@ public String getTestCode(List<TestCase> testCases) throws IOException {
String template = SourceCodeFile.readFileFromResource(TEST_JAVA_FILE);
return template.replace(REPLACED_TEST_CASES, testCaseCode.toString());
}

@Override
public String getMarkdownContent(int number, String title, String description) {
String template = DEFAULT_MARKDOWN_TEMPLATE;
UserConfigurationField markdownTemplate =
UserConfigurationLoader.getInstance().markdownTemplate;
boolean useCustomTemplate = markdownTemplate.getValue() != null;

if (useCustomTemplate) {
template = markdownTemplate.getValue();
}

return template
.replace(REPLACED_NUMBER, String.valueOf(number))
.replace(REPLACED_TITLE, title)
.replace(REPLACED_DESCRIPTION, description)
.replace(REPLACED_URL, PROBLEM_URL + number);
}
}
8 changes: 0 additions & 8 deletions src/main/java/kr/huni/code_runner/CodeOpenCommand.java

This file was deleted.

3 changes: 2 additions & 1 deletion src/main/java/kr/huni/code_runner/CodeOpenManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface CodeOpenManager {
* @throws IOException ์‹คํ–‰ ์‹คํŒจ
* @implSpec ์ฃผ์–ด์ง„ codePath๋ฅผ IDE๋กœ ์—ด์–ด์ค€๋‹ค
*/
void run(String codePath, OperatingSystem operatingSystem) throws IOException;
void run(String codePath, OperatingSystem operatingSystem)
throws IOException;

}
11 changes: 4 additions & 7 deletions src/main/java/kr/huni/code_runner/IdeaCodeOpenManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
public class IdeaCodeOpenManager implements CodeOpenManager {

public void run(String codePath, OperatingSystem os) throws IOException {
CodeOpenCommand command = switch (os) {
case WINDOWS -> new CodeOpenCommand("where idea", "idea");
case LINUX, MAC -> new CodeOpenCommand("which idea", "idea");
String command = switch (os) {
case WINDOWS -> "idea.bat";
case LINUX, MAC -> "idea";
};

boolean ideaExist = Runtime.getRuntime().exec(command.ideExistedCommand()).exitValue() == 0;
if (ideaExist) {
Runtime.getRuntime().exec(command.executeCommand() + " " + codePath);
}
Runtime.getRuntime().exec(command + " " + codePath);
}
}
10 changes: 9 additions & 1 deletion src/main/java/kr/huni/file_generator/JavaSourceCodeFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
public class JavaSourceCodeFile implements SourceCodeFile {

@Override
public void write(String sourceRootDirectory, String sourceCode, String testCode)
public void write(String sourceRootDirectory, String sourceCode, String testCode, String readme,
boolean enableReadme)
throws IOException {
File srcDir = new File(sourceRootDirectory, "src");

Expand All @@ -18,6 +19,13 @@ public void write(String sourceRootDirectory, String sourceCode, String testCode
log.info("์†Œ์Šค์ฝ”๋“œ ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ ์™„๋ฃŒ");
writeToFile(srcDir, "Main.java", sourceCode);
writeToFile(srcDir, "TestHelper.java", testCode);
log.info("์†Œ์Šค์ฝ”๋“œ ํŒŒ์ผ ์ƒ์„ฑ ์™„๋ฃŒ");

if (enableReadme) {
writeToFile(srcDir, "README.md", readme);
log.info("README.md ํŒŒ์ผ ์ƒ์„ฑ ์™„๋ฃŒ");
}
}


}
19 changes: 10 additions & 9 deletions src/main/java/kr/huni/file_generator/SourceCodeFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.io.InputStreamReader;
import java.util.Objects;
import java.util.Scanner;
import kr.huni.code_generator.SourceCodeTemplateImpl;
import kr.huni.code_generator.JavaTemplate;

/**
* ์†Œ์Šค์ฝ”๋“œ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ , ๋‚ด์šฉ์„ ์ฑ„์›Œ์ฃผ๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค
Expand All @@ -25,16 +25,18 @@ public interface SourceCodeFile {
* @implSpec ํ•ด๋‹น ๋ฉ”์„œ๋“œ์•ˆ์—์„œ ํ•„์š”ํ•œ ํ•˜์œ„ ํด๋”๋ฅผ ์ƒ์„ฑํ•˜๊ณ , {@link #writeToFile(File, String, String)}๋ฅผ ํ†ตํ•ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ• 
* ์†Œ์Šค์ฝ”๋“œ ํŒŒ์ผ๊ณผ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
*/
void write(String directory, String sourceCode, String testCode) throws IOException;
void write(String directory, String sourceCode, String testCode, String readme,
boolean enableReadme)
throws IOException;

/**
* ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ , ๋‚ด์šฉ์„ ์ฑ„์›๋‹ˆ๋‹ค.
*
* @param srcDir ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ €์žฅํ•  ์œ„์น˜๊ฐ€ ๋‹ด๊ธด ๊ฐ์ฒด
* @param fileName ํŒŒ์ผ ์ด๋ฆ„
* @param sourceCode ํŒŒ์ผ ๋‚ด์šฉ
* @param srcDir ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ €์žฅํ•  ์œ„์น˜๊ฐ€ ๋‹ด๊ธด ๊ฐ์ฒด
* @param fileName ํŒŒ์ผ ์ด๋ฆ„
* @param content ํŒŒ์ผ ๋‚ด์šฉ
*/
default void writeToFile(File srcDir, String fileName, String sourceCode) {
default void writeToFile(File srcDir, String fileName, String content) {
File file = new File(srcDir, fileName);
if (file.exists()) {
System.out.printf("%s/%s๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ์ƒˆ๋กญ๊ฒŒ ๋ฎ์–ด ์”Œ์šฐ์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (y, n): ", srcDir.getAbsoluteFile(),
Expand All @@ -50,7 +52,7 @@ default void writeToFile(File srcDir, String fileName, String sourceCode) {
}

try (FileWriter fileWriter = new FileWriter(file)) {
fileWriter.write(sourceCode);
fileWriter.write(content);
} catch (IOException e) {
System.out.println("ํŒŒ์ผ ์ƒ์„ฑ ์‹คํŒจ. ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");
throw new RuntimeException(e);
Expand All @@ -66,7 +68,7 @@ default void writeToFile(File srcDir, String fileName, String sourceCode) {
*/
static String readFileFromResource(String filePath) throws IOException {
StringBuilder sourceCode = new StringBuilder();
try (InputStream inputStream = SourceCodeTemplateImpl.class.getClassLoader()
try (InputStream inputStream = JavaTemplate.class.getClassLoader()
.getResourceAsStream(filePath);
InputStreamReader inputStreamReader = new InputStreamReader(
Objects.requireNonNull(inputStream));
Expand All @@ -80,5 +82,4 @@ static String readFileFromResource(String filePath) throws IOException {
}
return sourceCode.toString();
}

}
Loading
Loading