본문 바로가기
개발환경

[Env] Vscode: task runner 설정: tasks.json

by ds31x 2023. 8. 18.
Visualstudio Code (vscode)는
프로젝트의 build등을 자동화하기 위해 custom task를 작성 및 수행할 수 있도록
Task Runner라는 기능을 지원함.

Task Runner는 tasks.json을 통해 build, test 등의 task를 단축키로 수행하게 해 줌.

  • C / C++ 의 경우, tasks.json 을 통해 compiler 를 통한 build 를 수행함.
  • Python의 경우, build가 필요하지 않지만, 파일 실행용으로 많이 사용함.
    • Python file을 수행하기 위해 ctrl + f5 (디버그없이 실행) 등으로 수행할 수 있지만,
    • 간단한 수행을 위한 custom task를 만들어 두면 편하다.
  • 기본적으로 build task 에 대해 hot-key가 ctrl+shift+b 로 vscode는 지정이 되어 있음.
vscode에서 tasks.json과 비슷한 것으로 launch.json 이 있음.
이는 디버깅을 위한 설정등이 저장되는 파일임.

 

프로젝트 Directory마다

  • custom task를 지정할 수 있고,
  • 프로젝트 디렉토리 밑의 .vscode 디렉토리 밑의 tasks.json 파일에
  • 해당 task의 수행 내용이 저장된다.
  • 즉, tasks.jsontask runner 작업을 정의함.

작성방법 for Python

작성 방법은

  1. command palette (ctrl+shift+p)에
  2. Tasks: Configure task 를 입력하고
  3. create tasks.json from templeate를 선택하고
  4. Others를 선택하면

tasks.json 파일이 template로부터 생성되고 editing할 수 있게 된다.

 

초기 tasks.json 은 다음과 같음.

상단 주석에 Microsoft 사에서 제공하는 tasks.json 을 작성 방법을 설명해주고 있는 문서의 url이 있음.
이 문서도 대부분 해당 문서를 참고하여 작성되었다.

  • 하지만... 공식문서의 특징이 워낙 많은 내용이 있다보니...
  • googling을 하면 기본적으로 `tasks.json`의 샘플을 쉽게 구할 수 있으니, 우선 해당 파일로 사용하고
  • 사용하는 해당 파일의 각 설정을 공식문서에서 찾아보는 방법을 취하길 권한다.

다음이 Python 파일 작성 및 테스트를 하는데 편리한 tasks.json

현재 editor에 선택된 파일을 실행한다.

 

이 내용으로 tasks.json을 변경하고 저장한다.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Python Run",
            "type": "shell",
            //"command": "python",
            "command": "${command:python.interpreterPath}",
            "args": [
                "${file}"
            ],
            "presentation": { // task output 설정 : 매번 새 terminal로.
                "reveal": "always",
                "panel": "new"  
            },
            "options": {
                "env": {
                    "PYTHONIOENCODING": "UTF-8"
                }
            },
            "group": {
                "kind": "build", //hot-key를 ctrl+shift+b 로 하기위해.
                "isDefault": true
            }
        }
    ]
}

간단 설명

항목에 대한 간단한 설명이다.

  • "label"은 task 목록에서 보여질 해당 task의 이름에 해당함.
  • "type"은 "shell"을 선택하여 terminal에서 수행되는 command로 처리되도록 해줌.
  • "command"는 task에서 수행될 명령어이다.
  • "args"는 command에 넘겨질 argument임. ${file}은 현재 editor에서 선택된 파일(active file)의 path가 들어감.
  • "group"은 "build", "test", "none" 중 하나의 값을 가질 수 있음 
    • ctrl+shift+b hotkey로 실행되도록 "build"를 선택함.
    • isDefaulttrue 이어야 해당 hotkey의 기본으로 동작하는 task임.
    • 여러 build 용 task를  tasks.json에 작성가능하므로 기본을 하나 지정해놔야 함.
  • "options"는 한글이 깨지지 않도록 UTF-8로 지정함.

C / C++ 용 tasks.json

다음은 g++을 이용하는 tasks.json 의 예제임: multiple files 컴파일이 아닌 single file만 가능(active file)함.

위의 Python의 간단 설명에서 각 항목에 대한 부분을 참고하면 도움이 됨.

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "C/C++: g++ build active file",
      "command": "/usr/bin/g++",
      "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
      "options": {
        "cwd": "/usr/bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."
    }
  ]
}
  • g++  의 경우이나, gcc 로 바꿀 수도 있음: command 항목에서 변경가능
  • args 부분이 command 가 무엇이냐에 따라 달라짐.
  • ${fileDirname} : active file의 디렉토리 경로로 치환됨.
  • ${fileBasenameNoExtension} : active file의 확장자 없이 이름만.
  • 여러 개의 cpp 소스파일로 하나의 executable file로 빌드하려면
    • args 부분에서 ${file}${workspaceFolder}/*.cpp 로 바꾸면 됨.
    • C 의 경우엔 gcc를 권장하고, 확장자가 .c 가 되면 됨.
  • options 는 task를 실행할 때의 쉘 명령어의 동작 방식을 정의함.
    • 위의 예는  cwd (Current Working Directory) 로, 명령어가 실행될 working directory를 지정함.
    • 여기서는 /usr/bin으로 설정되어 있음.
    • 이는 g++ 명령어가 /usr/bin 디렉토리에서 실행된다는 것을 의미함.
    • 즉, 이 설정은 프로그램이 컴파일될 때, 컴파일러가 실행되는 working directory를 정의함.
  • options 의 다른 설정들도 가능함. 대표적인 예는 다음과 같음:
    • env : 환경 변수를 설정함 (위의 Python의 경우를 참고)
    • shell : 사용할 쉘의 경로를 지정함.

References

https://code.visualstudio.com/docs/editor/tasks#vscode

 

Tasks in Visual Studio Code

Expand your development workflow with task integration in Visual Studio Code.

code.visualstudio.com

https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=mingu216&logNo=221585237779

 

Visual Studio Code 세팅(Task Runner)

첫 포스트는 우분투에서 Visual Studio Code를 설치 하고, https://blog.naver.com/mingu216/2215831910...

blog.naver.com

https://code.visualstudio.com/docs/editor/tasks-appendix

 

Visual Studio Code Tasks Appendix

Additional info for using task runners in Visual Studio Code.

code.visualstudio.com


2025.01.17 - [utils] - [summary] vscode

 

[summary] vscode

vscode 소개 (visual studio 와 비교)https://ds31x.blogspot.com/2023/07/env-visual-studio-code-and-visual-studio.html?view=classic [Env] Visual Studio Code and Visual StudioVisual Studio Code (vscode)는 Visual Studio와 달리, code editor임을 강

ds31x.tistory.com