docs: 统一注释风格

This commit is contained in:
Luke 2025-06-24 12:03:08 +08:00
parent 493b970d9a
commit 2c4374554e
14 changed files with 247 additions and 175 deletions

View File

@ -6,9 +6,10 @@ import java.util.EnumMap;
import java.util.Map; import java.util.Map;
/** /**
* 管理不同生命周期阶段与其对应任务的工具类 * 生命周期任务管理器<br>
* 用于管理不同生命周期阶段与其对应 {@link Task}并支持顺序执行所有已注册任务
* <p> * <p>
* 可为每个 {@link LifecyclePhase} 注册对应的 {@link Task}按阶段顺序执行所有任务 * 可为每个 {@link LifecyclePhase} 注册对应的 {@link Task}在构建/部署流程中自动执行
* </p> * </p>
* *
* <pre> * <pre>
@ -20,9 +21,7 @@ import java.util.Map;
*/ */
public final class LifecycleManager { public final class LifecycleManager {
/** /** 生命周期阶段与对应任务的映射关系 */
* 存储生命周期阶段与对应任务的映射关系
*/
private final Map<LifecyclePhase, Task> tasks = new EnumMap<>(LifecyclePhase.class); private final Map<LifecyclePhase, Task> tasks = new EnumMap<>(LifecyclePhase.class);
/** /**
@ -45,11 +44,13 @@ public final class LifecycleManager {
/** /**
* {@link LifecyclePhase} 声明顺序依次执行所有已注册任务 * {@link LifecyclePhase} 声明顺序依次执行所有已注册任务
* <p> * <ul>
* 未注册任务的阶段将被跳过任务执行前会打印阶段名 * <li>未注册任务的阶段会被自动跳过</li>
* </p> * <li>每个任务执行前会输出当前阶段名</li>
* <li>执行中遇到异常将立即抛出并终止后续执行</li>
* </ul>
* *
* @throws Exception 若某个任务执行时抛出异常将直接抛出并终止后续任务执行 * @throws Exception 若某个任务执行时抛出异常将直接抛出
*/ */
public void executeAll() throws Exception { public void executeAll() throws Exception {
for (LifecyclePhase phase : LifecyclePhase.values()) { for (LifecyclePhase phase : LifecyclePhase.values()) {

View File

@ -1,7 +1,10 @@
package org.jcnc.snow.pkg.lifecycle; package org.jcnc.snow.pkg.lifecycle;
/** /**
* 定义了典型软件包生命周期的各个阶段 * 定义典型软件包生命周期的各个阶段枚举
* <p>
* 用于区分构建依赖发布等不同阶段的任务调度与管理
* </p>
*/ */
public enum LifecyclePhase { public enum LifecyclePhase {
/** 初始化阶段 */ /** 初始化阶段 */

View File

@ -6,16 +6,16 @@ import java.util.HashMap;
/** /**
* 构建配置对象封装构建过程中的所有选项 * 构建配置对象封装构建过程中的所有选项
* <p> * <p>
* 支持基于模板变量形如{@code @{key}}的选项值替换 * 支持模板变量形如 <code>@{key}</code>的值自动替换
* </p> * </p>
*/ */
public final class BuildConfiguration { public final class BuildConfiguration {
/** 存储配置项的键值对 */ /** 存储所有配置项 */
private final Map<String, String> options; private final Map<String, String> options;
/** /**
* 私有构造函数于初始化配置项 * 私有构造函数仅供工厂方法调
* *
* @param options 配置项键值对 * @param options 配置项键值对
*/ */
@ -24,14 +24,15 @@ public final class BuildConfiguration {
} }
/** /**
* 基于原始配置项和变量属性创建配置对象 * 基于原始配置项和属性集创建配置对象
* <p> * <ul>
* 会将原始配置中的所有值中的{@code @{key}}模板替换为属性props中对应的值 * <li>会将所有值中的 <code>@{key}</code> 模板变量替换为 props 中对应的值</li>
* </p> * <li>属性未匹配到时保留原模板</li>
* </ul>
* *
* @param flat 原始配置项值中可包含模板变量@{name} * @param flat 原始配置项值中可包含模板变量
* @param props 用于替换模板变量的属性集 * @param props 变量替换用的属性集
* @return 构建完成的配置对象 * @return 处理后生成的配置对象
*/ */
public static BuildConfiguration fromFlatMap(Map<String, String> flat, Map<String, String> props) { public static BuildConfiguration fromFlatMap(Map<String, String> flat, Map<String, String> props) {
Map<String, String> resolved = new HashMap<>(); Map<String, String> resolved = new HashMap<>();
@ -46,11 +47,11 @@ public final class BuildConfiguration {
} }
/** /**
* 获取指定key对应的配置值 * 获取指定配置项的
* *
* @param key 配置项名称 * @param key 配置项名称
* @param def 默认值若未找到key则返回此值 * @param def 默认值未找到时返回
* @return 配置项对应若不存在则返回默认值 * @return 配置项若不存在则返回默认值
*/ */
public String get(String key, String def) { public String get(String key, String def) {
return options.getOrDefault(key, def); return options.getOrDefault(key, def);

View File

@ -5,18 +5,23 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
* group:artifact:version 依赖坐标 * group:artifact:version 依赖坐标对象
* <p> * <p>
* 支持通过占位符和属性映射进行动态变量替换可用于构建工具或依赖管理场景 * 支持占位符和属性映射进行动态变量替换适用于 Snow 语言包管理和源码依赖场景
* </p> * </p>
* *
* <pre> * <pre>
* 示例 * 示例用法
* Dependency dep = Dependency.fromString( * Dependency dep = Dependency.fromString(
* "core", "com.example:core:@{version}", * "core", "com.example:core:@{version}",
* Map.of("version", "1.2.3") * Map.of("version", "1.2.3")
* ); * );
* </pre> * </pre>
*
* @param id 依赖唯一标识
* @param group 组织/分组名
* @param artifact 构件名
* @param version 版本号
*/ */
public record Dependency( public record Dependency(
String id, String id,
@ -25,25 +30,23 @@ public record Dependency(
String version String version
) { ) {
/** /** 匹配 group:artifact:version 格式的正则表达式。 */
* 用于匹配 group:artifact:version 格式的正则表达式
*/
private static final Pattern GAV = Pattern.compile("([^:]+):([^:]+):(.+)"); private static final Pattern GAV = Pattern.compile("([^:]+):([^:]+):(.+)");
/** /**
* 根据字符串坐标和属性映射创建依赖对象 * 根据字符串坐标和属性映射创建依赖对象
* <p> * <p>
* 坐标中的占位符 {@code @{key}} 会用 props 中对应的值替换 * 坐标中的占位符 <code>@{key}</code>会用 props 中对应的值替换
* </p> * </p>
* *
* @param id 依赖唯一标识 * @param id 依赖唯一标识
* @param coordinate 依赖坐标字符串格式为 group:artifact:version支持变量占位符 * @param coordinate 坐标字符串格式为 group:artifact:version支持占位符
* @param props 占位符替换属性映射 * @param props 占位符替换属性映射
* @return 解析后的 Dependency 实例 * @return 解析后的 Dependency 实例
* @throws IllegalArgumentException 如果坐标格式非法 * @throws IllegalArgumentException 坐标格式非法时抛出
*/ */
public static Dependency fromString(String id, String coordinate, Map<String, String> props) { public static Dependency fromString(String id, String coordinate, Map<String, String> props) {
// 替换 @{prop} 占位符 // 替换占位符
String resolved = coordinate; String resolved = coordinate;
for (Map.Entry<String, String> p : props.entrySet()) { for (Map.Entry<String, String> p : props.entrySet()) {
resolved = resolved.replace("@{" + p.getKey() + "}", p.getValue()); resolved = resolved.replace("@{" + p.getKey() + "}", p.getValue());
@ -57,23 +60,23 @@ public record Dependency(
} }
/** /**
* 生成依赖对应的标准仓库 jar 路径 * 生成依赖对应的源码文件路径
* <p> * <p>
* 路径格式通常为groupId/artifactId/version/artifactId-version.jar<br> * 路径格式groupId/artifactId/version/artifactId.snow
* 例如com/example/core/1.2.3/core-1.2.3.jar * 例如com/example/core/1.2.3/core.snow
* </p> * </p>
* *
* @return 仓库 jar 文件的相对路径 * @return 仓库源码文件的相对路径
*/ */
public String toPath() { public String toPath() {
String groupPath = group.replace('.', '/'); String groupPath = group.replace('.', '/');
return groupPath + "/" + artifact + "/" + version + "/" + artifact + "-" + version + ".jar"; return groupPath + "/" + artifact + "/" + version + "/" + artifact + ".snow";
} }
/** /**
* 返回该依赖的 group:artifact:version 字符串表示 * 返回该依赖的 group:artifact:version 字符串表示
* *
* @return Maven 坐标字符串 * @return 坐标字符串
*/ */
@Override @Override
public String toString() { public String toString() {

View File

@ -8,42 +8,41 @@ import java.util.Map;
/** /**
* 表示一个软件包/模块的项目信息包括元数据属性仓库依赖和构建配置等 * 表示一个软件包/模块的项目信息包括元数据属性仓库依赖和构建配置等
* <p> * <p>
* 本类为不可变对象仅提供gettersetter * 本类为不可变对象仅提供 getter 方法 setter<br>
* 支持通过 {@link #fromFlatMap(Map)} 静态工厂方法从扁平Map快速创建 * 支持通过 {@link #fromFlatMap(Map)} 静态工厂方法从扁平 Map 快速创建实例
* </p> * </p>
* *
* <pre> * <pre>
* Map&lt;String,String&gt; map = ...; * Map&lt;String, String&gt; map = ...;
* Project project = Project.fromFlatMap(map); * Project project = Project.fromFlatMap(map);
* </pre> * </pre>
*/ */
public final class Project { public final class Project {
/** 组织/分组名(如com.example */ /** 组织/分组名(如 com.example */
private final String group; private final String group;
/** 构件/模块名(如app-core */ /** 构件/模块名(如 app-core */
private final String artifact; private final String artifact;
/** 项目展示名称 */ /** 项目展示名称 */
private final String name; private final String name;
/** 版本号(如1.0.0 */ /** 版本号(如 1.0.0 */
private final String version; private final String version;
/** 项目描述 */ /** 项目描述 */
private final String description; private final String description;
/** 许可证标识 */ /** 许可证标识 */
private final String license; private final String license;
/** 项目主页URL */ /** 项目主页 URL */
private final String homepage; private final String homepage;
/** 额外属性(不影响主字段,可用于模板/占位符) */ /** 额外属性(不影响主字段,可用于模板/占位符) */
private final Map<String, String> properties; private final Map<String, String> properties;
/** 仓库列表(仓库ID -> 仓库对象) */ /** 仓库列表(仓库 ID -> 仓库对象) */
private final Map<String, Repository> repositories; private final Map<String, Repository> repositories;
/** 依赖列表 */ /** 依赖列表 */
private final List<Dependency> dependencies; private final List<Dependency> dependencies;
/** 构建配置 */ /** 构建配置 */
private final BuildConfiguration build; private final BuildConfiguration build;
/** /**
* 构造函数私有请使用 {@link #fromFlatMap(Map)} 创建实例 * 构造函数私有请使用 {@link #fromFlatMap(Map)} 创建实例
*/ */
@ -74,7 +73,7 @@ public final class Project {
} }
/** /**
* 通过扁平Map创建 Project 实例约定key格式如下 * 通过扁平 Map 创建 Project 实例key 格式约定如下
* <ul> * <ul>
* <li>project.* 项目元数据</li> * <li>project.* 项目元数据</li>
* <li>properties.* 额外属性</li> * <li>properties.* 额外属性</li>
@ -83,12 +82,11 @@ public final class Project {
* <li>build.* 构建配置</li> * <li>build.* 构建配置</li>
* </ul> * </ul>
* *
* @param map 扁平的配置map * @param map 扁平的配置 map
* @return Project 实例 * @return Project 实例
*/ */
public static Project fromFlatMap(Map<String, String> map) { public static Project fromFlatMap(Map<String, String> map) {
// 1. 基本元数据
// 1. simple project metadata
String group = map.getOrDefault("project.group", "unknown"); String group = map.getOrDefault("project.group", "unknown");
String artifact = map.getOrDefault("project.artifact", "unknown"); String artifact = map.getOrDefault("project.artifact", "unknown");
String name = map.getOrDefault("project.name", artifact); String name = map.getOrDefault("project.name", artifact);
@ -123,14 +121,13 @@ public final class Project {
} }
}); });
// 5. build.* simply hand the subtree map // 5. build.*
Map<String, String> buildMap = new LinkedHashMap<>(); Map<String, String> buildMap = new LinkedHashMap<>();
map.forEach((k, v) -> { map.forEach((k, v) -> {
if (k.startsWith("build.")) { if (k.startsWith("build.")) {
buildMap.put(k.substring("build.".length()), v); buildMap.put(k.substring("build.".length()), v);
} }
}); });
BuildConfiguration buildCfg = BuildConfiguration.fromFlatMap(buildMap, props); BuildConfiguration buildCfg = BuildConfiguration.fromFlatMap(buildMap, props);
return new Project(group, artifact, name, version, description, license, homepage, props, repos, deps, buildCfg); return new Project(group, artifact, name, version, description, license, homepage, props, repos, deps, buildCfg);
@ -161,12 +158,12 @@ public final class Project {
return description; return description;
} }
/** @return 许可证 */ /** @return 许可证标识 */
public String getLicense() { public String getLicense() {
return license; return license;
} }
/** @return 项目主页URL */ /** @return 项目主页 URL */
public String getHomepage() { public String getHomepage() {
return homepage; return homepage;
} }

View File

@ -3,16 +3,16 @@ package org.jcnc.snow.pkg.model;
/** /**
* 表示一个远程仓库的基本信息通常用于依赖解析和发布 * 表示一个远程仓库的基本信息通常用于依赖解析和发布
* <p> * <p>
* 每个仓库由唯一的ID和对应的URL确定 * 每个仓库由唯一的 ID 和对应的 URL 标识
* </p> * </p>
* *
* <pre> * <pre>
* 示例: * 示例用法
* Repository repo = new Repository("central", "https://"); * Repository repo = new Repository("central", "https://");
* </pre> * </pre>
* *
* @param id 仓库唯一标识 * @param id 仓库唯一标识
* @param url 仓库地址一般为HTTP(S)链接 * @param url 仓库地址通常为 HTTP(S) 链接
*/ */
public record Repository(String id, String url) { public record Repository(String id, String url) {
} }

View File

@ -6,17 +6,27 @@ import java.nio.file.Path;
import java.util.Comparator; import java.util.Comparator;
/** /**
* 清理构建输出目录 build dist的任务实现 * 用于清理构建输出目录 {@code build} {@code dist}的任务实现
* <p> * <p>
* 实现 {@link Task} 接口常用于构建流程的清理阶段 * 实现 {@link Task} 接口常用于自动化构建流程的清理阶段负责递归删除指定的构建产物目录
* </p> * </p>
* <p>
* 本类为无状态实现线程安全
* </p>
*
* <p><b>示例用法</b></p>
* <pre>{@code
* Task clean = new CleanTask();
* clean.run();
* }</pre>
*/ */
public final class CleanTask implements Task { public final class CleanTask implements Task {
/** /**
* 执行清理任务删除 "build" "dist" 目录及其所有内容 * 执行清理任务递归删除当前目录下的 {@code build} {@code dist} 目录及其所有内容
* 如果目标目录不存在则跳过不处理
* *
* @throws IOException 删除目录过程中出现 IO 错误时抛出 * @throws IOException 删除目录或文件过程中发生 IO 错误时抛出
*/ */
@Override @Override
public void run() throws IOException { public void run() throws IOException {
@ -27,10 +37,15 @@ public final class CleanTask implements Task {
/** /**
* 递归删除指定目录及其所有子文件和子目录 * 递归删除指定目录及其所有子文件和子目录
* 使用 try-with-resources 自动关闭文件流避免资源泄漏 * <p>
* 若目录不存在则直接返回
* </p>
* <p>
* 内部使用 try-with-resources 保证文件流自动关闭避免资源泄漏
* </p>
* *
* @param dir 需要删除的目录路径 * @param dir 需要删除的目录路径
* @throws IOException 删除过程中出现 IO 错误时抛出 * @throws IOException 删除目录或文件过程中发生 IO 错误时抛出
*/ */
private void deleteDir(Path dir) throws IOException { private void deleteDir(Path dir) throws IOException {
if (Files.notExists(dir)) return; if (Files.notExists(dir)) return;

View File

@ -25,17 +25,16 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
/** /**
* CLI 命令 .snow 源文件编译 VM 字节码.water 文件 * CLI 任务编译 .snow 源文件 VM 字节码.water 文件
* <p> * <p>
* 支持递归目录多文件编译可选编译后立即运行<br> * 支持单文件多文件和目录递归编译并可在编译后立即运行虚拟机<br>
* 命令参数支持 run-o-d * 命令参数支持 run-o-d 及直接指定源文件
* </p> * </p>
* *
* <pre> * <pre>
* 用法示例 * 用法示例
* $ snow compile [run] [-o &lt;name&gt;] [-d &lt;srcDir&gt;] [file1.snow file2.snow ] * $ snow compile [run] [-o &lt;name&gt;] [-d &lt;srcDir&gt;] [file1.snow file2.snow ...]
* </pre> * </pre>
*/ */
public final class CompileTask implements Task { public final class CompileTask implements Task {
@ -44,26 +43,49 @@ public final class CompileTask implements Task {
/** 原始命令行参数 */ /** 原始命令行参数 */
private final String[] args; private final String[] args;
/**
* 创建一个编译任务
*
* @param project 项目信息对象
* @param args 命令行参数数组
*/
public CompileTask(Project project, String[] args) { public CompileTask(Project project, String[] args) {
this.project = project; this.project = project;
this.args = args; this.args = args;
} }
/**
* 创建一个不带参数的编译任务
*
* @param project 项目信息对象
*/
public CompileTask(Project project) { public CompileTask(Project project) {
this(project, new String[0]); this(project, new String[0]);
} }
/**
* 执行编译任务该方法会解析参数并调用 {@link #execute(String[])} 进行实际编译流程
*
* @throws Exception 执行过程中出现任意异常时抛出
*/
@Override @Override
public void run() throws Exception { public void run() throws Exception {
// 将任务委托给原始 execute 实现
execute(this.args); execute(this.args);
} }
/* --------------------------------------------------------------------- */ /**
/* 执行 compile 子命令 */ * 编译 .snow 源文件为 VM 字节码并可选择立即运行
/* --------------------------------------------------------------------- */ * <ul>
* <li>支持参数 run编译后运行-o输出文件名-d递归目录直接指定多个源文件</li>
* <li>输出源代码ASTIR最终 VM code并写出 .water 文件</li>
* </ul>
*
* @param args 命令行参数数组
* @return 0 表示成功 0 表示失败
* @throws Exception 编译或写入过程中出现异常时抛出
*/
public int execute(String[] args) throws Exception { public int execute(String[] args) throws Exception {
/* ---------------- 解析命令行参数 ---------------- */ // ---------------- 解析命令行参数 ----------------
boolean runAfterCompile = false; boolean runAfterCompile = false;
String outputName = null; String outputName = null;
Path dir = null; Path dir = null;
@ -101,7 +123,7 @@ public final class CompileTask implements Task {
} }
} }
/* --------- 如果指定了目录则递归收集所有 *.snow --------- */ // --------- 如果指定了目录则递归收集所有 *.snow ---------
if (dir != null) { if (dir != null) {
if (!Files.isDirectory(dir)) { if (!Files.isDirectory(dir)) {
System.err.println("Not a directory: " + dir); System.err.println("Not a directory: " + dir);
@ -119,19 +141,17 @@ public final class CompileTask implements Task {
return 1; return 1;
} }
/* 多文件但未指定 -o 且非目录编译 —— 提示必须指定输出名 */ // 多文件但未指定 -o 且非目录编译 提示必须指定输出名
if (sources.size() > 1 && outputName == null && dir == null) { if (sources.size() > 1 && outputName == null && dir == null) {
System.err.println("Please specify output name using -o <name>"); System.err.println("Please specify output name using -o <name>");
return 1; return 1;
} }
/* ----------------------------------------------------------------- */ // ---------------- 1. 词法/语法分析并打印源代码 ----------------
/* 1. 词法 + 语法分析;同时打印源代码 */
/* ----------------------------------------------------------------- */
List<Node> allAst = new ArrayList<>(); List<Node> allAst = new ArrayList<>();
System.out.println("## 编译器输出"); System.out.println("## 编译器输出");
System.out.println("### Snow 源代码"); // ========== 新增二级标题 ========== System.out.println("### Snow 源代码");
for (Path p : sources) { for (Path p : sources) {
if (!Files.exists(p)) { if (!Files.exists(p)) {
@ -141,38 +161,31 @@ public final class CompileTask implements Task {
String code = Files.readString(p, StandardCharsets.UTF_8); String code = Files.readString(p, StandardCharsets.UTF_8);
// ------- 打印每个文件的源码 ------- // 打印源码
System.out.println("#### " + p.getFileName()); System.out.println("#### " + p.getFileName());
System.out.println(code); System.out.println(code);
// --------------------------------------------------------
/* 词法 + 语法 */ // 词法语法分析
LexerEngine lexer = new LexerEngine(code, p.toString()); LexerEngine lexer = new LexerEngine(code, p.toString());
ParserContext ctx = new ParserContext(lexer.getAllTokens(), p.toString()); ParserContext ctx = new ParserContext(lexer.getAllTokens(), p.toString());
allAst.addAll(new ParserEngine(ctx).parse()); allAst.addAll(new ParserEngine(ctx).parse());
} }
/* ----------------------------------------------------------------- */ // ---------------- 2. 语义分析 ----------------
/* 2. 语义分析 */
/* ----------------------------------------------------------------- */
SemanticAnalyzerRunner.runSemanticAnalysis(allAst, false); SemanticAnalyzerRunner.runSemanticAnalysis(allAst, false);
/* ----------------------------------------------------------------- */ // ---------------- 3. AST IR并将 main 函数移动至首位 ----------------
/* 3. AST → IR并把 main 函数调到首位 */
/* ----------------------------------------------------------------- */
IRProgram program = new IRProgramBuilder().buildProgram(allAst); IRProgram program = new IRProgramBuilder().buildProgram(allAst);
program = reorderForEntry(program); program = reorderForEntry(program);
/* ---------------- 打印 AST / IR ---------------- */ // 打印 AST IR
System.out.println("### AST"); System.out.println("### AST");
ASTPrinter.printJson(allAst); ASTPrinter.printJson(allAst);
System.out.println("### IR"); System.out.println("### IR");
System.out.println(program); System.out.println(program);
/* ----------------------------------------------------------------- */ // ---------------- 4. IR VM 指令 ----------------
/* 4. IR → VM 指令 */
/* ----------------------------------------------------------------- */
VMProgramBuilder builder = new VMProgramBuilder(); VMProgramBuilder builder = new VMProgramBuilder();
List<InstructionGenerator<? extends IRInstruction>> generators = List<InstructionGenerator<? extends IRInstruction>> generators =
InstructionGeneratorProvider.defaultGenerators(); InstructionGeneratorProvider.defaultGenerators();
@ -187,16 +200,12 @@ public final class CompileTask implements Task {
System.out.println("### VM code"); System.out.println("### VM code");
finalCode.forEach(System.out::println); finalCode.forEach(System.out::println);
/* ----------------------------------------------------------------- */ // ---------------- 5. 写出 .water 文件 ----------------
/* 5. 写出 .water 文件 */
/* ----------------------------------------------------------------- */
Path outputFile = deriveOutputPath(sources, outputName, dir); Path outputFile = deriveOutputPath(sources, outputName, dir);
Files.write(outputFile, finalCode, StandardCharsets.UTF_8); Files.write(outputFile, finalCode, StandardCharsets.UTF_8);
System.out.println("Written to " + outputFile.toAbsolutePath()); System.out.println("Written to " + outputFile.toAbsolutePath());
/* ----------------------------------------------------------------- */ // ---------------- 6. 可选立即运行 VM ----------------
/* 6. 可选:立即运行 VM */
/* ----------------------------------------------------------------- */
if (runAfterCompile) { if (runAfterCompile) {
System.out.println("\n=== Launching VM ==="); System.out.println("\n=== Launching VM ===");
VMLauncher.main(new String[]{outputFile.toString()}); VMLauncher.main(new String[]{outputFile.toString()});
@ -205,18 +214,19 @@ public final class CompileTask implements Task {
return 0; return 0;
} }
/* --------------------------------------------------------------------- */
/* 辅助方法 */
/* --------------------------------------------------------------------- */
/** /**
* 根据输入情况推断 .water 输出文件名 * 推断 .water 输出文件名
* <ul> * <ul>
* <li>指定 -o直接使用</li> * <li>如果指定 -o直接使用该名称</li>
* <li>目录编译取目录名</li> * <li>目录编译取目录名</li>
* <li>单文件编译取文件名去掉 .snow</li> * <li>单文件编译取文件名去掉 .snow 后缀</li>
* <li>其他情况兜底为 "program"</li> * <li>否则默认 "program"</li>
* </ul> * </ul>
*
* @param sources 源文件路径列表
* @param outName 输出文件名如有指定否则为 null
* @param dir 源码目录如有指定否则为 null
* @return 推断出的输出文件路径.water 文件
*/ */
private static Path deriveOutputPath(List<Path> sources, String outName, Path dir) { private static Path deriveOutputPath(List<Path> sources, String outName, Path dir) {
String base; String base;
@ -234,7 +244,10 @@ public final class CompileTask implements Task {
} }
/** /**
* main 函数交换到程序函数列表首位确保 PC=0 即入口 * main 函数调整至函数列表首位确保程序入口为 PC=0
*
* @param in 原始 IRProgram
* @return 调整入口后的 IRProgram
*/ */
private static IRProgram reorderForEntry(IRProgram in) { private static IRProgram reorderForEntry(IRProgram in) {
List<IRFunction> ordered = new ArrayList<>(in.functions()); List<IRFunction> ordered = new ArrayList<>(in.functions());

View File

@ -10,9 +10,10 @@ import java.nio.file.Paths;
import java.util.List; import java.util.List;
/** /**
* 任务依据 {@link Project} 元数据创建标准项目目录 * 项目脚手架生成任务<br>
* 根据 {@link Project} 元数据自动创建标准项目目录结构并生成示例入口文件 <code>src/main.snow</code>
* <p> * <p>
* 生成内容 * 生成内容包括
* <ul> * <ul>
* <li>src/ 源码目录</li> * <li>src/ 源码目录</li>
* <li>test/ 测试源码目录</li> * <li>test/ 测试源码目录</li>
@ -20,27 +21,44 @@ import java.util.List;
* <li>dist/ 打包输出目录</li> * <li>dist/ 打包输出目录</li>
* <li>src/main.snow Hello, Snow! 示例入口</li> * <li>src/main.snow Hello, Snow! 示例入口</li>
* </ul> * </ul>
* 如目录或入口文件已存在则自动跳过不会覆盖
* </p>
*/ */
public final class GenerateTask implements Task { public final class GenerateTask implements Task {
/** 项目信息元数据 */
private final Project project; private final Project project;
/**
* 创建项目生成任务
*
* @param project 项目信息元数据对象
*/
public GenerateTask(Project project) { public GenerateTask(Project project) {
this.project = project; this.project = project;
} }
/**
* 执行脚手架生成流程创建标准目录和入口示例文件
* <ul>
* <li>若相关目录不存在则创建</li>
* <li> <code>src/main.snow</code> 不存在则写入模板</li>
* <li>生成过程输出进度信息</li>
* </ul>
*
* @throws IOException 创建目录或写入文件时发生 IO 错误时抛出
*/
@Override @Override
public void run() throws IOException { public void run() throws IOException {
Path root = Paths.get(".").toAbsolutePath(); Path root = Paths.get(".").toAbsolutePath();
/* -------- 创建目录 -------- */ // 创建标准目录
List<Path> dirs = List.of( List<Path> dirs = List.of(
root.resolve("src"), root.resolve("src"),
root.resolve("test"), root.resolve("test"),
root.resolve("build"), root.resolve("build"),
root.resolve("dist") root.resolve("dist")
); );
for (Path dir : dirs) { for (Path dir : dirs) {
if (Files.notExists(dir)) { if (Files.notExists(dir)) {
Files.createDirectories(dir); Files.createDirectories(dir);
@ -48,7 +66,7 @@ public final class GenerateTask implements Task {
} }
} }
/* -------- 创建示例入口文件 -------- */ // 创建 src/main.snow 示例入口文件
Path mainSnow = root.resolve("src").resolve("main.snow"); Path mainSnow = root.resolve("src").resolve("main.snow");
if (Files.notExists(mainSnow)) { if (Files.notExists(mainSnow)) {
Files.writeString(mainSnow, SnowExampleTemplate.getMainModule()); Files.writeString(mainSnow, SnowExampleTemplate.getMainModule());

View File

@ -9,29 +9,38 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
/** /**
* 项目打包任务将编译输出目录 build/classes打包为 .ice 文件 * 项目打包任务将编译输出目录 <code>build/classes</code>打包为 .ice 文件
* <p> * <p>
* 实现 {@link Task} 接口通常用于构建流程中的打包阶段 * 实现 {@link Task} 接口通常用于构建流程的打包阶段<br>
* 只会打包 build/classes 目录下所有文件不含其他目录
* </p>
* <p>
* 输出文件位于 dist 目录命名为 artifact-version.ice
* </p> * </p>
*/ */
public final class PackageTask implements Task { public final class PackageTask implements Task {
/** 目标项目 */ /** 目标项目元数据 */
private final Project project; private final Project project;
/** /**
* 创建 PackageTask 实例 * 创建打包任务
* *
* @param project 目标项目 * @param project 目标项目元数据
*/ */
public PackageTask(Project project) { public PackageTask(Project project) {
this.project = project; this.project = project;
} }
/** /**
* 执行打包任务将编译输出目录压缩为 artifact-version.ice 文件 * 执行打包任务 build/classes 目录下所有文件压缩为 dist/artifact-version.ice
* <ul>
* <li>若输出目录 dist 不存在会自动创建</li>
* <li>只打包 build/classes 下所有普通文件保持相对目录结构</li>
* <li>如无 build/classes 则不会生成包</li>
* </ul>
* *
* @throws Exception 打包过程中出现 IO 或其他异常时抛出 * @throws Exception 打包过程中发生 IO 或其他异常时抛出
*/ */
@Override @Override
public void run() throws Exception { public void run() throws Exception {
@ -43,15 +52,15 @@ public final class PackageTask implements Task {
Path packageFile = distDir.resolve(fileName); Path packageFile = distDir.resolve(fileName);
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(packageFile))) { try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(packageFile))) {
// 仅将编译输出目录打包 // 仅将 build/classes 目录打包
Path classesDir = Path.of("build/classes"); Path classesDir = Path.of("build/classes");
if (Files.exists(classesDir)) { if (Files.exists(classesDir)) {
// 使用 try-with-resources 正确关闭 Stream<Path> // 遍历所有文件并写入 zip
try (Stream<Path> stream = Files.walk(classesDir)) { try (Stream<Path> stream = Files.walk(classesDir)) {
stream.filter(Files::isRegularFile) stream.filter(Files::isRegularFile)
.forEach(p -> { .forEach(p -> {
try { try {
// 将文件以相对路径加入压缩包 // classesDir 为根的相对路径存入 zip
zos.putNextEntry(new ZipEntry(classesDir.relativize(p).toString())); zos.putNextEntry(new ZipEntry(classesDir.relativize(p).toString()));
Files.copy(p, zos); Files.copy(p, zos);
zos.closeEntry(); zos.closeEntry();

View File

@ -5,25 +5,26 @@ import org.jcnc.snow.pkg.model.Project;
/** /**
* 发布项目构件到远程仓库的任务实现 * 发布项目构件到远程仓库的任务实现
* <p> * <p>
* 实现 {@link Task} 接口通常用于构建流程中的发布阶段目前仅为演示尚未实现实际上传 * 实现 {@link Task} 接口通常用于构建流程中的发布阶段
* 当前仅输出发布提示尚未实现实际上传功能
* </p> * </p>
*/ */
public final class PublishTask implements Task { public final class PublishTask implements Task {
/** 目标项目 */ /** 目标项目元数据 */
private final Project project; private final Project project;
/** /**
* 创建 PublishTask 实例 * 创建发布任务
* *
* @param project 目标项目 * @param project 目标项目元数据
*/ */
public PublishTask(Project project) { public PublishTask(Project project) {
this.project = project; this.project = project;
} }
/** /**
* 执行发布任务当前仅打印发布提示未实现实际上传逻辑 * 执行发布任务目前仅打印发布提示信息未实现实际上传逻辑
* *
* @throws Exception 预留未来实现上传逻辑时可能抛出异常 * @throws Exception 预留未来实现上传逻辑时可能抛出异常
*/ */

View File

@ -1,4 +1,3 @@
package org.jcnc.snow.pkg.tasks; package org.jcnc.snow.pkg.tasks;
import org.jcnc.snow.vm.VMLauncher; import org.jcnc.snow.vm.VMLauncher;
@ -6,26 +5,32 @@ import org.jcnc.snow.vm.VMLauncher;
/** /**
* 任务执行已编译的 VM 字节码文件.water * 任务执行已编译的 VM 字节码文件.water
* <p> * <p>
* 作为 CLIIDE 插件或其他宿主环境启动虚拟机的统一入口 * 作为 CLIIDE 插件或其他宿主环境启动虚拟机的统一入口<br>
* 调用 {@link VMLauncher#main(String[])} 执行 * 通过调用 {@link VMLauncher#main(String[])} 启动 VM 执行指定程序
* </p> * </p>
*/ */
public final class RunTask implements Task { public final class RunTask implements Task {
/** 传递给 VM 的完整参数列表(第一个应为 .water 文件路径) */ /**
* 传递给虚拟机的完整参数列表第一个应为 .water 文件路径
*/
private final String[] args; private final String[] args;
/** /**
* 创建运行任务 * 创建运行任务
* *
* @param args VM 参数数组第一个为程序路径其后为可选参数 * @param args VM 参数数组第一个为 .water 程序路径其后为可选参数
*/ */
public RunTask(String... args) { public RunTask(String... args) {
this.args = args; this.args = args;
} }
/** /**
* 执行运行任务内部委托 {@link VMLauncher#main(String[])} * 执行运行任务内部委托 {@link VMLauncher#main(String[])} 启动 VM
* <ul>
* <li>如果参数为空则抛出 {@link IllegalArgumentException}</li>
* <li>异常由虚拟机本身抛出直接透出</li>
* </ul>
* *
* @throws Exception 虚拟机启动或运行期间抛出的异常 * @throws Exception 虚拟机启动或运行期间抛出的异常
*/ */

View File

@ -1,9 +1,9 @@
package org.jcnc.snow.pkg.tasks; package org.jcnc.snow.pkg.tasks;
/** /**
* 构建任务的通用接口所有具体任务都应实现该接口 * 构建任务的通用接口所有具体任务如编译打包清理等都应实现该接口
* <p> * <p>
* 用于统一生命周期内的任务行为例如编译打包清理等 * 用于统一不同阶段任务的生命周期与执行行为
* </p> * </p>
*/ */
public interface Task { public interface Task {
@ -11,7 +11,7 @@ public interface Task {
/** /**
* 执行具体任务的入口方法 * 执行具体任务的入口方法
* *
* @throws Exception 任务执行过程中出现的异常 * @throws Exception 任务执行过程中出现的任意异常
*/ */
void run() throws Exception; void run() throws Exception;
} }

View File

@ -1,18 +1,24 @@
package org.jcnc.snow.pkg.utils; package org.jcnc.snow.pkg.utils;
/** /**
* 提供示例 .snow 模块代码模板 * 示例模块模板工具类提供 main.snow 的标准示例代码字符串
* <p>
* 用于项目脚手架生成或帮助用户快速上手 .snow 语言
* </p>
*/ */
public final class SnowExampleTemplate { public final class SnowExampleTemplate {
/**
* 工具类构造方法禁止实例化
*/
private SnowExampleTemplate() { private SnowExampleTemplate() {
// 工具类不允许实例化 // 工具类不允许实例化
} }
/** /**
* 返回 main.snow 示例模块内容 * 获取 main.snow 示例模块内容字符串
* *
* @return 示例模块代码字符串 * @return main.snow 示例模块的完整代码
*/ */
public static String getMainModule() { public static String getMainModule() {
return """ return """