Skip to content

Commit 58e25f6

Browse files
committed
Implemented -sdk-root custom path to the SDK root directory
Fixed 'Unauthorized access' error (after ab06f31) when there is no folder for the specified SDK Fixed second attempt for 'sys-or-pkg' when empty tfm
1 parent 4bef1cd commit 58e25f6

File tree

4 files changed

+112
-32
lines changed

4 files changed

+112
-32
lines changed

Readme.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,28 @@ Based on [hMSBuild.bat](https://github.com/3F/hMSBuild) + [GetNuTool](https://gi
1616
Copyright (c) 2021-2025 Denis Kuzmin <x-3F@outlook.com> github/3F
1717
```
1818

19-
[`gnt`](https://3F.github.io/GetNuTool/releases/latest/gnt/)`~netfx4sdk`
20-
2119
[ 「 ❤ 」 ](https://3F.github.io/fund) [![License](https://img.shields.io/badge/License-MIT-74A5C2.svg)](https://github.com/3F/netfx4sdk/blob/master/License.txt)
2220
[![Build status](https://ci.appveyor.com/api/projects/status/7d2jae48fii2m99o/branch/master?svg=true)](https://ci.appveyor.com/project/3Fs/netfx4sdk/branch/master)
2321
[![release](https://img.shields.io/github/release/3F/netfx4sdk.svg)](https://github.com/3F/netfx4sdk/releases/latest)
2422

23+
## Quick start
24+
25+
* Using GetNuTool: [`gnt`](https://3F.github.io/GetNuTool/releases/latest/gnt/)`~netfx4sdk`
26+
* Using hMSBuild: [`hMSBuild`](https://3F.github.io/hMSBuild/releases/latest/gnt/)`-GetNuTool ~netfx4sdk`
27+
* GitHub Releases: https://github.com/3F/netfx4sdk/releases/latest
28+
29+
For example: [`gnt`](https://3F.github.io/GetNuTool/releases/latest/gnt/)`~netfx4sdk & netfx4sdk -mode sys-or-pkg`
30+
31+
Or build and use from source:
32+
33+
```bat
34+
git clone https://github.com/3F/netfx4sdk.git src
35+
cd src & build & cd bin\Release\raw\
36+
netfx4sdk -help
37+
```
38+
39+
Note: starting with 2.0, in addition to `-rollback`, you can also use `-stub` key to check the commands for all planned changes without actually affecting the file system.
40+
2541
## Why netfx4sdk
2642

2743
*netfx4sdk* allows you to easily use the legacy (deprecated) SDK for modern toolsets and IDEs such as VS2022+ / MSBuild 17+ / and other tools without installing any other outdated versions of Visual Studio.
@@ -130,12 +146,6 @@ call netfx4sdk -mode sys || call netfx4sdk -mode pkg
130146
netfx4sdk -mode sys-or-pkg
131147
```
132148

133-
## Get netfx4sdk
134-
135-
* Using GetNuTool: [`gnt`](https://3F.github.io/GetNuTool/releases/latest/gnt/)`~netfx4sdk`
136-
* Using hMSBuild: [`hMSBuild`](https://3F.github.io/hMSBuild/releases/latest/gnt/)`-GetNuTool ~netfx4sdk`
137-
* GitHub Releases: https://github.com/3F/netfx4sdk/releases/latest
138-
139149
## Integration with scripts
140150

141151
### batch (.bat, .cmd)
@@ -153,15 +163,6 @@ call %sdk% -tfm 4.5.2 || (
153163

154164
More actual examples can be found in [tests/](tests/) folder.
155165

156-
157-
## Build and Use from source
158-
159-
```bat
160-
git clone https://github.com/3F/netfx4sdk.git src
161-
cd src & build & cd bin\Release\raw\
162-
netfx4sdk -help
163-
```
164-
165166
### .sha1 official distribution
166167

167168
*netfx4sdk* releases are now accompanied by a *.sha1* file in the official distribution; At the same time, commits from which releases are published are signed with the committer's verified signature (GPG).

netfx4sdk.cmd

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ echo -rollback - Rollback applied modifications.
5252
echo -global - To use the global toolset, like hMSBuild.
5353
echo -no-mklink - Use direct copying instead of mklink (junction / symbolic).
5454
echo -stub - Use a stub instead of actual processing.
55+
echo -sdk-root - Custom path to the SDK root directory.
5556
echo.
5657
echo -pkg-version {arg} - Specific package version in pkg mode. Where {arg}:
5758
echo * 1.0.3 ...
@@ -106,6 +107,7 @@ set "kTfm="
106107
set "tfm="
107108
set "kStub="
108109
set "kFallback="
110+
set "kSdkRoot="
109111

110112
set /a ERROR_SUCCESS=0
111113
set /a ERROR_FAILED=1
@@ -120,6 +122,7 @@ set /a ERROR_UNAUTHORIZED_ACCESS=1004
120122
set /a ERROR_ROLLBACK=1100
121123
set /a ERROR_INVALID_KEY_OR_VALUE=1200
122124
set /a ERROR_TFM_UNSUPPORTED=1202
125+
set /a ERROR_SDK_ROOT_NOT_EXIST=1203
123126
set /a ERROR_GNT_FAIL=1400
124127
set /a ERROR_CMD_BAD_COMMAND_OR_FILE=9009
125128

@@ -205,6 +208,17 @@ set key=!arg[%idx%]!
205208
set kStub=1
206209
goto continue
207210
)
211+
else if [!key!]==[-sdk-root]
212+
( set /a "idx+=1" & call :eval arg[!idx!] v
213+
214+
set "kSdkRoot=!v!"
215+
216+
if not defined kSdkRoot ( goto errkey )
217+
if not exist "!kSdkRoot!" ( set /a EXIT_CODE=%ERROR_SDK_ROOT_NOT_EXIST% & goto endpoint )
218+
219+
if not "!kSdkRoot:~-1!"=="\" ( set "kSdkRoot=!kSdkRoot!\" )
220+
goto continue
221+
)
208222
else if [!key!]==[-global]
209223
(
210224
set kGlobal=1
@@ -251,9 +265,18 @@ set /a "idx+=1" & if %idx% LSS !amax! goto loopargs
251265
call :dbgprint "run!kForce! action:" kMode
252266
)
253267

268+
:: F-44
254269
set "devdir=%ProgramFiles(x86)%"
255270
if not exist "!devdir!" set "devdir=%ProgramFiles%"
256-
set "devdir=!devdir!\Reference Assemblies\Microsoft\Framework\.NETFramework\"
271+
272+
if defined kSdkRoot
273+
(
274+
set "devdir=!kSdkRoot!"
275+
)
276+
else
277+
(
278+
set "devdir=!devdir!\Reference Assemblies\Microsoft\Framework\.NETFramework\"
279+
)
257280

258281
set "tdir=!devdir!!tfm!"
259282
set "rdir=!tdir!.%~nx0"
@@ -330,12 +353,14 @@ set /a "idx+=1" & if %idx% LSS !amax! goto loopargs
330353
set /a EXIT_CODE=%ERROR_TFM_UNSUPPORTED% & goto endpoint
331354
)
332355

333-
echo Apply hack using assemblies for Windows ...
356+
echo Applying hack using assemblies for Windows ...
334357

335358
call :invoke engine "-no-less-4 -no-vswhere -no-vs -only-path -notamd64"
336359
set /a EXIT_CODE=!ERRORLEVEL! & if !EXIT_CODE! NEQ 0 goto endpoint
337360

338361
call :getFirstMsg lDir
362+
call :stub "mkdir" "!tdir!" 2>nul
363+
339364
call :xcpDbg "!tdir!" "!rdir!" + || (
340365
set /a EXIT_CODE=%ERROR_UNAUTHORIZED_ACCESS% & goto endpoint
341366
)
@@ -349,7 +374,6 @@ set /a "idx+=1" & if %idx% LSS !amax! goto loopargs
349374
call :dbgprint "lDir " lDir
350375
if not exist "!lDir!" ( set /a EXIT_CODE=%ERROR_PATH_NOT_FOUND% & goto endpoint )
351376

352-
call :stub "mkdir" "!tdir!" 2>nul
353377
for /F "tokens=*" %%i in ('dir /B "!lDir!*.dll"') do call :copyOrLinkFileDbg "!lDir!%%i" "!tdir!\%%i"
354378
for /F "tokens=*" %%i in ('dir /B "!lDir!WPF\*.dll"') do call :copyOrLinkFileDbg "!lDir!WPF\%%i" "!tdir!\%%i"
355379

@@ -364,7 +388,7 @@ set /a "idx+=1" & if %idx% LSS !amax! goto loopargs
364388
else if "!kMode!"=="pkg"
365389
(
366390
set npkg=Microsoft.NETFramework.ReferenceAssemblies.!kTfm!
367-
echo Apply .NET Framework !tfm! package ...
391+
echo Applying .NET Framework !tfm! package ...
368392

369393
set opkg=%~nx0.!kTfm!.%vpkg%
370394
if "%vpkg%"=="latest" ( set "vpkg=" ) else ( set "vpkg=/%vpkg%" )
@@ -382,6 +406,8 @@ set /a "idx+=1" & if %idx% LSS !amax! goto loopargs
382406
set /a EXIT_CODE=%ERROR_ENV_W% & goto endpoint
383407
)
384408

409+
call :stub "mkdir" "!tdir!" 2>nul
410+
385411
call :stub "ren" "!tdir!" !tfm!.%~nx0 2>nul || (
386412
set /a EXIT_CODE=%ERROR_UNAUTHORIZED_ACCESS% & goto endpoint
387413
)
@@ -439,8 +465,12 @@ if !EXIT_CODE! NEQ 0
439465
(
440466
call :warn "Unauthorized access. Make sure you have read/write permissions to the folders listed in '-debug'. Try run %~nx0 as administrator."
441467
)
468+
else if !EXIT_CODE! EQU %ERROR_SDK_ROOT_NOT_EXIST%
469+
(
470+
call :warn "The path specified in '-sdk-root' does not exist: !kSdkRoot!. Try as -sdk-root `!kSdkRoot!` or make sure."
471+
)
442472

443-
if defined kFallback
473+
if defined kFallback if defined tfm
444474
(
445475
echo.& echo Switch to !kFallback! mode for second attempt due to '-mode !kMode!-or-!kFallback!'
446476
set "kMode=!kFallback!" & set "kFallback="
@@ -542,7 +572,9 @@ exit /B 1
542572
:: :isValidV
543573

544574
:warn {in:msg}
545-
echo [*] WARN: %~1 >&2
575+
set _wrnmsg=%~1
576+
set _wrnmsg=!_wrnmsg:`="! ::&:
577+
echo [*] WARN: !_wrnmsg! >&2
546578
exit /B 0
547579
:: :warn
548580

tests/_run.bat

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ if "!failedTotal!" LSS "1" set /a failedTotal=0
2929

3030
:::::::::::::::::: :::::::::::::: :::::::::::::::::::::::::
3131
:: Tests
32+
call :setup
33+
3234

3335
echo. & call a print "Tests - 'stubLogicTests'"
34-
call .\stubLogicTests gcount failedTotal "%core% -stub" "%rdir%"
36+
call .\stubLogicTests gcount failedTotal "%core% -stub -sdk-root .\sdks" "%rdir%"
3537

3638
echo. & call a print "Tests - 'PkgTests'"
3739
call .\PkgTests gcount failedTotal "tests\gnt.bat" "%rdir%"
@@ -40,7 +42,8 @@ if "!failedTotal!" LSS "1" set /a failedTotal=0
4042
call .\keysAndLogicTests gcount failedTotal "%core%" "%rdir%"
4143

4244

43-
::::::::::::::::::
45+
call :cleanup
46+
:::::::::::::
4447
::
4548
echo.
4649
call a cprint 0E ----------------
@@ -59,3 +62,11 @@ exit /B 0
5962
echo.
6063
call a cprint 0C "Tests failed." >&2
6164
exit /B 1
65+
66+
:setup
67+
mkdir sdks 2>nul>nul
68+
exit /B 0
69+
70+
:cleanup
71+
call a unsetDir sdks
72+
exit /B 0

tests/stubLogicTests.bat

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ set "exec=%~3" & set "wdir=%~4"
5151

5252
call a findInStreamOrFail "[*] WARN: Failed: 1202" 5,n || goto x
5353
call a findInStreamOrFail "Switch to pkg mode for second attempt due to '-mode sys-or-pkg'" 7,n || goto x
54-
call a findInStreamOrFail "Apply .NET Framework v4.5 package ..." 8,n || goto x
54+
call a findInStreamOrFail "Applying .NET Framework v4.5 package ..." 8,n || goto x
5555
call :failIfInStreamExcept "\\" "# 1 : " || goto x
5656
call a completeTest
5757
::_____________________________________________________
@@ -62,7 +62,7 @@ set "exec=%~3" & set "wdir=%~4"
6262
call a startTest "-debug -mode system -tfm 4.5 -force" 1202 || goto x
6363
call a msgOrFailAt 1 "run a forced action: sys" || goto x
6464
call a msgOrFailAt 2 "net45 v4.5" || goto x
65-
call a failIfInStream "Apply .NET Framework v4.5 package" || goto x
65+
call a failIfInStream "Applying .NET Framework v4.5 package" || goto x
6666
call :failIfInStreamExcept "\\" "# 1 : " || goto x
6767
call a completeTest
6868
::_____________________________________________________
@@ -156,6 +156,36 @@ set "exec=%~3" & set "wdir=%~4"
156156
::_____________________________________________________
157157

158158

159+
::_______ ------ ______________________________________
160+
161+
call a startTest "-debug -mode pkg -tfm 4.5 -sdk-root " 1200 || goto x
162+
call a msgOrFailAt 1 "[*] WARN: Invalid key or value for '-sdk-root'" || goto x
163+
call a completeTest
164+
::_____________________________________________________
165+
166+
167+
::_______ ------ ______________________________________
168+
169+
call a startTest "-debug -mode pkg -tfm 4.5 -sdk-root notrealdir" 1203 || goto x
170+
call a findInStreamOrFail "[*] WARN: The path specified in '-sdk-root' does not exist: notrealdir. Try as -sdk-root `notrealdir`" 1,n || goto x
171+
call a completeTest
172+
::_____________________________________________________
173+
174+
175+
::_______ ------ ______________________________________
176+
177+
mkdir sdkDirTest1 2>nul>nul
178+
call a startTest "-debug -mode pkg -tfm 4.5 -sdk-root sdkDirTest1" || goto x
179+
call a msgOrFailAt 2 "net45 v4.5 sdkDirTest1\v4.5" || goto x
180+
call a completeTest
181+
182+
call a startTest "-debug -mode pkg -sdk-root sdkDirTest1\ -tfm 4.6.2" || goto x
183+
call a msgOrFailAt 2 "net462 v4.6.2 sdkDirTest1\v4.6.2" || goto x
184+
call a completeTest
185+
rmdir sdkDirTest1 2>nul>nul
186+
::_____________________________________________________
187+
188+
159189
:::::::::::::
160190
call :cleanup
161191

@@ -180,21 +210,23 @@ exit /B 0
180210
call a msgOrFailAt 1 "run a forced action: sys" || exit /B 1
181211
call a msgOrFailAt 2 "net40 v4.0" || exit /B 1
182212

183-
call a findInStreamOrFail "Apply hack using assemblies for Windows ..." 4,n || exit /B 1
213+
call a findInStreamOrFail "Applying hack using assemblies for Windows ..." 4,n || exit /B 1
184214
call a findInStreamOrFail "-no-less-4 -no-vswhere -no-vs -only-path -notamd64" 5,n || exit /B 1
185215

186216
set /a n+=1
187217
call a msgOrFailAt !n! "# 1 : " || exit /B 1
188218
set /a n+=1
189219
call a msgOrFailAt !n! "# 2 : 0" || exit /B 1
190220

221+
set /a n+=1
222+
call a findInStreamOrFail "mkdir " !n!,n || exit /B 1
223+
call a msgOrFailAt !n! "v4.0" || exit /B 1
224+
191225
set /a n+=1
192226
call a msgOrFailAt !n! "xcopy " || exit /B 1
193227
call a msgOrFailAt !n! "v4.0.netfx4sdk.cmd` /E/I/Q/H/K/O/X/Y" || exit /B 1
194228

195229
set /a n+=2
196-
call a msgOrFailAt !n! "mkdir " || exit /B 1
197-
set /a n+=1
198230
call a msgOrFailAt !n! "%~1 " || exit /B 1
199231
set /a n+=100
200232
call a findInStreamOrFail "%~1 " !n!,n || exit /B 1
@@ -216,9 +248,13 @@ exit /B 0
216248
call a findInStreamOrFail "[*] WARN: Failed: 1202" 5,n || exit /B 1
217249
call a findInStreamOrFail "Switch to pkg mode for second attempt due to '-mode sys-or-pkg'" 6,n || exit /B 1
218250

219-
call a findInStreamOrFail "Apply .NET Framework v4.5 package ..." 8,n || exit /B 1
251+
call a findInStreamOrFail "Applying .NET Framework v4.5 package ..." 8,n || exit /B 1
220252
call a findInStreamOrFail "dpkg packages\netfx4sdk.cmd.net45.1.0.3\build\.NETFramework\v4.5" 10,n || exit /B 1
221253

254+
set /a n+=1
255+
call a findInStreamOrFail "mkdir " !n!,n || exit /B 1
256+
call a msgOrFailAt !n! "v4.5" || exit /B 1
257+
222258
set /a n+=1
223259
call a msgOrFailAt !n! "ren " || exit /B 1
224260
call a msgOrFailAt !n! "v4.5.netfx4sdk.cmd" || exit /B 1
@@ -240,7 +276,7 @@ exit /B 0
240276
call a failIfInStream "run a forced action:" || exit /B 1
241277
call a failIfInStream "net40 v4.0" || exit /B 1
242278

243-
call a findInStreamOrFail "Apply hack using assemblies for Windows ..." 1,n || exit /B 1
279+
call a findInStreamOrFail "Applying hack using assemblies for Windows ..." 1,n || exit /B 1
244280
call a findInStreamOrFail "%~1 " 100,n || exit /B 1
245281

246282
set /a n+=1

0 commit comments

Comments
 (0)