开发速记-emacs篇: clojure+gradle开发模式

安装nix并配置channel:

sudo install -d -m755 -o $(id -u) -g $(id -g) /nix
curl https://nixos.org/nix/install | sh

nix-channel --add https://nixos.org/channels/nixos-20.03 nixos-20.03
nix-channel --update nixos-20.03

安装gradle:

nix-env -f ~/.nix-defexpr/channels/nixos-20.03 -iA gradle

安装emacs:

nix-env -f ~/.nix-defexpr/channels/nixos-20.03 -iA emacs

配置emacs:

创建并拷贝github文件内容至~/.emacs.d/init.el
https://github.com/clojurians-org/my-emacs/blob/master/init.el

  1. 安装paredit插号被合及开发工具cider
(dolist (package '(paredit restclient json-mode typescript-mode                                                                     
                   dap-mode lsp-java gradle-mode                                                                                    
                           dante cider                                                                                              
                   ))                                                                                                               
  (unless (package-installed-p package)                                                                                             
    (package-install package)))                                                                                                     
  1. 开启paredit-mode
(add-hook 'clojure-mode-hook #'enable-paredit-mode)

创建项目:

初始化为java项目

larrys-MacBook-Pro:my-test larluo$ mkdir my-test
larrys-MacBook-Pro:my-test larluo$ cd my-test
larrys-MacBook-Pro:my-test larluo$ gradle init                                                                                      
                                                                                                                                    
Select type of project to generate:                                                                                                 
  1: basic                                                                                                                          
  2: application                                                                                                                    
  3: library                                                                                                                        
  4: Gradle plugin                                                                                                                  
Enter selection (default: basic) [1..4] 3                                                                                           
                                                                                                                                    
Select implementation language:                                                                                                     
  1: C++
  2: Groovy
  3: Java
  4: Kotlin
  5: Scala
  6: Swift
Enter selection (default: Java) [1..6]

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2]

Select test framework:
  1: JUnit 4
  2: TestNG
  3: Spock
  4: JUnit Jupiter
Enter selection (default: JUnit 4) [1..4]

Project name (default: my-test):
Source package (default: my.test):

> Task :init
Get more help with your project: https://docs.gradle.org/5.6.1/userguide/java_library_plugin.html

BUILD SUCCESSFUL in 5m 14s
2 actionable tasks: 2 executed

添加clojure repl支持

修改build.gradle文件内容

  1. 添加clojure gradle插件
plugins {                                                                                                                          $
    // Apply the java-library plugin to add support for Java Library                                                               $
    id 'java-library'                                                                                                              $
    id 'dev.clojurephant.clojure' version '0.5.0'                                                                                  $
}                                                                                                                                  $
  1. 设置国内源
repositories {                                                                                                                      
    maven {                                                                                                                         
        url "https://mirrors.tuna.tsinghua.edu.cn/clojars/"                                                                         
    }                                                                                                                               
    maven {                                                                                                                         
        url "http://maven.aliyun.com/nexus/content/groups/public"
    }
    mavenLocal()
}
  1. 添加依赖
dependencies {
    api 'commons-codec:commons-codec:1.14'
    implementation 'org.clojure:clojure:1.10.1'
    devImplementation 'cider:cider-nrepl:0.24.0'
    testImplementation 'junit:junit:4.12'
}
  1. 配置emacs cider hook:
clojureRepl {
  handler = 'cider.nrepl/cider-nrepl-handler'
}

使用emacs编码

  1. 创建并打开文件
mkdir -p src/main/clojure
emacs -nw src/main/clojure/larluo.clj
  1. 导入项目
i==>Import project root /Users/larluo/my-tmp/my-test/src/main/java/my/test/.
I==>Import project by selecting root directory interactively.
d==>Do not ask again for the current project by adding /Users/larluo/my-tmp/my-test/src/main/java/my/test/ to lsp-session-folders-b\
lacklist.
D==>Do not ask again for the current project by selecting ignore path interactively.
n==>Do nothing: ask again when opening other files from the current project.

选择I, 输入项目目录,后续可变改

  1. 启动cider repl进行开发
    https://github.com/clojure-emacs/cider

输入C-c, A-j 启动cider
移动代码,执行C-x, C-e运行代码


image.png

你可能感兴趣的:(开发速记-emacs篇: clojure+gradle开发模式)