Skip to content

Commit 17d70a7

Browse files
committed
Potential solution for #111 (Powershell script)
1 parent 8ddbc72 commit 17d70a7

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* eol=lf

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
nob
22
nob.old
33
nob.exe
4-
build/
4+
build/
5+
**/*.obj

nob.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@
9999
#ifndef NOB_H_
100100
#define NOB_H_
101101
#ifdef _WIN32
102-
#define _CRT_SECURE_NO_WARNINGS (1)
102+
# ifndef _CRT_SECURE_NO_WARNINGS
103+
# define _CRT_SECURE_NO_WARNINGS (1)
104+
# endif
103105
#endif
104106

105107
#ifndef NOBDEF
@@ -858,12 +860,36 @@ NOBDEF void nob__go_rebuild_urself(int argc, char **argv, const char *source_pat
858860
// TODO: this is an experimental behavior behind a compilation flag.
859861
// Once it is confirmed that it does not cause much problems on both POSIX and Windows
860862
// we may turn it on by default.
863+
# ifdef _WIN32
864+
nob_log(NOB_INFO, "Defer deleting of %s...", old_binary_path);
865+
# else
861866
nob_delete_file(old_binary_path);
867+
# endif
862868
#endif // NOB_EXPERIMENTAL_DELETE_OLD
863869

864870
nob_cmd_append(&cmd, binary_path);
865871
nob_da_append_many(&cmd, argv, argc);
866872
if (!nob_cmd_run_opt(&cmd, opt)) exit(1);
873+
874+
#if defined(NOB_EXPERIMENTAL_DELETE_OLD) && defined(_WIN32)
875+
// Workaround to delete the old nob from a script instead, only after we exit this process.
876+
// If we don't do this, Windows will complain with "Access is denied" error, since
877+
// the current process is still being executed from the old nob executable.
878+
Nob_String_Builder sb = {0};
879+
nob_sb_appendf(&sb, "$Attempts = 0; While ($Attempts++ -Lt 10) {\n");
880+
nob_sb_appendf(&sb, " Remove-Item -Path \"%s\" -ErrorAction SilentlyContinue\n", old_binary_path);
881+
nob_sb_appendf(&sb, " If (Test-Path -Path \"%s\") { Start-Sleep -Milliseconds 100 }\n", old_binary_path);
882+
nob_sb_appendf(&sb, "}");
883+
nob_sb_append_null(&sb);
884+
Nob_Procs procs = {0};
885+
opt.async = &procs;
886+
// WARN: Running a PowerShell script like this might trigger anti-virus software
887+
// to think we are trying to run malicious code.
888+
nob_cmd_append(&cmd, "PowerShell", "-Command", sb.items);
889+
if (!nob_cmd_run_opt(&cmd, opt)) exit(1);
890+
nob_sb_free(sb);
891+
#endif // NOB_EXPERIMENTAL_DELETE_OLD
892+
867893
exit(0);
868894
}
869895

@@ -1860,7 +1886,7 @@ NOBDEF bool nob_rename(const char *old_path, const char *new_path)
18601886
{
18611887
nob_log(NOB_INFO, "renaming %s -> %s", old_path, new_path);
18621888
#ifdef _WIN32
1863-
if (!MoveFileEx(old_path, new_path, MOVEFILE_REPLACE_EXISTING)) {
1889+
if (!MoveFileEx(old_path, new_path, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) {
18641890
nob_log(NOB_ERROR, "could not rename %s to %s: %s", old_path, new_path, nob_win32_error_message(GetLastError()));
18651891
return false;
18661892
}

0 commit comments

Comments
 (0)