2017-03-05

Mac OSX 安裝 scriptcs 使用 Visual Studio Code 編輯

ScriptCS 是一個將 C# 語言撰寫的程式碼檔案(*.csx)編譯與執行的工具。




安裝Homebrew套件管理工具

Homebrew 的安裝很簡單,只要在終端機(Terminal)中輸入以下指令就能進行安裝:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安裝 ScriptCS

brew install scriptcs

確認 scriptcs 安裝成功,查看 scriptcs 版本

安裝過程有看到版本號為 0.17.1
🍺  /usr/local/Cellar/mono/4.8.0.495: 1,604 files, 242.6M
==> Installing scriptcs 
==> Downloading https://homebrew.bintray.com/bottles/scriptcs-0.17.1.sierra.bott
######################################################################## 100.0%
==> Pouring scriptcs-0.17.1.sierra.bottle.tar.gz
🍺  /usr/local/Cellar/scriptcs/0.17.1: 38 files, 12.4
使用 scriptcs -version 查看版本號時顯示的是 Version: 0.0.0,可能是bug吧,使用很正常,沒遇到什麼特別的問題
scriptcs -version
               _       _
 ___  ___ _ __(_)_ __ | |__ ___ ___
/ __|/ __| '__| | '_ \| __// __/ __|
\__ \ (__| |  | | |_) | |_| (__\__ \
|___/\___|_|  |_| .__/ \__\\___|___/
                |_| Version: 0.0.0


建立 helloworld.csx

將以下內容存成 helloworld.csx

string s="Hello World!";
Console.WriteLine(s);

開啟終端機(Terminal),在helloworld.csx 所在資料夾中執行以下命令

scriptcs helloworld.csx
如果成功在畫面上看到 "Hello World!" 的文字表示正常執行.

安裝 Vusual Studio Code

到官網Visual Studio Code下載安裝即可

安裝 Code Runner

Code Runner 可以設定多種程式語言的編譯和執行,因為我有時會寫 javascript,所以我安裝這套可以支援多種程式語言,如果只有要編譯執行 csx 的檔 案,可以安裝 scriptcsRunner.

開啟 Visual Studio Code,在Extensions (Shift + Command + X)輸入「Code Runner 0.6.12」,找到後安裝它

設定scriptcs的編譯參數

到主選單 Code/Preferences/Settings 或 command + , 開啟設定 在User Settings 加入 scriptcs $fileName。
{
    "code-runner.executorMap": {
        "csharp": "scriptcs $fileName"
    }
}
預設只有 scriptcs 的後面沒有帶檔名參數,在執行時會自動帶入「完整檔案路徑和檔名」,這樣反而執行都失敗。

測試Visual Studio Code執行csx 檔

使用 Visual studio code 開啟csx檔,然後選取要執行的程式碼,點右鍵彈出選單,選RUN Code

adsense