heltonbiker
heltonbiker

Reputation: 27575

Build system to compile every *.CS file in the project directory into one executable

I started to use Sublime Text (synced Packages folder in Linux and Windows via Dropbox), and I am creating some small C# projects without Visual Studio.

I can compile a single file with csc in windows or gmcs in linux, and have already compiled more than one file in linux from command line (outside sublime) with gmsc *.cs, which smartly resolved dependencies and created an executable named after the "topmost" class in the sources.

After fumbling a bit with Sublime Text build configuration files while reading the docs, and not having understood much, my question is:

How should I configure Sublime Text (via JSON file) to build every *.cs file inside a project/directory into one single executable?

These projects, at least for now, are very simple, I am just separating the files to keep things organized.

Upvotes: 2

Views: 1589

Answers (1)

J0e3gan
J0e3gan

Reputation: 8938

For Windows consider an approach to doing what you want on the Sublime Text forums. Although it was posted with respect to ST2, I found that it works fine in ST3 too.

I think you will find the poster's specific approach easier to follow and adapt than trying to arrive at a solution from scratch after reading the Build Systems documentation.

Here is the .sublime-build file's contents - with 1) the cmd path set for my Windows environment and 2) $file replaced by *.cs to compile all .cs files in the current file's directory:

{
   "cmd": ["C:\\Bin\\Scripts\\sublimetext_csharp_builder.bat", "*.cs", "${file/\\.cs/\\.exe/}"], // NOTE: path set for my Windows environment
   //"cmd": ["\\path\\to\\sublimetext_csharp_builder.bat", "$file", "${file/\\.cs/\\.exe/}"],
   "working_dir": "${project_path:${folder:${file_path}}}",
   "file_regex": "^(...*?)[(]([0-9]*),([0-9]*)[)]",
   "selector": "source.cs"
}

You can find the related sublimetext_csharp_builter.bat file on GitHub. Below I have just 1) removed its reliance on the Visual Studio batch file to set up environment variables, which really is not necessary as long as csc resolves in %PATH% or you use the full path to it and 2) quoted its path to the resulting exe, which I found necessary for the path I was using (since it contained spaces):

:: Assumptions:
:: - Sublime Text has set the working directory and both the source and executable files
::   are in that directory
:: - The script is only capable of handling simple C# apps that do not reference 3rd-party
::   libraries

:: Inputs from Sublime Text
:: %1 - The full path and filename of the source file to build
:: %2 - The name of the executable file
@SET SRC_FILE="%1"
@SET EXE_NAME="%2"

csc %SRC_FILE% /nologo /debug:full /platform:x86
@IF errorlevel 1 GOTO end

:: Execute compiled binary if build was successful.
@ECHO.
@ECHO Executing %EXE_NAME%:
@ECHO.
:: NOTE: path quoted to accommodate spaces in it
@"%EXE_NAME%"
@%EXE_NAME%

:end

Upvotes: 1

Related Questions