LoginSignup
6
8

More than 5 years have passed since last update.

[Salesforce][Vim] MacOSX にて Vim で force.com 開発ができる環境を作るまで

Last updated at Posted at 2018-02-20

こんにちは。黒い画面(ターミナルね)大好き、進地です。

force.com の開発、主に Apex と Visualforce の開発をなんとか黒い画面で、Vim でやれないものかと探したところ、 vim-force.com なる素敵な Vim プラグインがあったので導入しました。

以下は、 MacOSX ( High Sierra ) に vim-force.com プラグインをインストール・設定して、既存組織が突っ込まれている git リポジトリ ともマージし、 Vim でバージョン管理されたコードを開発できるように環境を作った時のメモです。

準備

Java JDK/JRE と ant のインストール

Java JDK/JREant が必要なので、入ってない場合はインストールします。

私の環境では ant が入っていなかったので、 Homebrew でサクッとインストールしました(良い時代だ)。

ant-install.sh
$ brew install ant

Tooling-force.com の設置

ant は ~/.ant/lib/ ディレクトリの jar をロードしてくれますので、 ~/.ant/lib/ ディレクトリを掘り、toolng-force.com.jar をダウンロードして ~/.ant/lib/ に設置します。

set-tooling-force.jar.sh
$ cd ~/
$ mkdir -p .ant/lib
$ cd ~/.ant/lib/
$ wget https://github.com/neowit/tooling-force.com/releases/download/v0.4.0.2/tooling-force.com-0.4.0.2.jar

ワークスペースの作成

ワークスペースにするディレクトリを作成します。 ~/force_workspace/ としました。

mk_workspace.sh
$ cd ~/
$ mkdir force_workspace

vim-force.com のインストール

neobundle を使っていれば簡単です。
.vimrc の call neobundle#begincall neobundle#end の間に以下を追記します。

NeoBundle 'neowit/vim-force.com'

追記したら、 :NeoBundleInstall を実行すればインストールされます。

参考: NeoBundleを用いたVimプラグイン管理と色設定メモ - Qiita

.vimrc の設定

.vimrc に 以下の設定を行います。パスはフルパスで指定しなければいけないことに注意してください。
以下は カレントユーザーが foo の場合の設定です。

" vim-force.com 設定
let g:apex_properties_folder = "/Users/foo/force_workspace/properties/"
let g:apex_temp_folder = "/tmp/apex/gvim-deproyment"
let g:apex_deployment_error_log = "/tmp/gvim-deployment-error.log"
let g:apex_tooling_force_dot_com_path = "/Users/foo/.ant/lib/tooling-force.com-0.4.0.2.jar"
let g:apex_workspace_path = "/Users/foo/force_workspace"
let g:apex_backup_folder = "/Users/foo/force_workspace/backup"
let g:apex_API_version = "40.0"

2018/02/21 追記

.vimrc に let g:apex_API_version = "40.0" を追記しました。
この設定がないとメタデータの保存時 ( :ApexSave 実行時など ) に

ERROR: Operation failed                                                            
ERROR: Entity type 'CspTrustedSite' is not available in this api version          
ERROR: Entity type 'EclairGeoData' is not available in this api version            
ERROR: Entity type 'EmbeddedServiceBranding' is not available in this api version  
ERROR: Entity type 'MacroSettings' is not available in this api version            
ERROR: Entity type 'FileUploadAndDownloadSecuritySettings' is not available in this api version

という API バージョンに起因するエラーが生じて処理が行えませんでした。

Apex プロジェクトを既存組織から作る

Vim を立ち上げて、以下を実行します。

:ApexInitProject
Enter project name: myproject
Enter username: (対象組織のユーザー名を入力) 
Enter password: (対象組織のパスワードを入力)
Enter security token: (対象組織のセキュリティトークンを入力。使っているなら)
Enter org type (test|login), if blank then defaults to "test": (Sandbox なら test 、本番組織なら login を入力)

これで、 /Users/foo/force_workspace/myproject/ に対象組織の Apex や Visualforce などがロードされます。

git のリポジトリとマージする

既に git でバージョン管理している組織だったので、 git clone して、差分を管理できるようにします。
リモートリポジトリの URL を https://github.com/foo/myproject.git とします。

git-clone.sh
$ cd ~/force_workspace/myproject/src/
$ git init
$ git remote add origin https://github.com/foo/myproject.git
$ git fetch origin
$ git merge origin/master

git merge origin/master で The following untracked working tree files would be overwritten by merge が出る場合

未コミットの修正が組織にある場合、 The following untracked working tree files would be overwritten by merge のエラーが出ます(私は出ました)。その場合は、強制的に git の状態でまず上書きして、その後、組織の状態を反映します。

fetch-hard-and-refresh.sh
$ git reset --hard FETCH_HEAD ' リポジトリの状態で強制上書き
$ cd ~/force_workspace/myproject/
$ vim src/classes/SomeApex.cls
:ApexRefreshProject

ハマりどころ

:ApexRefreshProject などの一部の vim-force.com プラグインのコマンドは Vim で 当該プロジェクトのなにがしかのファイルを開いた状態でないと Vim に認識されず、実行されないようです。これで私は結構ハマりました。

変更を組織に反映する

ここまでで環境はできていますので、あとは Vim で Apex などを編集したら、組織に :ApexSave:ApexDeploy で反映します。

まとめ

Sublime Text や Visual Studio Code のキーバインドを無理矢理 Vim 化してみたり、色々試行錯誤していたのですが、 vim-force.com を導入すれば使い慣れた Vim で開発ができるので大変幸せになれました。 Vimmer で、 Salesforce の開発を幸か不幸かすることになった方は是非お試しください。捗りますよー。

参考

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8