2626
2727如果还需初始化 Git,可使用 ` git init ` 命令。
2828
29+ ### PkgTemplates.jl
30+ [ PkgTemplates.jl] ( https://github.com/JuliaCI/PkgTemplates.jl ) 可以自动化完成创建包时的常见配置,生成比 ` generate ` 更丰富的项目框架(包含 ` .gitignore ` 、` LICENSE ` 、GitHub Actions 工作流等)。[ ^ 2 ]
31+
32+ ``` julia-repl
33+ julia> using PkgTemplates
34+ julia> t = Template(user="myuser", interactive=false)
35+ julia> t("MyAwesomePackage")
36+ ```
37+
38+ 之后将生成的目录推送到 GitHub 仓库即可。[ PackageMaker.jl] ( https://github.com/Eben60/PackageMaker.jl ) 提供了 PkgTemplates.jl 的图形化封装,操作更加便捷。
39+
2940也可以参照:
3041- [ 官方提供的包示例] ( https://github.com/JuliaLang/Example.jl )
31- - [ 包模板生成器] ( https://invenia.github.io/PkgTemplates.jl/stable/ )
32-
33- 获得更丰富的模板。
42+ - [ 包模板生成器文档] ( https://juliaci.github.io/PkgTemplates.jl/stable/ )
3443
3544## 包的配置
3645包的配置数据写在 ` Project.toml ` 文件中。这个文件使用 [ TOML 格式] ( ../knowledge/toml.md )
@@ -104,6 +113,73 @@ Base.Math
104113
105114[ FAQ] ( https://github.com/JuliaRegistries/General#faq )
106115
116+ ## GitHub Actions 与 CI
117+ [ PkgTemplates.jl] ( https://github.com/JuliaCI/PkgTemplates.jl ) 会在 ` .github/workflows/ ` 目录下自动生成 [ GitHub Actions] ( https://docs.github.com/en/actions/quickstart ) 工作流文件(YAML 格式)。其中 ` CI.yml ` 会在每次 pull request、tag 或推送到 ` main ` 分支时自动运行测试。对于公开仓库,GitHub 提供免费的无限工作流配额。[ ^ 2 ]
118+
119+ 还可以通过 PkgTemplates.jl 的[ 插件] ( https://juliaci.github.io/PkgTemplates.jl/stable/user/#Plugins-1 ) 启用更多功能,如文档构建、代码覆盖率统计、格式化检查等。使用 ` Template(..., interactive=true) ` 可在交互模式下选择所需插件。
120+
121+ ## 代码风格
122+ 为使代码易于阅读,建议遵循统一的代码风格规范。官方[ 风格指南] ( https://docs.julialang.org/en/v1/manual/style-guide/ ) 比较简短,大多数人会使用第三方规范,如 [ BlueStyle] ( https://github.com/JuliaDiff/BlueStyle ) 或 [ SciMLStyle] ( https://github.com/SciML/SciMLStyle ) 。[ ^ 2 ]
123+
124+ [ JuliaFormatter.jl] ( https://github.com/domluna/JuliaFormatter.jl ) 是 Julia 文件的自动格式化工具。在仓库根目录添加 ` .JuliaFormatter.toml ` 文件并指定风格,然后调用:
125+
126+ ``` julia-repl
127+ julia> using JuliaFormatter
128+ julia> JuliaFormatter.format(MyAwesomePackage)
129+ true
130+ ```
131+
132+ VSCode 的默认格式化功能即基于 JuliaFormatter.jl 实现。也可通过 [ julia-format action] ( https://github.com/julia-actions/julia-format ) 在 GitHub pull request 中自动格式化代码。
133+
134+ ## 代码质量
135+ 除格式之外,还有更多维度的代码质量检查工具。[ ^ 2 ]
136+
137+ [ Aqua.jl] ( https://github.com/JuliaTesting/Aqua.jl ) 提供一系列自动检查,涵盖未使用的依赖、方法二义性等问题,建议在测试中包含:
138+
139+ ``` julia-repl
140+ julia> using Aqua, MyAwesomePackage
141+ julia> Aqua.test_all(MyAwesomePackage)
142+ ```
143+
144+ [ JET.jl] ( https://github.com/aviatesk/JET.jl ) 是一个静态分析工具,通过类型推断在不运行代码的情况下检测错误和潜在问题,提供错误分析和优化分析两种模式:
145+
146+ ``` julia-repl
147+ julia> using JET, MyAwesomePackage
148+ julia> JET.test_package(MyAwesomePackage)
149+ Test Passed
150+ ```
151+
152+ [ ExplicitImports.jl] ( https://github.com/ericphanson/ExplicitImports.jl ) 帮助消除泛化导入,明确每个名称的来源,增强代码对依赖项名称冲突的鲁棒性。
153+
154+ ## 版本兼容性
155+ Julia 社区采用[ 语义化版本控制] ( https://semver.org/ ) ,每个包必须在 ` Project.toml ` 的 ` [compat] ` 节中指定依赖的版本兼容范围。可使用 REPL 中的 ` ]compat ` 命令或 [ PackageCompatUI.jl] ( https://github.com/GunnarFarneback/PackageCompatUI.jl ) 来初始化这些范围。[ ^ 2 ]
156+
157+ 随着依赖包发布新版本,[ CompatHelper.jl] ( https://github.com/JuliaRegistries/CompatHelper.jl ) GitHub Action 会自动监控并提交 PR 更新 ` Project.toml ` 。[ Dependabot] ( https://docs.github.com/en/code-security/dependabot ) 则可监控 GitHub Actions 本身的依赖更新。两者均是 PkgTemplates.jl 的默认插件。
158+
159+ ## 可重现性
160+ 获得一致且可重现的实验结果对科学研究至关重要。[ DrWatson.jl] ( https://github.com/JuliaDynamics/DrWatson.jl ) 是一个通用的实验管理工具箱,提供规范化运行和复现实验的功能。[ ^ 2 ]
161+
162+ 其他常用工具:
163+ - [ StableRNGs.jl] ( https://github.com/JuliaRandom/StableRNGs.jl ) :确保随机数流在不同 Julia 版本之间保持一致
164+ - [ DataDeps.jl] ( https://github.com/oxinabox/DataDeps.jl ) 、[ DataToolkit.jl] ( https://github.com/tecosaur/DataToolkit.jl ) :管理非代码资产(数据集等)的下载与绑定
165+ - [ PkgCite.jl] ( https://github.com/SebastianM-C/PkgCite.jl ) :生成依赖包的学术引用信息
166+ - [ Zenodo] ( https://zenodo.org/ ) :为包分配 DOI,便于学术引用
167+
168+ ## 互操作性
169+ [ Compat.jl] ( https://github.com/JuliaLang/Compat.jl ) 是保证与旧版 Julia 兼容的最佳工具。[ ^ 2 ]
170+
171+ Julia 1.9 起支持[ 包扩展(Package Extensions)] ( https://pkgdocs.julialang.org/v1/creating-packages/#Conditional-loading-of-code-in-packages-(Extensions) ) ,可根据环境中是否存在特定包来覆盖特定行为,实现包之间的互操作。[ PackageExtensionTools.jl] ( https://github.com/cjdoris/PackageExtensionTools.jl ) 简化了扩展的设置流程。
172+
173+ Julia 生态系统也与其他编程语言良好协作:
174+ - C 和 Fortran:Julia 原生支持
175+ - Python:[ CondaPkg.jl] ( https://github.com/cjdoris/CondaPkg.jl ) + [ PythonCall.jl] ( https://github.com/cjdoris/PythonCall.jl ) 组合
176+ - R:[ RCall.jl] ( https://github.com/JuliaInterop/RCall.jl )
177+
178+ 更多语言互操作包可在 [ JuliaInterop] ( https://github.com/JuliaInterop ) 组织中找到。
179+
180+ ## 协作规范
181+ 包规模增大后可能需要团队合作。[ SciML ColPrac] ( https://github.com/SciML/ColPrac ) 提供了一套被广泛采用的协作规范。同时,如果你喜欢某个 Julia 包,也非常欢迎通过提交 issue 或 pull request 来[ 参与贡献] ( https://julialang.org/contribute/ ) 。[ ^ 2 ]
182+
107183## 最佳实践
108184包应该避免改变自己的状态(写入包目录中的文件)。一般来说,包不应该假定它们位于可写的位置,甚至不应该假定它们位于稳定的位置(例如,如果它被捆绑到一个系统映像中)。为了支持 Julia 包生态系统中的各种用例,Pkg 开发人员创建了许多辅助包和技术,以帮助包作者创建自包含的、不可变的和可重定位的包:
109185
@@ -116,7 +192,5 @@ Julia 1.5 以后,[Scratch](../packages/scratch.md)提供了*临时空间*的
116192### Preferences
117193Julia 1.6 以后,` Preferences ` 允许包读写首选项到顶级的 ` Project.toml ` 。这些首选项可以在运行时或编译时读取,以启用或禁用包行为的不同方面(以前,包会将文件写入到它们自己的包目录中以记录由用户或环境设置的选项,但现在不鼓励该行为)
118194
119- ## 参阅
120- - [ Modern Julia Workflows] ( https://modernjuliaworkflows.org/ )
121-
122195[ ^ 1 ] : https://juliaregistries.github.io/RegistryCI.jl/stable/guidelines/
196+ [ ^ 2 ] : [ Modern Julia Workflows - Sharing your code] ( https://modernjuliaworkflows.org/sharing/ ) by G. Dalle, J. Smit, A. Hill(CC BY-SA 4.0)
0 commit comments