fsavje / math-with-slack

Rendered math (MathJax) with Slack's desktop client
MIT License
431 stars 62 forks source link

Changed Windows installation batch file to use delayed expansion #21

Closed gauss256 closed 6 years ago

gauss256 commented 6 years ago

For issue #20

fsavje commented 6 years ago

As usual, thank you very much for contributing! Could you explain why enabledelayedexpansion and enableextensions are necessary? From my (admittedly limited) knowledge of batch scripts, enableextensions is enabled by default since Windows 95 or something like that, and I thought enabledelayedexpansion was only necessary when setting and retrieving a variable was done on the same line.

gauss256 commented 6 years ago

You are partially right: enableextensions is not needed. However, enabledelayedexpansion is necessary because of the IF block. The block is parsed all at once, so the situation is similar to setting and retrieving a variable on the same line.

The following code tests this.

@ECHO OFF

REM The next line is necessary so we can use the delayed expansion syntax
setlocal enabledelayedexpansion

SET SLACK_DIR=

REM The command processor parses entire IF block before executing so
REM %SLACK_DIR% is evaluated before it is set. The delayed expansion syntax
REM !SLACK_DIR! avoids this problem.
IF "%SLACK_DIR%" == "" (
    SET SLACK_DIR=something
    IF "%SLACK_DIR%" == "" (
        ECHO inner SLACK_DIR not found with percent
    ) ELSE (
        ECHO inner SLACK_DIR found with percent: value="%SLACK_DIR%"
    )
    IF "!SLACK_DIR!" == "" (
        ECHO inner SLACK_DIR not found with bang
    ) ELSE (
        ECHO inner SLACK_DIR found with bang: value="!SLACK_DIR!"
    )
)

REM We are outside the IF block now so we can see the new value of SLACK_DIR
REM without having to use delayed expansion. (Although delayed expansion still
REM works.)
IF "%SLACK_DIR%" == "" (
    ECHO outer SLACK_DIR not found with percent
) ELSE (
    ECHO outer SLACK_DIR found with percent: value="%SLACK_DIR%"
)

IF "!SLACK_DIR!" == "" (
    ECHO outer SLACK_DIR not found with bang
) ELSE (
    ECHO outer SLACK_DIR found with bang: value="!SLACK_DIR!"
)

When it runs the output is

inner SLACK_DIR not found with percent
inner SLACK_DIR found with bang: value="something"
outer SLACK_DIR found with percent: value="something"
outer SLACK_DIR found with bang: value="something"

If you comment out the enabledelayedexpansion the output is

inner SLACK_DIR not found with percent
inner SLACK_DIR found with bang: value="!SLACK_DIR!"
outer SLACK_DIR found with percent: value="something"
outer SLACK_DIR found with bang: value="!SLACK_DIR!"
fsavje commented 6 years ago

Thank you for clarifying -- you are, of course, completely right. I decided to move everything outside the IF statement instead. (It doesn't hurt to always do the check and to inform the user which installation is used). I'm, therefore, going to close this pull request. But, needless to say, I very much appreciate your contributions!