美文网首页
Visual Studio Code开发Swift+Vapor环

Visual Studio Code开发Swift+Vapor环

作者: 芮淼一线 | 来源:发表于2020-06-01 22:01 被阅读0次

说明:使用VS Code实现Swift 跨平台开发

第一步:Swift安装

MacOS:直接安装Xcode即可!
Ubuntu:

  1. 准备:
    创建目录:sudo mkdir /usr/local/dev
    创建dev目录的软连接到桌面:ln -s /usr/local/dev ~/Desktop/dev
  2. 进入官网下载对应的swift Toolchain文件
  3. 配置swift环境:
    1.将下载好的软件包解压到指定路径,可以自定义。我是解压到:/usr/local/dev/swift/5.2.4
    2.添加环境变量,打开~/.bashrc, 在文件最后一行添加:
    export PATH=/usr/local/dev/swift/5.2.4/usr/bin:"${PATH}"
    3.终端运行swift -version ,有结果说明swift安装成功!

PS:注意不需要去下载编译sourcekit-lsp工具,因为swift工具包中有sourcekit-lsp

第二步:Swift命令测试

1.创建项目: swift package init --type executable
2.构建项目: swift build
3.运行项目: swift run
4.测试项目: swift test

第三步:安装Visual Studio Code与Node.js

这儿不做描述,不会的请自行百度

第四步:Visual Studio Code环境配置

1.安装插件:
Swift Language: swift语法支持
Code Runner : swift运行插件,选中某个swift文件鼠标右键运行或者点击VS右上角的运行按钮。注意:Code Runner只能运行单个的swift文件。
Maintained Swift Development Environment:配置如下
   1. Language Server Mode选择:sourcekit-lsp
   2. Enable Tracing LSPServer: 选项开启
   3. "ToolChain Path","Swift_driver_bin": 填写swift bin目录路径
   Ubuntu: /usr/local/dev/swift/5.2.4/usr/bin
   MacOS:下同样填入swift路径,在终端输入swift cc 命令,可以根据错误提示找到swift路径。
   4. "Server Path"
   Ubuntu: /usr/local/dev/swift/5.2.4/usr/bin/sourcekit-lsp
   MacOS:下同样填入swift路径,在终端输入swift cc 命令,可以根据错误提示找到swift路径

CodeLLDB: lldb swift调试插件
SourceKit-LSP-VSCode: sourcekit-lsp的vscode插件下载,解压后直接拖到vscode 插件中心进行安装即可

第五步:配置launch.json与tasks.json

在VS中想要指定swift项目,则需要配置launch.json与tasks.json。
launch.json如下(注意:Test时需要将xxx.xctest修改为你自己的测试包名)

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        //运行
        {
            "type": "lldb",
            "name": "Run",
            "request": "launch",
            "cwd": "${workspaceFolder}",
            "program": "${workspaceFolder}/.build/debug/Run",
            "preLaunchTask": "swift-build",
            "args": ["serve","--hostname","0.0.0.0","--port","8080"]
        },
        //测试
        {
            "type": "lldb",
            "name": "Test",
            "request":"launch",
            "cwd": "${workspaceFolder}",
            "program": "",
            "linux": {
                "program": "${workspaceFolder}/.build/x86_64-unknown-linux-gnu/debug/appPackageTests.xctest",//YourPackageTests.xctest
            },
            "osx": {
                "program": "/Applications/Xcode.app/Contents/Developer/usr/bin/xctest",
                "args": [
                    "${workspaceFolder}/.build/debug/webPackageTests.xctest"//YourPackageTests.xctest
                ]
            },
            "preLaunchTask": "swift-tests"
        },
        //Clean: 
        {
            "type": "lldb",
            "name": "Clean",
            "request":"custom",
            "preLaunchTask": "swift-clean",
        },
         //构建发行: 
        //    MacOS下为Release版本,
        //    Linux下为Debug版本(Release会出错,可能是当前项目影响,需要更具具体情况调节)
        {
            "type": "lldb",
            "name": "Release",
            "request":"custom",
            "preLaunchTask": "swift-release",
        }    
        
    ]
}

tasks.json如下(PS:Release时将linux平台下swift build模式修改为release)

{
  "version": "2.0.0",
  "tasks": [
    //tasks Run
    {
      "label": "swift-build",
      "type": "shell",
      "command": "swift build"
    },
    //tasks Test
    {
      "label": "swift-tests",
      "type": "process",
      "command": "swift",
      "group": "build",
      "args": [
          "build",
          "--build-tests"
         // for TensorFlow add "-Xlinker", "-ltensorflow"
      ]
    },
    //tasks Claen
    {
      "label": "swift-clean",
      "type": "shell",
      "command":[
        "rm -rf",
        "${workspaceFolder}/Products",
        "${workspaceFolder}/.build/x86_64-unknown-linux-gnu",
        "${workspaceFolder}/.build/x86_64-apple-macosx",
        "${workspaceFolder}/.build/debug",
        "${workspaceFolder}/.build/release"
      ]
    },    
    //tasks Release
    {
      "label": "swift-release",
      "type": "shell",
      "linux":{
        "command":[
          "swift build -c debug\n",//可修改为release模式
          "mkdir -p ${workspaceFolder}/Products\n",
          "cp -rf ${workspaceFolder}/.build/debug/Run ${workspaceFolder}/Products\n",//路径可以切换为release,路径与swift build模式要匹配
          "cp -rf ${workspaceFolder}/Public ${workspaceFolder}/Products\n",
          "cp -rf ${workspaceFolder}/Resources ${workspaceFolder}/Products\n",
          "nautilus ${workspaceFolder}/Products"
        ]
      },
      "osx":{
        "command":[
          "swift build -c release\n",
          "mkdir -p ${workspaceFolder}/Products\n",
          "cp -rf ${workspaceFolder}/.build/release/Run ${workspaceFolder}/Products\n",
          "cp -rf ${workspaceFolder}/Public ${workspaceFolder}/Products\n",
          "cp -rf ${workspaceFolder}/Resources ${workspaceFolder}/Products\n",
          "open ${workspaceFolder}/Products"
        ]
      }
    }

  ]
}

下载

喜欢的请点个👍🏻

相关文章

网友评论

      本文标题:Visual Studio Code开发Swift+Vapor环

      本文链接:https://www.haomeiwen.com/subject/uleezhtx.html