Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion for better and faster .net batch prologue without redundant .bat output #26

Open
SergeAgeyev opened this issue Feb 11, 2020 · 3 comments

Comments

@SergeAgeyev
Copy link

There is some suggestions to improvement on .net batch prologue, future improvement of (#25)

It runs .exe files immediately if .exe file is located near .bat file.
It does not generate output // 2>nul || at the begin

Here is updated prologue I use, attached to hello world example:

@goto :batch_start_label_cs_hybrid
/*
@:batch_start_label_cs_hybrid
@echo off
if exist "%~dpn0.exe" (
"%~dpn0.exe" %*
exit /b %errorlevel% 
)

setlocal

:: Additional options to csc.exe (like /r:"references")
set CSC_OPS=

:: Runner options
set CS_FAIL_EXIT_CODE=1001

:: find csc.exe
set "csc="
for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*csc.exe") do  set "csc=%%#"

if not exist "%csc%" (
   echo Error: no .net framework installed
   exit /b %CS_FAIL_EXIT_CODE%
)

set BAT_START_LINE=batch_start
set BAT_START_LINE=%BAT_START_LINE%_
set BAT_START_LINE=%BAT_START_LINE%label_cs_hybrid

call findstr /V "%BAT_START_LINE%" "%~dpn0.bat" > "%~dpn0.bat.cs"
if not exist "%~dpn0.bat.cs" (
   echo Error: fail to prepare executable
   exit /b %CS_FAIL_EXIT_CODE%
)

call %csc% /nologo %CSC_OPS% /out:"%~dpn0.exe" "%~dpn0.bat.cs" || (
   exit /b %CS_FAIL_EXIT_CODE%
)

del "%~dpn0.bat.cs"

"%~dpn0.exe" %*
endlocal & exit /b %errorlevel%

*/
using System;

public class Program
{
    public static int Main(string[] Argv)
    {
         Console.WriteLine("Hello, world!");
         return 0;
    }
}

I use this prologue in zip.bat utility:
https://github.com/openlab-vn-ua/BatArt/blob/master/zip.bat

@ildar-shaimordanov
Copy link

You can make it a bit shorter and better. Some improvements:

  • no call for binaries
  • %~f0 instead %~dpn0.bat
  • add >&2 to output error messages to STDERR.
  • goto :EOF is transparent for %ERRORLEVEL%, so no needs to exit /b %ERRORLEVEL%. But you can leave your original command for sure.
  • no dancing around batch_start_label_cs_hybrid now. It's obviously shorter and clearer: the caret in @rem ^ tells cmd.exe to concatenate it with the next line before executing, thus you don't need to goto :LABEL and :LABEL and too complicated code to exclude the first line. And if you want, you can shorten it further replacing findstr /v /b "@rem ^" with more +1.
@rem ^
/*
@echo off

if exist "%~dpn0.exe" (
	"%~dpn0.exe" %*
	goto :EOF
)

setlocal

:: Additional options to csc.exe (like /r:"references")
set CSC_OPS=

:: Runner options
set CS_FAIL_EXIT_CODE=1001

:: find csc.exe
set "csc="
for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*csc.exe") do  set "csc=%%#"

if not exist "%csc%" (
	echo Error: no .net framework installed>&2
	exit /b %CS_FAIL_EXIT_CODE%
)

findstr /v /b "@rem ^" "%~f0" >"%~f0.cs" || (
	echo Error: fail to prepare executable>&2
	exit /b %CS_FAIL_EXIT_CODE%
)

"%csc%" /nologo %CSC_OPS% /out:"%~dpn0.exe" "%~f0.cs" || (
	exit /b %CS_FAIL_EXIT_CODE%
)

del "%~f0.cs"

"%~dpn0.exe" %*
goto :EOF

*/
using System;

public class Program
{
	public static int Main(string[] Argv)
	{
		Console.WriteLine("Hello, world!");
		return 42;
	}
}

@npocmaka
Copy link
Owner

Thanks. Probably I'll have time to check it at the weekend. Is really @rem ^ works fine at the beginning of the file? It is also possible to use msbuild to reduce the redundant output: https://github.com/npocmaka/batch.scripts/blob/master/hybrids/.net/msbuild/checkPort.bat

@ildar-shaimordanov
Copy link

ildar-shaimordanov commented Oct 19, 2021

Is really @Rem ^ works fine at the beginning of the file?

Yes. before publishing I tested it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants